From b563be7af9c2d2bcab9c8b7baa94ad7342dc23a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20L=C3=B3pez?= Date: Sun, 18 Sep 2022 10:39:14 -0300 Subject: [PATCH] Add tests for rational and scientific notation constants --- tests/constant_folding_rational.sol | 7 +++++++ tests/test_constant_folding.py | 30 ++++++++++++++++++++++++++++ tests/test_constant_folding_unary.py | 5 ----- 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 tests/constant_folding_rational.sol create mode 100644 tests/test_constant_folding.py delete mode 100644 tests/test_constant_folding_unary.py diff --git a/tests/constant_folding_rational.sol b/tests/constant_folding_rational.sol new file mode 100644 index 000000000..36c44fb35 --- /dev/null +++ b/tests/constant_folding_rational.sol @@ -0,0 +1,7 @@ +contract C { + uint256 constant a = 2.5 + 7 + 0.5; + int128 b = 6 / 3.0; + int64 constant c = 5 * 0.5 + 0.5; + int256 d = 1e3 + 5E2; + uint256 e = 2 ** 255; +} \ No newline at end of file diff --git a/tests/test_constant_folding.py b/tests/test_constant_folding.py new file mode 100644 index 000000000..8a7f882af --- /dev/null +++ b/tests/test_constant_folding.py @@ -0,0 +1,30 @@ +from slither import Slither +from slither.visitors.expression.constants_folding import ConstantFolding + + +def test_constant_folding_unary(): + Slither("./tests/constant_folding_unary.sol") + +def test_constant_folding_rational(): + s = Slither("./tests/constant_folding_rational.sol") + contract = s.get_contract_from_name("C")[0] + + variable_a = contract.get_state_variable_from_name("a") + assert str(variable_a.type) == "uint256" + assert str(ConstantFolding(variable_a.expression, "uint256").result()) == "10" + + variable_b = contract.get_state_variable_from_name("b") + assert str(variable_b.type) == "int128" + assert str(ConstantFolding(variable_b.expression, "int128").result()) == "2" + + variable_c = contract.get_state_variable_from_name("c") + assert str(variable_c.type) == "int64" + assert str(ConstantFolding(variable_c.expression, "int64").result()) == "3" + + variable_d = contract.get_state_variable_from_name("d") + assert str(variable_d.type) == "int256" + assert str(ConstantFolding(variable_d.expression, "int256").result()) == "1500" + + variable_e = contract.get_state_variable_from_name("e") + assert str(variable_e.type) == "uint256" + assert str(ConstantFolding(variable_e.expression, "uint256").result()) == "57896044618658097711785492504343953926634992332820282019728792003956564819968" \ No newline at end of file diff --git a/tests/test_constant_folding_unary.py b/tests/test_constant_folding_unary.py deleted file mode 100644 index fa51d0934..000000000 --- a/tests/test_constant_folding_unary.py +++ /dev/null @@ -1,5 +0,0 @@ -from slither import Slither - - -def test_constant_folding_unary(): - Slither("./tests/constant_folding_unary.sol")