integer_width_rounding.rs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::build_solidity;
  3. use soroban_sdk::{IntoVal, Val};
  4. #[test]
  5. fn test_int56_rounds_to_int64() {
  6. let runtime = build_solidity(
  7. r#"contract test {
  8. function test_int56(int56 a) public returns (int64) {
  9. return int64(a);
  10. }
  11. }"#,
  12. |_| {},
  13. );
  14. // Check that the function compiles and works with the rounded type
  15. let arg: Val = 42_i64.into_val(&runtime.env);
  16. let addr = runtime.contracts.last().unwrap();
  17. let res = runtime.invoke_contract(addr, "test_int56", vec![arg]);
  18. let expected: Val = 42_i64.into_val(&runtime.env);
  19. assert!(expected.shallow_eq(&res));
  20. }
  21. #[test]
  22. fn test_uint56_rounds_to_uint64() {
  23. let runtime = build_solidity(
  24. r#"contract test {
  25. function test_uint56(uint56 a) public returns (uint64) {
  26. return uint64(a);
  27. }
  28. }"#,
  29. |_| {},
  30. );
  31. let arg: Val = 42_u64.into_val(&runtime.env);
  32. let addr = runtime.contracts.last().unwrap();
  33. let res = runtime.invoke_contract(addr, "test_uint56", vec![arg]);
  34. let expected: Val = 42_u64.into_val(&runtime.env);
  35. assert!(expected.shallow_eq(&res));
  36. }
  37. #[test]
  38. fn test_int96_rounds_to_int128() {
  39. let runtime = build_solidity(
  40. r#"contract test {
  41. function test_int96(int96 a) public returns (int128) {
  42. return int128(a);
  43. }
  44. }"#,
  45. |_| {},
  46. );
  47. let arg: Val = 42_i128.into_val(&runtime.env);
  48. let addr = runtime.contracts.last().unwrap();
  49. let res = runtime.invoke_contract(addr, "test_int96", vec![arg]);
  50. let expected: Val = 42_i128.into_val(&runtime.env);
  51. assert!(expected.shallow_eq(&res));
  52. }
  53. #[test]
  54. fn test_uint96_rounds_to_uint128() {
  55. let runtime = build_solidity(
  56. r#"contract test {
  57. function test_uint96(uint96 a) public returns (uint128) {
  58. return uint128(a);
  59. }
  60. }"#,
  61. |_| {},
  62. );
  63. let arg: Val = 42_u128.into_val(&runtime.env);
  64. let addr = runtime.contracts.last().unwrap();
  65. let res = runtime.invoke_contract(addr, "test_uint96", vec![arg]);
  66. let expected: Val = 42_u128.into_val(&runtime.env);
  67. assert!(expected.shallow_eq(&res));
  68. }