format.rs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::build_solidity;
  3. use parity_scale_codec::Encode;
  4. #[test]
  5. fn output() {
  6. let mut runtime = build_solidity(
  7. r##"
  8. contract format {
  9. function foo(bool x) public {
  10. print("val:{}".format(x));
  11. }
  12. }"##,
  13. );
  14. runtime.constructor(0, Vec::new());
  15. runtime.function("foo", true.encode());
  16. assert_eq!(runtime.printbuf, "val:true");
  17. runtime.printbuf.truncate(0);
  18. runtime.function("foo", false.encode());
  19. assert_eq!(runtime.printbuf, "val:false");
  20. let mut runtime = build_solidity(
  21. r##"
  22. contract format {
  23. function foo(bytes bar) public {
  24. print("bar:{}".format(bar));
  25. }
  26. }"##,
  27. );
  28. runtime.constructor(0, Vec::new());
  29. runtime.function("foo", b"ABCD".to_vec().encode());
  30. assert_eq!(runtime.printbuf, "bar:41424344");
  31. let mut runtime = build_solidity(
  32. r##"
  33. contract format {
  34. function foo(bytes5 bar) public {
  35. print("bar:{}".format(bar));
  36. }
  37. }"##,
  38. );
  39. runtime.constructor(0, Vec::new());
  40. runtime.function("foo", b"\x01\x03\xfe\x07\x09".encode());
  41. assert_eq!(runtime.printbuf, "bar:0103fe0709");
  42. let mut runtime = build_solidity(
  43. r##"
  44. contract format {
  45. function foo(string bar) public {
  46. print("bar:{} address:{}".format(bar, this));
  47. }
  48. }"##,
  49. );
  50. runtime.constructor(0, Vec::new());
  51. runtime.function("foo", "ladida".encode());
  52. assert_eq!(
  53. runtime.printbuf,
  54. format!("bar:ladida address:{}", hex::encode(runtime.vm.account))
  55. );
  56. let mut runtime = build_solidity(
  57. r##"
  58. contract format {
  59. function foo(uint64 bar) public {
  60. print("bar:{:x}".format(bar));
  61. }
  62. }"##,
  63. );
  64. runtime.constructor(0, Vec::new());
  65. runtime.function("foo", 0xcafedu64.encode());
  66. assert_eq!(runtime.printbuf, "bar:0xcafed");
  67. runtime.printbuf.truncate(0);
  68. runtime.function("foo", 0x1u64.encode());
  69. assert_eq!(runtime.printbuf, "bar:0x1");
  70. runtime.printbuf.truncate(0);
  71. runtime.function("foo", 0x0u64.encode());
  72. assert_eq!(runtime.printbuf, "bar:0x0");
  73. let mut runtime = build_solidity(
  74. r##"
  75. contract format {
  76. function foo(int128 bar) public {
  77. print("bar:{:x}".format(bar));
  78. }
  79. }"##,
  80. );
  81. runtime.constructor(0, Vec::new());
  82. runtime.function("foo", (-0xca5cadab1efeeb1eeffab1ei128).encode());
  83. assert_eq!(runtime.printbuf, "bar:-0xca5cadab1efeeb1eeffab1e");
  84. let mut runtime = build_solidity(
  85. r##"
  86. contract format {
  87. function foo(int128 bar) public {
  88. print("there is an old android joke which goes {:b} ".format(bar));
  89. }
  90. }"##,
  91. );
  92. runtime.constructor(0, Vec::new());
  93. runtime.function("foo", (0x3fi128).encode());
  94. runtime.function("foo", (-0x3fi128).encode());
  95. assert_eq!(
  96. runtime.printbuf,
  97. "there is an old android joke which goes 0b111111 there is an old android joke which goes -0b111111 "
  98. );
  99. let mut runtime = build_solidity(
  100. r##"
  101. contract format {
  102. function foo(int64 bar) public {
  103. print("number:{} ".format(bar));
  104. }
  105. }"##,
  106. );
  107. runtime.constructor(0, Vec::new());
  108. runtime.function("foo", (102i64).encode());
  109. runtime.function("foo", (-102i64).encode());
  110. assert_eq!(runtime.printbuf, "number:102 number:-102 ");
  111. let mut runtime = build_solidity(
  112. r##"
  113. contract format {
  114. function foo(int128 bar) public {
  115. print("number:{} ".format(bar));
  116. }
  117. }"##,
  118. );
  119. runtime.constructor(0, Vec::new());
  120. runtime.function("foo", (8462643383279502884i128).encode());
  121. assert_eq!(runtime.printbuf, "number:8462643383279502884 ");
  122. runtime.printbuf.truncate(0);
  123. runtime.function("foo", (18462643383279502884i128).encode());
  124. assert_eq!(runtime.printbuf, "number:18462643383279502884 ");
  125. runtime.printbuf.truncate(0);
  126. runtime.function("foo", (3141592653589793238462643383279502884i128).encode());
  127. runtime.function("foo", (-3141592653589793238462643383279502884i128).encode());
  128. assert_eq!(runtime.printbuf, "number:3141592653589793238462643383279502884 number:-3141592653589793238462643383279502884 ");
  129. runtime.printbuf.truncate(0);
  130. let mut runtime = build_solidity(
  131. r##"
  132. contract format {
  133. enum enum1 { bar1, bar2, bar3 }
  134. function foo(int256 bar) public {
  135. print("number:{} ".format(bar));
  136. }
  137. function hex(int256 bar) public {
  138. print("number:{:x} ".format(bar));
  139. }
  140. function unsigned(uint256 bar) public {
  141. print("number:{} ".format(bar));
  142. }
  143. function e() public returns (string) {
  144. return "number<{}>".format(enum1.bar3);
  145. }
  146. }"##,
  147. );
  148. runtime.constructor(0, Vec::new());
  149. runtime.function("foo", (0u128, 102u128).encode());
  150. runtime.function("foo", (0u128, -102i128).encode());
  151. assert_eq!(
  152. runtime.printbuf,
  153. "number:34708801425935723273264209958040357568512 number:-34708801425935723273264209958040357568512 "
  154. );
  155. runtime.printbuf.truncate(0);
  156. runtime.function("hex", (0u128, 0x102u128).encode());
  157. runtime.function("unsigned", (0u128, 102i128).encode());
  158. assert_eq!(
  159. runtime.printbuf,
  160. "number:0x10200000000000000000000000000000000 number:34708801425935723273264209958040357568512 "
  161. );
  162. runtime.function("e", Vec::new());
  163. assert_eq!(runtime.vm.output, "number<2>".encode());
  164. }
  165. #[test]
  166. fn div128() {
  167. let mut runtime = build_solidity(
  168. r##"
  169. contract div {
  170. function foo(uint128 bar) public returns (uint128) {
  171. return bar / 1_00000_00000_00000_00000;
  172. }
  173. function rem(uint128 bar) public returns (uint128) {
  174. return bar % 1_00000_00000_00000_00000;
  175. }
  176. }"##,
  177. );
  178. runtime.constructor(0, Vec::new());
  179. runtime.function("foo", (3141592653589793238462643383279502884u128).encode());
  180. assert_eq!(runtime.vm.output, 31415926535897932u128.encode());
  181. runtime.function("rem", (3141592653589793238462643383279502884u128).encode());
  182. assert_eq!(runtime.vm.output, 38462643383279502884u128.encode());
  183. runtime.function("rem", (18462643383279502884i128).encode());
  184. assert_eq!(runtime.vm.output, 18462643383279502884i128.encode());
  185. }