From 558d0999c8c8bb8d09501830bf90aec4db74256e Mon Sep 17 00:00:00 2001 From: Christian Date: Thu, 13 Nov 2014 01:12:57 +0100 Subject: [PATCH] Struct types. --- solidityEndToEndTest.cpp | 37 ++++++++++++++++++++++++++++++ solidityNameAndTypeResolution.cpp | 38 +++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/solidityEndToEndTest.cpp b/solidityEndToEndTest.cpp index 5e6631df6..617cbabc9 100644 --- a/solidityEndToEndTest.cpp +++ b/solidityEndToEndTest.cpp @@ -663,6 +663,43 @@ BOOST_AUTO_TEST_CASE(multi_level_mapping) testSolidityAgainstCpp(0, f, u256(5), u256(4), u256(0)); } +BOOST_AUTO_TEST_CASE(structs) +{ + char const* sourceCode = "contract test {\n" + " struct s1 {\n" + " uint8 x;\n" + " bool y;\n" + " }\n" + " struct s2 {\n" + " uint32 z;\n" + " s1 s1data;\n" + " mapping(uint8 => s2) recursive;\n" + " }\n" + " s2 data;\n" + " function check() returns (bool ok) {\n" + " return data.z == 1 && data.s1data.x == 2 && \n" + " data.s1data.y == true && \n" + " data.recursive[3].recursive[4].z == 5 && \n" + " data.recursive[4].recursive[3].z == 6 && \n" + " data.recursive[0].s1data.y == false && \n" + " data.recursive[4].z == 9;\n" + " }\n" + " function set() {\n" + " data.z = 1;\n" + " data.s1data.x = 2;\n" + " data.s1data.y = true;\n" + " data.recursive[3].recursive[4].z = 5;\n" + " data.recursive[4].recursive[3].z = 6;\n" + " data.recursive[0].s1data.y = false;\n" + " data.recursive[4].z = 9;\n" + " }\n" + "}\n"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction(0) == bytes({0x00})); + BOOST_CHECK(callContractFunction(1) == bytes()); + BOOST_CHECK(callContractFunction(0) == bytes({0x01})); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/solidityNameAndTypeResolution.cpp b/solidityNameAndTypeResolution.cpp index f46ad6733..930bba0e3 100644 --- a/solidityNameAndTypeResolution.cpp +++ b/solidityNameAndTypeResolution.cpp @@ -121,6 +121,44 @@ BOOST_AUTO_TEST_CASE(reference_to_later_declaration) BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text)); } +BOOST_AUTO_TEST_CASE(struct_definition_directly_recursive) +{ + char const* text = "contract test {\n" + " struct MyStructName {\n" + " address addr;\n" + " MyStructName x;\n" + " }\n" + "}\n"; + BOOST_CHECK_THROW(parseTextAndResolveNames(text), ParserError); +} + +BOOST_AUTO_TEST_CASE(struct_definition_indirectly_recursive) +{ + char const* text = "contract test {\n" + " struct MyStructName1 {\n" + " address addr;\n" + " uint256 count;\n" + " MyStructName2 x;\n" + " }\n" + " struct MyStructName2 {\n" + " MyStructName1 x;\n" + " }\n" + "}\n"; + BOOST_CHECK_THROW(parseTextAndResolveNames(text), ParserError); +} + +BOOST_AUTO_TEST_CASE(struct_definition_recursion_via_mapping) +{ + char const* text = "contract test {\n" + " struct MyStructName1 {\n" + " address addr;\n" + " uint256 count;\n" + " mapping(uint => MyStructName1) x;\n" + " }\n" + "}\n"; + BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text)); +} + BOOST_AUTO_TEST_CASE(type_inference_smoke_test) { char const* text = "contract test {\n"