mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Tests for suffixes + operators
This commit is contained in:
parent
d0701ccc44
commit
2c7ab0cf48
@ -0,0 +1,26 @@
|
||||
type Bool is bool;
|
||||
using {bitor as |, bitand as &, bitnot as ~} for Bool global;
|
||||
|
||||
function bitor(Bool x, Bool y) pure returns (Bool) {
|
||||
return Bool.wrap(Bool.unwrap(x) || Bool.unwrap(y));
|
||||
}
|
||||
|
||||
function bitand(Bool x, Bool y) pure returns (Bool) {
|
||||
return Bool.wrap(Bool.unwrap(x) && Bool.unwrap(y));
|
||||
}
|
||||
|
||||
function bitnot(Bool x) pure returns (Bool) {
|
||||
return Bool.wrap(!Bool.unwrap(x));
|
||||
}
|
||||
|
||||
function b(bool x) pure suffix returns (Bool) {
|
||||
return Bool.wrap(x);
|
||||
}
|
||||
|
||||
contract C {
|
||||
function test() public pure returns (Bool) {
|
||||
return ~ ~ ~(false b | true b & ~false b) & true b;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// test() -> false
|
@ -0,0 +1,26 @@
|
||||
type Int is int;
|
||||
using {add as +, mul as *, unsub as -} for Int global;
|
||||
|
||||
function add(Int a, Int b) pure returns (Int) {
|
||||
return Int.wrap(Int.unwrap(a) + Int.unwrap(b));
|
||||
}
|
||||
|
||||
function mul(Int a, Int b) pure returns (Int) {
|
||||
return Int.wrap(Int.unwrap(a) * Int.unwrap(b));
|
||||
}
|
||||
|
||||
function unsub(Int a) pure returns (Int) {
|
||||
return Int.wrap(-Int.unwrap(a));
|
||||
}
|
||||
|
||||
function i(int x) pure suffix returns (Int) {
|
||||
return Int.wrap(x);
|
||||
}
|
||||
|
||||
contract C {
|
||||
function test() public pure returns (Int) {
|
||||
return - - -(4 i + 4 i * -3 i) * 2 i;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// test() -> 16
|
@ -0,0 +1,36 @@
|
||||
type Int is int;
|
||||
using {sub as -, unsub as -} for Int global;
|
||||
|
||||
function sub(Int a, Int b) pure returns (Int) {
|
||||
return Int.wrap((Int.unwrap(a) - Int.unwrap(b)) * 100);
|
||||
}
|
||||
|
||||
function unsub(Int a) pure returns (Int) {
|
||||
return Int.wrap(Int.unwrap(a) + 10);
|
||||
}
|
||||
|
||||
function u(int x) pure suffix returns (Int) {
|
||||
return Int.wrap(x + 1);
|
||||
}
|
||||
|
||||
function u(Int x) pure suffix returns (Int) {
|
||||
return Int.wrap(Int.unwrap(x) + 1);
|
||||
}
|
||||
|
||||
contract C {
|
||||
function testUnary() public pure returns (Int) {
|
||||
return -2 u;
|
||||
}
|
||||
|
||||
function testUnaryWithParens() public pure returns (Int) {
|
||||
return -(2 u);
|
||||
}
|
||||
|
||||
function testBinary() public pure returns (Int) {
|
||||
return 4 u - 2 u;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// testUnary() -> 13
|
||||
// testUnaryWithParens() -> 13
|
||||
// testBinary() -> 200
|
@ -0,0 +1,34 @@
|
||||
type UncheckedCounter is uint;
|
||||
|
||||
using {
|
||||
add as +,
|
||||
lt as <
|
||||
} for UncheckedCounter global;
|
||||
|
||||
function add(UncheckedCounter x, UncheckedCounter y) pure returns (UncheckedCounter) {
|
||||
unchecked {
|
||||
return UncheckedCounter.wrap(UncheckedCounter.unwrap(x) + UncheckedCounter.unwrap(y));
|
||||
}
|
||||
}
|
||||
|
||||
function lt(UncheckedCounter x, UncheckedCounter y) pure returns (bool) {
|
||||
return UncheckedCounter.unwrap(x) < UncheckedCounter.unwrap(y);
|
||||
}
|
||||
|
||||
function cycles(uint c) pure suffix returns (UncheckedCounter) {
|
||||
return UncheckedCounter.wrap(c);
|
||||
}
|
||||
|
||||
contract C {
|
||||
uint public total = 0;
|
||||
|
||||
function testCounter() public returns (uint) {
|
||||
for (UncheckedCounter i = 20 cycles; i < 23 cycles; i = i + 1 cycles)
|
||||
total += UncheckedCounter.unwrap(i);
|
||||
|
||||
return total;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// testCounter() -> 63
|
||||
// total() -> 63
|
@ -0,0 +1,9 @@
|
||||
function suffix(string memory s) pure suffix returns (string memory) { return s; }
|
||||
|
||||
contract C {
|
||||
function f() public pure {
|
||||
delete "abc" suffix;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError 4247: (143-155): Expression has to be an lvalue.
|
@ -0,0 +1,9 @@
|
||||
function suffix(uint) pure suffix returns (uint) {}
|
||||
|
||||
contract C {
|
||||
function f() public {
|
||||
(1) suffix;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// ParserError 2314: (104-110): Expected ';' but got identifier
|
Loading…
Reference in New Issue
Block a user