Skip to content
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

Merged
merged 4 commits into from
Sep 27, 2022

Conversation

linh2931
Copy link
Member

@linh2931 linh2931 commented Sep 27, 2022

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

  • API Changes

Documentation Additions

  • Documentation Additions

@linh2931 linh2931 requested review from larryk85 and arhag September 27, 2022 00:16
@linh2931 linh2931 changed the title enable constexpr support in varint types [3.0] add constexpr support to varint types Sep 27, 2022
Comment on lines 280 to 440
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 );
Copy link

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).

Copy link
Member Author

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; }
Copy link
Contributor

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.

Suggested change
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; }

Copy link
Member Author

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.

Copy link
Member Author

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; }
Copy link
Contributor

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.

Suggested change
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; }

@linh2931 linh2931 merged commit ef7462e into release/3.0 Sep 27, 2022
@linh2931 linh2931 deleted the constexpr_varint_3_0 branch September 27, 2022 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants