using.rs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::build_solidity;
  3. #[test]
  4. fn using_for_contracts() {
  5. let mut runtime = build_solidity(
  6. r#"
  7. interface I {
  8. function f(int) external;
  9. }
  10. library L {
  11. function F(I i, bool b, int n) public {
  12. if (b) {
  13. print("Hello");
  14. }
  15. }
  16. }
  17. contract C {
  18. using L for I;
  19. function test() public {
  20. I i = I(address(0));
  21. i.F(true, 102);
  22. }
  23. }"#,
  24. );
  25. runtime.constructor(&[]);
  26. runtime.function("test", &[]);
  27. assert_eq!(runtime.logs, "Hello");
  28. let mut runtime = build_solidity(
  29. r#"
  30. interface I {
  31. function f1(int) external;
  32. function X(int) external;
  33. }
  34. library L {
  35. function f1_2(I i) external {
  36. i.f1(2);
  37. }
  38. function X(I i) external {
  39. print("X lib");
  40. }
  41. }
  42. contract foo is I {
  43. using L for I;
  44. function test() public {
  45. I i = I(address(this));
  46. i.X();
  47. i.X(2);
  48. i.f1_2();
  49. }
  50. function f1(int x) public {
  51. print("x:{}".format(x));
  52. }
  53. function X(int) public {
  54. print("X contract");
  55. }
  56. }"#,
  57. );
  58. runtime.constructor(&[]);
  59. runtime.function("test", &[]);
  60. assert_eq!(runtime.logs, "X libX contractx:2");
  61. }
  62. #[test]
  63. fn user_defined_oper() {
  64. let mut runtime = build_solidity(
  65. r#"
  66. type Bitmap is int256;
  67. function eq(Bitmap a, Bitmap b) pure returns (bool) {
  68. return Bitmap.unwrap(a) == Bitmap.unwrap(b);
  69. }
  70. function ne(Bitmap a, Bitmap b) pure returns (bool) {
  71. return Bitmap.unwrap(a) != Bitmap.unwrap(b);
  72. }
  73. function gt(Bitmap a, Bitmap b) pure returns (bool) {
  74. return Bitmap.unwrap(a) > Bitmap.unwrap(b);
  75. }
  76. function gte(Bitmap a, Bitmap b) pure returns (bool) {
  77. return Bitmap.unwrap(a) >= Bitmap.unwrap(b);
  78. }
  79. function lt(Bitmap a, Bitmap b) pure returns (bool) {
  80. return Bitmap.unwrap(a) < Bitmap.unwrap(b);
  81. }
  82. function lte(Bitmap a, Bitmap b) pure returns (bool) {
  83. return Bitmap.unwrap(a) <= Bitmap.unwrap(b);
  84. }
  85. using {eq as ==, ne as !=, lt as <, lte as <=, gt as >, gte as >=} for Bitmap global;
  86. // arithmetic
  87. function neg(Bitmap a) pure returns (Bitmap) {
  88. return Bitmap.wrap(-Bitmap.unwrap(a));
  89. }
  90. function sub(Bitmap a, Bitmap b) pure returns (Bitmap) {
  91. return Bitmap.wrap(Bitmap.unwrap(a) - Bitmap.unwrap(b));
  92. }
  93. function add(Bitmap a, Bitmap b) pure returns (Bitmap) {
  94. return Bitmap.wrap(Bitmap.unwrap(a) + Bitmap.unwrap(b));
  95. }
  96. function mul(Bitmap a, Bitmap b) pure returns (Bitmap) {
  97. return Bitmap.wrap(Bitmap.unwrap(a) * Bitmap.unwrap(b));
  98. }
  99. function div(Bitmap a, Bitmap b) pure returns (Bitmap) {
  100. return Bitmap.wrap(Bitmap.unwrap(a) / Bitmap.unwrap(b));
  101. }
  102. function mod(Bitmap a, Bitmap b) pure returns (Bitmap) {
  103. return Bitmap.wrap(Bitmap.unwrap(a) % Bitmap.unwrap(b));
  104. }
  105. using {neg as -, sub as -, add as +, mul as *, div as /, mod as %} for Bitmap global;
  106. function and(Bitmap a, Bitmap b) pure returns (Bitmap) {
  107. return Bitmap.wrap(Bitmap.unwrap(a) & Bitmap.unwrap(b));
  108. }
  109. function or(Bitmap a, Bitmap b) pure returns (Bitmap) {
  110. return Bitmap.wrap(Bitmap.unwrap(a) | Bitmap.unwrap(b));
  111. }
  112. function xor(Bitmap a, Bitmap b) pure returns (Bitmap) {
  113. return Bitmap.wrap(Bitmap.unwrap(a) ^ Bitmap.unwrap(b));
  114. }
  115. function cpl(Bitmap a) pure returns (Bitmap) {
  116. return Bitmap.wrap(~Bitmap.unwrap(a));
  117. }
  118. using {and as &, or as |, xor as ^, cpl as ~} for Bitmap global;
  119. contract C {
  120. Bitmap a;
  121. function test_cmp() public view {
  122. Bitmap zero = Bitmap.wrap(0);
  123. Bitmap one = Bitmap.wrap(1);
  124. Bitmap one2 = Bitmap.wrap(1);
  125. assert(zero != one);
  126. assert(zero < one);
  127. assert(zero <= one);
  128. assert(one == one2);
  129. assert(one <= one2);
  130. assert(one >= zero);
  131. assert(one >= one2);
  132. assert(one > zero);
  133. }
  134. function test_arith() public view {
  135. Bitmap two = Bitmap.wrap(2);
  136. Bitmap three = Bitmap.wrap(3);
  137. Bitmap seven = Bitmap.wrap(7);
  138. assert(Bitmap.unwrap(two + three) == 5);
  139. assert(Bitmap.unwrap(two - three) == -1);
  140. assert(Bitmap.unwrap(two * three) == 6);
  141. assert(Bitmap.unwrap(seven / two) == 3);
  142. assert(Bitmap.unwrap(seven / two) == 3);
  143. assert(Bitmap.unwrap(-seven) == -7);
  144. }
  145. function test_bit() public view {
  146. Bitmap two = Bitmap.wrap(2);
  147. Bitmap three = Bitmap.wrap(3);
  148. Bitmap seven = Bitmap.wrap(7);
  149. Bitmap eight = Bitmap.wrap(8);
  150. assert(Bitmap.unwrap(two | three) == 3);
  151. assert(Bitmap.unwrap(eight | three) == 11);
  152. assert(Bitmap.unwrap(eight & three) == 0);
  153. assert(Bitmap.unwrap(eight & seven) == 0);
  154. assert(Bitmap.unwrap(two ^ three) == 1);
  155. assert((Bitmap.unwrap(~three) & 255) == 252);
  156. }
  157. }"#,
  158. );
  159. runtime.constructor(&[]);
  160. runtime.function("test_cmp", &[]);
  161. runtime.function("test_arith", &[]);
  162. runtime.function("test_bit", &[]);
  163. }