aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2018-08-09 03:36:57 +0800
committerGitHub <noreply@github.com>2018-08-09 03:36:57 +0800
commitd634d20b5b0dac3e5caf1741073fa123fdd56ab9 (patch)
treee88008bc8a8e665d32d8ac8c9125595dd90e8a22 /test
parent551343ae3eb1b3f1575d91a4f7021c0f9529d5bd (diff)
parentb9222808f61e00833f8c11cd196cafb50ec9e1b9 (diff)
downloaddexon-solidity-d634d20b5b0dac3e5caf1741073fa123fdd56ab9.tar
dexon-solidity-d634d20b5b0dac3e5caf1741073fa123fdd56ab9.tar.gz
dexon-solidity-d634d20b5b0dac3e5caf1741073fa123fdd56ab9.tar.bz2
dexon-solidity-d634d20b5b0dac3e5caf1741073fa123fdd56ab9.tar.lz
dexon-solidity-d634d20b5b0dac3e5caf1741073fa123fdd56ab9.tar.xz
dexon-solidity-d634d20b5b0dac3e5caf1741073fa123fdd56ab9.tar.zst
dexon-solidity-d634d20b5b0dac3e5caf1741073fa123fdd56ab9.zip
Merge pull request #4684 from ethereum/underscores_in_numeric_literals
[BREAKING] Underscores in numeric literals
Diffstat (limited to 'test')
-rw-r--r--test/libsolidity/SolidityEndToEndTest.cpp16
-rw-r--r--test/libsolidity/SolidityScanner.cpp70
-rw-r--r--test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_decimal.sol13
-rw-r--r--test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_decimal_fail.sol13
-rw-r--r--test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_fixed.sol9
-rw-r--r--test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_fixed_fail.sol17
-rw-r--r--test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_hex.sol13
-rw-r--r--test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_hex_fail.sol7
8 files changed, 158 insertions, 0 deletions
diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp
index 9f7602d1..e487f0ae 100644
--- a/test/libsolidity/SolidityEndToEndTest.cpp
+++ b/test/libsolidity/SolidityEndToEndTest.cpp
@@ -12835,6 +12835,22 @@ BOOST_AUTO_TEST_CASE(write_storage_external)
ABI_CHECK(callContractFunction("h()"), encodeArgs(12));
}
+BOOST_AUTO_TEST_CASE(test_underscore_in_hex)
+{
+ char const* sourceCode = R"(
+ contract test {
+ function f(bool cond) public pure returns (uint) {
+ uint32 x = 0x1234_ab;
+ uint y = 0x1234_abcd_1234;
+ return cond ? x : y;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ ABI_CHECK(callContractFunction("f(bool)", true), encodeArgs(u256(0x1234ab)));
+ ABI_CHECK(callContractFunction("f(bool)", false), encodeArgs(u256(0x1234abcd1234)));
+}
+
BOOST_AUTO_TEST_SUITE_END()
}
diff --git a/test/libsolidity/SolidityScanner.cpp b/test/libsolidity/SolidityScanner.cpp
index 42e1b18e..f2e756bb 100644
--- a/test/libsolidity/SolidityScanner.cpp
+++ b/test/libsolidity/SolidityScanner.cpp
@@ -155,6 +155,76 @@ BOOST_AUTO_TEST_CASE(trailing_dot)
BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
}
+BOOST_AUTO_TEST_CASE(leading_underscore_decimal_is_identifier)
+{
+ // Actual error is cought by SyntaxChecker.
+ Scanner scanner(CharStream("_1.2"));
+ BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Number);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+}
+
+BOOST_AUTO_TEST_CASE(leading_underscore_decimal_after_dot_illegal)
+{
+ // Actual error is cought by SyntaxChecker.
+ Scanner scanner(CharStream("1._2"));
+ BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Number);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+
+ scanner.reset(CharStream("1._"), "");
+ BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Number);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+}
+
+BOOST_AUTO_TEST_CASE(leading_underscore_exp_are_identifier)
+{
+ // Actual error is cought by SyntaxChecker.
+ Scanner scanner(CharStream("_1e2"));
+ BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+}
+
+BOOST_AUTO_TEST_CASE(leading_underscore_exp_after_e_illegal)
+{
+ // Actual error is cought by SyntaxChecker.
+ Scanner scanner(CharStream("1e_2"));
+ BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Number);
+ BOOST_CHECK_EQUAL(scanner.currentLiteral(), "1e_2");
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+}
+
+BOOST_AUTO_TEST_CASE(leading_underscore_hex_illegal)
+{
+ Scanner scanner(CharStream("0x_abc"));
+ BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Illegal);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+}
+
+BOOST_AUTO_TEST_CASE(fixed_number_invalid_underscore_front)
+{
+ // Actual error is cought by SyntaxChecker.
+ Scanner scanner(CharStream("12._1234_1234"));
+ BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Number);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+}
+
+BOOST_AUTO_TEST_CASE(number_literals_with_trailing_underscore_at_eos)
+{
+ // Actual error is cought by SyntaxChecker.
+ Scanner scanner(CharStream("0x123_"));
+ BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Number);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+
+ scanner.reset(CharStream("123_"), "");
+ BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Number);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+
+ scanner.reset(CharStream("12.34_"), "");
+ BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Number);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+}
+
BOOST_AUTO_TEST_CASE(negative_numbers)
{
Scanner scanner(CharStream("var x = -.2 + -0x78 + -7.3 + 8.9 + 2e-2;"));
diff --git a/test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_decimal.sol b/test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_decimal.sol
new file mode 100644
index 00000000..d4d3299f
--- /dev/null
+++ b/test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_decimal.sol
@@ -0,0 +1,13 @@
+contract C {
+ function f() public pure {
+ uint d1 = 654_321;
+ uint d2 = 54_321;
+ uint d3 = 4_321;
+ uint d4 = 5_43_21;
+ uint d5 = 1_2e10;
+ uint d6 = 12e1_0;
+
+ d1; d2; d3; d4; d5; d6;
+ }
+}
+// ----
diff --git a/test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_decimal_fail.sol b/test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_decimal_fail.sol
new file mode 100644
index 00000000..8978996d
--- /dev/null
+++ b/test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_decimal_fail.sol
@@ -0,0 +1,13 @@
+contract C {
+ function f() public pure {
+ uint D1 = 1234_;
+ uint D2 = 12__34;
+ uint D3 = 12_e34;
+ uint D4 = 12e_34;
+ }
+}
+// ----
+// SyntaxError: (56-61): Invalid use of underscores in number literal. No trailing underscores allowed.
+// SyntaxError: (77-83): Invalid use of underscores in number literal. Only one consecutive underscores between digits allowed.
+// SyntaxError: (99-105): Invalid use of underscores in number literal. No underscore at the end of the mantissa allowed.
+// SyntaxError: (121-127): Invalid use of underscores in number literal. No underscore in front of exponent allowed.
diff --git a/test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_fixed.sol b/test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_fixed.sol
new file mode 100644
index 00000000..4910ee82
--- /dev/null
+++ b/test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_fixed.sol
@@ -0,0 +1,9 @@
+contract C {
+ function f() public pure {
+ fixed f1 = 3.14_15;
+ fixed f2 = 3_1.4_15;
+
+ f1; f2;
+ }
+}
+// ----
diff --git a/test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_fixed_fail.sol b/test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_fixed_fail.sol
new file mode 100644
index 00000000..3b91895d
--- /dev/null
+++ b/test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_fixed_fail.sol
@@ -0,0 +1,17 @@
+contract C {
+ function f() public pure {
+ fixed F1 = 3.1415_;
+ fixed F2 = 3__1.4__15;
+ fixed F3 = 1_.2;
+ fixed F4 = 1._2;
+ fixed F5 = 1.2e_12;
+ fixed F6 = 1._;
+ }
+}
+// ----
+// SyntaxError: (57-64): Invalid use of underscores in number literal. No trailing underscores allowed.
+// SyntaxError: (81-91): Invalid use of underscores in number literal. Only one consecutive underscores between digits allowed.
+// SyntaxError: (108-112): Invalid use of underscores in number literal. No underscores in front of the fraction part allowed.
+// SyntaxError: (129-133): Invalid use of underscores in number literal. No underscores in front of the fraction part allowed.
+// SyntaxError: (150-157): Invalid use of underscores in number literal. No underscore in front of exponent allowed.
+// SyntaxError: (174-177): Invalid use of underscores in number literal. No trailing underscores allowed.
diff --git a/test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_hex.sol b/test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_hex.sol
new file mode 100644
index 00000000..999a4634
--- /dev/null
+++ b/test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_hex.sol
@@ -0,0 +1,13 @@
+contract C {
+ function f() public pure {
+ uint x1 = 0x8765_4321;
+ uint x2 = 0x765_4321;
+ uint x3 = 0x65_4321;
+ uint x4 = 0x5_4321;
+ uint x5 = 0x123_1234_1234_1234;
+ uint x6 = 0x123456_1234_1234;
+
+ x1; x2; x3; x4; x5; x6;
+ }
+}
+// ----
diff --git a/test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_hex_fail.sol b/test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_hex_fail.sol
new file mode 100644
index 00000000..a8a488c1
--- /dev/null
+++ b/test/libsolidity/syntaxTests/parsing/lexer_numbers_with_underscores_hex_fail.sol
@@ -0,0 +1,7 @@
+contract C {
+ function f() public pure {
+ uint X1 = 0x1234__1234__1234__123;
+ }
+}
+// ----
+// SyntaxError: (56-79): Invalid use of underscores in number literal. Only one consecutive underscores between digits allowed.