array_boundary_check.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::build_solidity;
  3. use parity_scale_codec::{Decode, Encode};
  4. #[test]
  5. fn array_boundary_check() {
  6. #[derive(Encode, Decode)]
  7. struct SetArg(u32);
  8. #[derive(Encode, Decode)]
  9. struct BooleanArg(bool);
  10. let mut contract = build_solidity(
  11. r#"
  12. contract Array_bound_Test {
  13. function array_bound() public pure {
  14. uint256[] a = new uint256[](10);
  15. uint256 sesa = 0;
  16. if (1 > 2) {
  17. a.push(5);
  18. } else {
  19. a.push(1);
  20. }
  21. for (uint256 i = 0; i < a.length; i++) {
  22. sesa = sesa + a[10];
  23. }
  24. assert(sesa == 11);
  25. }
  26. }
  27. "#,
  28. );
  29. contract.function("array_bound", Vec::new());
  30. let mut contract = build_solidity(
  31. r#"
  32. contract Array_bound_Test {
  33. function array_bound(uint32 size32) public {
  34. uint256[] c = new uint256[](size32);
  35. uint256[] d = new uint256[](20);
  36. uint32 sesa = c.length + d.length;
  37. assert(sesa == 31);
  38. }
  39. }
  40. "#,
  41. );
  42. contract.function("array_bound", SetArg(11).encode());
  43. let mut contract = build_solidity(
  44. r#"
  45. contract c {
  46. function test(bool cond) public returns (uint32) {
  47. bool[] b = new bool[](100);
  48. if (cond) {
  49. b.push(true);
  50. }
  51. assert(b.length == 101);
  52. if (cond) {
  53. b.pop();
  54. b.pop();
  55. }
  56. assert(b.length == 99);
  57. return b.length;
  58. }
  59. }
  60. "#,
  61. );
  62. contract.function("test", BooleanArg(true).encode());
  63. let mut contract = build_solidity(
  64. r#"
  65. contract c {
  66. function test_for_loop() public {
  67. uint256[] a = new uint256[](20);
  68. a.push(1);
  69. uint256 sesa = 0;
  70. for (uint256 i = 0; i < a.length; i++) {
  71. sesa = sesa + a[20];
  72. }
  73. assert(sesa == 21);
  74. }
  75. }
  76. "#,
  77. );
  78. contract.function("test_for_loop", Vec::new());
  79. let mut contract = build_solidity(
  80. r#"
  81. contract c {
  82. function test_loop_2() public {
  83. int256[] vec = new int256[](10);
  84. for (int256 i = 0; i < 5; i++) {
  85. if (vec.length > 20) {
  86. break;
  87. }
  88. vec.push(3);
  89. }
  90. assert (vec.length == 15);
  91. }
  92. }
  93. "#,
  94. );
  95. contract.function("test_loop_2", Vec::new());
  96. let mut contract = build_solidity(
  97. r#"
  98. contract foo {
  99. function fool() public {
  100. int256[] a = new int256[](3);
  101. // copy by reference/pointer
  102. int256[] b = a;
  103. b = new int256[](6);
  104. assert(a.length == 3);
  105. assert(b.length == 6);
  106. }
  107. function fool2() public {
  108. int256[] a = new int256[](3);
  109. // copy by reference/pointer
  110. int256[] b = a;
  111. a.pop();
  112. assert(a.length == b.length);
  113. assert(a.length == 2);
  114. assert(b.length == 2);
  115. // now both a and b have length 2.
  116. }
  117. }
  118. "#,
  119. );
  120. contract.function("fool", Vec::new());
  121. contract.function("fool2", Vec::new());
  122. let mut contract = build_solidity(
  123. r#"
  124. contract c {
  125. function nested_assign() public pure {
  126. uint32[] a;
  127. uint32 b = (a = new uint32[](4))[0];
  128. a.pop();
  129. assert(a.length == 3);
  130. }
  131. function edgy() public pure {
  132. uint256[] a;
  133. uint256[] b;
  134. uint256[] c = new uint256[](30);
  135. a = b = c = new uint256[](40);
  136. a.pop();
  137. assert(a.length == b.length);
  138. assert(b.length == c.length);
  139. assert(c.length == 39);
  140. }
  141. function edgy_2() public pure {
  142. uint256[] a;
  143. uint256[] b;
  144. uint256[] c = new uint256[](30);
  145. a = b = c;
  146. a.pop();
  147. assert(a.length == b.length);
  148. assert(b.length == c.length);
  149. assert(c.length == 29);
  150. }
  151. }
  152. "#,
  153. );
  154. contract.function("nested_assign", Vec::new());
  155. contract.function("edgy", Vec::new());
  156. contract.function("edgy_2", Vec::new());
  157. }