format.rs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.debug_buffer(), "print: val:true,\n");
  17. runtime.debug_buffer().truncate(0);
  18. runtime.function("foo", false.encode());
  19. assert_eq!(runtime.debug_buffer(), "print: val:false,\n");
  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.debug_buffer(), "print: bar:41424344,\n");
  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.debug_buffer(), "print: bar:0103fe0709,\n");
  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.debug_buffer(),
  54. format!(
  55. "print: bar:ladida address:{},\n",
  56. hex::encode(runtime.caller())
  57. )
  58. );
  59. let mut runtime = build_solidity(
  60. r#"
  61. contract format {
  62. function foo(uint64 bar) public {
  63. print("bar:{:x}".format(bar));
  64. }
  65. }"#,
  66. );
  67. runtime.constructor(0, Vec::new());
  68. runtime.function("foo", 0xcafedu64.encode());
  69. assert_eq!(runtime.debug_buffer(), "print: bar:0xcafed,\n");
  70. runtime.debug_buffer().truncate(0);
  71. runtime.function("foo", 0x1u64.encode());
  72. assert_eq!(runtime.debug_buffer(), "print: bar:0x1,\n");
  73. runtime.debug_buffer().truncate(0);
  74. runtime.function("foo", 0x0u64.encode());
  75. assert_eq!(runtime.debug_buffer(), "print: bar:0x0,\n");
  76. let mut runtime = build_solidity(
  77. r#"
  78. contract format {
  79. function foo(int128 bar) public {
  80. print("bar:{:x}".format(bar));
  81. }
  82. }"#,
  83. );
  84. runtime.constructor(0, Vec::new());
  85. runtime.function("foo", (-0xca5cadab1efeeb1eeffab1ei128).encode());
  86. assert_eq!(
  87. runtime.debug_buffer(),
  88. "print: bar:-0xca5cadab1efeeb1eeffab1e,\n"
  89. );
  90. let mut runtime = build_solidity(
  91. r#"
  92. contract format {
  93. function foo(int128 bar) public {
  94. print("there is an old android joke which goes {:b} ".format(bar));
  95. }
  96. }"#,
  97. );
  98. runtime.constructor(0, Vec::new());
  99. runtime.function("foo", (0x3fi128).encode());
  100. assert!(runtime.debug_buffer().contains("goes 0b111111 ,"));
  101. runtime.function("foo", (-0x3fi128).encode());
  102. assert!(runtime.debug_buffer().contains("goes -0b111111 ,"));
  103. let mut runtime = build_solidity(
  104. r#"
  105. contract format {
  106. function foo(int64 bar) public {
  107. print("number:{} ".format(bar));
  108. }
  109. }"#,
  110. );
  111. runtime.constructor(0, Vec::new());
  112. runtime.function("foo", (102i64).encode());
  113. assert!(runtime.debug_buffer().contains("print: number:102 ,"));
  114. runtime.function("foo", (-102i64).encode());
  115. assert!(runtime.debug_buffer().contains("print: number:-102 ,"));
  116. let mut runtime = build_solidity(
  117. r#"
  118. contract format {
  119. function foo(int128 bar) public {
  120. print("number:{} ".format(bar));
  121. }
  122. }"#,
  123. );
  124. runtime.constructor(0, Vec::new());
  125. runtime.function("foo", (8462643383279502884i128).encode());
  126. assert_eq!(
  127. runtime.debug_buffer(),
  128. "print: number:8462643383279502884 ,\n"
  129. );
  130. runtime.debug_buffer().truncate(0);
  131. runtime.function("foo", (18462643383279502884i128).encode());
  132. assert_eq!(
  133. runtime.debug_buffer(),
  134. "print: number:18462643383279502884 ,\n"
  135. );
  136. runtime.debug_buffer().truncate(0);
  137. runtime.function("foo", (3141592653589793238462643383279502884i128).encode());
  138. assert!(runtime
  139. .debug_buffer()
  140. .contains("number:3141592653589793238462643383279502884 ,"));
  141. runtime.function("foo", (-3141592653589793238462643383279502884i128).encode());
  142. assert!(runtime
  143. .debug_buffer()
  144. .contains("number:-3141592653589793238462643383279502884 ,"));
  145. runtime.debug_buffer().truncate(0);
  146. let mut runtime = build_solidity(
  147. r#"
  148. contract format {
  149. enum enum1 { bar1, bar2, bar3 }
  150. function foo(int256 bar) public {
  151. print("number:{} ".format(bar));
  152. }
  153. function hex(int256 bar) public {
  154. print("number:{:x} ".format(bar));
  155. }
  156. function unsigned(uint256 bar) public {
  157. print("number:{} ".format(bar));
  158. }
  159. function e() public returns (string) {
  160. return "number<{}>".format(enum1.bar3);
  161. }
  162. }"#,
  163. );
  164. runtime.constructor(0, Vec::new());
  165. runtime.function("foo", (0u128, 102u128).encode());
  166. assert!(runtime
  167. .debug_buffer()
  168. .contains("number:34708801425935723273264209958040357568512 ,"));
  169. runtime.function("foo", (0u128, -102i128).encode());
  170. assert!(runtime
  171. .debug_buffer()
  172. .contains("number:-34708801425935723273264209958040357568512 ,"));
  173. runtime.debug_buffer().truncate(0);
  174. runtime.function("hex", (0u128, 0x102u128).encode());
  175. assert!(runtime
  176. .debug_buffer()
  177. .contains("number:0x10200000000000000000000000000000000 ,"));
  178. runtime.function("unsigned", (0u128, 102i128).encode());
  179. assert!(runtime
  180. .debug_buffer()
  181. .contains("number:34708801425935723273264209958040357568512 ,"));
  182. runtime.function("e", Vec::new());
  183. assert_eq!(runtime.output(), "number<2>".encode());
  184. }
  185. #[test]
  186. fn div128() {
  187. let mut runtime = build_solidity(
  188. r##"
  189. contract div {
  190. function foo(uint128 bar) public returns (uint128) {
  191. return bar / 1_00000_00000_00000_00000;
  192. }
  193. function rem(uint128 bar) public returns (uint128) {
  194. return bar % 1_00000_00000_00000_00000;
  195. }
  196. }"##,
  197. );
  198. runtime.constructor(0, Vec::new());
  199. runtime.function("foo", (3141592653589793238462643383279502884u128).encode());
  200. assert_eq!(runtime.output(), 31415926535897932u128.encode());
  201. runtime.function("rem", (3141592653589793238462643383279502884u128).encode());
  202. assert_eq!(runtime.output(), 38462643383279502884u128.encode());
  203. runtime.function("rem", (18462643383279502884i128).encode());
  204. assert_eq!(runtime.output(), 18462643383279502884i128.encode());
  205. }