unused_variable_elimination.rs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. vm.constructor(&[]);
  34. let _ = vm.function("assign", &[]);
  35. let returns = vm.function("pb1", &[]).unwrap();
  36. assert_eq!(
  37. returns,
  38. BorshToken::Int {
  39. width: 256,
  40. value: BigInt::from(5u8)
  41. }
  42. );
  43. let returns = vm.function("test1", &[]).unwrap();
  44. assert_eq!(
  45. returns,
  46. BorshToken::Int {
  47. width: 256,
  48. value: BigInt::from(52u8)
  49. }
  50. );
  51. let returns = vm.function("test2", &[]).unwrap();
  52. assert_eq!(
  53. returns,
  54. BorshToken::Int {
  55. width: 256,
  56. value: BigInt::from(5u8)
  57. }
  58. );
  59. }