| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- // SPDX-License-Identifier: Apache-2.0
- use crate::build_solidity;
- #[test]
- fn using_for_contracts() {
- let mut runtime = build_solidity(
- r#"
- interface I {
- function f(int) external;
- }
- library L {
- function F(I i, bool b, int n) public {
- if (b) {
- print("Hello");
- }
- }
- }
- contract C {
- using L for I;
- function test() public {
- I i = I(address(0));
- i.F(true, 102);
- }
- }"#,
- );
- runtime.constructor(&[]);
- runtime.function("test", &[]);
- assert_eq!(runtime.logs, "Hello");
- let mut runtime = build_solidity(
- r#"
- interface I {
- function f1(int) external;
- function X(int) external;
- }
- library L {
- function f1_2(I i) external {
- i.f1(2);
- }
- function X(I i) external {
- print("X lib");
- }
- }
- contract foo is I {
- using L for I;
- function test() public {
- I i = I(address(this));
- i.X();
- i.X(2);
- i.f1_2();
- }
- function f1(int x) public {
- print("x:{}".format(x));
- }
- function X(int) public {
- print("X contract");
- }
- }"#,
- );
- runtime.constructor(&[]);
- runtime.function("test", &[]);
- assert_eq!(runtime.logs, "X libX contractx:2");
- }
- #[test]
- fn user_defined_oper() {
- let mut runtime = build_solidity(
- r#"
- type Bitmap is int256;
- function eq(Bitmap a, Bitmap b) pure returns (bool) {
- return Bitmap.unwrap(a) == Bitmap.unwrap(b);
- }
- function ne(Bitmap a, Bitmap b) pure returns (bool) {
- return Bitmap.unwrap(a) != Bitmap.unwrap(b);
- }
- function gt(Bitmap a, Bitmap b) pure returns (bool) {
- return Bitmap.unwrap(a) > Bitmap.unwrap(b);
- }
- function gte(Bitmap a, Bitmap b) pure returns (bool) {
- return Bitmap.unwrap(a) >= Bitmap.unwrap(b);
- }
- function lt(Bitmap a, Bitmap b) pure returns (bool) {
- return Bitmap.unwrap(a) < Bitmap.unwrap(b);
- }
- function lte(Bitmap a, Bitmap b) pure returns (bool) {
- return Bitmap.unwrap(a) <= Bitmap.unwrap(b);
- }
- using {eq as ==, ne as !=, lt as <, lte as <=, gt as >, gte as >=} for Bitmap global;
- // arithmetic
- function neg(Bitmap a) pure returns (Bitmap) {
- return Bitmap.wrap(-Bitmap.unwrap(a));
- }
- function sub(Bitmap a, Bitmap b) pure returns (Bitmap) {
- return Bitmap.wrap(Bitmap.unwrap(a) - Bitmap.unwrap(b));
- }
- function add(Bitmap a, Bitmap b) pure returns (Bitmap) {
- return Bitmap.wrap(Bitmap.unwrap(a) + Bitmap.unwrap(b));
- }
- function mul(Bitmap a, Bitmap b) pure returns (Bitmap) {
- return Bitmap.wrap(Bitmap.unwrap(a) * Bitmap.unwrap(b));
- }
- function div(Bitmap a, Bitmap b) pure returns (Bitmap) {
- return Bitmap.wrap(Bitmap.unwrap(a) / Bitmap.unwrap(b));
- }
- function mod(Bitmap a, Bitmap b) pure returns (Bitmap) {
- return Bitmap.wrap(Bitmap.unwrap(a) % Bitmap.unwrap(b));
- }
- using {neg as -, sub as -, add as +, mul as *, div as /, mod as %} for Bitmap global;
- function and(Bitmap a, Bitmap b) pure returns (Bitmap) {
- return Bitmap.wrap(Bitmap.unwrap(a) & Bitmap.unwrap(b));
- }
- function or(Bitmap a, Bitmap b) pure returns (Bitmap) {
- return Bitmap.wrap(Bitmap.unwrap(a) | Bitmap.unwrap(b));
- }
- function xor(Bitmap a, Bitmap b) pure returns (Bitmap) {
- return Bitmap.wrap(Bitmap.unwrap(a) ^ Bitmap.unwrap(b));
- }
- function cpl(Bitmap a) pure returns (Bitmap) {
- return Bitmap.wrap(~Bitmap.unwrap(a));
- }
- using {and as &, or as |, xor as ^, cpl as ~} for Bitmap global;
- contract C {
- Bitmap a;
- function test_cmp() public view {
- Bitmap zero = Bitmap.wrap(0);
- Bitmap one = Bitmap.wrap(1);
- Bitmap one2 = Bitmap.wrap(1);
- assert(zero != one);
- assert(zero < one);
- assert(zero <= one);
- assert(one == one2);
- assert(one <= one2);
- assert(one >= zero);
- assert(one >= one2);
- assert(one > zero);
- }
- function test_arith() public view {
- Bitmap two = Bitmap.wrap(2);
- Bitmap three = Bitmap.wrap(3);
- Bitmap seven = Bitmap.wrap(7);
- assert(Bitmap.unwrap(two + three) == 5);
- assert(Bitmap.unwrap(two - three) == -1);
- assert(Bitmap.unwrap(two * three) == 6);
- assert(Bitmap.unwrap(seven / two) == 3);
- assert(Bitmap.unwrap(seven / two) == 3);
- assert(Bitmap.unwrap(-seven) == -7);
- }
- function test_bit() public view {
- Bitmap two = Bitmap.wrap(2);
- Bitmap three = Bitmap.wrap(3);
- Bitmap seven = Bitmap.wrap(7);
- Bitmap eight = Bitmap.wrap(8);
- assert(Bitmap.unwrap(two | three) == 3);
- assert(Bitmap.unwrap(eight | three) == 11);
- assert(Bitmap.unwrap(eight & three) == 0);
- assert(Bitmap.unwrap(eight & seven) == 0);
- assert(Bitmap.unwrap(two ^ three) == 1);
- assert((Bitmap.unwrap(~three) & 255) == 252);
- }
- }"#,
- );
- runtime.constructor(&[]);
- runtime.function("test_cmp", &[]);
- runtime.function("test_arith", &[]);
- runtime.function("test_bit", &[]);
- }
|