aboutsummaryrefslogtreecommitdiffstats
path: root/test/libsolidity
diff options
context:
space:
mode:
authorchriseth <c@ethdev.com>2015-12-01 21:07:33 +0800
committerchriseth <c@ethdev.com>2015-12-01 21:07:33 +0800
commitd909f3a57bdd1ca43a132a80293e280910459f0d (patch)
treebd87da97c529b0ca7c7103f6ccff9909c348766a /test/libsolidity
parente853eb22fa0ec391812144675d4f000638db11b7 (diff)
parent7f415da886db51a5243749a7968917087fb4ca35 (diff)
downloaddexon-solidity-d909f3a57bdd1ca43a132a80293e280910459f0d.tar.gz
dexon-solidity-d909f3a57bdd1ca43a132a80293e280910459f0d.tar.zst
dexon-solidity-d909f3a57bdd1ca43a132a80293e280910459f0d.zip
Merge pull request #262 from chriseth/bind_codegeneration
Code generation for bound methods
Diffstat (limited to 'test/libsolidity')
-rw-r--r--test/libsolidity/SolidityEndToEndTest.cpp120
1 files changed, 120 insertions, 0 deletions
diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp
index 78ceacfa..b69e253d 100644
--- a/test/libsolidity/SolidityEndToEndTest.cpp
+++ b/test/libsolidity/SolidityEndToEndTest.cpp
@@ -5957,6 +5957,126 @@ BOOST_AUTO_TEST_CASE(string_allocation_bug)
));
}
+BOOST_AUTO_TEST_CASE(using_for_function_on_int)
+{
+ char const* sourceCode = R"(
+ library D { function double(uint self) returns (uint) { return 2*self; } }
+ contract C {
+ using D for uint;
+ function f(uint a) returns (uint) {
+ return a.double();
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "D");
+ compileAndRun(sourceCode, 0, "C", bytes(), map<string, Address>{{"D", m_contractAddress}});
+ BOOST_CHECK(callContractFunction("f(uint256)", u256(9)) == encodeArgs(u256(2 * 9)));
+}
+
+BOOST_AUTO_TEST_CASE(using_for_function_on_struct)
+{
+ char const* sourceCode = R"(
+ library D { struct s { uint a; } function mul(s storage self, uint x) returns (uint) { return self.a *= x; } }
+ contract C {
+ using D for D.s;
+ D.s public x;
+ function f(uint a) returns (uint) {
+ x.a = 3;
+ return x.mul(a);
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "D");
+ compileAndRun(sourceCode, 0, "C", bytes(), map<string, Address>{{"D", m_contractAddress}});
+ BOOST_CHECK(callContractFunction("f(uint256)", u256(7)) == encodeArgs(u256(3 * 7)));
+ BOOST_CHECK(callContractFunction("x()") == encodeArgs(u256(3 * 7)));
+}
+
+BOOST_AUTO_TEST_CASE(using_for_overload)
+{
+ char const* sourceCode = R"(
+ library D {
+ struct s { uint a; }
+ function mul(s storage self, uint x) returns (uint) { return self.a *= x; }
+ function mul(s storage self, bytes32 x) returns (bytes32) { }
+ }
+ contract C {
+ using D for D.s;
+ D.s public x;
+ function f(uint a) returns (uint) {
+ x.a = 6;
+ return x.mul(a);
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "D");
+ compileAndRun(sourceCode, 0, "C", bytes(), map<string, Address>{{"D", m_contractAddress}});
+ BOOST_CHECK(callContractFunction("f(uint256)", u256(7)) == encodeArgs(u256(6 * 7)));
+ BOOST_CHECK(callContractFunction("x()") == encodeArgs(u256(6 * 7)));
+}
+
+BOOST_AUTO_TEST_CASE(using_for_by_name)
+{
+ char const* sourceCode = R"(
+ library D { struct s { uint a; } function mul(s storage self, uint x) returns (uint) { return self.a *= x; } }
+ contract C {
+ using D for D.s;
+ D.s public x;
+ function f(uint a) returns (uint) {
+ x.a = 6;
+ return x.mul({x: a});
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "D");
+ compileAndRun(sourceCode, 0, "C", bytes(), map<string, Address>{{"D", m_contractAddress}});
+ BOOST_CHECK(callContractFunction("f(uint256)", u256(7)) == encodeArgs(u256(6 * 7)));
+ BOOST_CHECK(callContractFunction("x()") == encodeArgs(u256(6 * 7)));
+}
+
+BOOST_AUTO_TEST_CASE(bound_function_in_var)
+{
+ char const* sourceCode = R"(
+ library D { struct s { uint a; } function mul(s storage self, uint x) returns (uint) { return self.a *= x; } }
+ contract C {
+ using D for D.s;
+ D.s public x;
+ function f(uint a) returns (uint) {
+ x.a = 6;
+ var g = x.mul;
+ return g({x: a});
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "D");
+ compileAndRun(sourceCode, 0, "C", bytes(), map<string, Address>{{"D", m_contractAddress}});
+ BOOST_CHECK(callContractFunction("f(uint256)", u256(7)) == encodeArgs(u256(6 * 7)));
+ BOOST_CHECK(callContractFunction("x()") == encodeArgs(u256(6 * 7)));
+}
+
+BOOST_AUTO_TEST_CASE(bound_function_to_string)
+{
+ char const* sourceCode = R"(
+ library D { function length(string memory self) returns (uint) { return bytes(self).length; } }
+ contract C {
+ using D for string;
+ string x;
+ function f() returns (uint) {
+ x = "abc";
+ return x.length();
+ }
+ function g() returns (uint) {
+ string memory s = "abc";
+ return s.length();
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "D");
+ compileAndRun(sourceCode, 0, "C", bytes(), map<string, Address>{{"D", m_contractAddress}});
+ BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(3)));
+ BOOST_CHECK(callContractFunction("g()") == encodeArgs(u256(3)));
+}
+
BOOST_AUTO_TEST_SUITE_END()
}