rational.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::{build_solidity, BorshToken};
  3. use num_bigint::BigInt;
  4. use num_traits::Zero;
  5. #[test]
  6. fn rational() {
  7. let mut vm = build_solidity(
  8. r#"
  9. contract foo {
  10. function test() public returns (uint) {
  11. uint x = .5 * 8;
  12. return x;
  13. }
  14. function test2() public returns (uint) {
  15. uint x = .4 * 8 + 0.8;
  16. return x;
  17. }
  18. }"#,
  19. );
  20. let data_account = vm.initialize_data_account();
  21. vm.function("new")
  22. .accounts(vec![("dataAccount", data_account)])
  23. .call();
  24. let returns = vm.function("test").call().unwrap();
  25. assert_eq!(
  26. returns,
  27. BorshToken::Uint {
  28. width: 256,
  29. value: BigInt::from(4u8)
  30. }
  31. );
  32. let returns = vm.function("test2").call().unwrap();
  33. assert_eq!(
  34. returns,
  35. BorshToken::Uint {
  36. width: 256,
  37. value: BigInt::from(4u8)
  38. }
  39. );
  40. let mut vm = build_solidity(
  41. r#"
  42. contract foo {
  43. function test() public returns (uint) {
  44. uint x = 4.8 + 0.2;
  45. return x;
  46. }
  47. }"#,
  48. );
  49. let data_account = vm.initialize_data_account();
  50. vm.function("new")
  51. .accounts(vec![("dataAccount", data_account)])
  52. .call();
  53. let returns = vm.function("test").call().unwrap();
  54. assert_eq!(
  55. returns,
  56. BorshToken::Uint {
  57. width: 256,
  58. value: BigInt::from(5u8)
  59. }
  60. );
  61. let mut vm = build_solidity(
  62. r#"
  63. contract foo {
  64. function test() public returns (uint) {
  65. uint x = 4.8 / 0.2;
  66. return x;
  67. }
  68. }"#,
  69. );
  70. let data_account = vm.initialize_data_account();
  71. vm.function("new")
  72. .accounts(vec![("dataAccount", data_account)])
  73. .call();
  74. let returns = vm.function("test").call().unwrap();
  75. assert_eq!(
  76. returns,
  77. BorshToken::Uint {
  78. width: 256,
  79. value: BigInt::from(24)
  80. }
  81. );
  82. let mut vm = build_solidity(
  83. r#"
  84. contract foo {
  85. function test() public returns (uint) {
  86. uint x = 4.8 % 0.2;
  87. return x;
  88. }
  89. }"#,
  90. );
  91. let data_account = vm.initialize_data_account();
  92. vm.function("new")
  93. .accounts(vec![("dataAccount", data_account)])
  94. .call();
  95. let returns = vm.function("test").call().unwrap();
  96. assert_eq!(
  97. returns,
  98. BorshToken::Uint {
  99. width: 256,
  100. value: BigInt::zero(),
  101. }
  102. );
  103. let mut vm = build_solidity(
  104. r#"
  105. contract foo {
  106. function test() public returns (uint) {
  107. uint x = 5.2 - 1.2;
  108. return x;
  109. }
  110. }"#,
  111. );
  112. let data_account = vm.initialize_data_account();
  113. vm.function("new")
  114. .accounts(vec![("dataAccount", data_account)])
  115. .call();
  116. let returns = vm.function("test").call().unwrap();
  117. assert_eq!(
  118. returns,
  119. BorshToken::Uint {
  120. width: 256,
  121. value: BigInt::from(4u8),
  122. }
  123. );
  124. let mut vm = build_solidity(
  125. r#"
  126. contract foo {
  127. function test() public returns (uint) {
  128. return 1.4 + 1.6;
  129. }
  130. }"#,
  131. );
  132. let data_account = vm.initialize_data_account();
  133. vm.function("new")
  134. .accounts(vec![("dataAccount", data_account)])
  135. .call();
  136. let returns = vm.function("test").call().unwrap();
  137. assert_eq!(
  138. returns,
  139. BorshToken::Uint {
  140. width: 256,
  141. value: BigInt::from(3u8)
  142. }
  143. );
  144. let mut vm = build_solidity(
  145. r#"
  146. contract foo {
  147. function test() public returns (uint) {
  148. return 1.4e4 + 1.6e3;
  149. }
  150. }"#,
  151. );
  152. let data_account = vm.initialize_data_account();
  153. vm.function("new")
  154. .accounts(vec![("dataAccount", data_account)])
  155. .call();
  156. let returns = vm.function("test").call().unwrap();
  157. assert_eq!(
  158. returns,
  159. BorshToken::Uint {
  160. width: 256,
  161. value: BigInt::from(15600u32)
  162. }
  163. );
  164. let mut vm = build_solidity(
  165. r#"
  166. contract foo {
  167. function test(uint64 x) public returns (uint64, uint) {
  168. return (x * 961748941, 2.5 + 3.5 - 1);
  169. }
  170. }"#,
  171. );
  172. let data_account = vm.initialize_data_account();
  173. vm.function("new")
  174. .accounts(vec![("dataAccount", data_account)])
  175. .call();
  176. let returns = vm
  177. .function("test")
  178. .arguments(&[BorshToken::Uint {
  179. width: 64,
  180. value: BigInt::from(982451653u32),
  181. }])
  182. .call()
  183. .unwrap()
  184. .unwrap_tuple();
  185. assert_eq!(
  186. returns,
  187. vec![
  188. BorshToken::Uint {
  189. width: 64,
  190. value: BigInt::from(961748941u64 * 982451653u64)
  191. },
  192. BorshToken::Uint {
  193. width: 256,
  194. value: BigInt::from(5u8)
  195. },
  196. ]
  197. );
  198. }