From 4e4957dcdd7e23a325d863e51509d07b8abb74cc Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Mon, 26 Sep 2022 20:08:21 -0400 Subject: [PATCH 1/4] enable constexpr in varint --- libraries/eosiolib/core/eosio/varint.hpp | 66 ++++---- tests/unit/varint_tests.cpp | 190 +++++++++++++++++++++++ 2 files changed, 223 insertions(+), 33 deletions(-) diff --git a/libraries/eosiolib/core/eosio/varint.hpp b/libraries/eosiolib/core/eosio/varint.hpp index f3a8804961..3365ca12e0 100644 --- a/libraries/eosiolib/core/eosio/varint.hpp +++ b/libraries/eosiolib/core/eosio/varint.hpp @@ -25,7 +25,7 @@ namespace eosio { * * @param v - Source */ - unsigned_int( uint32_t v = 0 ):value(v){} + constexpr unsigned_int( uint32_t v = 0 ):value(v){} /** * Construct a new unsigned int object from a type that is convertible to uint32_t @@ -35,7 +35,7 @@ namespace eosio { * @pre T must be convertible to uint32_t */ template - unsigned_int( T v ):value(v){} + constexpr unsigned_int( T v ):value(v){} //operator uint32_t()const { return value; } //operator uint64_t()const { return value; } @@ -47,7 +47,7 @@ namespace eosio { * @return T - Converted target */ template - operator T()const { return static_cast(value); } + constexpr operator T()const { return static_cast(value); } /// @cond OPERATORS @@ -57,7 +57,7 @@ namespace eosio { * @param v - Soruce * @return unsigned_int& - Reference to this object */ - unsigned_int& operator=( uint32_t v ) { value = v; return *this; } + constexpr unsigned_int& operator=( uint32_t v ) { value = v; return *this; } /// @endcond @@ -76,7 +76,7 @@ namespace eosio { * @return true - if equal * @return false - otherwise */ - friend bool operator==( const unsigned_int& i, const uint32_t& v ) { return i.value == v; } + constexpr friend bool operator==( const unsigned_int& i, const uint32_t& v ) { return i.value == v; } /** * Check equality between 32-bit unsigned integer and a unsigned_int object @@ -86,7 +86,7 @@ namespace eosio { * @return true - if equal * @return false - otherwise */ - friend bool operator==( const uint32_t& i, const unsigned_int& v ) { return i == v.value; } + constexpr friend bool operator==( const uint32_t& i, const unsigned_int& v ) { return i == v.value; } /** * Check equality between two unsigned_int objects @@ -96,7 +96,7 @@ namespace eosio { * @return true - if equal * @return false - otherwise */ - friend bool operator==( const unsigned_int& i, const unsigned_int& v ) { return i.value == v.value; } + constexpr friend bool operator==( const unsigned_int& i, const unsigned_int& v ) { return i.value == v.value; } /** * Check inequality between a unsigned_int object and 32-bit unsigned integer @@ -106,7 +106,7 @@ namespace eosio { * @return true - if inequal * @return false - otherwise */ - friend bool operator!=( const unsigned_int& i, const uint32_t& v ) { return i.value != v; } + constexpr friend bool operator!=( const unsigned_int& i, const uint32_t& v ) { return i.value != v; } /** * Check inequality between 32-bit unsigned integer and a unsigned_int object @@ -116,7 +116,7 @@ namespace eosio { * @return true - if unequal * @return false - otherwise */ - friend bool operator!=( const uint32_t& i, const unsigned_int& v ) { return i != v.value; } + constexpr friend bool operator!=( const uint32_t& i, const unsigned_int& v ) { return i != v.value; } /** * Check inequality between two unsigned_int objects @@ -126,7 +126,7 @@ namespace eosio { * @return true - if inequal * @return false - otherwise */ - friend bool operator!=( const unsigned_int& i, const unsigned_int& v ) { return i.value != v.value; } + constexpr friend bool operator!=( const unsigned_int& i, const unsigned_int& v ) { return i.value != v.value; } /** * Check if the given unsigned_int object is less than the given 32-bit unsigned integer @@ -136,7 +136,7 @@ namespace eosio { * @return true - if i less than v * @return false - otherwise */ - friend bool operator<( const unsigned_int& i, const uint32_t& v ) { return i.value < v; } + constexpr friend bool operator<( const unsigned_int& i, const uint32_t& v ) { return i.value < v; } /** * Check if the given 32-bit unsigned integer is less than the given unsigned_int object @@ -146,7 +146,7 @@ namespace eosio { * @return true - if i less than v * @return false - otherwise */ - friend bool operator<( const uint32_t& i, const unsigned_int& v ) { return i < v.value; } + constexpr friend bool operator<( const uint32_t& i, const unsigned_int& v ) { return i < v.value; } /** * Check if the first given unsigned_int is less than the second given unsigned_int object @@ -156,7 +156,7 @@ namespace eosio { * @return true - if i less than v * @return false - otherwise */ - friend bool operator<( const unsigned_int& i, const unsigned_int& v ) { return i.value < v.value; } + constexpr friend bool operator<( const unsigned_int& i, const unsigned_int& v ) { return i.value < v.value; } /** * Check if the given unsigned_int object is greater or equal to the given 32-bit unsigned integer @@ -166,7 +166,7 @@ namespace eosio { * @return true - if i is greater or equal to v * @return false - otherwise */ - friend bool operator>=( const unsigned_int& i, const uint32_t& v ) { return i.value >= v; } + constexpr friend bool operator>=( const unsigned_int& i, const uint32_t& v ) { return i.value >= v; } /** * Check if the given 32-bit unsigned integer is greater or equal to the given unsigned_int object @@ -176,7 +176,7 @@ namespace eosio { * @return true - if i is greater or equal to v * @return false - otherwise */ - friend bool operator>=( const uint32_t& i, const unsigned_int& v ) { return i >= v.value; } + constexpr friend bool operator>=( const uint32_t& i, const unsigned_int& v ) { return i >= v.value; } /** * Check if the first given unsigned_int is greater or equal to the second given unsigned_int object @@ -186,7 +186,7 @@ namespace eosio { * @return true - if i is greater or equal to v * @return false - otherwise */ - friend bool operator>=( const unsigned_int& i, const unsigned_int& v ) { return i.value >= v.value; } + constexpr friend bool operator>=( const unsigned_int& i, const unsigned_int& v ) { return i.value >= v.value; } /// @endcond @@ -249,7 +249,7 @@ namespace eosio { * * @param v - Source */ - signed_int( int32_t v = 0 ):value(v){} + constexpr signed_int( int32_t v = 0 ):value(v){} /// @cond OPERATORS @@ -258,7 +258,7 @@ namespace eosio { * * @return int32_t - The converted result */ - operator int32_t()const { return value; } + constexpr operator int32_t()const { return value; } /** @@ -269,21 +269,21 @@ namespace eosio { * @return unsigned_int& - Reference to this object */ template - signed_int& operator=( const T& v ) { value = v; return *this; } + constexpr signed_int& operator=( const T& v ) { value = v; return *this; } /** * Increment operator * * @return signed_int - New signed_int with value incremented from the current object's value */ - signed_int operator++(int) { return value++; } + constexpr signed_int operator++(int) { return value++; } /** * Increment operator * * @return signed_int - Reference to current object */ - signed_int& operator++(){ ++value; return *this; } + constexpr signed_int& operator++(){ ++value; return *this; } /// @endcond @@ -302,7 +302,7 @@ namespace eosio { * @return true - if equal * @return false - otherwise */ - friend bool operator==( const signed_int& i, const int32_t& v ) { return i.value == v; } + constexpr friend bool operator==( const signed_int& i, const int32_t& v ) { return i.value == v; } /** * Check equality between 32-bit integer and a signed_int object @@ -312,7 +312,7 @@ namespace eosio { * @return true - if equal * @return false - otherwise */ - friend bool operator==( const int32_t& i, const signed_int& v ) { return i == v.value; } + constexpr friend bool operator==( const int32_t& i, const signed_int& v ) { return i == v.value; } /** * Check equality between two signed_int objects @@ -322,7 +322,7 @@ namespace eosio { * @return true - if equal * @return false - otherwise */ - friend bool operator==( const signed_int& i, const signed_int& v ) { return i.value == v.value; } + constexpr friend bool operator==( const signed_int& i, const signed_int& v ) { return i.value == v.value; } /** @@ -333,7 +333,7 @@ namespace eosio { * @return true - if inequal * @return false - otherwise */ - friend bool operator!=( const signed_int& i, const int32_t& v ) { return i.value != v; } + constexpr friend bool operator!=( const signed_int& i, const int32_t& v ) { return i.value != v; } /** * Check inequality between 32-bit integer and a signed_int object @@ -343,7 +343,7 @@ namespace eosio { * @return true - if unequal * @return false - otherwise */ - friend bool operator!=( const int32_t& i, const signed_int& v ) { return i != v.value; } + constexpr friend bool operator!=( const int32_t& i, const signed_int& v ) { return i != v.value; } /** * Check inequality between two signed_int objects @@ -353,7 +353,7 @@ namespace eosio { * @return true - if inequal * @return false - otherwise */ - friend bool operator!=( const signed_int& i, const signed_int& v ) { return i.value != v.value; } + constexpr friend bool operator!=( const signed_int& i, const signed_int& v ) { return i.value != v.value; } /** * Check if the given signed_int object is less than the given 32-bit integer @@ -363,7 +363,7 @@ namespace eosio { * @return true - if i less than v * @return false - otherwise */ - friend bool operator<( const signed_int& i, const int32_t& v ) { return i.value < v; } + constexpr friend bool operator<( const signed_int& i, const int32_t& v ) { return i.value < v; } /** * Check if the given 32-bit integer is less than the given signed_int object @@ -373,7 +373,7 @@ namespace eosio { * @return true - if i less than v * @return false - otherwise */ - friend bool operator<( const int32_t& i, const signed_int& v ) { return i < v.value; } + constexpr friend bool operator<( const int32_t& i, const signed_int& v ) { return i < v.value; } /** * Check if the first given signed_int is less than the second given signed_int object @@ -383,7 +383,7 @@ namespace eosio { * @return true - if i less than v * @return false - otherwise */ - friend bool operator<( const signed_int& i, const signed_int& v ) { return i.value < v.value; } + constexpr friend bool operator<( const signed_int& i, const signed_int& v ) { return i.value < v.value; } /** @@ -394,7 +394,7 @@ namespace eosio { * @return true - if i is greater or equal to v * @return false - otherwise */ - friend bool operator>=( const signed_int& i, const int32_t& v ) { return i.value >= v; } + constexpr friend bool operator>=( const signed_int& i, const int32_t& v ) { return i.value >= v; } /** * Check if the given 32-bit integer is greater or equal to the given signed_int object @@ -404,7 +404,7 @@ namespace eosio { * @return true - if i is greater or equal to v * @return false - otherwise */ - friend bool operator>=( const int32_t& i, const signed_int& v ) { return i >= v.value; } + constexpr friend bool operator>=( const int32_t& i, const signed_int& v ) { return i >= v.value; } /** * Check if the first given signed_int is greater or equal to the second given signed_int object @@ -414,7 +414,7 @@ namespace eosio { * @return true - if i is greater or equal to v * @return false - otherwise */ - friend bool operator>=( const signed_int& i, const signed_int& v ) { return i.value >= v.value; } + constexpr friend bool operator>=( const signed_int& i, const signed_int& v ) { return i.value >= v.value; } /// @endcond diff --git a/tests/unit/varint_tests.cpp b/tests/unit/varint_tests.cpp index f79313ddf0..d1936be3f5 100644 --- a/tests/unit/varint_tests.cpp +++ b/tests/unit/varint_tests.cpp @@ -252,6 +252,194 @@ EOSIO_TEST_BEGIN(signed_int_type_test) CHECK_EQUAL( d, dd ) EOSIO_TEST_END +EOSIO_TEST_BEGIN(unsigned_int_constexpr_test) + //// unsigned_int(uint32_t) + static_assert( unsigned_int{}.value == 0 ); + static_assert( unsigned_int{u32min}.value == 0 ); + static_assert( unsigned_int{u32max}.value == 4294967295); + + //// unsigned_int(T) + static_assert( unsigned_int{uint8_t{0}}.value == 0 ); + static_assert( unsigned_int{uint16_t{1}}.value == 1 ); + static_assert( unsigned_int{uint32_t{2}}.value == 2 ); + static_assert( unsigned_int{uint64_t{3}}.value == 3 ); + + // ----------------- + // operator T()const + static_assert( unsigned_int{0}.operator bool() == false ); + static_assert( unsigned_int{1}.operator bool() == true ); + + // --------------------------------- + // unsigned_int& operator=(uint32_t) + constexpr unsigned_int ui0{42}; + constexpr unsigned_int ui1 = ui0; + static_assert( ui0 == ui1 ); + + // ------------------------------------------------------------ + // friend bool operator==(const unsigned_int&, const uint32_t&) + static_assert( unsigned_int{42} == uint32_t{42} ); + static_assert( (unsigned_int{42} == uint32_t{43}) == false ); + + // ------------------------------------------------------------ + // friend bool operator==(const uint32_t&, const unsigned_int&) + static_assert( uint32_t{42} == unsigned_int{42} ); + static_assert( (uint32_t{43} == unsigned_int{42}) == false ); + + // ---------------------------------------------------------------- + // friend bool operator==(const unsigned_int&, const unsigned_int&) + static_assert( unsigned_int{42} == unsigned_int{42} ); + static_assert( (unsigned_int{42} == unsigned_int{43}) == false); + + // ------------------------------------------------------------ + // friend bool operator!=(const unsigned_int&, const uint32_t&) + static_assert( (unsigned_int{42} != uint32_t{42}) == false ); + static_assert( unsigned_int{42} != uint32_t{43} ); + + // ------------------------------------------------------------ + // friend bool operator!=(const uint32_t&, const unsigned_int&) + static_assert( (uint32_t{42} != unsigned_int{42}) == false ); + static_assert( uint32_t{43} != unsigned_int{42} ); + + // ---------------------------------------------------------------- + // friend bool operator!=(const unsigned_int&, const unsigned_int&) + static_assert( (unsigned_int{42} != unsigned_int{42}) == false ); + static_assert( unsigned_int{42} != unsigned_int{43} ); + + // ------------------------------------------------------------ + // friend bool operator< (const unsigned_int&, const uint32_t&) + static_assert( (unsigned_int{42} < uint32_t{42}) == false ); + static_assert( unsigned_int{42} < uint32_t{43} ); + + // ------------------------------------------------------------ + // friend bool operator< (const uint32_t&, const unsigned_int&) + static_assert( (uint32_t{42} < unsigned_int{42}) == false ); + static_assert( uint32_t{42} < unsigned_int{43} ); + + // ---------------------------------------------------------------- + // friend bool operator< (const unsigned_int&, const unsigned_int&) + static_assert( (unsigned_int{42} < unsigned_int{42}) == false ); + static_assert( unsigned_int{42} < unsigned_int{43} ); + + // ------------------------------------------------------------ + // friend bool operator>=(const unsigned_int&, const uint32_t&) + static_assert( unsigned_int{42} >= uint32_t{42} ); + static_assert( (unsigned_int{42} >= uint32_t{43}) == false ); + + // ------------------------------------------------------------ + // friend bool operator>=(const uint32_t&, const unsigned_int&) + static_assert( uint32_t{42} >= unsigned_int{42} ); + static_assert( (uint32_t{42} >= unsigned_int{43}) == false ); + + // ---------------------------------------------------------------- + // friend bool operator>=(const unsigned_int&, const unsigned_int&) + static_assert( unsigned_int{42} >= unsigned_int{42} ); + static_assert( (unsigned_int{42} >= unsigned_int{43}) == false ); +EOSIO_TEST_END + +EOSIO_TEST_BEGIN(signed_int_constexpr_test) + //// signed_int(uint32_t) + static_assert( signed_int{}.value == 0 ); + static_assert( signed_int{i32min}.value == -2147483648 ); + static_assert( signed_int{i32max}.value == 2147483647 ); + + // ----------------------- + // operator int32_t()const + static_assert( signed_int{}.operator int32_t() == 0 ); + static_assert( signed_int{i32min}.operator int32_t() == -2147483648 ); + static_assert( signed_int{i32max}.operator int32_t() == 2147483647 ); + + // -------------------------------- + // signed_int& operator=(const T&) + constexpr int8_t i8{0}; + constexpr int16_t i16{1}; + constexpr int32_t i32{2}; + constexpr int64_t i64{3}; + + constexpr signed_int si0 = i8; + constexpr signed_int si1 = i16; + constexpr signed_int si2 = i32; + constexpr signed_int si3 = i64; + + static_assert( si0.value == 0 ); + static_assert( si1.value == 1 ); + static_assert( si2.value == 2 ); + static_assert( si3.value == 3 ); + + // -------------------------- + // signed_int operator++(int) + constexpr signed_int si_post_inc0{ signed_int{0}++ }; + constexpr signed_int si_post_inc1{ signed_int{1}++ }; + static_assert( si_post_inc0.value == 0 ); + static_assert( si_post_inc1.value == 1 ); + + // ------------------------ + // signed_int& operator++() + constexpr signed_int si_pre_inc0{ ++signed_int{0} }; + constexpr signed_int si_pre_inc1{ ++signed_int{1} }; + static_assert( si_pre_inc0.value == 1 ); + static_assert( si_pre_inc1.value == 2 ); + + // ------------------------------------------------------------ + // friend bool operator==(const signed_int&, const uint32_t&) + static_assert( signed_int{42} == int32_t{42} ); + static_assert( (signed_int{42} == int32_t{43}) == false ); + + // ---------------------------------------------------------- + // friend bool operator==(const uint32_t&, const signed_int&) + static_assert( int32_t{42} == signed_int{42} ); + static_assert( (int32_t{43} == signed_int{42}) == false ); + + // ------------------------------------------------------------ + // friend bool operator==(const signed_int&, const signed_int&) + static_assert( signed_int{42} == signed_int{42} ); + static_assert( (signed_int{42} == signed_int{43}) == false ); + + // ---------------------------------------------------------- + // friend bool operator!=(const signed_int&, const uint32_t&) + static_assert( (signed_int{42} != int32_t{42}) == false ); + static_assert( signed_int{42} != int32_t{43} ); + + // ---------------------------------------------------------- + // friend bool operator!=(const uint32_t&, const signed_int&) + static_assert( (int32_t{42} != signed_int{42}) == false ); + static_assert( int32_t{43} != signed_int{42} ); + + // ------------------------------------------------------------ + // friend bool operator!=(const signed_int&, const signed_int&) + static_assert( (signed_int{42} != signed_int{42}) == false ); + static_assert( signed_int{42} != signed_int{43} ); + + // ---------------------------------------------------------- + // friend bool operator< (const signed_int&, const uint32_t&) + static_assert( (signed_int{42} < int32_t{42}) == false ); + static_assert( signed_int{42} < int32_t{43} ); + + // ---------------------------------------------------------- + // friend bool operator< (const uint32_t&, const signed_int&) + static_assert( (int32_t{42} < signed_int{42}) == false ); + static_assert( int32_t{42} < signed_int{43} ); + + // ------------------------------------------------------------ + // friend bool operator< (const signed_int&, const signed_int&) + static_assert( (signed_int{42} < signed_int{42}) == false ); + static_assert( signed_int{42} < signed_int{43} ); + + // ---------------------------------------------------------- + // friend bool operator>=(const signed_int&, const uint32_t&) + static_assert( signed_int{42} >= int32_t{42} ); + static_assert( (signed_int{42} >= int32_t{43}) == false ); + + // ---------------------------------------------------------- + // friend bool operator>=(const uint32_t&, const signed_int&) + static_assert( int32_t{42} >= signed_int{42} ); + static_assert( (int32_t{42} >= signed_int{43}) == false ); + + // ------------------------------------------------------------ + // friend bool operator>=(const signed_int&, const signed_int&) + static_assert( signed_int{42} >= signed_int{42} ); + static_assert( (signed_int{42} >= signed_int{43}) == false ); +EOSIO_TEST_END + int main(int argc, char* argv[]) { bool verbose = false; if( argc >= 2 && std::strcmp( argv[1], "-v" ) == 0 ) { @@ -261,5 +449,7 @@ int main(int argc, char* argv[]) { EOSIO_TEST(unsigned_int_type_test) EOSIO_TEST(signed_int_type_test); + EOSIO_TEST(unsigned_int_constexpr_test); + EOSIO_TEST(signed_int_constexpr_test); return has_failed(); } From 588e260e103165774b6b936979f5a84eb1f83209 Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Tue, 27 Sep 2022 09:42:55 -0400 Subject: [PATCH 2/4] add more test cases to varint_tests for constexpr --- tests/unit/varint_tests.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/unit/varint_tests.cpp b/tests/unit/varint_tests.cpp index d1936be3f5..5b9f743821 100644 --- a/tests/unit/varint_tests.cpp +++ b/tests/unit/varint_tests.cpp @@ -334,6 +334,15 @@ EOSIO_TEST_BEGIN(unsigned_int_constexpr_test) // friend bool operator>=(const unsigned_int&, const unsigned_int&) static_assert( unsigned_int{42} >= unsigned_int{42} ); static_assert( (unsigned_int{42} >= unsigned_int{43}) == false ); + + // ---------- + // misc tests + static_assert( unsigned_int{0xff}.value == 255 ); // 1 byte + static_assert( unsigned_int{0xffff}.value == 65535 ); // 2 bytes + static_assert( unsigned_int{0xffffff}.value == 16777215 ); // 3 bytes + static_assert( unsigned_int{0xffffffff}.value == 4294967295 ); // 4 bytes + static_assert( unsigned_int{100l}.value == 100 ); // cast from long + static_assert( sizeof(unsigned_int{0xf}) == 4 ); EOSIO_TEST_END EOSIO_TEST_BEGIN(signed_int_constexpr_test) @@ -438,6 +447,16 @@ EOSIO_TEST_BEGIN(signed_int_constexpr_test) // friend bool operator>=(const signed_int&, const signed_int&) static_assert( signed_int{42} >= signed_int{42} ); static_assert( (signed_int{42} >= signed_int{43}) == false ); + + // ---------- + // misc tests + static_assert( signed_int{0xff}.value == 255 ); // 1 byte + static_assert( signed_int{0xffff}.value == 65535 ); // 2 bytes + static_assert( signed_int{0xffffff}.value == 16777215 ); // 3 bytes + static_assert( signed_int{0x7fffffff}.value == 2147483647 ); // 4 bytes + static_assert( signed_int{-0x7fffffff}.value == -2147483647 ); // 4 bytes + static_assert( signed_int{100u}.value == 100 ); // cast from unsigned + static_assert( sizeof(signed_int{0xf}) == 4 ); EOSIO_TEST_END int main(int argc, char* argv[]) { From 4e92d2acecc1ff401b4dcef3ed2d989ec88260ea Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Tue, 27 Sep 2022 10:13:15 -0400 Subject: [PATCH 3/4] remove extra whitespaces from previous code --- libraries/eosiolib/core/eosio/varint.hpp | 34 ++++++++++++------------ 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/libraries/eosiolib/core/eosio/varint.hpp b/libraries/eosiolib/core/eosio/varint.hpp index 3365ca12e0..c23e80dc66 100644 --- a/libraries/eosiolib/core/eosio/varint.hpp +++ b/libraries/eosiolib/core/eosio/varint.hpp @@ -76,7 +76,7 @@ namespace eosio { * @return true - if equal * @return false - otherwise */ - constexpr friend bool operator==( const unsigned_int& i, const uint32_t& v ) { return i.value == v; } + constexpr friend bool operator==( const unsigned_int& i, const uint32_t& v ) { return i.value == v; } /** * Check equality between 32-bit unsigned integer and a unsigned_int object @@ -86,7 +86,7 @@ namespace eosio { * @return true - if equal * @return false - otherwise */ - constexpr friend bool operator==( const uint32_t& i, const unsigned_int& v ) { return i == v.value; } + constexpr friend bool operator==( const uint32_t& i, const unsigned_int& v ) { return i == v.value; } /** * Check equality between two unsigned_int objects @@ -106,7 +106,7 @@ namespace eosio { * @return true - if inequal * @return false - otherwise */ - constexpr friend bool operator!=( const unsigned_int& i, const uint32_t& v ) { return i.value != v; } + constexpr friend bool operator!=( const unsigned_int& i, const uint32_t& v ) { return i.value != v; } /** * Check inequality between 32-bit unsigned integer and a unsigned_int object @@ -116,7 +116,7 @@ namespace eosio { * @return true - if unequal * @return false - otherwise */ - constexpr friend bool operator!=( const uint32_t& i, const unsigned_int& v ) { return i != v.value; } + constexpr friend bool operator!=( const uint32_t& i, const unsigned_int& v ) { return i != v.value; } /** * Check inequality between two unsigned_int objects @@ -136,7 +136,7 @@ namespace eosio { * @return true - if i less than v * @return false - otherwise */ - constexpr friend bool operator<( const unsigned_int& i, const uint32_t& v ) { return i.value < v; } + constexpr friend bool operator<( const unsigned_int& i, const uint32_t& v ) { return i.value < v; } /** * Check if the given 32-bit unsigned integer is less than the given unsigned_int object @@ -146,7 +146,7 @@ namespace eosio { * @return true - if i less than v * @return false - otherwise */ - constexpr friend bool operator<( const uint32_t& i, const unsigned_int& v ) { return i < v.value; } + constexpr friend bool operator<( const uint32_t& i, const unsigned_int& v ) { return i < v.value; } /** * Check if the first given unsigned_int is less than the second given unsigned_int object @@ -156,7 +156,7 @@ namespace eosio { * @return true - if i less than v * @return false - otherwise */ - constexpr friend bool operator<( const unsigned_int& i, const unsigned_int& v ) { return i.value < v.value; } + constexpr friend bool operator<( const unsigned_int& i, const unsigned_int& v ) { return i.value < v.value; } /** * Check if the given unsigned_int object is greater or equal to the given 32-bit unsigned integer @@ -166,7 +166,7 @@ namespace eosio { * @return true - if i is greater or equal to v * @return false - otherwise */ - constexpr friend bool operator>=( const unsigned_int& i, const uint32_t& v ) { return i.value >= v; } + constexpr friend bool operator>=( const unsigned_int& i, const uint32_t& v ) { return i.value >= v; } /** * Check if the given 32-bit unsigned integer is greater or equal to the given unsigned_int object @@ -176,7 +176,7 @@ namespace eosio { * @return true - if i is greater or equal to v * @return false - otherwise */ - constexpr friend bool operator>=( const uint32_t& i, const unsigned_int& v ) { return i >= v.value; } + constexpr friend bool operator>=( const uint32_t& i, const unsigned_int& v ) { return i >= v.value; } /** * Check if the first given unsigned_int is greater or equal to the second given unsigned_int object @@ -302,7 +302,7 @@ namespace eosio { * @return true - if equal * @return false - otherwise */ - constexpr friend bool operator==( const signed_int& i, const int32_t& v ) { return i.value == v; } + constexpr friend bool operator==( const signed_int& i, const int32_t& v ) { return i.value == v; } /** * Check equality between 32-bit integer and a signed_int object @@ -312,7 +312,7 @@ namespace eosio { * @return true - if equal * @return false - otherwise */ - constexpr friend bool operator==( const int32_t& i, const signed_int& v ) { return i == v.value; } + constexpr friend bool operator==( const int32_t& i, const signed_int& v ) { return i == v.value; } /** * Check equality between two signed_int objects @@ -343,7 +343,7 @@ namespace eosio { * @return true - if unequal * @return false - otherwise */ - constexpr friend bool operator!=( const int32_t& i, const signed_int& v ) { return i != v.value; } + constexpr friend bool operator!=( const int32_t& i, const signed_int& v ) { return i != v.value; } /** * Check inequality between two signed_int objects @@ -363,7 +363,7 @@ namespace eosio { * @return true - if i less than v * @return false - otherwise */ - constexpr friend bool operator<( const signed_int& i, const int32_t& v ) { return i.value < v; } + constexpr friend bool operator<( const signed_int& i, const int32_t& v ) { return i.value < v; } /** * Check if the given 32-bit integer is less than the given signed_int object @@ -373,7 +373,7 @@ namespace eosio { * @return true - if i less than v * @return false - otherwise */ - constexpr friend bool operator<( const int32_t& i, const signed_int& v ) { return i < v.value; } + constexpr friend bool operator<( const int32_t& i, const signed_int& v ) { return i < v.value; } /** * Check if the first given signed_int is less than the second given signed_int object @@ -383,7 +383,7 @@ namespace eosio { * @return true - if i less than v * @return false - otherwise */ - constexpr friend bool operator<( const signed_int& i, const signed_int& v ) { return i.value < v.value; } + constexpr friend bool operator<( const signed_int& i, const signed_int& v ) { return i.value < v.value; } /** @@ -394,7 +394,7 @@ namespace eosio { * @return true - if i is greater or equal to v * @return false - otherwise */ - constexpr friend bool operator>=( const signed_int& i, const int32_t& v ) { return i.value >= v; } + constexpr friend bool operator>=( const signed_int& i, const int32_t& v ) { return i.value >= v; } /** * Check if the given 32-bit integer is greater or equal to the given signed_int object @@ -404,7 +404,7 @@ namespace eosio { * @return true - if i is greater or equal to v * @return false - otherwise */ - constexpr friend bool operator>=( const int32_t& i, const signed_int& v ) { return i >= v.value; } + constexpr friend bool operator>=( const int32_t& i, const signed_int& v ) { return i >= v.value; } /** * Check if the first given signed_int is greater or equal to the second given signed_int object From 92ad5b75d1c649e10e830d18b733ee7b8088fe94 Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Tue, 27 Sep 2022 11:12:19 -0400 Subject: [PATCH 4/4] add a test for signed_int{-0x80000000l} --- tests/unit/varint_tests.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/varint_tests.cpp b/tests/unit/varint_tests.cpp index 5b9f743821..bd4e76d936 100644 --- a/tests/unit/varint_tests.cpp +++ b/tests/unit/varint_tests.cpp @@ -455,6 +455,7 @@ EOSIO_TEST_BEGIN(signed_int_constexpr_test) static_assert( signed_int{0xffffff}.value == 16777215 ); // 3 bytes static_assert( signed_int{0x7fffffff}.value == 2147483647 ); // 4 bytes static_assert( signed_int{-0x7fffffff}.value == -2147483647 ); // 4 bytes + static_assert( signed_int{-0x80000000l}.value == -2147483648 ); // 4 bytes static_assert( signed_int{100u}.value == 100 ); // cast from unsigned static_assert( sizeof(signed_int{0xf}) == 4 ); EOSIO_TEST_END