From cbd933ceedc9d38df356856eec7bfca07d440523 Mon Sep 17 00:00:00 2001 From: VoR0220 Date: Fri, 9 Sep 2016 21:03:23 -0500 Subject: Solidity helper function for testing fixed points Signed-off-by: VoR0220 --- test/libsolidity/SolidityExecutionFramework.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'test/libsolidity/SolidityExecutionFramework.h') diff --git a/test/libsolidity/SolidityExecutionFramework.h b/test/libsolidity/SolidityExecutionFramework.h index 6cf7e0ee..a4181dfe 100644 --- a/test/libsolidity/SolidityExecutionFramework.h +++ b/test/libsolidity/SolidityExecutionFramework.h @@ -39,6 +39,7 @@ namespace dev { namespace solidity { + using rational = boost::rational; /// An Ethereum address: 20 bytes. /// @NOTE This is not endian-specific; it's just a bunch of bytes. using Address = h160; @@ -186,7 +187,12 @@ public: { return encodeArgs(u256(0x20), u256(_arg.size()), _arg); } - + static u256 fixed(dev::bigint _numerator, dev::bigint _denominator, int _fixedBits) + { + rational _value = rational(dev::bigint(_numerator), dev::bigint(_denominator)); + rational value = _value * boost::multiprecision::pow(bigint(2), _fixedBits); + return u256(value.numerator()/value.denominator()); + } class ContractInterface { public: -- cgit From 5fb0bcce4532e9dec8a5aa0ef056d5a1c12405a2 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 12 Oct 2016 17:28:47 +0200 Subject: Replace `fixed` by standard `encode` function. This way, we can use ``` BOOST_CHECK(callContractFunction("t(fixed8x128)", make_pair(rational(2, 3), 128)) == encodeArgs(u256(9))); ``` which is a bit more self-explanatory, I would say. --- test/libsolidity/SolidityExecutionFramework.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'test/libsolidity/SolidityExecutionFramework.h') diff --git a/test/libsolidity/SolidityExecutionFramework.h b/test/libsolidity/SolidityExecutionFramework.h index a4181dfe..bfd87c2b 100644 --- a/test/libsolidity/SolidityExecutionFramework.h +++ b/test/libsolidity/SolidityExecutionFramework.h @@ -156,6 +156,14 @@ public: static bytes encode(char const* _value) { return encode(std::string(_value)); } static bytes encode(byte _value) { return bytes(31, 0) + bytes{_value}; } static bytes encode(u256 const& _value) { return toBigEndian(_value); } + /// @returns the fixed-point encoding of a rational number with a given + /// number of fractional bits. + static bytes encode(std::pair const& _valueAndPrecision) + { + rational const& value = _valueAndPrecision.first; + int fractionalBits = _valueAndPrecision.second; + return encode(u256((value.numerator() << fractionalBits) / value.denominator())); + } static bytes encode(h256 const& _value) { return _value.asBytes(); } static bytes encode(bytes const& _value, bool _padLeft = true) { @@ -187,12 +195,6 @@ public: { return encodeArgs(u256(0x20), u256(_arg.size()), _arg); } - static u256 fixed(dev::bigint _numerator, dev::bigint _denominator, int _fixedBits) - { - rational _value = rational(dev::bigint(_numerator), dev::bigint(_denominator)); - rational value = _value * boost::multiprecision::pow(bigint(2), _fixedBits); - return u256(value.numerator()/value.denominator()); - } class ContractInterface { public: -- cgit