-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[3.0] add constexpr support to varint types #40
Conversation
tests/unit/varint_tests.cpp
Outdated
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 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, I'd add more 2,3 & 4 bytes int/uint tests with casts or integer literals i.e. xU
, xL
etc.. and std::size_t
, just to make sure everything passes at compile time (which of course should).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. More tests were added.
@@ -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; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason the whitespace is like this? Several more like this throughout.
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; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The whitespaces had been there before. That's why I did not touch them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided to take the whitespaces out. Thanks.
@@ -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; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason the whitespace is like this? Several more throughout like this.
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; } |
Change Description
Resolve #13
Based on #14. Thanks @smlu. To conform to our practice, fix release branch and then merge the fix to main.
Add comprehensive unit test cases.
API Changes
Documentation Additions