From 5076f532d8f0989e394452b40186888b30ad70ab Mon Sep 17 00:00:00 2001 From: liana Date: Fri, 23 Jan 2015 17:36:12 +0100 Subject: [PATCH 1/3] - added conversion for string/hash of equal sizes - added tests --- SolidityEndToEndTest.cpp | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/SolidityEndToEndTest.cpp b/SolidityEndToEndTest.cpp index cf04edaad..b0b450738 100644 --- a/SolidityEndToEndTest.cpp +++ b/SolidityEndToEndTest.cpp @@ -940,6 +940,72 @@ BOOST_AUTO_TEST_CASE(type_conversions_cleanup) 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22})); } +BOOST_AUTO_TEST_CASE(convert_string_to_string) +{ + char const* sourceCode = R"( + contract Test { + function pipeTrough(string3 input) returns (string3 ret) { + return string3(input); + } + })"; + compileAndRun(sourceCode); + string s = "abc"; + auto res = callContractFunction("pipeTrough(string3)", "abc"); + auto goldenRes = encodeArgs(s); + BOOST_CHECK(callContractFunction("pipeTrough(string3)", "abc") == encodeArgs("abc")); +} + +BOOST_AUTO_TEST_CASE(convert_hash_to_string_same_size) +{ + char const* sourceCode = R"( + contract Test { + function hashToString(hash h) returns (string32 s) { + return string32(h); + } + })"; + compileAndRun(sourceCode); + u256 a("0x6162630000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK(callContractFunction("hashToString(hash256)", a) == encodeArgs(a)); +} + +BOOST_AUTO_TEST_CASE(convert_hash_to_string_different_size) +{ + char const* sourceCode = R"( + contract Test { + function hashToString(hash160 h) returns (string20 s) { + return string20(h); + } + })"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("hashToString(hash160)", u160("0x00000000000000616263")) == + encodeArgs(u256("0x0000000000000000000000000000000000616263000000000000000000000000"))); +} + +BOOST_AUTO_TEST_CASE(convert_string_to_hash_same_size) +{ + char const* sourceCode = R"( + contract Test { + function stringToHash(string32 s) returns (hash h) { + return hash(s); + } + })"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("stringToHash(string32)", string("abc2")) == + encodeArgs(u256("0x6162633200000000000000000000000000000000000000000000000000000000"))); +} + +BOOST_AUTO_TEST_CASE(convert_string_to_hash_different_size) +{ + char const* sourceCode = R"( + contract Test { + function stringToHash(string20 s) returns (hash160 h) { + return hash160(s); + } + })"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("stringToHash(string20)", u256("0x0000000000000000000000000000000000616263000000000000000000000000")) == + encodeArgs(u160("0x00000000000000616263"))); +} BOOST_AUTO_TEST_CASE(send_ether) { From f5b739da9a7ae3a1dac4532b7616e11a64722cbe Mon Sep 17 00:00:00 2001 From: liana Date: Mon, 26 Jan 2015 13:24:16 +0100 Subject: [PATCH 2/3] - modifications according to PR review --- SolidityEndToEndTest.cpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/SolidityEndToEndTest.cpp b/SolidityEndToEndTest.cpp index b0b450738..1d4839635 100644 --- a/SolidityEndToEndTest.cpp +++ b/SolidityEndToEndTest.cpp @@ -949,9 +949,6 @@ BOOST_AUTO_TEST_CASE(convert_string_to_string) } })"; compileAndRun(sourceCode); - string s = "abc"; - auto res = callContractFunction("pipeTrough(string3)", "abc"); - auto goldenRes = encodeArgs(s); BOOST_CHECK(callContractFunction("pipeTrough(string3)", "abc") == encodeArgs("abc")); } @@ -977,8 +974,8 @@ BOOST_AUTO_TEST_CASE(convert_hash_to_string_different_size) } })"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("hashToString(hash160)", u160("0x00000000000000616263")) == - encodeArgs(u256("0x0000000000000000000000000000000000616263000000000000000000000000"))); + BOOST_CHECK(callContractFunction("hashToString(hash160)", u160("0x6161626361626361626361616263616263616263")) == + encodeArgs(string("aabcabcabcaabcabcabc"))); } BOOST_AUTO_TEST_CASE(convert_string_to_hash_same_size) @@ -1003,8 +1000,22 @@ BOOST_AUTO_TEST_CASE(convert_string_to_hash_different_size) } })"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("stringToHash(string20)", u256("0x0000000000000000000000000000000000616263000000000000000000000000")) == - encodeArgs(u160("0x00000000000000616263"))); + BOOST_CHECK(callContractFunction("stringToHash(string20)", string("aabcabcabcaabcabcabc")) == + encodeArgs(u160("0x6161626361626361626361616263616263616263"))); +} + + +BOOST_AUTO_TEST_CASE(convert_string_to_hash_different_min_size) +{ + char const* sourceCode = R"( + contract Test { + function stringToHash(string1 s) returns (hash8 h) { + return hash8(s); + } + })"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("stringToHash(string1)", string("a")) == + encodeArgs(u128("0x00000000000000000000000000000061"))); } BOOST_AUTO_TEST_CASE(send_ether) From b06de7e8de391c7b9b2070d0bbfa5038271691f2 Mon Sep 17 00:00:00 2001 From: liana Date: Tue, 27 Jan 2015 10:48:10 +0100 Subject: [PATCH 3/3] -added test case for hash8 to string1 conversion --- SolidityEndToEndTest.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/SolidityEndToEndTest.cpp b/SolidityEndToEndTest.cpp index 1d4839635..4f4347bff 100644 --- a/SolidityEndToEndTest.cpp +++ b/SolidityEndToEndTest.cpp @@ -1015,7 +1015,21 @@ BOOST_AUTO_TEST_CASE(convert_string_to_hash_different_min_size) })"; compileAndRun(sourceCode); BOOST_CHECK(callContractFunction("stringToHash(string1)", string("a")) == - encodeArgs(u128("0x00000000000000000000000000000061"))); + encodeArgs(u256("0x61"))); +} + + +BOOST_AUTO_TEST_CASE(convert_hash_to_string_different_min_size) +{ + char const* sourceCode = R"( + contract Test { + function HashToString(hash8 h) returns (string1 s) { + return string1(h); + } + })"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("HashToString(hash8)", u256("0x61")) == + encodeArgs(string("a"))); } BOOST_AUTO_TEST_CASE(send_ether)