@@ -5957,6 +5957,126 @@ BOOST_AUTO_TEST_CASE(string_allocation_bug)
59575957 ));
59585958}
59595959
5960+ BOOST_AUTO_TEST_CASE (using_for_function_on_int)
5961+ {
5962+ char const * sourceCode = R"(
5963+ library D { function double(uint self) returns (uint) { return 2*self; } }
5964+ contract C {
5965+ using D for uint;
5966+ function f(uint a) returns (uint) {
5967+ return a.double();
5968+ }
5969+ }
5970+ )" ;
5971+ compileAndRun (sourceCode, 0 , " D" );
5972+ compileAndRun (sourceCode, 0 , " C" , bytes (), map<string, Address>{{" D" , m_contractAddress}});
5973+ BOOST_CHECK (callContractFunction (" f(uint256)" , u256 (9 )) == encodeArgs (u256 (2 * 9 )));
5974+ }
5975+
5976+ BOOST_AUTO_TEST_CASE (using_for_function_on_struct)
5977+ {
5978+ char const * sourceCode = R"(
5979+ library D { struct s { uint a; } function mul(s storage self, uint x) returns (uint) { return self.a *= x; } }
5980+ contract C {
5981+ using D for D.s;
5982+ D.s public x;
5983+ function f(uint a) returns (uint) {
5984+ x.a = 3;
5985+ return x.mul(a);
5986+ }
5987+ }
5988+ )" ;
5989+ compileAndRun (sourceCode, 0 , " D" );
5990+ compileAndRun (sourceCode, 0 , " C" , bytes (), map<string, Address>{{" D" , m_contractAddress}});
5991+ BOOST_CHECK (callContractFunction (" f(uint256)" , u256 (7 )) == encodeArgs (u256 (3 * 7 )));
5992+ BOOST_CHECK (callContractFunction (" x()" ) == encodeArgs (u256 (3 * 7 )));
5993+ }
5994+
5995+ BOOST_AUTO_TEST_CASE (using_for_overload)
5996+ {
5997+ char const * sourceCode = R"(
5998+ library D {
5999+ struct s { uint a; }
6000+ function mul(s storage self, uint x) returns (uint) { return self.a *= x; }
6001+ function mul(s storage self, bytes32 x) returns (bytes32) { }
6002+ }
6003+ contract C {
6004+ using D for D.s;
6005+ D.s public x;
6006+ function f(uint a) returns (uint) {
6007+ x.a = 6;
6008+ return x.mul(a);
6009+ }
6010+ }
6011+ )" ;
6012+ compileAndRun (sourceCode, 0 , " D" );
6013+ compileAndRun (sourceCode, 0 , " C" , bytes (), map<string, Address>{{" D" , m_contractAddress}});
6014+ BOOST_CHECK (callContractFunction (" f(uint256)" , u256 (7 )) == encodeArgs (u256 (6 * 7 )));
6015+ BOOST_CHECK (callContractFunction (" x()" ) == encodeArgs (u256 (6 * 7 )));
6016+ }
6017+
6018+ BOOST_AUTO_TEST_CASE (using_for_by_name)
6019+ {
6020+ char const * sourceCode = R"(
6021+ library D { struct s { uint a; } function mul(s storage self, uint x) returns (uint) { return self.a *= x; } }
6022+ contract C {
6023+ using D for D.s;
6024+ D.s public x;
6025+ function f(uint a) returns (uint) {
6026+ x.a = 6;
6027+ return x.mul({x: a});
6028+ }
6029+ }
6030+ )" ;
6031+ compileAndRun (sourceCode, 0 , " D" );
6032+ compileAndRun (sourceCode, 0 , " C" , bytes (), map<string, Address>{{" D" , m_contractAddress}});
6033+ BOOST_CHECK (callContractFunction (" f(uint256)" , u256 (7 )) == encodeArgs (u256 (6 * 7 )));
6034+ BOOST_CHECK (callContractFunction (" x()" ) == encodeArgs (u256 (6 * 7 )));
6035+ }
6036+
6037+ BOOST_AUTO_TEST_CASE (bound_function_in_var)
6038+ {
6039+ char const * sourceCode = R"(
6040+ library D { struct s { uint a; } function mul(s storage self, uint x) returns (uint) { return self.a *= x; } }
6041+ contract C {
6042+ using D for D.s;
6043+ D.s public x;
6044+ function f(uint a) returns (uint) {
6045+ x.a = 6;
6046+ var g = x.mul;
6047+ return g({x: a});
6048+ }
6049+ }
6050+ )" ;
6051+ compileAndRun (sourceCode, 0 , " D" );
6052+ compileAndRun (sourceCode, 0 , " C" , bytes (), map<string, Address>{{" D" , m_contractAddress}});
6053+ BOOST_CHECK (callContractFunction (" f(uint256)" , u256 (7 )) == encodeArgs (u256 (6 * 7 )));
6054+ BOOST_CHECK (callContractFunction (" x()" ) == encodeArgs (u256 (6 * 7 )));
6055+ }
6056+
6057+ BOOST_AUTO_TEST_CASE (bound_function_to_string)
6058+ {
6059+ char const * sourceCode = R"(
6060+ library D { function length(string memory self) returns (uint) { return bytes(self).length; } }
6061+ contract C {
6062+ using D for string;
6063+ string x;
6064+ function f() returns (uint) {
6065+ x = "abc";
6066+ return x.length();
6067+ }
6068+ function g() returns (uint) {
6069+ string memory s = "abc";
6070+ return s.length();
6071+ }
6072+ }
6073+ )" ;
6074+ compileAndRun (sourceCode, 0 , " D" );
6075+ compileAndRun (sourceCode, 0 , " C" , bytes (), map<string, Address>{{" D" , m_contractAddress}});
6076+ BOOST_CHECK (callContractFunction (" f()" ) == encodeArgs (u256 (3 )));
6077+ BOOST_CHECK (callContractFunction (" g()" ) == encodeArgs (u256 (3 )));
6078+ }
6079+
59606080BOOST_AUTO_TEST_SUITE_END ()
59616081
59626082}
0 commit comments