statements.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::build_solidity;
  3. use parity_scale_codec::Encode;
  4. #[test]
  5. fn destruct_from_array() {
  6. #[derive(Encode)]
  7. struct Arg {
  8. values: Vec<u32>,
  9. fakes: Vec<bool>,
  10. id: Vec<u8>,
  11. }
  12. let mut runtime = build_solidity(
  13. "
  14. contract BlindAuction {
  15. function reveal(
  16. uint32[] values,
  17. bool[] fakes,
  18. bytes memory id
  19. ) public pure returns (bool) {
  20. (uint32 value, bool fake) = (values[0], fakes[0]);
  21. return id == abi.encodePacked(value, fake);
  22. }
  23. }",
  24. );
  25. let values = vec![1];
  26. let fakes = vec![true];
  27. let id = vec![1, 0, 0, 0, 1];
  28. runtime.function("reveal", Arg { values, fakes, id }.encode());
  29. assert_eq!(runtime.output(), true.encode());
  30. }
  31. #[test]
  32. fn destruct_from_struct() {
  33. #[derive(Encode)]
  34. struct S1(Vec<u32>);
  35. #[derive(Encode)]
  36. struct S2(Vec<bool>);
  37. #[derive(Encode)]
  38. struct Arg {
  39. values: S1,
  40. fakes: S2,
  41. id: Vec<u8>,
  42. }
  43. let mut runtime = build_solidity(
  44. "
  45. contract BlindAuction {
  46. struct S1 { uint32[] u; }
  47. struct S2 { bool[] b; }
  48. function reveal(
  49. S1 values,
  50. S2 fakes,
  51. bytes memory id
  52. ) external pure returns (bool) {
  53. (uint32 value, bool fake) = (values.u[0], fakes.b[0]);
  54. return id == abi.encodePacked(value, fake);
  55. }
  56. }",
  57. );
  58. let values = S1(vec![1]);
  59. let fakes = S2(vec![true]);
  60. let id = vec![1, 0, 0, 0, 1];
  61. runtime.function("reveal", Arg { values, fakes, id }.encode());
  62. assert_eq!(runtime.output(), true.encode());
  63. }