mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
New tests.
This commit is contained in:
parent
e262f47f21
commit
c9ef727136
@ -0,0 +1,13 @@
|
||||
contract C {
|
||||
function f() public returns (uint y) {
|
||||
unchecked{{
|
||||
uint max = type(uint).max;
|
||||
uint x = max + 1;
|
||||
y = x;
|
||||
}}
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f() -> 0x00
|
@ -0,0 +1,18 @@
|
||||
contract C {
|
||||
uint public x = msg.value - 10;
|
||||
constructor() payable {}
|
||||
}
|
||||
|
||||
contract D {
|
||||
function f() public {
|
||||
unchecked {
|
||||
new C();
|
||||
}
|
||||
}
|
||||
function g() public payable returns (uint) {
|
||||
return (new C{value: 11}()).x();
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// f() -> FAILURE
|
||||
// g(), 100 wei -> 1
|
13
test/libsolidity/semanticTests/arithmetics/checked_add.sol
Normal file
13
test/libsolidity/semanticTests/arithmetics/checked_add.sol
Normal file
@ -0,0 +1,13 @@
|
||||
contract C {
|
||||
// Input is still not checked - this needs ABIEncoderV2!
|
||||
function f(uint16 a, uint16 b) public returns (uint16) {
|
||||
return a + b;
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// ABIEncoderV1Only: true
|
||||
// ----
|
||||
// f(uint16,uint16): 65534, 0 -> 0xfffe
|
||||
// f(uint16,uint16): 65536, 0 -> 0x00
|
||||
// f(uint16,uint16): 65535, 0 -> 0xffff
|
||||
// f(uint16,uint16): 65535, 1 -> FAILURE
|
@ -0,0 +1,14 @@
|
||||
contract C {
|
||||
function add(uint16 a, uint16 b) public returns (uint16) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
function f(uint16 a, uint16 b, uint16 c) public returns (uint16) {
|
||||
unchecked { return add(a, b) + c; }
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f(uint16,uint16,uint16): 0xe000, 0xe500, 2 -> FAILURE
|
||||
// f(uint16,uint16,uint16): 0xe000, 0x1000, 0x1000 -> 0x00
|
@ -0,0 +1,15 @@
|
||||
contract C {
|
||||
modifier add(uint16 a, uint16 b) {
|
||||
unchecked { a + b; }
|
||||
_;
|
||||
}
|
||||
|
||||
function f(uint16 a, uint16 b, uint16 c) public add(a, b) returns (uint16) {
|
||||
return b + c;
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f(uint16,uint16,uint16): 0xe000, 0xe500, 2 -> 58626
|
||||
// f(uint16,uint16,uint16): 0x1000, 0xe500, 0xe000 -> FAILURE
|
@ -2,6 +2,14 @@ contract C {
|
||||
function f(int a, int b) public pure returns (int) {
|
||||
return a % b;
|
||||
}
|
||||
function g(bool _check) public pure returns (int) {
|
||||
int x = type(int).min;
|
||||
if (_check) {
|
||||
return x / -1;
|
||||
} else {
|
||||
unchecked { return x / -1; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ====
|
||||
@ -12,3 +20,5 @@ contract C {
|
||||
// f(int256,int256): -7, 5 -> -2
|
||||
// f(int256,int256): -7, 5 -> -2
|
||||
// f(int256,int256): -5, -5 -> 0
|
||||
// g(bool): true -> FAILURE
|
||||
// g(bool): false -> -57896044618658097711785492504343953926634992332820282019728792003956564819968
|
||||
|
@ -0,0 +1,17 @@
|
||||
contract C {
|
||||
function add(uint16 a, uint16 b) public returns (uint16) {
|
||||
unchecked {
|
||||
return a + b;
|
||||
}
|
||||
}
|
||||
|
||||
function f(uint16 a) public returns (uint16) {
|
||||
return add(a, 0x100) + 0x100;
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f(uint16): 7 -> 0x0207
|
||||
// f(uint16): 0xffff -> 511
|
||||
// f(uint16): 0xfeff -> FAILURE
|
@ -0,0 +1,22 @@
|
||||
contract C {
|
||||
function div(uint256 a, uint256 b) public returns (uint256) {
|
||||
// Does not disable div by zero check
|
||||
unchecked {
|
||||
return a / b;
|
||||
}
|
||||
}
|
||||
|
||||
function mod(uint256 a, uint256 b) public returns (uint256) {
|
||||
// Does not disable div by zero check
|
||||
unchecked {
|
||||
return a % b;
|
||||
}
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// div(uint256,uint256): 7, 2 -> 3
|
||||
// div(uint256,uint256): 7, 0 -> FAILURE # throws #
|
||||
// mod(uint256,uint256): 7, 2 -> 1
|
||||
// mod(uint256,uint256): 7, 0 -> FAILURE # throws #
|
@ -0,0 +1,9 @@
|
||||
contract C {
|
||||
function f() public pure {
|
||||
for (unchecked { uint x = 2 }; x < 2; x ++) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// ParserError 6933: (57-66): Expected primary expression.
|
@ -0,0 +1,3 @@
|
||||
function f() pure returns (uint) unchecked {}
|
||||
// ----
|
||||
// ParserError 5296: (33-42): "unchecked" blocks can only be used inside regular blocks.
|
@ -0,0 +1,5 @@
|
||||
contract C {
|
||||
modifier m() { unchecked { _; } }
|
||||
}
|
||||
// ----
|
||||
// SyntaxError 2573: (44-45): The placeholder statement "_" cannot be used inside an "unchecked" block.
|
11
test/libsolidity/syntaxTests/unchecked/unchecked_nested.sol
Normal file
11
test/libsolidity/syntaxTests/unchecked/unchecked_nested.sol
Normal file
@ -0,0 +1,11 @@
|
||||
contract C {
|
||||
function f() public pure {
|
||||
unchecked {
|
||||
unchecked {
|
||||
uint x = 2 + 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// SyntaxError 1941: (76-133): "unchecked" blocks cannot be nested.
|
@ -0,0 +1,9 @@
|
||||
contract C {
|
||||
function f() public pure {
|
||||
for (uint x = 2; x < 2; unchecked { x ++; }) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// ParserError 6933: (76-85): Expected primary expression.
|
@ -0,0 +1,8 @@
|
||||
contract C {
|
||||
uint x = unchecked { f() + 2 }
|
||||
function f() public pure returns (uint) {
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// ParserError 6933: (26-35): Expected primary expression.
|
@ -0,0 +1,9 @@
|
||||
contract C {
|
||||
function f() public pure {
|
||||
while (true) unchecked {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// ParserError 5296: (65-74): "unchecked" blocks can only be used inside regular blocks.
|
Loading…
Reference in New Issue
Block a user