unused_variable_elimination.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::{build_solidity, BorshToken};
  3. use num_bigint::BigInt;
  4. /// This tests check that a public storage variable is not eliminated
  5. /// and that an assignment inside an expression works
  6. #[test]
  7. fn test_returns() {
  8. let file = r#"
  9. contract c1 {
  10. int public pb1;
  11. function assign() public {
  12. pb1 = 5;
  13. }
  14. int t1;
  15. int t2;
  16. function test1() public returns (int) {
  17. t1 = 2;
  18. t2 = 3;
  19. int f = 6;
  20. int c = 32 +4 *(f = t1+t2);
  21. return c;
  22. }
  23. function test2() public returns (int) {
  24. t1 = 2;
  25. t2 = 3;
  26. int f = 6;
  27. int c = 32 + 4*(f= t1+t2);
  28. return f;
  29. }
  30. }
  31. "#;
  32. let mut vm = build_solidity(file);
  33. let data_account = vm.initialize_data_account();
  34. vm.function("new")
  35. .accounts(vec![("dataAccount", data_account)])
  36. .call();
  37. let _ = vm
  38. .function("assign")
  39. .accounts(vec![("dataAccount", data_account)])
  40. .call();
  41. let returns = vm
  42. .function("pb1")
  43. .accounts(vec![("dataAccount", data_account)])
  44. .call()
  45. .unwrap();
  46. assert_eq!(
  47. returns,
  48. BorshToken::Int {
  49. width: 256,
  50. value: BigInt::from(5u8)
  51. }
  52. );
  53. let returns = vm
  54. .function("test1")
  55. .accounts(vec![("dataAccount", data_account)])
  56. .call()
  57. .unwrap();
  58. assert_eq!(
  59. returns,
  60. BorshToken::Int {
  61. width: 256,
  62. value: BigInt::from(52u8)
  63. }
  64. );
  65. let returns = vm
  66. .function("test2")
  67. .accounts(vec![("dataAccount", data_account)])
  68. .call()
  69. .unwrap();
  70. assert_eq!(
  71. returns,
  72. BorshToken::Int {
  73. width: 256,
  74. value: BigInt::from(5u8)
  75. }
  76. );
  77. }