User-defined operators: Tests

This commit is contained in:
wechman
2023-02-22 00:40:03 +01:00
committed by Kamil Śliwak
parent 5b5e853ea0
commit aba5ac5e2a
114 changed files with 3956 additions and 1 deletions
@@ -0,0 +1,92 @@
type Int is int8;
using {
bitor as |, bitand as &, bitxor as ^, bitnot as ~,
add as +, sub as -, unsub as -, mul as *, div as /, mod as %,
eq as ==, noteq as !=, lt as <, gt as >, leq as <=, geq as >=
} for Int global;
function bitor(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) | Int.unwrap(y)); }
function bitand(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) & Int.unwrap(y)); }
function bitxor(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) ^ Int.unwrap(y)); }
function bitnot(Int x) pure returns (Int) { return Int.wrap(~Int.unwrap(x)); }
function add(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) + Int.unwrap(y)); }
function sub(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) - Int.unwrap(y)); }
function unsub(Int x) pure returns (Int) { return Int.wrap(-Int.unwrap(x)); }
function mul(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) * Int.unwrap(y)); }
function div(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) / Int.unwrap(y)); }
function mod(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) % Int.unwrap(y)); }
function eq(Int x, Int y) pure returns (bool) { return Int.unwrap(x) == Int.unwrap(y); }
function noteq(Int x, Int y) pure returns (bool) { return Int.unwrap(x) != Int.unwrap(y); }
function lt(Int x, Int y) pure returns (bool) { return Int.unwrap(x) < Int.unwrap(y); }
function gt(Int x, Int y) pure returns (bool) { return Int.unwrap(x) > Int.unwrap(y); }
function leq(Int x, Int y) pure returns (bool) { return Int.unwrap(x) <= Int.unwrap(y); }
function geq(Int x, Int y) pure returns (bool) { return Int.unwrap(x) >= Int.unwrap(y); }
contract C {
Int constant ZERO = Int.wrap(0);
Int constant ONE = Int.wrap(1);
Int constant TWO = Int.wrap(2);
Int constant THREE = Int.wrap(3);
Int constant SIX = Int.wrap(6);
function testBitwise() public pure {
assert(Int.unwrap(ONE | TWO) == 3);
assert(Int.unwrap(ONE | ZERO) == 1);
assert(Int.unwrap(ONE & THREE) == 1);
assert(Int.unwrap(ONE & ONE) == 1);
assert(Int.unwrap(TWO ^ TWO) == 0);
assert(Int.unwrap(TWO ^ ONE) == 3);
assert(Int.unwrap(~ZERO) == -1);
assert(Int.unwrap(~ONE) == -2);
assert(Int.unwrap(~TWO) == -3);
}
function testArithmetic() public pure {
assert(Int.unwrap(ONE + TWO) == 3);
assert(Int.unwrap(ONE + ZERO) == 1);
assert(Int.unwrap(TWO - ONE) == 1);
assert(Int.unwrap(THREE - THREE) == 0);
assert(Int.unwrap(-TWO) == -2);
assert(Int.unwrap(-ZERO) == 0);
assert(Int.unwrap(ONE * ONE) == 1);
assert(Int.unwrap(THREE * TWO) == 6);
assert(Int.unwrap(SIX / TWO) == 3);
assert(Int.unwrap(THREE / TWO) == 1);
assert(Int.unwrap(SIX % TWO) == 0);
assert(Int.unwrap(THREE % TWO) == 1);
}
function testComparison() public pure {
assert((ONE == ONE) == true);
assert((ONE == TWO) == false);
assert((ONE != ONE) == false);
assert((ONE != TWO) == true);
assert((ONE < TWO) == true);
assert((TWO < ONE) == false);
assert((ONE <= TWO) == true);
assert((TWO <= ONE) == false);
assert((ONE > TWO) == false);
assert((TWO > ONE) == true);
assert((ONE >= TWO) == false);
assert((TWO >= ONE) == true);
}
}
// ----
// testBitwise() ->
// testArithmetic() ->
// testComparison() ->
@@ -0,0 +1,670 @@
type Int8 is int8;
type Int16 is int16;
type Int24 is int24;
type Int32 is int32;
type Int40 is int40;
type Int48 is int48;
type Int56 is int56;
type Int64 is int64;
type Int72 is int72;
type Int80 is int80;
type Int88 is int88;
type Int96 is int96;
type Int104 is int104;
type Int112 is int112;
type Int120 is int120;
type Int128 is int128;
type Int136 is int136;
type Int144 is int144;
type Int152 is int152;
type Int160 is int160;
type Int168 is int168;
type Int176 is int176;
type Int184 is int184;
type Int192 is int192;
type Int200 is int200;
type Int208 is int208;
type Int216 is int216;
type Int224 is int224;
type Int232 is int232;
type Int240 is int240;
type Int248 is int248;
type Int256 is int256;
type Int is int;
type Uint8 is uint8;
type Uint16 is uint16;
type Uint24 is uint24;
type Uint32 is uint32;
type Uint40 is uint40;
type Uint48 is uint48;
type Uint56 is uint56;
type Uint64 is uint64;
type Uint72 is uint72;
type Uint80 is uint80;
type Uint88 is uint88;
type Uint96 is uint96;
type Uint104 is uint104;
type Uint112 is uint112;
type Uint120 is uint120;
type Uint128 is uint128;
type Uint136 is uint136;
type Uint144 is uint144;
type Uint152 is uint152;
type Uint160 is uint160;
type Uint168 is uint168;
type Uint176 is uint176;
type Uint184 is uint184;
type Uint192 is uint192;
type Uint200 is uint200;
type Uint208 is uint208;
type Uint216 is uint216;
type Uint224 is uint224;
type Uint232 is uint232;
type Uint240 is uint240;
type Uint248 is uint248;
type Uint256 is uint256;
type Uint is uint;
type Bytes1 is bytes1;
type Bytes2 is bytes2;
type Bytes3 is bytes3;
type Bytes4 is bytes4;
type Bytes5 is bytes5;
type Bytes6 is bytes6;
type Bytes7 is bytes7;
type Bytes8 is bytes8;
type Bytes9 is bytes9;
type Bytes10 is bytes10;
type Bytes11 is bytes11;
type Bytes12 is bytes12;
type Bytes13 is bytes13;
type Bytes14 is bytes14;
type Bytes15 is bytes15;
type Bytes16 is bytes16;
type Bytes17 is bytes17;
type Bytes18 is bytes18;
type Bytes19 is bytes19;
type Bytes20 is bytes20;
type Bytes21 is bytes21;
type Bytes22 is bytes22;
type Bytes23 is bytes23;
type Bytes24 is bytes24;
type Bytes25 is bytes25;
type Bytes26 is bytes26;
type Bytes27 is bytes27;
type Bytes28 is bytes28;
type Bytes29 is bytes29;
type Bytes30 is bytes30;
type Bytes31 is bytes31;
type Bytes32 is bytes32;
type Address is address;
type AddressPayable is address payable;
type Bool is bool;
using {bitorInt8 as |, unsubInt8 as -} for Int8 global;
using {bitorInt16 as |, unsubInt16 as -} for Int16 global;
using {bitorInt24 as |, unsubInt24 as -} for Int24 global;
using {bitorInt32 as |, unsubInt32 as -} for Int32 global;
using {bitorInt40 as |, unsubInt40 as -} for Int40 global;
using {bitorInt48 as |, unsubInt48 as -} for Int48 global;
using {bitorInt56 as |, unsubInt56 as -} for Int56 global;
using {bitorInt64 as |, unsubInt64 as -} for Int64 global;
using {bitorInt72 as |, unsubInt72 as -} for Int72 global;
using {bitorInt80 as |, unsubInt80 as -} for Int80 global;
using {bitorInt88 as |, unsubInt88 as -} for Int88 global;
using {bitorInt96 as |, unsubInt96 as -} for Int96 global;
using {bitorInt104 as |, unsubInt104 as -} for Int104 global;
using {bitorInt112 as |, unsubInt112 as -} for Int112 global;
using {bitorInt120 as |, unsubInt120 as -} for Int120 global;
using {bitorInt128 as |, unsubInt128 as -} for Int128 global;
using {bitorInt136 as |, unsubInt136 as -} for Int136 global;
using {bitorInt144 as |, unsubInt144 as -} for Int144 global;
using {bitorInt152 as |, unsubInt152 as -} for Int152 global;
using {bitorInt160 as |, unsubInt160 as -} for Int160 global;
using {bitorInt168 as |, unsubInt168 as -} for Int168 global;
using {bitorInt176 as |, unsubInt176 as -} for Int176 global;
using {bitorInt184 as |, unsubInt184 as -} for Int184 global;
using {bitorInt192 as |, unsubInt192 as -} for Int192 global;
using {bitorInt200 as |, unsubInt200 as -} for Int200 global;
using {bitorInt208 as |, unsubInt208 as -} for Int208 global;
using {bitorInt216 as |, unsubInt216 as -} for Int216 global;
using {bitorInt224 as |, unsubInt224 as -} for Int224 global;
using {bitorInt232 as |, unsubInt232 as -} for Int232 global;
using {bitorInt240 as |, unsubInt240 as -} for Int240 global;
using {bitorInt248 as |, unsubInt248 as -} for Int248 global;
using {bitorInt256 as |, unsubInt256 as -} for Int256 global;
using {bitorInt as |, unsubInt as -} for Int global;
using {bitorUint8 as |, bitnotUint8 as ~} for Uint8 global;
using {bitorUint16 as |, bitnotUint16 as ~} for Uint16 global;
using {bitorUint24 as |, bitnotUint24 as ~} for Uint24 global;
using {bitorUint32 as |, bitnotUint32 as ~} for Uint32 global;
using {bitorUint40 as |, bitnotUint40 as ~} for Uint40 global;
using {bitorUint48 as |, bitnotUint48 as ~} for Uint48 global;
using {bitorUint56 as |, bitnotUint56 as ~} for Uint56 global;
using {bitorUint64 as |, bitnotUint64 as ~} for Uint64 global;
using {bitorUint72 as |, bitnotUint72 as ~} for Uint72 global;
using {bitorUint80 as |, bitnotUint80 as ~} for Uint80 global;
using {bitorUint88 as |, bitnotUint88 as ~} for Uint88 global;
using {bitorUint96 as |, bitnotUint96 as ~} for Uint96 global;
using {bitorUint104 as |, bitnotUint104 as ~} for Uint104 global;
using {bitorUint112 as |, bitnotUint112 as ~} for Uint112 global;
using {bitorUint120 as |, bitnotUint120 as ~} for Uint120 global;
using {bitorUint128 as |, bitnotUint128 as ~} for Uint128 global;
using {bitorUint136 as |, bitnotUint136 as ~} for Uint136 global;
using {bitorUint144 as |, bitnotUint144 as ~} for Uint144 global;
using {bitorUint152 as |, bitnotUint152 as ~} for Uint152 global;
using {bitorUint160 as |, bitnotUint160 as ~} for Uint160 global;
using {bitorUint168 as |, bitnotUint168 as ~} for Uint168 global;
using {bitorUint176 as |, bitnotUint176 as ~} for Uint176 global;
using {bitorUint184 as |, bitnotUint184 as ~} for Uint184 global;
using {bitorUint192 as |, bitnotUint192 as ~} for Uint192 global;
using {bitorUint200 as |, bitnotUint200 as ~} for Uint200 global;
using {bitorUint208 as |, bitnotUint208 as ~} for Uint208 global;
using {bitorUint216 as |, bitnotUint216 as ~} for Uint216 global;
using {bitorUint224 as |, bitnotUint224 as ~} for Uint224 global;
using {bitorUint232 as |, bitnotUint232 as ~} for Uint232 global;
using {bitorUint240 as |, bitnotUint240 as ~} for Uint240 global;
using {bitorUint248 as |, bitnotUint248 as ~} for Uint248 global;
using {bitorUint256 as |, bitnotUint256 as ~} for Uint256 global;
using {bitorUint as |, bitnotUint as ~} for Uint global;
using {bitorBytes1 as |, bitnotBytes1 as ~} for Bytes1 global;
using {bitorBytes2 as |, bitnotBytes2 as ~} for Bytes2 global;
using {bitorBytes3 as |, bitnotBytes3 as ~} for Bytes3 global;
using {bitorBytes4 as |, bitnotBytes4 as ~} for Bytes4 global;
using {bitorBytes5 as |, bitnotBytes5 as ~} for Bytes5 global;
using {bitorBytes6 as |, bitnotBytes6 as ~} for Bytes6 global;
using {bitorBytes7 as |, bitnotBytes7 as ~} for Bytes7 global;
using {bitorBytes8 as |, bitnotBytes8 as ~} for Bytes8 global;
using {bitorBytes9 as |, bitnotBytes9 as ~} for Bytes9 global;
using {bitorBytes10 as |, bitnotBytes10 as ~} for Bytes10 global;
using {bitorBytes11 as |, bitnotBytes11 as ~} for Bytes11 global;
using {bitorBytes12 as |, bitnotBytes12 as ~} for Bytes12 global;
using {bitorBytes13 as |, bitnotBytes13 as ~} for Bytes13 global;
using {bitorBytes14 as |, bitnotBytes14 as ~} for Bytes14 global;
using {bitorBytes15 as |, bitnotBytes15 as ~} for Bytes15 global;
using {bitorBytes16 as |, bitnotBytes16 as ~} for Bytes16 global;
using {bitorBytes17 as |, bitnotBytes17 as ~} for Bytes17 global;
using {bitorBytes18 as |, bitnotBytes18 as ~} for Bytes18 global;
using {bitorBytes19 as |, bitnotBytes19 as ~} for Bytes19 global;
using {bitorBytes20 as |, bitnotBytes20 as ~} for Bytes20 global;
using {bitorBytes21 as |, bitnotBytes21 as ~} for Bytes21 global;
using {bitorBytes22 as |, bitnotBytes22 as ~} for Bytes22 global;
using {bitorBytes23 as |, bitnotBytes23 as ~} for Bytes23 global;
using {bitorBytes24 as |, bitnotBytes24 as ~} for Bytes24 global;
using {bitorBytes25 as |, bitnotBytes25 as ~} for Bytes25 global;
using {bitorBytes26 as |, bitnotBytes26 as ~} for Bytes26 global;
using {bitorBytes27 as |, bitnotBytes27 as ~} for Bytes27 global;
using {bitorBytes28 as |, bitnotBytes28 as ~} for Bytes28 global;
using {bitorBytes29 as |, bitnotBytes29 as ~} for Bytes29 global;
using {bitorBytes30 as |, bitnotBytes30 as ~} for Bytes30 global;
using {bitorBytes31 as |, bitnotBytes31 as ~} for Bytes31 global;
using {bitorBytes32 as |, bitnotBytes32 as ~} for Bytes32 global;
function bitorInt8(Int8 x, Int8 y) pure returns (Int8) { return Int8.wrap(Int8.unwrap(x) | Int8.unwrap(y)); }
function bitorInt16(Int16 x, Int16 y) pure returns (Int16) { return Int16.wrap(Int16.unwrap(x) | Int16.unwrap(y)); }
function bitorInt24(Int24 x, Int24 y) pure returns (Int24) { return Int24.wrap(Int24.unwrap(x) | Int24.unwrap(y)); }
function bitorInt32(Int32 x, Int32 y) pure returns (Int32) { return Int32.wrap(Int32.unwrap(x) | Int32.unwrap(y)); }
function bitorInt40(Int40 x, Int40 y) pure returns (Int40) { return Int40.wrap(Int40.unwrap(x) | Int40.unwrap(y)); }
function bitorInt48(Int48 x, Int48 y) pure returns (Int48) { return Int48.wrap(Int48.unwrap(x) | Int48.unwrap(y)); }
function bitorInt56(Int56 x, Int56 y) pure returns (Int56) { return Int56.wrap(Int56.unwrap(x) | Int56.unwrap(y)); }
function bitorInt64(Int64 x, Int64 y) pure returns (Int64) { return Int64.wrap(Int64.unwrap(x) | Int64.unwrap(y)); }
function bitorInt72(Int72 x, Int72 y) pure returns (Int72) { return Int72.wrap(Int72.unwrap(x) | Int72.unwrap(y)); }
function bitorInt80(Int80 x, Int80 y) pure returns (Int80) { return Int80.wrap(Int80.unwrap(x) | Int80.unwrap(y)); }
function bitorInt88(Int88 x, Int88 y) pure returns (Int88) { return Int88.wrap(Int88.unwrap(x) | Int88.unwrap(y)); }
function bitorInt96(Int96 x, Int96 y) pure returns (Int96) { return Int96.wrap(Int96.unwrap(x) | Int96.unwrap(y)); }
function bitorInt104(Int104 x, Int104 y) pure returns (Int104) { return Int104.wrap(Int104.unwrap(x) | Int104.unwrap(y)); }
function bitorInt112(Int112 x, Int112 y) pure returns (Int112) { return Int112.wrap(Int112.unwrap(x) | Int112.unwrap(y)); }
function bitorInt120(Int120 x, Int120 y) pure returns (Int120) { return Int120.wrap(Int120.unwrap(x) | Int120.unwrap(y)); }
function bitorInt128(Int128 x, Int128 y) pure returns (Int128) { return Int128.wrap(Int128.unwrap(x) | Int128.unwrap(y)); }
function bitorInt136(Int136 x, Int136 y) pure returns (Int136) { return Int136.wrap(Int136.unwrap(x) | Int136.unwrap(y)); }
function bitorInt144(Int144 x, Int144 y) pure returns (Int144) { return Int144.wrap(Int144.unwrap(x) | Int144.unwrap(y)); }
function bitorInt152(Int152 x, Int152 y) pure returns (Int152) { return Int152.wrap(Int152.unwrap(x) | Int152.unwrap(y)); }
function bitorInt160(Int160 x, Int160 y) pure returns (Int160) { return Int160.wrap(Int160.unwrap(x) | Int160.unwrap(y)); }
function bitorInt168(Int168 x, Int168 y) pure returns (Int168) { return Int168.wrap(Int168.unwrap(x) | Int168.unwrap(y)); }
function bitorInt176(Int176 x, Int176 y) pure returns (Int176) { return Int176.wrap(Int176.unwrap(x) | Int176.unwrap(y)); }
function bitorInt184(Int184 x, Int184 y) pure returns (Int184) { return Int184.wrap(Int184.unwrap(x) | Int184.unwrap(y)); }
function bitorInt192(Int192 x, Int192 y) pure returns (Int192) { return Int192.wrap(Int192.unwrap(x) | Int192.unwrap(y)); }
function bitorInt200(Int200 x, Int200 y) pure returns (Int200) { return Int200.wrap(Int200.unwrap(x) | Int200.unwrap(y)); }
function bitorInt208(Int208 x, Int208 y) pure returns (Int208) { return Int208.wrap(Int208.unwrap(x) | Int208.unwrap(y)); }
function bitorInt216(Int216 x, Int216 y) pure returns (Int216) { return Int216.wrap(Int216.unwrap(x) | Int216.unwrap(y)); }
function bitorInt224(Int224 x, Int224 y) pure returns (Int224) { return Int224.wrap(Int224.unwrap(x) | Int224.unwrap(y)); }
function bitorInt232(Int232 x, Int232 y) pure returns (Int232) { return Int232.wrap(Int232.unwrap(x) | Int232.unwrap(y)); }
function bitorInt240(Int240 x, Int240 y) pure returns (Int240) { return Int240.wrap(Int240.unwrap(x) | Int240.unwrap(y)); }
function bitorInt248(Int248 x, Int248 y) pure returns (Int248) { return Int248.wrap(Int248.unwrap(x) | Int248.unwrap(y)); }
function bitorInt256(Int256 x, Int256 y) pure returns (Int256) { return Int256.wrap(Int256.unwrap(x) | Int256.unwrap(y)); }
function bitorInt(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) | Int.unwrap(y)); }
function unsubInt8(Int8 x) pure returns (Int8) { return Int8.wrap(-Int8.unwrap(x)); }
function unsubInt16(Int16 x) pure returns (Int16) { return Int16.wrap(-Int16.unwrap(x)); }
function unsubInt24(Int24 x) pure returns (Int24) { return Int24.wrap(-Int24.unwrap(x)); }
function unsubInt32(Int32 x) pure returns (Int32) { return Int32.wrap(-Int32.unwrap(x)); }
function unsubInt40(Int40 x) pure returns (Int40) { return Int40.wrap(-Int40.unwrap(x)); }
function unsubInt48(Int48 x) pure returns (Int48) { return Int48.wrap(-Int48.unwrap(x)); }
function unsubInt56(Int56 x) pure returns (Int56) { return Int56.wrap(-Int56.unwrap(x)); }
function unsubInt64(Int64 x) pure returns (Int64) { return Int64.wrap(-Int64.unwrap(x)); }
function unsubInt72(Int72 x) pure returns (Int72) { return Int72.wrap(-Int72.unwrap(x)); }
function unsubInt80(Int80 x) pure returns (Int80) { return Int80.wrap(-Int80.unwrap(x)); }
function unsubInt88(Int88 x) pure returns (Int88) { return Int88.wrap(-Int88.unwrap(x)); }
function unsubInt96(Int96 x) pure returns (Int96) { return Int96.wrap(-Int96.unwrap(x)); }
function unsubInt104(Int104 x) pure returns (Int104) { return Int104.wrap(-Int104.unwrap(x)); }
function unsubInt112(Int112 x) pure returns (Int112) { return Int112.wrap(-Int112.unwrap(x)); }
function unsubInt120(Int120 x) pure returns (Int120) { return Int120.wrap(-Int120.unwrap(x)); }
function unsubInt128(Int128 x) pure returns (Int128) { return Int128.wrap(-Int128.unwrap(x)); }
function unsubInt136(Int136 x) pure returns (Int136) { return Int136.wrap(-Int136.unwrap(x)); }
function unsubInt144(Int144 x) pure returns (Int144) { return Int144.wrap(-Int144.unwrap(x)); }
function unsubInt152(Int152 x) pure returns (Int152) { return Int152.wrap(-Int152.unwrap(x)); }
function unsubInt160(Int160 x) pure returns (Int160) { return Int160.wrap(-Int160.unwrap(x)); }
function unsubInt168(Int168 x) pure returns (Int168) { return Int168.wrap(-Int168.unwrap(x)); }
function unsubInt176(Int176 x) pure returns (Int176) { return Int176.wrap(-Int176.unwrap(x)); }
function unsubInt184(Int184 x) pure returns (Int184) { return Int184.wrap(-Int184.unwrap(x)); }
function unsubInt192(Int192 x) pure returns (Int192) { return Int192.wrap(-Int192.unwrap(x)); }
function unsubInt200(Int200 x) pure returns (Int200) { return Int200.wrap(-Int200.unwrap(x)); }
function unsubInt208(Int208 x) pure returns (Int208) { return Int208.wrap(-Int208.unwrap(x)); }
function unsubInt216(Int216 x) pure returns (Int216) { return Int216.wrap(-Int216.unwrap(x)); }
function unsubInt224(Int224 x) pure returns (Int224) { return Int224.wrap(-Int224.unwrap(x)); }
function unsubInt232(Int232 x) pure returns (Int232) { return Int232.wrap(-Int232.unwrap(x)); }
function unsubInt240(Int240 x) pure returns (Int240) { return Int240.wrap(-Int240.unwrap(x)); }
function unsubInt248(Int248 x) pure returns (Int248) { return Int248.wrap(-Int248.unwrap(x)); }
function unsubInt256(Int256 x) pure returns (Int256) { return Int256.wrap(-Int256.unwrap(x)); }
function unsubInt(Int x) pure returns (Int) { return Int.wrap(-Int.unwrap(x)); }
function bitorUint8(Uint8 x, Uint8 y) pure returns (Uint8) { return Uint8.wrap(Uint8.unwrap(x) | Uint8.unwrap(y)); }
function bitorUint16(Uint16 x, Uint16 y) pure returns (Uint16) { return Uint16.wrap(Uint16.unwrap(x) | Uint16.unwrap(y)); }
function bitorUint24(Uint24 x, Uint24 y) pure returns (Uint24) { return Uint24.wrap(Uint24.unwrap(x) | Uint24.unwrap(y)); }
function bitorUint32(Uint32 x, Uint32 y) pure returns (Uint32) { return Uint32.wrap(Uint32.unwrap(x) | Uint32.unwrap(y)); }
function bitorUint40(Uint40 x, Uint40 y) pure returns (Uint40) { return Uint40.wrap(Uint40.unwrap(x) | Uint40.unwrap(y)); }
function bitorUint48(Uint48 x, Uint48 y) pure returns (Uint48) { return Uint48.wrap(Uint48.unwrap(x) | Uint48.unwrap(y)); }
function bitorUint56(Uint56 x, Uint56 y) pure returns (Uint56) { return Uint56.wrap(Uint56.unwrap(x) | Uint56.unwrap(y)); }
function bitorUint64(Uint64 x, Uint64 y) pure returns (Uint64) { return Uint64.wrap(Uint64.unwrap(x) | Uint64.unwrap(y)); }
function bitorUint72(Uint72 x, Uint72 y) pure returns (Uint72) { return Uint72.wrap(Uint72.unwrap(x) | Uint72.unwrap(y)); }
function bitorUint80(Uint80 x, Uint80 y) pure returns (Uint80) { return Uint80.wrap(Uint80.unwrap(x) | Uint80.unwrap(y)); }
function bitorUint88(Uint88 x, Uint88 y) pure returns (Uint88) { return Uint88.wrap(Uint88.unwrap(x) | Uint88.unwrap(y)); }
function bitorUint96(Uint96 x, Uint96 y) pure returns (Uint96) { return Uint96.wrap(Uint96.unwrap(x) | Uint96.unwrap(y)); }
function bitorUint104(Uint104 x, Uint104 y) pure returns (Uint104) { return Uint104.wrap(Uint104.unwrap(x) | Uint104.unwrap(y)); }
function bitorUint112(Uint112 x, Uint112 y) pure returns (Uint112) { return Uint112.wrap(Uint112.unwrap(x) | Uint112.unwrap(y)); }
function bitorUint120(Uint120 x, Uint120 y) pure returns (Uint120) { return Uint120.wrap(Uint120.unwrap(x) | Uint120.unwrap(y)); }
function bitorUint128(Uint128 x, Uint128 y) pure returns (Uint128) { return Uint128.wrap(Uint128.unwrap(x) | Uint128.unwrap(y)); }
function bitorUint136(Uint136 x, Uint136 y) pure returns (Uint136) { return Uint136.wrap(Uint136.unwrap(x) | Uint136.unwrap(y)); }
function bitorUint144(Uint144 x, Uint144 y) pure returns (Uint144) { return Uint144.wrap(Uint144.unwrap(x) | Uint144.unwrap(y)); }
function bitorUint152(Uint152 x, Uint152 y) pure returns (Uint152) { return Uint152.wrap(Uint152.unwrap(x) | Uint152.unwrap(y)); }
function bitorUint160(Uint160 x, Uint160 y) pure returns (Uint160) { return Uint160.wrap(Uint160.unwrap(x) | Uint160.unwrap(y)); }
function bitorUint168(Uint168 x, Uint168 y) pure returns (Uint168) { return Uint168.wrap(Uint168.unwrap(x) | Uint168.unwrap(y)); }
function bitorUint176(Uint176 x, Uint176 y) pure returns (Uint176) { return Uint176.wrap(Uint176.unwrap(x) | Uint176.unwrap(y)); }
function bitorUint184(Uint184 x, Uint184 y) pure returns (Uint184) { return Uint184.wrap(Uint184.unwrap(x) | Uint184.unwrap(y)); }
function bitorUint192(Uint192 x, Uint192 y) pure returns (Uint192) { return Uint192.wrap(Uint192.unwrap(x) | Uint192.unwrap(y)); }
function bitorUint200(Uint200 x, Uint200 y) pure returns (Uint200) { return Uint200.wrap(Uint200.unwrap(x) | Uint200.unwrap(y)); }
function bitorUint208(Uint208 x, Uint208 y) pure returns (Uint208) { return Uint208.wrap(Uint208.unwrap(x) | Uint208.unwrap(y)); }
function bitorUint216(Uint216 x, Uint216 y) pure returns (Uint216) { return Uint216.wrap(Uint216.unwrap(x) | Uint216.unwrap(y)); }
function bitorUint224(Uint224 x, Uint224 y) pure returns (Uint224) { return Uint224.wrap(Uint224.unwrap(x) | Uint224.unwrap(y)); }
function bitorUint232(Uint232 x, Uint232 y) pure returns (Uint232) { return Uint232.wrap(Uint232.unwrap(x) | Uint232.unwrap(y)); }
function bitorUint240(Uint240 x, Uint240 y) pure returns (Uint240) { return Uint240.wrap(Uint240.unwrap(x) | Uint240.unwrap(y)); }
function bitorUint248(Uint248 x, Uint248 y) pure returns (Uint248) { return Uint248.wrap(Uint248.unwrap(x) | Uint248.unwrap(y)); }
function bitorUint256(Uint256 x, Uint256 y) pure returns (Uint256) { return Uint256.wrap(Uint256.unwrap(x) | Uint256.unwrap(y)); }
function bitorUint(Uint x, Uint y) pure returns (Uint) { return Uint.wrap(Uint.unwrap(x) | Uint.unwrap(y)); }
function bitnotUint8(Uint8 x) pure returns (Uint8) { return Uint8.wrap(~Uint8.unwrap(x)); }
function bitnotUint16(Uint16 x) pure returns (Uint16) { return Uint16.wrap(~Uint16.unwrap(x)); }
function bitnotUint24(Uint24 x) pure returns (Uint24) { return Uint24.wrap(~Uint24.unwrap(x)); }
function bitnotUint32(Uint32 x) pure returns (Uint32) { return Uint32.wrap(~Uint32.unwrap(x)); }
function bitnotUint40(Uint40 x) pure returns (Uint40) { return Uint40.wrap(~Uint40.unwrap(x)); }
function bitnotUint48(Uint48 x) pure returns (Uint48) { return Uint48.wrap(~Uint48.unwrap(x)); }
function bitnotUint56(Uint56 x) pure returns (Uint56) { return Uint56.wrap(~Uint56.unwrap(x)); }
function bitnotUint64(Uint64 x) pure returns (Uint64) { return Uint64.wrap(~Uint64.unwrap(x)); }
function bitnotUint72(Uint72 x) pure returns (Uint72) { return Uint72.wrap(~Uint72.unwrap(x)); }
function bitnotUint80(Uint80 x) pure returns (Uint80) { return Uint80.wrap(~Uint80.unwrap(x)); }
function bitnotUint88(Uint88 x) pure returns (Uint88) { return Uint88.wrap(~Uint88.unwrap(x)); }
function bitnotUint96(Uint96 x) pure returns (Uint96) { return Uint96.wrap(~Uint96.unwrap(x)); }
function bitnotUint104(Uint104 x) pure returns (Uint104) { return Uint104.wrap(~Uint104.unwrap(x)); }
function bitnotUint112(Uint112 x) pure returns (Uint112) { return Uint112.wrap(~Uint112.unwrap(x)); }
function bitnotUint120(Uint120 x) pure returns (Uint120) { return Uint120.wrap(~Uint120.unwrap(x)); }
function bitnotUint128(Uint128 x) pure returns (Uint128) { return Uint128.wrap(~Uint128.unwrap(x)); }
function bitnotUint136(Uint136 x) pure returns (Uint136) { return Uint136.wrap(~Uint136.unwrap(x)); }
function bitnotUint144(Uint144 x) pure returns (Uint144) { return Uint144.wrap(~Uint144.unwrap(x)); }
function bitnotUint152(Uint152 x) pure returns (Uint152) { return Uint152.wrap(~Uint152.unwrap(x)); }
function bitnotUint160(Uint160 x) pure returns (Uint160) { return Uint160.wrap(~Uint160.unwrap(x)); }
function bitnotUint168(Uint168 x) pure returns (Uint168) { return Uint168.wrap(~Uint168.unwrap(x)); }
function bitnotUint176(Uint176 x) pure returns (Uint176) { return Uint176.wrap(~Uint176.unwrap(x)); }
function bitnotUint184(Uint184 x) pure returns (Uint184) { return Uint184.wrap(~Uint184.unwrap(x)); }
function bitnotUint192(Uint192 x) pure returns (Uint192) { return Uint192.wrap(~Uint192.unwrap(x)); }
function bitnotUint200(Uint200 x) pure returns (Uint200) { return Uint200.wrap(~Uint200.unwrap(x)); }
function bitnotUint208(Uint208 x) pure returns (Uint208) { return Uint208.wrap(~Uint208.unwrap(x)); }
function bitnotUint216(Uint216 x) pure returns (Uint216) { return Uint216.wrap(~Uint216.unwrap(x)); }
function bitnotUint224(Uint224 x) pure returns (Uint224) { return Uint224.wrap(~Uint224.unwrap(x)); }
function bitnotUint232(Uint232 x) pure returns (Uint232) { return Uint232.wrap(~Uint232.unwrap(x)); }
function bitnotUint240(Uint240 x) pure returns (Uint240) { return Uint240.wrap(~Uint240.unwrap(x)); }
function bitnotUint248(Uint248 x) pure returns (Uint248) { return Uint248.wrap(~Uint248.unwrap(x)); }
function bitnotUint256(Uint256 x) pure returns (Uint256) { return Uint256.wrap(~Uint256.unwrap(x)); }
function bitnotUint(Uint x) pure returns (Uint) { return Uint.wrap(~Uint.unwrap(x)); }
function bitorBytes1(Bytes1 x, Bytes1 y) pure returns (Bytes1) { return Bytes1.wrap(Bytes1.unwrap(x) | Bytes1.unwrap(y)); }
function bitorBytes2(Bytes2 x, Bytes2 y) pure returns (Bytes2) { return Bytes2.wrap(Bytes2.unwrap(x) | Bytes2.unwrap(y)); }
function bitorBytes3(Bytes3 x, Bytes3 y) pure returns (Bytes3) { return Bytes3.wrap(Bytes3.unwrap(x) | Bytes3.unwrap(y)); }
function bitorBytes4(Bytes4 x, Bytes4 y) pure returns (Bytes4) { return Bytes4.wrap(Bytes4.unwrap(x) | Bytes4.unwrap(y)); }
function bitorBytes5(Bytes5 x, Bytes5 y) pure returns (Bytes5) { return Bytes5.wrap(Bytes5.unwrap(x) | Bytes5.unwrap(y)); }
function bitorBytes6(Bytes6 x, Bytes6 y) pure returns (Bytes6) { return Bytes6.wrap(Bytes6.unwrap(x) | Bytes6.unwrap(y)); }
function bitorBytes7(Bytes7 x, Bytes7 y) pure returns (Bytes7) { return Bytes7.wrap(Bytes7.unwrap(x) | Bytes7.unwrap(y)); }
function bitorBytes8(Bytes8 x, Bytes8 y) pure returns (Bytes8) { return Bytes8.wrap(Bytes8.unwrap(x) | Bytes8.unwrap(y)); }
function bitorBytes9(Bytes9 x, Bytes9 y) pure returns (Bytes9) { return Bytes9.wrap(Bytes9.unwrap(x) | Bytes9.unwrap(y)); }
function bitorBytes10(Bytes10 x, Bytes10 y) pure returns (Bytes10) { return Bytes10.wrap(Bytes10.unwrap(x) | Bytes10.unwrap(y)); }
function bitorBytes11(Bytes11 x, Bytes11 y) pure returns (Bytes11) { return Bytes11.wrap(Bytes11.unwrap(x) | Bytes11.unwrap(y)); }
function bitorBytes12(Bytes12 x, Bytes12 y) pure returns (Bytes12) { return Bytes12.wrap(Bytes12.unwrap(x) | Bytes12.unwrap(y)); }
function bitorBytes13(Bytes13 x, Bytes13 y) pure returns (Bytes13) { return Bytes13.wrap(Bytes13.unwrap(x) | Bytes13.unwrap(y)); }
function bitorBytes14(Bytes14 x, Bytes14 y) pure returns (Bytes14) { return Bytes14.wrap(Bytes14.unwrap(x) | Bytes14.unwrap(y)); }
function bitorBytes15(Bytes15 x, Bytes15 y) pure returns (Bytes15) { return Bytes15.wrap(Bytes15.unwrap(x) | Bytes15.unwrap(y)); }
function bitorBytes16(Bytes16 x, Bytes16 y) pure returns (Bytes16) { return Bytes16.wrap(Bytes16.unwrap(x) | Bytes16.unwrap(y)); }
function bitorBytes17(Bytes17 x, Bytes17 y) pure returns (Bytes17) { return Bytes17.wrap(Bytes17.unwrap(x) | Bytes17.unwrap(y)); }
function bitorBytes18(Bytes18 x, Bytes18 y) pure returns (Bytes18) { return Bytes18.wrap(Bytes18.unwrap(x) | Bytes18.unwrap(y)); }
function bitorBytes19(Bytes19 x, Bytes19 y) pure returns (Bytes19) { return Bytes19.wrap(Bytes19.unwrap(x) | Bytes19.unwrap(y)); }
function bitorBytes20(Bytes20 x, Bytes20 y) pure returns (Bytes20) { return Bytes20.wrap(Bytes20.unwrap(x) | Bytes20.unwrap(y)); }
function bitorBytes21(Bytes21 x, Bytes21 y) pure returns (Bytes21) { return Bytes21.wrap(Bytes21.unwrap(x) | Bytes21.unwrap(y)); }
function bitorBytes22(Bytes22 x, Bytes22 y) pure returns (Bytes22) { return Bytes22.wrap(Bytes22.unwrap(x) | Bytes22.unwrap(y)); }
function bitorBytes23(Bytes23 x, Bytes23 y) pure returns (Bytes23) { return Bytes23.wrap(Bytes23.unwrap(x) | Bytes23.unwrap(y)); }
function bitorBytes24(Bytes24 x, Bytes24 y) pure returns (Bytes24) { return Bytes24.wrap(Bytes24.unwrap(x) | Bytes24.unwrap(y)); }
function bitorBytes25(Bytes25 x, Bytes25 y) pure returns (Bytes25) { return Bytes25.wrap(Bytes25.unwrap(x) | Bytes25.unwrap(y)); }
function bitorBytes26(Bytes26 x, Bytes26 y) pure returns (Bytes26) { return Bytes26.wrap(Bytes26.unwrap(x) | Bytes26.unwrap(y)); }
function bitorBytes27(Bytes27 x, Bytes27 y) pure returns (Bytes27) { return Bytes27.wrap(Bytes27.unwrap(x) | Bytes27.unwrap(y)); }
function bitorBytes28(Bytes28 x, Bytes28 y) pure returns (Bytes28) { return Bytes28.wrap(Bytes28.unwrap(x) | Bytes28.unwrap(y)); }
function bitorBytes29(Bytes29 x, Bytes29 y) pure returns (Bytes29) { return Bytes29.wrap(Bytes29.unwrap(x) | Bytes29.unwrap(y)); }
function bitorBytes30(Bytes30 x, Bytes30 y) pure returns (Bytes30) { return Bytes30.wrap(Bytes30.unwrap(x) | Bytes30.unwrap(y)); }
function bitorBytes31(Bytes31 x, Bytes31 y) pure returns (Bytes31) { return Bytes31.wrap(Bytes31.unwrap(x) | Bytes31.unwrap(y)); }
function bitorBytes32(Bytes32 x, Bytes32 y) pure returns (Bytes32) { return Bytes32.wrap(Bytes32.unwrap(x) | Bytes32.unwrap(y)); }
function bitnotBytes1(Bytes1 x) pure returns (Bytes1) { return Bytes1.wrap(~Bytes1.unwrap(x)); }
function bitnotBytes2(Bytes2 x) pure returns (Bytes2) { return Bytes2.wrap(~Bytes2.unwrap(x)); }
function bitnotBytes3(Bytes3 x) pure returns (Bytes3) { return Bytes3.wrap(~Bytes3.unwrap(x)); }
function bitnotBytes4(Bytes4 x) pure returns (Bytes4) { return Bytes4.wrap(~Bytes4.unwrap(x)); }
function bitnotBytes5(Bytes5 x) pure returns (Bytes5) { return Bytes5.wrap(~Bytes5.unwrap(x)); }
function bitnotBytes6(Bytes6 x) pure returns (Bytes6) { return Bytes6.wrap(~Bytes6.unwrap(x)); }
function bitnotBytes7(Bytes7 x) pure returns (Bytes7) { return Bytes7.wrap(~Bytes7.unwrap(x)); }
function bitnotBytes8(Bytes8 x) pure returns (Bytes8) { return Bytes8.wrap(~Bytes8.unwrap(x)); }
function bitnotBytes9(Bytes9 x) pure returns (Bytes9) { return Bytes9.wrap(~Bytes9.unwrap(x)); }
function bitnotBytes10(Bytes10 x) pure returns (Bytes10) { return Bytes10.wrap(~Bytes10.unwrap(x)); }
function bitnotBytes11(Bytes11 x) pure returns (Bytes11) { return Bytes11.wrap(~Bytes11.unwrap(x)); }
function bitnotBytes12(Bytes12 x) pure returns (Bytes12) { return Bytes12.wrap(~Bytes12.unwrap(x)); }
function bitnotBytes13(Bytes13 x) pure returns (Bytes13) { return Bytes13.wrap(~Bytes13.unwrap(x)); }
function bitnotBytes14(Bytes14 x) pure returns (Bytes14) { return Bytes14.wrap(~Bytes14.unwrap(x)); }
function bitnotBytes15(Bytes15 x) pure returns (Bytes15) { return Bytes15.wrap(~Bytes15.unwrap(x)); }
function bitnotBytes16(Bytes16 x) pure returns (Bytes16) { return Bytes16.wrap(~Bytes16.unwrap(x)); }
function bitnotBytes17(Bytes17 x) pure returns (Bytes17) { return Bytes17.wrap(~Bytes17.unwrap(x)); }
function bitnotBytes18(Bytes18 x) pure returns (Bytes18) { return Bytes18.wrap(~Bytes18.unwrap(x)); }
function bitnotBytes19(Bytes19 x) pure returns (Bytes19) { return Bytes19.wrap(~Bytes19.unwrap(x)); }
function bitnotBytes20(Bytes20 x) pure returns (Bytes20) { return Bytes20.wrap(~Bytes20.unwrap(x)); }
function bitnotBytes21(Bytes21 x) pure returns (Bytes21) { return Bytes21.wrap(~Bytes21.unwrap(x)); }
function bitnotBytes22(Bytes22 x) pure returns (Bytes22) { return Bytes22.wrap(~Bytes22.unwrap(x)); }
function bitnotBytes23(Bytes23 x) pure returns (Bytes23) { return Bytes23.wrap(~Bytes23.unwrap(x)); }
function bitnotBytes24(Bytes24 x) pure returns (Bytes24) { return Bytes24.wrap(~Bytes24.unwrap(x)); }
function bitnotBytes25(Bytes25 x) pure returns (Bytes25) { return Bytes25.wrap(~Bytes25.unwrap(x)); }
function bitnotBytes26(Bytes26 x) pure returns (Bytes26) { return Bytes26.wrap(~Bytes26.unwrap(x)); }
function bitnotBytes27(Bytes27 x) pure returns (Bytes27) { return Bytes27.wrap(~Bytes27.unwrap(x)); }
function bitnotBytes28(Bytes28 x) pure returns (Bytes28) { return Bytes28.wrap(~Bytes28.unwrap(x)); }
function bitnotBytes29(Bytes29 x) pure returns (Bytes29) { return Bytes29.wrap(~Bytes29.unwrap(x)); }
function bitnotBytes30(Bytes30 x) pure returns (Bytes30) { return Bytes30.wrap(~Bytes30.unwrap(x)); }
function bitnotBytes31(Bytes31 x) pure returns (Bytes31) { return Bytes31.wrap(~Bytes31.unwrap(x)); }
function bitnotBytes32(Bytes32 x) pure returns (Bytes32) { return Bytes32.wrap(~Bytes32.unwrap(x)); }
using {bitorAddress as |, bitnotAddress as ~} for Address global;
using {bitorAddressPayable as |, bitnotAddressPayable as ~} for AddressPayable global;
using {bitorBool as |, bitnotBool as ~} for Bool global;
function bitorAddress(Address x, Address y) pure returns (Address) {
return Address.wrap(address(bytes20(Address.unwrap(x)) | bytes20(Address.unwrap(y))));
}
function bitnotAddress(Address x) pure returns (Address) {
return Address.wrap(address(~bytes20(Address.unwrap(x))));
}
function bitorAddressPayable(AddressPayable x, AddressPayable y) pure returns (AddressPayable) {
return AddressPayable.wrap(payable(address(bytes20(address(AddressPayable.unwrap(x))) | bytes20(address(AddressPayable.unwrap(y))))));
}
function bitnotAddressPayable(AddressPayable x) pure returns (AddressPayable) {
return AddressPayable.wrap(payable(address(~bytes20(address(AddressPayable.unwrap(x))))));
}
function bitorBool(Bool x, Bool y) pure returns (Bool) {
return Bool.wrap(Bool.unwrap(x) || Bool.unwrap(y));
}
function bitnotBool(Bool x) pure returns (Bool) {
return Bool.wrap(!Bool.unwrap(x));
}
contract C {
function testIntBinary() public pure {
assert(Int8.unwrap(Int8.wrap(1) | Int8.wrap(2)) == 3);
assert(Int16.unwrap(Int16.wrap(1) | Int16.wrap(2)) == 3);
assert(Int24.unwrap(Int24.wrap(1) | Int24.wrap(2)) == 3);
assert(Int32.unwrap(Int32.wrap(1) | Int32.wrap(2)) == 3);
assert(Int40.unwrap(Int40.wrap(1) | Int40.wrap(2)) == 3);
assert(Int48.unwrap(Int48.wrap(1) | Int48.wrap(2)) == 3);
assert(Int56.unwrap(Int56.wrap(1) | Int56.wrap(2)) == 3);
assert(Int64.unwrap(Int64.wrap(1) | Int64.wrap(2)) == 3);
assert(Int72.unwrap(Int72.wrap(1) | Int72.wrap(2)) == 3);
assert(Int80.unwrap(Int80.wrap(1) | Int80.wrap(2)) == 3);
assert(Int88.unwrap(Int88.wrap(1) | Int88.wrap(2)) == 3);
assert(Int96.unwrap(Int96.wrap(1) | Int96.wrap(2)) == 3);
assert(Int104.unwrap(Int104.wrap(1) | Int104.wrap(2)) == 3);
assert(Int112.unwrap(Int112.wrap(1) | Int112.wrap(2)) == 3);
assert(Int120.unwrap(Int120.wrap(1) | Int120.wrap(2)) == 3);
assert(Int128.unwrap(Int128.wrap(1) | Int128.wrap(2)) == 3);
assert(Int136.unwrap(Int136.wrap(1) | Int136.wrap(2)) == 3);
assert(Int144.unwrap(Int144.wrap(1) | Int144.wrap(2)) == 3);
assert(Int152.unwrap(Int152.wrap(1) | Int152.wrap(2)) == 3);
assert(Int160.unwrap(Int160.wrap(1) | Int160.wrap(2)) == 3);
assert(Int168.unwrap(Int168.wrap(1) | Int168.wrap(2)) == 3);
assert(Int176.unwrap(Int176.wrap(1) | Int176.wrap(2)) == 3);
assert(Int184.unwrap(Int184.wrap(1) | Int184.wrap(2)) == 3);
assert(Int192.unwrap(Int192.wrap(1) | Int192.wrap(2)) == 3);
assert(Int200.unwrap(Int200.wrap(1) | Int200.wrap(2)) == 3);
assert(Int208.unwrap(Int208.wrap(1) | Int208.wrap(2)) == 3);
assert(Int216.unwrap(Int216.wrap(1) | Int216.wrap(2)) == 3);
assert(Int224.unwrap(Int224.wrap(1) | Int224.wrap(2)) == 3);
assert(Int232.unwrap(Int232.wrap(1) | Int232.wrap(2)) == 3);
assert(Int240.unwrap(Int240.wrap(1) | Int240.wrap(2)) == 3);
assert(Int248.unwrap(Int248.wrap(1) | Int248.wrap(2)) == 3);
assert(Int256.unwrap(Int256.wrap(1) | Int256.wrap(2)) == 3);
assert(Int.unwrap(Int.wrap(1) | Int.wrap(2)) == 3);
}
function testIntUnary() public pure {
assert(Int8.unwrap(-Int8.wrap(1)) == -1);
assert(Int16.unwrap(-Int16.wrap(1)) == -1);
assert(Int24.unwrap(-Int24.wrap(1)) == -1);
assert(Int32.unwrap(-Int32.wrap(1)) == -1);
assert(Int40.unwrap(-Int40.wrap(1)) == -1);
assert(Int48.unwrap(-Int48.wrap(1)) == -1);
assert(Int56.unwrap(-Int56.wrap(1)) == -1);
assert(Int64.unwrap(-Int64.wrap(1)) == -1);
assert(Int72.unwrap(-Int72.wrap(1)) == -1);
assert(Int80.unwrap(-Int80.wrap(1)) == -1);
assert(Int88.unwrap(-Int88.wrap(1)) == -1);
assert(Int96.unwrap(-Int96.wrap(1)) == -1);
assert(Int104.unwrap(-Int104.wrap(1)) == -1);
assert(Int112.unwrap(-Int112.wrap(1)) == -1);
assert(Int120.unwrap(-Int120.wrap(1)) == -1);
assert(Int128.unwrap(-Int128.wrap(1)) == -1);
assert(Int136.unwrap(-Int136.wrap(1)) == -1);
assert(Int144.unwrap(-Int144.wrap(1)) == -1);
assert(Int152.unwrap(-Int152.wrap(1)) == -1);
assert(Int160.unwrap(-Int160.wrap(1)) == -1);
assert(Int168.unwrap(-Int168.wrap(1)) == -1);
assert(Int176.unwrap(-Int176.wrap(1)) == -1);
assert(Int184.unwrap(-Int184.wrap(1)) == -1);
assert(Int192.unwrap(-Int192.wrap(1)) == -1);
assert(Int200.unwrap(-Int200.wrap(1)) == -1);
assert(Int208.unwrap(-Int208.wrap(1)) == -1);
assert(Int216.unwrap(-Int216.wrap(1)) == -1);
assert(Int224.unwrap(-Int224.wrap(1)) == -1);
assert(Int232.unwrap(-Int232.wrap(1)) == -1);
assert(Int240.unwrap(-Int240.wrap(1)) == -1);
assert(Int248.unwrap(-Int248.wrap(1)) == -1);
assert(Int256.unwrap(-Int256.wrap(1)) == -1);
assert(Int.unwrap(-Int.wrap(1)) == -1);
}
function testUintBinary() public pure {
assert(Uint8.unwrap(Uint8.wrap(1) | Uint8.wrap(2)) == 3);
assert(Uint16.unwrap(Uint16.wrap(1) | Uint16.wrap(2)) == 3);
assert(Uint24.unwrap(Uint24.wrap(1) | Uint24.wrap(2)) == 3);
assert(Uint32.unwrap(Uint32.wrap(1) | Uint32.wrap(2)) == 3);
assert(Uint40.unwrap(Uint40.wrap(1) | Uint40.wrap(2)) == 3);
assert(Uint48.unwrap(Uint48.wrap(1) | Uint48.wrap(2)) == 3);
assert(Uint56.unwrap(Uint56.wrap(1) | Uint56.wrap(2)) == 3);
assert(Uint64.unwrap(Uint64.wrap(1) | Uint64.wrap(2)) == 3);
assert(Uint72.unwrap(Uint72.wrap(1) | Uint72.wrap(2)) == 3);
assert(Uint80.unwrap(Uint80.wrap(1) | Uint80.wrap(2)) == 3);
assert(Uint88.unwrap(Uint88.wrap(1) | Uint88.wrap(2)) == 3);
assert(Uint96.unwrap(Uint96.wrap(1) | Uint96.wrap(2)) == 3);
assert(Uint104.unwrap(Uint104.wrap(1) | Uint104.wrap(2)) == 3);
assert(Uint112.unwrap(Uint112.wrap(1) | Uint112.wrap(2)) == 3);
assert(Uint120.unwrap(Uint120.wrap(1) | Uint120.wrap(2)) == 3);
assert(Uint128.unwrap(Uint128.wrap(1) | Uint128.wrap(2)) == 3);
assert(Uint136.unwrap(Uint136.wrap(1) | Uint136.wrap(2)) == 3);
assert(Uint144.unwrap(Uint144.wrap(1) | Uint144.wrap(2)) == 3);
assert(Uint152.unwrap(Uint152.wrap(1) | Uint152.wrap(2)) == 3);
assert(Uint160.unwrap(Uint160.wrap(1) | Uint160.wrap(2)) == 3);
assert(Uint168.unwrap(Uint168.wrap(1) | Uint168.wrap(2)) == 3);
assert(Uint176.unwrap(Uint176.wrap(1) | Uint176.wrap(2)) == 3);
assert(Uint184.unwrap(Uint184.wrap(1) | Uint184.wrap(2)) == 3);
assert(Uint192.unwrap(Uint192.wrap(1) | Uint192.wrap(2)) == 3);
assert(Uint200.unwrap(Uint200.wrap(1) | Uint200.wrap(2)) == 3);
assert(Uint208.unwrap(Uint208.wrap(1) | Uint208.wrap(2)) == 3);
assert(Uint216.unwrap(Uint216.wrap(1) | Uint216.wrap(2)) == 3);
assert(Uint224.unwrap(Uint224.wrap(1) | Uint224.wrap(2)) == 3);
assert(Uint232.unwrap(Uint232.wrap(1) | Uint232.wrap(2)) == 3);
assert(Uint240.unwrap(Uint240.wrap(1) | Uint240.wrap(2)) == 3);
assert(Uint248.unwrap(Uint248.wrap(1) | Uint248.wrap(2)) == 3);
assert(Uint256.unwrap(Uint256.wrap(1) | Uint256.wrap(2)) == 3);
assert(Uint.unwrap(Uint.wrap(1) | Uint.wrap(2)) == 3);
}
function testUintUnary() public pure {
assert(Uint8.unwrap(~Uint8.wrap(1)) == ~uint8(1));
assert(Uint16.unwrap(~Uint16.wrap(1)) == ~uint16(1));
assert(Uint24.unwrap(~Uint24.wrap(1)) == ~uint24(1));
assert(Uint32.unwrap(~Uint32.wrap(1)) == ~uint32(1));
assert(Uint40.unwrap(~Uint40.wrap(1)) == ~uint40(1));
assert(Uint48.unwrap(~Uint48.wrap(1)) == ~uint48(1));
assert(Uint56.unwrap(~Uint56.wrap(1)) == ~uint56(1));
assert(Uint64.unwrap(~Uint64.wrap(1)) == ~uint64(1));
assert(Uint72.unwrap(~Uint72.wrap(1)) == ~uint72(1));
assert(Uint80.unwrap(~Uint80.wrap(1)) == ~uint80(1));
assert(Uint88.unwrap(~Uint88.wrap(1)) == ~uint88(1));
assert(Uint96.unwrap(~Uint96.wrap(1)) == ~uint96(1));
assert(Uint104.unwrap(~Uint104.wrap(1)) == ~uint104(1));
assert(Uint112.unwrap(~Uint112.wrap(1)) == ~uint112(1));
assert(Uint120.unwrap(~Uint120.wrap(1)) == ~uint120(1));
assert(Uint128.unwrap(~Uint128.wrap(1)) == ~uint128(1));
assert(Uint136.unwrap(~Uint136.wrap(1)) == ~uint136(1));
assert(Uint144.unwrap(~Uint144.wrap(1)) == ~uint144(1));
assert(Uint152.unwrap(~Uint152.wrap(1)) == ~uint152(1));
assert(Uint160.unwrap(~Uint160.wrap(1)) == ~uint160(1));
assert(Uint168.unwrap(~Uint168.wrap(1)) == ~uint168(1));
assert(Uint176.unwrap(~Uint176.wrap(1)) == ~uint176(1));
assert(Uint184.unwrap(~Uint184.wrap(1)) == ~uint184(1));
assert(Uint192.unwrap(~Uint192.wrap(1)) == ~uint192(1));
assert(Uint200.unwrap(~Uint200.wrap(1)) == ~uint200(1));
assert(Uint208.unwrap(~Uint208.wrap(1)) == ~uint208(1));
assert(Uint216.unwrap(~Uint216.wrap(1)) == ~uint216(1));
assert(Uint224.unwrap(~Uint224.wrap(1)) == ~uint224(1));
assert(Uint232.unwrap(~Uint232.wrap(1)) == ~uint232(1));
assert(Uint240.unwrap(~Uint240.wrap(1)) == ~uint240(1));
assert(Uint248.unwrap(~Uint248.wrap(1)) == ~uint248(1));
assert(Uint256.unwrap(~Uint256.wrap(1)) == ~uint256(1));
assert(Uint.unwrap(~Uint.wrap(1)) == ~uint(1));
}
function testBytesBinary() public pure {
assert(Bytes1.unwrap(Bytes1.wrap(0x01) | Bytes1.wrap(0x02)) == bytes1(0x03));
assert(Bytes2.unwrap(Bytes2.wrap(bytes2(bytes1(0x01))) | Bytes2.wrap(bytes2(bytes1(0x02)))) == bytes2(bytes1(0x03)));
assert(Bytes3.unwrap(Bytes3.wrap(bytes3(bytes1(0x01))) | Bytes3.wrap(bytes3(bytes1(0x02)))) == bytes3(bytes1(0x03)));
assert(Bytes4.unwrap(Bytes4.wrap(bytes4(bytes1(0x01))) | Bytes4.wrap(bytes4(bytes1(0x02)))) == bytes4(bytes1(0x03)));
assert(Bytes5.unwrap(Bytes5.wrap(bytes5(bytes1(0x01))) | Bytes5.wrap(bytes5(bytes1(0x02)))) == bytes5(bytes1(0x03)));
assert(Bytes6.unwrap(Bytes6.wrap(bytes6(bytes1(0x01))) | Bytes6.wrap(bytes6(bytes1(0x02)))) == bytes6(bytes1(0x03)));
assert(Bytes7.unwrap(Bytes7.wrap(bytes7(bytes1(0x01))) | Bytes7.wrap(bytes7(bytes1(0x02)))) == bytes7(bytes1(0x03)));
assert(Bytes8.unwrap(Bytes8.wrap(bytes8(bytes1(0x01))) | Bytes8.wrap(bytes8(bytes1(0x02)))) == bytes8(bytes1(0x03)));
assert(Bytes9.unwrap(Bytes9.wrap(bytes9(bytes1(0x01))) | Bytes9.wrap(bytes9(bytes1(0x02)))) == bytes9(bytes1(0x03)));
assert(Bytes10.unwrap(Bytes10.wrap(bytes10(bytes1(0x01))) | Bytes10.wrap(bytes10(bytes1(0x02)))) == bytes10(bytes1(0x03)));
assert(Bytes11.unwrap(Bytes11.wrap(bytes11(bytes1(0x01))) | Bytes11.wrap(bytes11(bytes1(0x02)))) == bytes11(bytes1(0x03)));
assert(Bytes12.unwrap(Bytes12.wrap(bytes12(bytes1(0x01))) | Bytes12.wrap(bytes12(bytes1(0x02)))) == bytes12(bytes1(0x03)));
assert(Bytes13.unwrap(Bytes13.wrap(bytes13(bytes1(0x01))) | Bytes13.wrap(bytes13(bytes1(0x02)))) == bytes13(bytes1(0x03)));
assert(Bytes14.unwrap(Bytes14.wrap(bytes14(bytes1(0x01))) | Bytes14.wrap(bytes14(bytes1(0x02)))) == bytes14(bytes1(0x03)));
assert(Bytes15.unwrap(Bytes15.wrap(bytes15(bytes1(0x01))) | Bytes15.wrap(bytes15(bytes1(0x02)))) == bytes15(bytes1(0x03)));
assert(Bytes16.unwrap(Bytes16.wrap(bytes16(bytes1(0x01))) | Bytes16.wrap(bytes16(bytes1(0x02)))) == bytes16(bytes1(0x03)));
assert(Bytes17.unwrap(Bytes17.wrap(bytes17(bytes1(0x01))) | Bytes17.wrap(bytes17(bytes1(0x02)))) == bytes17(bytes1(0x03)));
assert(Bytes18.unwrap(Bytes18.wrap(bytes18(bytes1(0x01))) | Bytes18.wrap(bytes18(bytes1(0x02)))) == bytes18(bytes1(0x03)));
assert(Bytes19.unwrap(Bytes19.wrap(bytes19(bytes1(0x01))) | Bytes19.wrap(bytes19(bytes1(0x02)))) == bytes19(bytes1(0x03)));
assert(Bytes20.unwrap(Bytes20.wrap(bytes20(bytes1(0x01))) | Bytes20.wrap(bytes20(bytes1(0x02)))) == bytes20(bytes1(0x03)));
assert(Bytes21.unwrap(Bytes21.wrap(bytes21(bytes1(0x01))) | Bytes21.wrap(bytes21(bytes1(0x02)))) == bytes21(bytes1(0x03)));
assert(Bytes22.unwrap(Bytes22.wrap(bytes22(bytes1(0x01))) | Bytes22.wrap(bytes22(bytes1(0x02)))) == bytes22(bytes1(0x03)));
assert(Bytes23.unwrap(Bytes23.wrap(bytes23(bytes1(0x01))) | Bytes23.wrap(bytes23(bytes1(0x02)))) == bytes23(bytes1(0x03)));
assert(Bytes24.unwrap(Bytes24.wrap(bytes24(bytes1(0x01))) | Bytes24.wrap(bytes24(bytes1(0x02)))) == bytes24(bytes1(0x03)));
assert(Bytes25.unwrap(Bytes25.wrap(bytes25(bytes1(0x01))) | Bytes25.wrap(bytes25(bytes1(0x02)))) == bytes25(bytes1(0x03)));
assert(Bytes26.unwrap(Bytes26.wrap(bytes26(bytes1(0x01))) | Bytes26.wrap(bytes26(bytes1(0x02)))) == bytes26(bytes1(0x03)));
assert(Bytes27.unwrap(Bytes27.wrap(bytes27(bytes1(0x01))) | Bytes27.wrap(bytes27(bytes1(0x02)))) == bytes27(bytes1(0x03)));
assert(Bytes28.unwrap(Bytes28.wrap(bytes28(bytes1(0x01))) | Bytes28.wrap(bytes28(bytes1(0x02)))) == bytes28(bytes1(0x03)));
assert(Bytes29.unwrap(Bytes29.wrap(bytes29(bytes1(0x01))) | Bytes29.wrap(bytes29(bytes1(0x02)))) == bytes29(bytes1(0x03)));
assert(Bytes30.unwrap(Bytes30.wrap(bytes30(bytes1(0x01))) | Bytes30.wrap(bytes30(bytes1(0x02)))) == bytes30(bytes1(0x03)));
assert(Bytes31.unwrap(Bytes31.wrap(bytes31(bytes1(0x01))) | Bytes31.wrap(bytes31(bytes1(0x02)))) == bytes31(bytes1(0x03)));
assert(Bytes32.unwrap(Bytes32.wrap(bytes32(bytes1(0x01))) | Bytes32.wrap(bytes32(bytes1(0x02)))) == bytes32(bytes1(0x03)));
}
function testBytesUnary() public pure {
assert(Bytes1.unwrap(~Bytes1.wrap(bytes1(0x01))) == ~bytes1(0x01));
assert(Bytes2.unwrap(~Bytes2.wrap(bytes2(bytes1(0x01)))) == ~bytes2(bytes1(0x01)));
assert(Bytes3.unwrap(~Bytes3.wrap(bytes3(bytes1(0x01)))) == ~bytes3(bytes1(0x01)));
assert(Bytes4.unwrap(~Bytes4.wrap(bytes4(bytes1(0x01)))) == ~bytes4(bytes1(0x01)));
assert(Bytes5.unwrap(~Bytes5.wrap(bytes5(bytes1(0x01)))) == ~bytes5(bytes1(0x01)));
assert(Bytes6.unwrap(~Bytes6.wrap(bytes6(bytes1(0x01)))) == ~bytes6(bytes1(0x01)));
assert(Bytes7.unwrap(~Bytes7.wrap(bytes7(bytes1(0x01)))) == ~bytes7(bytes1(0x01)));
assert(Bytes8.unwrap(~Bytes8.wrap(bytes8(bytes1(0x01)))) == ~bytes8(bytes1(0x01)));
assert(Bytes9.unwrap(~Bytes9.wrap(bytes9(bytes1(0x01)))) == ~bytes9(bytes1(0x01)));
assert(Bytes10.unwrap(~Bytes10.wrap(bytes10(bytes1(0x01)))) == ~bytes10(bytes1(0x01)));
assert(Bytes11.unwrap(~Bytes11.wrap(bytes11(bytes1(0x01)))) == ~bytes11(bytes1(0x01)));
assert(Bytes12.unwrap(~Bytes12.wrap(bytes12(bytes1(0x01)))) == ~bytes12(bytes1(0x01)));
assert(Bytes13.unwrap(~Bytes13.wrap(bytes13(bytes1(0x01)))) == ~bytes13(bytes1(0x01)));
assert(Bytes14.unwrap(~Bytes14.wrap(bytes14(bytes1(0x01)))) == ~bytes14(bytes1(0x01)));
assert(Bytes15.unwrap(~Bytes15.wrap(bytes15(bytes1(0x01)))) == ~bytes15(bytes1(0x01)));
assert(Bytes16.unwrap(~Bytes16.wrap(bytes16(bytes1(0x01)))) == ~bytes16(bytes1(0x01)));
assert(Bytes17.unwrap(~Bytes17.wrap(bytes17(bytes1(0x01)))) == ~bytes17(bytes1(0x01)));
assert(Bytes18.unwrap(~Bytes18.wrap(bytes18(bytes1(0x01)))) == ~bytes18(bytes1(0x01)));
assert(Bytes19.unwrap(~Bytes19.wrap(bytes19(bytes1(0x01)))) == ~bytes19(bytes1(0x01)));
assert(Bytes20.unwrap(~Bytes20.wrap(bytes20(bytes1(0x01)))) == ~bytes20(bytes1(0x01)));
assert(Bytes21.unwrap(~Bytes21.wrap(bytes21(bytes1(0x01)))) == ~bytes21(bytes1(0x01)));
assert(Bytes22.unwrap(~Bytes22.wrap(bytes22(bytes1(0x01)))) == ~bytes22(bytes1(0x01)));
assert(Bytes23.unwrap(~Bytes23.wrap(bytes23(bytes1(0x01)))) == ~bytes23(bytes1(0x01)));
assert(Bytes24.unwrap(~Bytes24.wrap(bytes24(bytes1(0x01)))) == ~bytes24(bytes1(0x01)));
assert(Bytes25.unwrap(~Bytes25.wrap(bytes25(bytes1(0x01)))) == ~bytes25(bytes1(0x01)));
assert(Bytes26.unwrap(~Bytes26.wrap(bytes26(bytes1(0x01)))) == ~bytes26(bytes1(0x01)));
assert(Bytes27.unwrap(~Bytes27.wrap(bytes27(bytes1(0x01)))) == ~bytes27(bytes1(0x01)));
assert(Bytes28.unwrap(~Bytes28.wrap(bytes28(bytes1(0x01)))) == ~bytes28(bytes1(0x01)));
assert(Bytes29.unwrap(~Bytes29.wrap(bytes29(bytes1(0x01)))) == ~bytes29(bytes1(0x01)));
assert(Bytes30.unwrap(~Bytes30.wrap(bytes30(bytes1(0x01)))) == ~bytes30(bytes1(0x01)));
assert(Bytes31.unwrap(~Bytes31.wrap(bytes31(bytes1(0x01)))) == ~bytes31(bytes1(0x01)));
assert(Bytes32.unwrap(~Bytes32.wrap(bytes32(bytes1(0x01)))) == ~bytes32(bytes1(0x01)));
}
function testOtherBinary() public pure {
assert(Address.unwrap(Address.wrap(address(0x01)) | Address.wrap(address(0x02))) == address(0x03));
assert(AddressPayable.unwrap(AddressPayable.wrap(payable(address(0x01))) | AddressPayable.wrap(payable(address(0x02)))) == payable(address(0x03)));
assert(Bool.unwrap(~Bool.wrap(true)) == false);
}
function testOtherUnary() public pure {
assert(Address.unwrap(~Address.wrap(address(0))) == address(~bytes20(0)));
assert(AddressPayable.unwrap(~AddressPayable.wrap(payable(address(0)))) == payable(address(~bytes20(0))));
assert(Bool.unwrap(~Bool.wrap(true)) == false);
}
}
// ----
// testIntBinary() ->
// testIntUnary() ->
// testUintBinary() ->
// testUintUnary() ->
// testBytesBinary() ->
// testBytesUnary() ->
// testOtherBinary() ->
// testOtherUnary() ->
@@ -0,0 +1,20 @@
type Int is int16;
using {add as +, add} for Int global;
function add(Int _a, Int _b) pure returns (Int) {
return Int.wrap(Int.unwrap(_a) + Int.unwrap(_b));
}
contract C {
function f() pure public returns (Int) {
return Int.wrap(5) + Int.wrap(5);
}
function g() pure public returns (Int) {
return Int.wrap(7).add(Int.wrap(6));
}
}
// ----
// f() -> 10
// g() -> 13
@@ -0,0 +1,22 @@
type U8 is uint8;
function checkedAdd(U8 x, U8 y) pure returns (U8) {
return U8.wrap(U8.unwrap(x) + U8.unwrap(y));
}
using {checkedAdd as +} for U8 global;
contract C {
function testCheckedOperator() public pure returns (U8) {
return U8.wrap(250) + U8.wrap(10);
}
function testCheckedOperatorInUncheckedBlock() public pure returns (U8) {
unchecked {
return U8.wrap(250) + U8.wrap(10);
}
}
}
// ----
// testCheckedOperator() -> FAILURE, hex"4e487b71", 0x11
// testCheckedOperatorInUncheckedBlock() -> FAILURE, hex"4e487b71", 0x11
@@ -0,0 +1,18 @@
type A is address;
using {add as +} for A global;
function add(A a, A b) pure returns (A) {
return A.wrap(address(uint160(A.unwrap(a)) + uint160(A.unwrap(b))));
}
contract C {
function g() public pure returns (A) {
A a = A.wrap(0x3333333333333333333333333333333333333333);
A b = A.wrap(0x1111111111111111111111111111111111111111);
A c = A.wrap(0x5555555555555555555555555555555555555555);
return a + b + c;
}
}
// ----
// g() -> 0x9999999999999999999999999999999999999999
@@ -0,0 +1,22 @@
type Fixed is int128;
using {add as +, mul as *} for Fixed global;
int constant MULTIPLIER = 10**18;
function add(Fixed a, Fixed b) pure returns (Fixed) {
return Fixed.wrap(Fixed.unwrap(a) + Fixed.unwrap(b));
}
function mul(Fixed a, Fixed b) pure returns (Fixed) {
int intermediate = (int(Fixed.unwrap(a)) * int(Fixed.unwrap(b))) / MULTIPLIER;
if (int128(intermediate) != intermediate) { revert("Overflow"); }
return Fixed.wrap(int128(intermediate));
}
contract C {
function applyInterest(Fixed value, Fixed percentage) public pure returns (Fixed result) {
return value + value * percentage;
}
}
// ----
// applyInterest(int128,int128): 500000000000000000000, 100000000000000000 -> 550000000000000000000
@@ -0,0 +1,26 @@
type SmallInt is int;
type BigInt is int;
using {addSmall as +} for SmallInt global;
using {addBig as +} for BigInt global;
function addSmall(SmallInt a, SmallInt b) pure returns (SmallInt) {
return SmallInt.wrap(SmallInt.unwrap(a) + SmallInt.unwrap(b));
}
function addBig(BigInt a, BigInt b) pure returns (BigInt) {
return BigInt.wrap(10 * (BigInt.unwrap(a) + BigInt.unwrap(b)));
}
contract C {
function small() public pure returns (SmallInt) {
return SmallInt.wrap(1) + SmallInt.wrap(2);
}
function big() public pure returns (BigInt) {
return BigInt.wrap(3) + BigInt.wrap(4);
}
}
// ----
// small() -> 3
// big() -> 70
@@ -0,0 +1,20 @@
type Int is int32;
using {foo as +, foo as -} for Int global;
function foo(Int a, Int b) pure returns(Int) {
return Int.wrap(Int.unwrap(a) + Int.unwrap(b));
}
contract C {
function f() pure public returns (Int) {
return Int.wrap(2) + Int.wrap(3);
}
function g() pure public returns (Int) {
return Int.wrap(6) - Int.wrap(1);
}
}
// ----
// f() -> 5
// g() -> 7
@@ -0,0 +1,15 @@
type Int is int16;
using {keccak256 as +} for Int global;
function keccak256(Int a, Int b) pure returns (Int) {
return Int.wrap(Int.unwrap(a) + Int.unwrap(b));
}
contract C {
function test() public returns (Int) {
return Int.wrap(3) + Int.wrap(4);
}
}
// ----
// test() -> 7
@@ -0,0 +1,83 @@
type Bool is bool;
using {add as +, mul as *, unsub as -} for Bool global;
function add(Bool x, Bool y) pure returns (Bool) {
return Bool.wrap(Bool.unwrap(x) || Bool.unwrap(y));
}
function mul(Bool x, Bool y) pure returns (Bool) {
return Bool.wrap(Bool.unwrap(x) && Bool.unwrap(y));
}
function unsub(Bool x) pure returns (Bool) {
return Bool.wrap(!Bool.unwrap(x));
}
contract C {
event Wrapped(uint);
event Probe(Bool);
function toBool(uint x) public returns (Bool) {
emit Wrapped(x);
return Bool.wrap(x > 0);
}
function probe(Bool x) public returns (Bool) {
emit Probe(x);
return x;
}
function testSingleOperator() public {
toBool(0) +
(toBool(1) + toBool(2)) +
toBool(3);
}
function testTwoBinaryOperators() public {
toBool(0) * toBool(1) +
(toBool(2) * toBool(3)) +
toBool(4) * toBool(5);
}
function testBinaryAndUnaryOperators() public {
-toBool(0) * -toBool(1) +
(-toBool(2) * -toBool(3)) +
-toBool(4) * -toBool(5);
}
function testOperatorsNestedInCalls() public {
-probe(toBool(0) * -toBool(1)) +
(-probe(toBool(2) * -toBool(3))) +
-probe(toBool(4) * -toBool(5));
}
}
// ----
// testSingleOperator() ->
// ~ emit Wrapped(uint256): 0x00
// ~ emit Wrapped(uint256): 0x01
// ~ emit Wrapped(uint256): 0x02
// ~ emit Wrapped(uint256): 0x03
// testTwoBinaryOperators() ->
// ~ emit Wrapped(uint256): 0x00
// ~ emit Wrapped(uint256): 0x01
// ~ emit Wrapped(uint256): 0x02
// ~ emit Wrapped(uint256): 0x03
// ~ emit Wrapped(uint256): 0x04
// ~ emit Wrapped(uint256): 0x05
// testBinaryAndUnaryOperators() ->
// ~ emit Wrapped(uint256): 0x00
// ~ emit Wrapped(uint256): 0x01
// ~ emit Wrapped(uint256): 0x02
// ~ emit Wrapped(uint256): 0x03
// ~ emit Wrapped(uint256): 0x04
// ~ emit Wrapped(uint256): 0x05
// testOperatorsNestedInCalls() ->
// ~ emit Wrapped(uint256): 0x00
// ~ emit Wrapped(uint256): 0x01
// ~ emit Probe(bool): 0x00
// ~ emit Wrapped(uint256): 0x02
// ~ emit Wrapped(uint256): 0x03
// ~ emit Probe(bool): 0x00
// ~ emit Wrapped(uint256): 0x04
// ~ emit Wrapped(uint256): 0x05
// ~ emit Probe(bool): 0x00
@@ -0,0 +1,61 @@
type Int32 is int32;
using {add as +, unsub as -} for Int32 global;
function add(Int32 x, Int32 y) pure returns (Int32) {
return loadAdder().mul(x, y);
}
function unsub(Int32 x) pure returns (Int32) {
return loadAdder().inc(x);
}
interface IAdder {
function mul(Int32, Int32) external pure returns (Int32);
function inc(Int32) external pure returns (Int32);
}
contract Adder is IAdder {
function mul(Int32 x, Int32 y) external pure override returns (Int32) {
return Int32.wrap(Int32.unwrap(x) * Int32.unwrap(y));
}
function inc(Int32 x) external pure override returns (Int32) {
return Int32.wrap(Int32.unwrap(x) + 1);
}
}
function storeAdder(IAdder adder) pure {
assembly {
// This test would also work without assembly if we could hard-code an address here.
mstore(0, adder)
}
}
function loadAdder() pure returns (IAdder adder) {
assembly {
adder := mload(0)
}
}
contract C {
function testMul(Int32 x, Int32 y) public returns (Int32) {
storeAdder(new Adder());
return x + y;
}
function testInc(Int32 x) public returns (Int32) {
storeAdder(new Adder());
return -x;
}
}
// ----
// testMul(int32,int32): 42, 10 -> 420
// gas irOptimized: 103347
// gas legacy: 188203
// gas legacyOptimized: 126164
// testInc(int32): 42 -> 43
// gas irOptimized: 103173
// gas legacy: 187452
// gas legacyOptimized: 125851
@@ -0,0 +1,67 @@
type Int32 is int32;
using {add as +, unsub as -} for Int32 global;
function add(Int32 x, Int32 y) pure returns (Int32) {
return loadAdder().mul(x, y);
}
function unsub(Int32 x) pure returns (Int32) {
return loadAdder().inc(x);
}
interface IAdderPure {
function mul(Int32, Int32) external pure returns (Int32);
function inc(Int32) external pure returns (Int32);
}
interface IAdderView {
function mul(Int32, Int32) external view returns (Int32);
function inc(Int32) external view returns (Int32);
}
contract Adder is IAdderView {
function mul(Int32 x, Int32 y) external view override returns (Int32) {
return Int32.wrap(Int32.unwrap(x) * Int32.unwrap(y));
}
function inc(Int32 x) external view override returns (Int32) {
return Int32.wrap(Int32.unwrap(x) + 1);
}
}
function storeAdder(IAdderView adder) pure {
assembly {
// This test would also work without assembly if we could hard-code an address here.
mstore(0, adder)
}
}
function loadAdder() pure returns (IAdderPure adder) {
assembly {
// The adder we stored is view but we cheat by using a modified version with pure functions
adder := mload(0)
}
}
contract C {
function testMul(Int32 x, Int32 y) public returns (Int32) {
storeAdder(new Adder());
return x + y;
}
function testInc(Int32 x) public returns (Int32) {
storeAdder(new Adder());
return -x;
}
}
// ----
// testMul(int32,int32): 42, 10 -> 420
// gas irOptimized: 103347
// gas legacy: 188203
// gas legacyOptimized: 126164
// testInc(int32): 42 -> 43
// gas irOptimized: 103173
// gas legacy: 187452
// gas legacyOptimized: 125851
@@ -0,0 +1,40 @@
type U8 is uint8;
using {f as ~, add as +} for U8 global;
function f(U8 x) pure returns (U8 z) {
assembly {
// NOTE: Not using shr so that the test works pre-constantinople too
z := div(x, 256)
}
}
function add(U8 x, U8 y) pure returns (U8 z) {
assembly {
z := add(div(x, 256), div(x, 256))
}
}
contract C {
function testUnary() external pure returns (U8, U8) {
U8 a;
assembly {
a := 0x4200
}
// If the result is not 0, no cleanup was performed.
return (~a, f(a));
}
function testBinary() external pure returns (U8, U8) {
U8 a;
U8 b;
assembly {
a := 0x4200
b := 0x4200
}
// If the result is not 0, no cleanup was performed.
return (a + b, add(a, b));
}
}
// ----
// testUnary() -> 0x42, 0x42
// testBinary() -> 0x84, 0x84
@@ -0,0 +1,71 @@
type Int is int64;
using {
bitor as |, bitand as &, bitxor as ^, bitnot as ~,
add as +, sub as -, unsub as -, mul as *, div as /, mod as %
} for Int global;
function bitor(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) | Int.unwrap(y)); }
function bitand(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) & Int.unwrap(y)); }
function bitxor(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) ^ Int.unwrap(y)); }
function bitnot(Int x) pure returns (Int) { return Int.wrap(~Int.unwrap(x)); }
function add(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) + Int.unwrap(y)); }
function sub(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) - Int.unwrap(y)); }
function unsub(Int x) pure returns (Int) { return Int.wrap(-Int.unwrap(x)); }
function mul(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) * Int.unwrap(y)); }
function div(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) / Int.unwrap(y)); }
function mod(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) % Int.unwrap(y)); }
contract C {
Int constant I0 = Int.wrap(0);
Int constant I1 = Int.wrap(1);
Int constant I2 = Int.wrap(2);
Int constant I3 = Int.wrap(3);
Int constant I4 = Int.wrap(4);
Int constant I5 = Int.wrap(5);
Int constant I6 = Int.wrap(6);
Int constant I7 = Int.wrap(7);
Int constant I8 = Int.wrap(8);
Int constant I10 = Int.wrap(10);
Int constant I13 = Int.wrap(13);
Int constant I15 = Int.wrap(15);
Int constant I20 = Int.wrap(20);
Int constant I128 = Int.wrap(128);
function testBitwise() public pure {
assert(Int.unwrap(I0 & I0 | I1) == (0 & 0 | 1));
assert(Int.unwrap(I0 & I0 | I1) == ((0 & 0) | 1));
}
function testBitwise_arithmetic() public pure {
assert(Int.unwrap(I2 + I2 & ~I1 | I6 * I6 - I4 & ~I3) == (2 + 2 & ~1 | 6 * 6 - 4 & ~3));
assert(Int.unwrap(I2 + I2 & ~I1 | I6 * I6 - I4 & ~I3) == (((2 + 2) & (~1)) | (((6 * 6) - 4) & (~3))));
}
function testArithmetic() public pure {
assert(Int.unwrap(I1 + I8 / I4 - I5 % I6 * I7) == (1 + 8 / 4 - 5 % 6 * 7));
assert(Int.unwrap(I1 + I8 / I4 - I5 % I6 * I7) == ((1 + (8 / 4)) - ((5 % 6) * 7)));
}
function testAll() public pure {
assert(
Int.unwrap(I128 + I1 - I10 + I4 & ~I1 ^ ~I1 * I2 | -I15 % -I10 * I20 / I2 + I13 & ~I3) ==
(128 + 1 - 10 + 4 & ~1 ^ ~1 * 2 | -15 % -10 * 20 / 2 + 13 & ~3)
);
assert(
Int.unwrap(I128 + I1 - I10 + I4 & ~I1 ^ ~I1 * I2 | -I15 % -I10 * I20 / I2 + I13 & ~I3) ==
(
(
((((128 + 1) - 10) + 4) & (~1)) ^
((~1) * 2)
) |
((((((-15) % (-10)) * 20) / 2) + 13) & (~3))
)
);
}
}
// ----
// testBitwise() ->
// testBitwise_arithmetic() ->
// testArithmetic() ->
// testAll() ->
@@ -0,0 +1,58 @@
type U8 is uint8;
using {f as ~, g as +} for U8 global;
function f(U8) pure returns (U8 z) {
assembly {
// Return a value with dirty bytes outside of uint8
z := 0xffff
}
}
function g(U8, U8) pure returns (U8 z) {
assembly {
// Return a value with dirty bytes outside of uint8
z := 0xffff
}
}
contract C {
function testUnary() external pure returns (uint, uint) {
U8 a; // Value does not matter
U8 opResult = ~a;
U8 fResult = f(a);
// Get the slot, including bytes outside of uint8
uint opResultFull;
uint fResultFull;
assembly {
opResultFull := opResult
fResultFull := fResult
}
// If the result is not 0xff, no cleanup was performed.
return (opResultFull, fResultFull);
}
function testBinary() external pure returns (uint, uint) {
U8 a; // Value does not matter
U8 b; // Value does not matter
U8 opResult = a + b;
U8 fResult = g(a, b);
// Get the slot, including bytes outside of uint8
uint opResultFull;
uint fResultFull;
assembly {
opResultFull := opResult
fResultFull := fResult
}
// If the result is not 0xff, no cleanup was performed.
return (opResultFull, fResultFull);
}
}
// ----
// testUnary() -> 0xffff, 0xffff
// testBinary() -> 0xffff, 0xffff
@@ -0,0 +1,41 @@
type Uint is uint;
using {unaryCountdown as ~, binaryCountdown as ^, eq as ==} for Uint global;
function unaryCountdown(Uint x) pure returns (Uint) {
if (x == Uint.wrap(0))
return Uint.wrap(0);
return ~Uint.wrap(Uint.unwrap(x) - 1);
}
function binaryCountdown(Uint x, Uint y) pure returns (Uint) {
if (x == Uint.wrap(0) && y == Uint.wrap(0))
return Uint.wrap(0);
if (x == Uint.wrap(0))
return y ^ x;
return Uint.wrap(Uint.unwrap(x) - 1) ^ y;
}
function eq(Uint x, Uint y) pure returns (bool) {
return Uint.unwrap(x) == Uint.unwrap(y);
}
contract C {
function testUnary(Uint x) public pure returns (Uint) {
return ~x;
}
function testBinary(Uint x, Uint y) public pure returns (Uint) {
return x ^ y;
}
}
// ----
// testUnary(uint256): 0 -> 0
// testUnary(uint256): 1 -> 0
// testUnary(uint256): 99999999999 -> FAILURE
// testBinary(uint256,uint256): 0, 0 -> 0
// testBinary(uint256,uint256): 1, 0 -> 0
// testBinary(uint256,uint256): 0, 1 -> 0
// testBinary(uint256,uint256): 1, 1 -> 0
// testBinary(uint256,uint256): 99999999999, 99999999999 -> FAILURE
@@ -0,0 +1,25 @@
type U8 is uint8;
function uncheckedAdd(U8 x, U8 y) pure returns (U8) {
unchecked {
return U8.wrap(U8.unwrap(x) + U8.unwrap(y));
}
}
using {uncheckedAdd as +} for U8 global;
contract D {
function testUncheckedOperator() public pure returns (U8) {
return U8.wrap(250) + U8.wrap(10);
}
function testUncheckedOperatorInUncheckedBlock() public pure returns (U8) {
unchecked {
return U8.wrap(250) + U8.wrap(10);
}
}
}
// ----
// testUncheckedOperator() -> 4
// testUncheckedOperatorInUncheckedBlock() -> 4