Skip to content

Commit

Permalink
Add solidity tests for bin number literal
Browse files Browse the repository at this point in the history
  • Loading branch information
pcw109550 committed Apr 22, 2024
1 parent 25fc913 commit eb6d22e
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/libsolidity/semanticTests/various/test_underscore_in_bin.sol
@@ -0,0 +1,10 @@
contract test {
function f(bool cond) public pure returns (uint256) {
uint32 x = 0b0001001000110100_10101011;
uint256 y = 0b0001001000110100_1010101111001101_0001001000110100;
return cond ? x : y;
}
}
// ----
// f(bool): true -> 0x1234ab
// f(bool): false -> 0x1234abcd1234
@@ -0,0 +1,5 @@
contract C {
uint constant x = 0b01 wei;
}
// ----
// TypeError 5146: (32-40): Binary numbers cannot be used with unit denominations. You can use an expression of the form "0b1011 * 1 days" instead.
5 changes: 5 additions & 0 deletions test/libsolidity/syntaxTests/literals/invalid_bin_number.sol
@@ -0,0 +1,5 @@
contract C {
uint x = 0b10a;
}
// ----
// ParserError 8936: (26-30): Identifier-start is not allowed at end of a number.
@@ -0,0 +1,17 @@
contract C {
function f() public pure {
uint x1 = 0b1111111_010100000000;
uint x2 = 0b111111_010100000000;
uint x3 = 0b11111_010100000000;
uint x4 = 0b1111_010100000000;
uint x5 = 0b111_010100000000;
uint x6 = 0b11_010100000000;
uint x7 = 0b1_010100000000;
uint x8 = 0b1010_0101_010_101;
uint x9 = 0b010_10101_1100_001;
uint x10 = 0b100101010_001010101111_010010101;

x1; x2; x3; x4; x5; x6; x7; x8; x9; x10;
}
}
// ----
@@ -0,0 +1,7 @@
contract C {
function f() public pure {
uint X1 = 0b010101__1100__001100__1001001;
}
}
// ----
// SyntaxError 2990: (56-87): Invalid use of underscores in number literal. Only one consecutive underscores between digits allowed.
@@ -0,0 +1,31 @@
contract C {
function f() public pure {
bytes1 b1 = bytes1(0b1);
bytes1 b2 = bytes1(0b100000000);
bytes2 b3 = bytes2(0b11111111);
bytes2 b4 = bytes2(0b100000000);
bytes2 b5 = bytes2(0b10000000000000000);
bytes3 b6 = bytes3(0b1111111111111111);
bytes3 b7 = bytes3(0b10000000000000000);
bytes3 b8 = bytes3(0b1000000000000000000000000);
bytes4 b9 = bytes4(0b111111111111111111111111);
bytes4 b10 = bytes4(0b1000000000000000000000000);
bytes4 b11 = bytes4(0b100000000000000000000000000000000);
bytes16 b12 = bytes16(0b1);
bytes32 b13 = bytes32(0b1);
}
}
// ----
// TypeError 9640: (60-71): Explicit type conversion not allowed from "int_const 1" to "bytes1".
// TypeError 9640: (90-109): Explicit type conversion not allowed from "int_const 256" to "bytes1".
// TypeError 9640: (128-146): Explicit type conversion not allowed from "int_const 255" to "bytes2".
// TypeError 9640: (165-184): Explicit type conversion not allowed from "int_const 256" to "bytes2".
// TypeError 9640: (203-230): Explicit type conversion not allowed from "int_const 65536" to "bytes2".
// TypeError 9640: (249-275): Explicit type conversion not allowed from "int_const 65535" to "bytes3".
// TypeError 9640: (294-321): Explicit type conversion not allowed from "int_const 65536" to "bytes3".
// TypeError 9640: (340-375): Explicit type conversion not allowed from "int_const 16777216" to "bytes3".
// TypeError 9640: (394-428): Explicit type conversion not allowed from "int_const 16777215" to "bytes4".
// TypeError 9640: (448-483): Explicit type conversion not allowed from "int_const 16777216" to "bytes4".
// TypeError 9640: (503-546): Explicit type conversion not allowed from "int_const 4294967296" to "bytes4".
// TypeError 9640: (567-579): Explicit type conversion not allowed from "int_const 1" to "bytes16".
// TypeError 9640: (600-612): Explicit type conversion not allowed from "int_const 1" to "bytes32".
@@ -0,0 +1,31 @@
contract C {
function f() public pure {
bytes1 b1 = 0b1;
bytes1 b2 = 0b100000000;
bytes2 b3 = 0b11111111;
bytes2 b4 = 0b100000000;
bytes2 b5 = 0b10000000000000000;
bytes3 b6 = 0b1111111111111111;
bytes3 b7 = 0b10000000000000000;
bytes3 b8 = 0b1000000000000000000000000;
bytes4 b9 = 0b111111111111111111111111;
bytes4 b10 = 0b1000000000000000000000000;
bytes4 b11 = 0b100000000000000000000000000000000;
bytes16 b12 = 0b1;
bytes32 b13 = 0b1;
}
}
// ----
// TypeError 9574: (48-63): Type int_const 1 is not implicitly convertible to expected type bytes1.
// TypeError 9574: (70-93): Type int_const 256 is not implicitly convertible to expected type bytes1.
// TypeError 9574: (100-122): Type int_const 255 is not implicitly convertible to expected type bytes2.
// TypeError 9574: (129-152): Type int_const 256 is not implicitly convertible to expected type bytes2.
// TypeError 9574: (159-190): Type int_const 65536 is not implicitly convertible to expected type bytes2.
// TypeError 9574: (197-227): Type int_const 65535 is not implicitly convertible to expected type bytes3.
// TypeError 9574: (234-265): Type int_const 65536 is not implicitly convertible to expected type bytes3.
// TypeError 9574: (272-311): Type int_const 16777216 is not implicitly convertible to expected type bytes3.
// TypeError 9574: (318-356): Type int_const 16777215 is not implicitly convertible to expected type bytes4.
// TypeError 9574: (363-403): Type int_const 16777216 is not implicitly convertible to expected type bytes4.
// TypeError 9574: (410-458): Type int_const 4294967296 is not implicitly convertible to expected type bytes4.
// TypeError 9574: (465-482): Type int_const 1 is not implicitly convertible to expected type bytes16.
// TypeError 9574: (489-506): Type int_const 1 is not implicitly convertible to expected type bytes32.
@@ -0,0 +1,14 @@
contract C {
function f() public pure {
bytes1 b1 = bytes1(0b00000001);
bytes1 b2 = bytes1(0b11111111);
bytes2 b3 = bytes2(0b0000000100000000);
bytes2 b4 = bytes2(0b1111111111111111);
bytes3 b5 = bytes3(0b000000010000000000000000);
bytes3 b6 = bytes3(0b111111111111111111111111);
bytes4 b7 = bytes4(0b00000001000000000000000000000000);
bytes4 b8 = bytes4(0b11111111111111111111111111111111);
b1; b2; b3; b4; b5; b6; b7; b8;
}
}
// ----
@@ -0,0 +1,14 @@
contract C {
function f() public pure {
bytes1 b1 = 0b00000001;
bytes1 b2 = 0b11111111;
bytes2 b3 = 0b0000000100000000;
bytes2 b4 = 0b1111111111111111;
bytes3 b5 = 0b000000010000000000000000;
bytes3 b6 = 0b111111111111111111111111;
bytes4 b7 = 0b00000001000000000000000000000000;
bytes4 b8 = 0b11111111111111111111111111111111;
b1; b2; b3; b4; b5; b6; b7; b8;
}
}
// ----
6 changes: 6 additions & 0 deletions test/libsolidity/syntaxTests/types/rational_number_huge.sol
Expand Up @@ -5,6 +5,12 @@ contract C {

// fits exactly into FixedBytes (32), ensures underscored literals won't hurt
y = 0xffffffff00000000ffffffff00000000ffffffff00000000ffffffff_00000000;

// fits FixedBytes with exactly 32-bytes
y = 0b1111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000;

// fits exactly into FixedBytes (32), ensures underscored literals won't hurt
y = 0b11111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000_11111111000000001111111100000000;
}
}
// ----
Expand Up @@ -2,7 +2,11 @@ contract C {
function f(uint y) public pure {
// one byte too long for storing in Fixedbytes (would require 33 bytes)
y = 0xffffffff00000000ffffffff00000000ffffffff00000000ffffffff000000001;

// one bit too long for storing in Fixedbytes (would require 33 bytes)
y = 0b11111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000;
}
}
// ----
// TypeError 7407: (142-209): Type int_const 1852...(71 digits omitted)...7281 is not implicitly convertible to expected type uint256. Literal is too large to fit in uint256.
// TypeError 7407: (303-562): Type int_const 2311...(70 digits omitted)...0416 is not implicitly convertible to expected type uint256. Literal is too large to fit in uint256.
Expand Up @@ -27,5 +27,23 @@ contract C {
bytes32 b32 = bytes32(0x0);
b1; b2; b3; b4; b8; b16; b32;
}
function i() public pure {
bytes1 b1 = bytes1(0b0);
bytes2 b2 = bytes2(0b00000);
bytes3 b3 = bytes3(0b0000000);
bytes4 b4 = bytes4(0b000000000);
bytes8 b8 = bytes8(0b00000000000000000);
b1; b2; b3; b4; b8;
}
function j() public pure {
bytes1 b1 = bytes1(0b0);
bytes2 b2 = bytes2(0b0);
bytes3 b3 = bytes3(0b0);
bytes4 b4 = bytes4(0b0);
bytes8 b8 = bytes8(0b0);
bytes16 b16 = bytes16(0b0);
bytes32 b32 = bytes32(0b0);
b1; b2; b3; b4; b8; b16; b32;
}
}
// ----
Expand Up @@ -27,5 +27,23 @@ contract C {
bytes32 b32 = 0x0;
b1; b2; b3; b4; b8; b16; b32;
}
function i() public pure {
bytes1 b1 = 0b000;
bytes2 b2 = 0b00000;
bytes3 b3 = 0b0000000;
bytes4 b4 = 0b000000000;
bytes8 b8 = 0b00000000000000000;
b1; b2; b3; b4; b8;
}
function j() public pure {
bytes1 b1 = 0b0;
bytes2 b2 = 0b0;
bytes3 b3 = 0b0;
bytes4 b4 = 0b0;
bytes8 b8 = 0b0;
bytes16 b16 = 0b0;
bytes32 b32 = 0b0;
b1; b2; b3; b4; b8; b16; b32;
}
}
// ----

0 comments on commit eb6d22e

Please sign in to comment.