diff --git a/.gitignore b/.gitignore index 608b1d4..ddc707a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,12 @@ .vscode/ -build/ \ No newline at end of file +.idea + +build/ +.build_debug/* +.build_release/* +distribute/* +*.testbin +*.bin +cmake_build +.cmake_build +cmake-build-* \ No newline at end of file diff --git a/README.md b/README.md index 1008301..86f646f 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,12 @@ numbers [![CMake CI Matrix](https://github.com/guuzaa/numbers/actions/workflows/cmake.yml/badge.svg?branch=main)](https://github.com/guuzaa/numbers/actions/workflows/cmake.yml) +![Static Badge](https://img.shields.io/badge/Language-C++17-red) +![Static Badge](https://img.shields.io/badge/License-MIT-pink) +![Static Badge](https://img.shields.io/badge/OS-Linux-blue) +![Static Badge](https://img.shields.io/badge/OS-macOS-blue) + +**Note: This project is in the early stages of development. The codebase is subject to significant changes and reorganization. Expect breaking changes as we refine the architecture, fix bugs, and implement new features.** `numbers` is a library for C++17 and later versions that handles integer overflow similar to Rust. It simplifies integer computations and offers control over how to handle overflow situations. @@ -12,29 +18,29 @@ numbers - **Like Primitive Types** -- **Signed Integers**: Int8 Int16 Int32 Int64 +- **Signed Integers**: int8 int16 int32 int64 int128 -- **Unsigned Integers**: Uint8 Uint16 Uint32 Uint64 +- **Unsigned Integers**: uint8 uint16 uint32 uint64 -## How to use +## Usage ### operator + ```c++ -Int8 a = 100_i8; +numbers::int8 a = 100; std::cout << a << '\n'; try { a = a + a; std::cout << a << '\n'; -} catch (std::runtime_error err) { +} catch (std::runtime_error &err) { std::cout << "Catch error: " << err.what() << '\n'; } ``` ### checked sub ```c++ -Int8 a = Int8::MIN; +numbers::int8 a = numbers::int8::MIN; std::cout << a << '\n'; -std::optional ret = a.checked_sub(1); +std::optional ret = a.checked_sub(1); if (ret) { std::cout << ret.value() << '\n'; } else { @@ -44,8 +50,8 @@ if (ret) { ### overflowing div ```c++ -Int16 a = 40_i16; -Int16 b = 2_i16; +numbers::int16 a = 40; +numbers::int16 b = 2; auto [ret, overflowing] = a.overflowing_div(b); std::cout <<"a= " << a << ", b= " << b << '\n'; if (!overflowing) { @@ -57,9 +63,17 @@ if (!overflowing) { ### saturating mul ```c++ -Int64 a = 40_i64; -Int64 b = Int64::MAX; +numbers::int64 a = 40; +numbers::int64 b = numbers::int64::MAX; std::cout << "a= " << a << ", b= " << b << '\n'; -Int64 ret = a.saturating_mul(b); +numbers::int64 ret = a.saturating_mul(b); std::cout << ret << '\n'; -``` \ No newline at end of file +``` + +## Contribute + +We welcome contributions, but please be aware that the project's design and conventions are still evolving. If you'd like to contribute, it's a good idea to discuss your plans with the project maintainers before starting work. + +For the latest updates and discussions, please see our [issues](./issues) and [pull requests](./pulls). + +Stay tuned for more updates, and thank you for your interest in contributing to our project! diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 4f40408..b8c05b8 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -4,6 +4,8 @@ project(NumbersExample LANGUAGES CXX) add_executable(${PROJECT_NAME} EXCLUDE_FROM_ALL main.cc) +target_link_libraries(${PROJECT_NAME} PRIVATE numbers_obj) + add_custom_target(example COMMAND ${PROJECT_NAME} DEPENDS ${PROJECT_NAME} diff --git a/examples/main.cc b/examples/main.cc index 1cafe91..269b011 100644 --- a/examples/main.cc +++ b/examples/main.cc @@ -5,9 +5,9 @@ void checked_sub_example() { std::cout << "==== checked_sub_example ==== \n"; - Int8 a = Int8::MIN; + numbers::int8 a = numbers::int8::MIN; std::cout << a << '\n'; - std::optional ret = a.checked_sub(1); + std::optional ret = a.checked_sub(1); if (ret) { std::cout << ret.value() << '\n'; } else { @@ -17,8 +17,8 @@ void checked_sub_example() { void overflowing_div_example() { std::cout << "==== overflowing_div_example ==== \n"; - Int16 a = 40_i16; - Int16 b = 2_i16; + numbers::int16 a = 40; + numbers::int16 b = 2; auto [ret, overflowing] = a.overflowing_div(b); std::cout << "a= " << a << ", b= " << b << '\n'; if (!overflowing) { @@ -30,46 +30,57 @@ void overflowing_div_example() { void saturating_mul_example() { std::cout << "==== saturating_mul_example ==== \n"; - Int64 a = 40_i64; - Int64 b = Int64::MAX; + numbers::int64 a = 40; + numbers::int64 b = numbers::int64::MAX; std::cout << "a= " << a << ", b= " << b << '\n'; - Int64 ret = a.saturating_mul(b); + numbers::int64 ret = a.saturating_mul(b); std::cout << ret << '\n'; } +void int128_example() { + std::cout << "==== int128_example ==== \n"; + numbers::int128 a = 40; + numbers::int128 max = numbers::int128::MAX; + numbers::int128 min = numbers::int128::MIN; + std::cout << "a= " << a << ", max= " << max << ", min= " << min << '\n'; + numbers::int128 ret = max - a; + std::cout << "max - a = " << ret << '\n'; +} + int main(int argc, char const *argv[]) { - auto a = 100_i8; + auto a = 100; std::cout << a << '\n'; try { a = a + a; std::cout << a << '\n'; - } catch (std::runtime_error err) { + } catch (std::runtime_error &err) { std::cout << "Catch error: " << err.what() << '\n'; } - auto b = 127_i8; - Int8 c = 0; + numbers::int8 b = 127; + numbers::int8 c = 0; try { - Int8 ret = c - b; + numbers::int8 ret = c - b; std::cout << ret << '\n'; c = -10; ret = c - b; std::cout << ret << '\n'; - } catch (std::runtime_error err) { + } catch (std::runtime_error &err) { std::cout << "Catch error: " << err.what() << '\n'; } - auto d = static_cast(b); + auto d = static_cast(b); try { d = d + d; std::cout << d << '\n'; - } catch (std::runtime_error err) { + } catch (std::runtime_error &err) { std::cout << "Catch error: " << err.what() << '\n'; } checked_sub_example(); overflowing_div_example(); saturating_mul_example(); + int128_example(); return 0; } diff --git a/src/include/int128.hh b/src/include/int128.hh index af2a918..6c59541 100644 --- a/src/include/int128.hh +++ b/src/include/int128.hh @@ -18,9 +18,10 @@ #ifndef NUMBERS_INT128_HH #define NUMBERS_INT128_HH +#include #include #include -#include +#include namespace numbers { class int128; @@ -240,15 +241,15 @@ class int128 { int128 &operator<<=(int amount); int128 &operator>>=(int amount); - // Int128Low64() + // int128_low64() // // Returns the lower 64-bit value of a `int128` value. - friend constexpr uint64_t Int128Low64(int128 v); + friend constexpr uint64_t int128_low64(int128 v); - // Int128High64() + // int128_high64() // // Returns the higher 64-bit value of a `int128` value. - friend constexpr int64_t Int128High64(int128 v); + friend constexpr int64_t int128_high64(int128 v); // make_int128() // @@ -269,35 +270,43 @@ class int128 { // Int128Max() // // Returns the maximum value for a 128-bit signed integer. - friend constexpr int128 Int128Max(); + friend constexpr int128 int128_max(); // Int128Min() // // Returns the minimum value for a 128-bit signed integer. - friend constexpr int128 Int128Min(); + friend constexpr int128 int128_min(); + static int128 MAX; + static int128 MIN; private: constexpr int128(int64_t high, uint64_t low); - // TODO Unimplemented! - std::string ToString() const; + std::string to_string() const; // little endian uint64_t lo_; int64_t hi_; }; -// std::ostream &operator<<(std::ostream &os, int128 v); + std::ostream &operator<<(std::ostream &os, int128 v); -constexpr int128 Int128Max() { +constexpr int128 int128_max() { return int128((std::numeric_limits::max)(), (std::numeric_limits::max)()); } -constexpr int128 Int128Min() { return int128((std::numeric_limits::min)(), 0); } +constexpr int128 int128_min() { return int128((std::numeric_limits::min)(), 0); } + +inline int128 int128::MAX = int128_max(); + +inline int128 int128::MIN = int128_min(); } // namespace numbers namespace std { +template <> +struct is_signed : std::true_type {}; + template <> class numeric_limits { public: @@ -325,9 +334,9 @@ class numeric_limits { static constexpr bool traps = numeric_limits::traps; static constexpr bool tinyness_before = false; - static constexpr numbers::int128(min)() { return numbers::Int128Min(); } - static constexpr numbers::int128 lowest() { return numbers::Int128Min(); } - static constexpr numbers::int128(max)() { return numbers::Int128Max(); } + static constexpr numbers::int128(min)() { return numbers::int128_min(); } + static constexpr numbers::int128 lowest() { return numbers::int128_min(); } + static constexpr numbers::int128(max)() { return numbers::int128_max(); } static constexpr numbers::int128 epsilon() { return 0; } static constexpr numbers::int128 round_error() { return 0; } static constexpr numbers::int128 infinity() { return 0; } @@ -424,7 +433,7 @@ constexpr uint128::uint128(unsigned long v) : lo_{v}, hi_{0} {} constexpr uint128::uint128(unsigned long long v) : lo_{v}, hi_{0} {} -constexpr uint128::uint128(int128 v) : lo_{Int128Low64(v)}, hi_{static_cast(Int128High64(v))} {} +constexpr uint128::uint128(int128 v) : lo_{int128_low64(v)}, hi_{static_cast(int128_high64(v))} {} // Conversion operators to integer types. @@ -581,8 +590,6 @@ inline uint128 operator*(uint128 lhs, uint128 rhs) { // TODO implement division and modulo - - // Increment/decrement operators inline uint128 uint128::operator++(int) { uint128 tmp(*this); @@ -694,9 +701,9 @@ constexpr int64_t BitCastToSigned(uint64_t v) { } } // namespace int128_internal -constexpr uint64_t Int128Low64(int128 v) { return v.lo_; } +constexpr uint64_t int128_low64(int128 v) { return v.lo_; } -constexpr int64_t Int128High64(int128 v) { return v.hi_; } +constexpr int64_t int128_high64(int128 v) { return v.hi_; } constexpr int128::int128(int64_t high, uint64_t low) : lo_{low}, hi_{high} {} @@ -744,62 +751,64 @@ constexpr int128::operator unsigned long long() const { return static_cast(int128 lhs, int128 rhs) { - return (Int128High64(lhs) == Int128High64(rhs)) ? (Int128Low64(lhs) > Int128Low64(rhs)) - : (Int128High64(lhs) > Int128High64(rhs)); + return (int128_high64(lhs) == int128_high64(rhs)) ? (int128_low64(lhs) > int128_low64(rhs)) + : (int128_high64(lhs) > int128_high64(rhs)); } -constexpr bool operator<=(int128 lhs, int128 rhs) { return !(rhs > lhs); } +constexpr bool operator<=(int128 lhs, int128 rhs) { return !(lhs > rhs); } -constexpr bool operator>=(int128 lhs, int128 rhs) { return !(rhs < lhs); } +constexpr bool operator>=(int128 lhs, int128 rhs) { return !(lhs < rhs); } // Unary operators constexpr int128 operator-(int128 v) { - return make_int128(~Int128High64(v) + (Int128Low64(v) == 0), ~Int128Low64(v) + 1); + return make_int128(~int128_high64(v) + (int128_low64(v) == 0), ~int128_low64(v) + 1); } -constexpr int128 operator!(int128 v) { return !Int128High64(v) && !Int128Low64(v); } +constexpr int128 operator!(int128 v) { return !int128_high64(v) && !int128_low64(v); } -constexpr int128 operator~(int128 v) { return make_int128(~Int128High64(v), ~Int128Low64(v)); } +constexpr int128 operator~(int128 v) { return make_int128(~int128_high64(v), ~int128_low64(v)); } // Arithmetic operators namespace int128_internal { constexpr int128 SignedAddResult(int128 result, int128 lhs) { // check for carry - return (Int128Low64(result) < Int128Low64(lhs)) ? make_int128(Int128High64(result) + 1, Int128Low64(result)) : result; + return (int128_low64(result) < int128_low64(lhs)) ? make_int128(int128_high64(result) + 1, int128_low64(result)) + : result; } } // namespace int128_internal constexpr int128 operator+(int128 lhs, int128 rhs) { return int128_internal::SignedAddResult( - make_int128(Int128High64(lhs) + Int128High64(rhs), Int128Low64(lhs) + Int128Low64(rhs)), lhs); + make_int128(int128_high64(lhs) + int128_high64(rhs), int128_low64(lhs) + int128_low64(rhs)), lhs); } namespace int128_internal { constexpr int128 SignedSubstructResult(int128 result, int128 lhs, int128 rhs) { // check for carry - return (Int128Low64(lhs) < Int128Low64(rhs)) ? make_int128(Int128High64(result) - 1, Int128Low64(result)) : result; + return (int128_low64(lhs) < int128_low64(rhs)) ? make_int128(int128_high64(result) - 1, int128_low64(result)) + : result; } } // namespace int128_internal constexpr int128 operator-(int128 lhs, int128 rhs) { return int128_internal::SignedSubstructResult( - make_int128(Int128High64(lhs) - Int128High64(rhs), Int128Low64(lhs) - Int128Low64(rhs)), lhs, rhs); + make_int128(int128_high64(lhs) - int128_high64(rhs), int128_low64(lhs) - int128_low64(rhs)), lhs, rhs); } inline int128 operator*(int128 lhs, int128 rhs) { return make_int128(int128_internal::BitCastToSigned(Uint128High64(uint128(lhs) * uint128(rhs))), - Uint128Low64(uint128(lhs) * uint128(rhs))); + Uint128Low64(uint128(lhs) * uint128(rhs))); } inline int128 int128::operator++(int) { @@ -825,15 +834,15 @@ inline int128 &int128::operator--() { } constexpr int128 operator|(int128 lhs, int128 rhs) { - return make_int128(Int128High64(lhs) | Int128High64(rhs), Int128Low64(lhs) | Int128Low64(rhs)); + return make_int128(int128_high64(lhs) | int128_high64(rhs), int128_low64(lhs) | int128_low64(rhs)); } constexpr int128 operator&(int128 lhs, int128 rhs) { - return make_int128(Int128High64(lhs) & Int128High64(rhs), Int128Low64(lhs) & Int128Low64(rhs)); + return make_int128(int128_high64(lhs) & int128_high64(rhs), int128_low64(lhs) & int128_low64(rhs)); } constexpr int128 operator^(int128 lhs, int128 rhs) { - return make_int128(Int128High64(lhs) ^ Int128High64(rhs), Int128Low64(lhs) ^ Int128Low64(rhs)); + return make_int128(int128_high64(lhs) ^ int128_high64(rhs), int128_low64(lhs) ^ int128_low64(rhs)); } constexpr int128 operator<<(int128 lhs, int amount) { @@ -843,17 +852,17 @@ constexpr int128 operator<<(int128 lhs, int amount) { if (amount <= 0) { return lhs; } else if (amount < 63) { - return make_int128((Int128High64(lhs) << amount) | static_cast(Int128Low64(lhs) >> (64 - amount)), - Int128Low64(lhs) << amount); + return make_int128((int128_high64(lhs) << amount) | static_cast(int128_low64(lhs) >> (64 - amount)), + int128_low64(lhs) << amount); } else if (amount == 63) { - return make_int128(((Int128High64(lhs) << 32) << 31) | static_cast(Int128Low64(lhs) >> 1), - (Int128Low64(lhs) << 32) << 31); + return make_int128(((int128_high64(lhs) << 32) << 31) | static_cast(int128_low64(lhs) >> 1), + (int128_low64(lhs) << 32) << 31); } else if (amount == 127) { - return make_int128(static_cast(Int128Low64(lhs) << 63), 0); + return make_int128(static_cast(int128_low64(lhs) << 63), 0); } else if (amount > 127) { return make_int128(0, 0); } else { // amount >= 64 && amount < 127 - return make_int128(static_cast(Int128Low64(lhs) << (amount - 64)), 0); + return make_int128(static_cast(int128_low64(lhs) << (amount - 64)), 0); } } @@ -864,15 +873,15 @@ constexpr int128 operator>>(int128 lhs, int amount) { if (amount <= 0) { return lhs; } else if (amount < 63) { - return make_int128(Int128High64(lhs) >> amount, Int128Low64(lhs) >> amount | static_cast(Int128High64(lhs)) - << (64 - amount)); + return make_int128(int128_high64(lhs) >> amount, + int128_low64(lhs) >> amount | static_cast(int128_high64(lhs)) << (64 - amount)); } else if (amount == 63) { - return make_int128((Int128High64(lhs) >> 32) >> 31, - static_cast(Int128High64(lhs) << 1) | (Int128Low64(lhs) >> 32) >> 31); + return make_int128((int128_high64(lhs) >> 32) >> 31, + static_cast(int128_high64(lhs) << 1) | (int128_low64(lhs) >> 32) >> 31); } else if (amount >= 127) { - return make_int128((Int128High64(lhs) >> 32) >> 31, static_cast((Int128High64(lhs) >> 32) >> 31)); + return make_int128((int128_high64(lhs) >> 32) >> 31, static_cast((int128_high64(lhs) >> 32) >> 31)); } else { // amount >= 64 && amount < 127 - return make_int128((Int128High64(lhs) >> 32) >> 31, static_cast(Int128High64(lhs) >> (amount - 64))); + return make_int128((int128_high64(lhs) >> 32) >> 31, static_cast(int128_high64(lhs) >> (amount - 64))); } } diff --git a/src/include/integer.hh b/src/include/integer.hh index 291bb4b..557a4a1 100644 --- a/src/include/integer.hh +++ b/src/include/integer.hh @@ -6,6 +6,10 @@ #include #include +namespace numbers { + +namespace { + template >> class Integer { private: @@ -264,18 +268,13 @@ class Integer { T num_; }; -using Int8 = Integer; -using Int16 = Integer; -using Int32 = Integer; -using Int64 = Integer; - -constexpr Int8 operator"" _i8(unsigned long long val) { return Int8(val); } - -constexpr Int16 operator"" _i16(unsigned long long val) { return Int16(val); } - -constexpr Int32 operator"" _i32(unsigned long long val) { return Int32(val); } +} // namespace -constexpr Int64 operator"" _i64(unsigned long long val) { return Int64(val); } +using int8 = Integer; +using int16 = Integer; +using int32 = Integer; +using int64 = Integer; +} // namespace numbers #endif \ No newline at end of file diff --git a/src/include/internal/bits.hh b/src/include/internal/bits.hh index 26280c4..6c5f9bf 100644 --- a/src/include/internal/bits.hh +++ b/src/include/internal/bits.hh @@ -2,6 +2,7 @@ #define NUMBERS_INTERNAL_BITS_HH #include +#include namespace numbers_internal { diff --git a/src/include/uinteger.hh b/src/include/uinteger.hh index e5d7032..43fdf08 100644 --- a/src/include/uinteger.hh +++ b/src/include/uinteger.hh @@ -6,6 +6,10 @@ #include #include +namespace numbers { + +namespace { + template >> class Uinteger { private: @@ -210,17 +214,13 @@ class Uinteger { T num_; }; -using Uint8 = Uinteger; -using Uint16 = Uinteger; -using Uint32 = Uinteger; -using Uint64 = Uinteger; - -constexpr Uint8 operator"" _u8(unsigned long long val) { return Uint8(val); } - -constexpr Uint16 operator"" _u16(unsigned long long val) { return Uint16(val); } +} // namespace -constexpr Uint32 operator"" _u32(unsigned long long val) { return Uint32(val); } +using uint8 = Uinteger; +using uint16 = Uinteger; +using uint32 = Uinteger; +using uint64 = Uinteger; -constexpr Uint64 operator"" _u64(unsigned long long val) { return Uint64(val); } +} // namespace numbers #endif \ No newline at end of file diff --git a/src/numbers/int128.cc b/src/numbers/int128.cc index 1ca76e5..38ecaf2 100644 --- a/src/numbers/int128.cc +++ b/src/numbers/int128.cc @@ -14,9 +14,12 @@ // Modified from abseil-app guuzaa #include +#include +#include +#include -#include "int128.hh" #include "bits.hh" +#include "int128.hh" namespace numbers { @@ -68,6 +71,48 @@ inline void DivModImpl(uint128 dividend, uint128 divisor, uint128 *quotient_ret, } } // namespace +namespace { +std::string uint128_to_formatted_string(uint128 v, std::ios_base::fmtflags flags) { + uint128 div; + int div_base_log; + switch (flags & std::ios::basefield) { + case std::ios::hex: + div = 0x1000000000000000; // 16^15 + div_base_log = 15; + break; + case std::ios::oct: + div = 01000000000000000000000; + div_base_log = 21; + break; + default: // std::ios::dec + div = 10000000000000000000u; + div_base_log = 19; + break; + } + + std::ostringstream os; + std::ios_base::fmtflags copy_mask = std::ios::basefield | std::ios::showbase | std::ios::uppercase; + os.setf(flags & copy_mask, copy_mask); + uint128 high = v; + uint128 low; + DivModImpl(high, div, &high, &low); + uint128 mid; + DivModImpl(high, div, &high, &mid); + if (Uint128Low64(high) != 0) { + os << Uint128Low64(high); + os << std::noshowbase << std::setfill('0') << std::setw(div_base_log); + os << Uint128Low64(mid); + os << std::setw(div_base_log); + } else if (Uint128Low64(mid) != 0) { + os << Uint128Low64(mid); + os << std::noshowbase << std::setfill('0') << std::setw(div_base_log); + } + os << Uint128Low64(low); + return os.str(); +} + +} // namespace + } // namespace numbers namespace numbers { @@ -80,19 +125,77 @@ uint128 operator/(uint128 lhs, uint128 rhs) { } namespace { -uint128 UnsignedAbsoluteValue(int128 v) { return Int128High64(v) < 0 ? -uint128(v) : uint128(v); } +uint128 UnsignedAbsoluteValue(int128 v) { return int128_high64(v) < 0 ? -uint128(v) : uint128(v); } } // namespace int128 operator/(int128 lhs, int128 rhs) { - assert(lhs != Int128Min() || rhs != -1); // overflowing + assert(lhs != int128::MIN || rhs != -1); // overflowing uint128 quotient = 0; uint128 remainder = 0; DivModImpl(UnsignedAbsoluteValue(lhs), UnsignedAbsoluteValue(rhs), "ient, &remainder); - if ((Int128High64(lhs) < 0) != (Int128High64(rhs) < 0)) { + if ((int128_high64(lhs) < 0) != (int128_high64(rhs) < 0)) { quotient = -quotient; } return make_int128(int128_internal::BitCastToSigned(Uint128High64(quotient)), Uint128Low64(quotient)); } + +int128 operator%(int128 lhs, int128 rhs) { + assert(lhs != int128_min() || rhs != -1); // overflowing + + uint128 quotient = 0; + uint128 remainder = 0; + DivModImpl(UnsignedAbsoluteValue(lhs), UnsignedAbsoluteValue(rhs), "ient, &remainder); + if (int128_high64(lhs) < 0) { + remainder = -remainder; + } + return make_int128(int128_internal::BitCastToSigned(Uint128High64(remainder)), Uint128Low64(remainder)); +} + +std::ostream &operator<<(std::ostream &os, int128 v) { + std::ios_base::fmtflags flags = os.flags(); + std::string rep; + // Add the sign if needed + bool print_as_decimal = + (flags & std::ios::basefield) == std::ios::dec || (flags & std::ios::basefield) == std::ios_base::fmtflags(); + if (print_as_decimal) { + if (int128_high64(v) < 0) { + rep = "-"; + } else if (flags & std::ios::showpos) { + rep = "+"; + } + } + + rep.append(uint128_to_formatted_string((print_as_decimal ? UnsignedAbsoluteValue(v) : uint128(v)), os.flags())); + + // Add the requisite padding + std::streamsize width = os.width(0); + if (static_cast(width) > rep.size()) { + const size_t count = static_cast(width) - rep.size(); + switch (flags & std::ios::adjustfield) { + case std::ios::left: + rep.append(count, os.fill()); + break; + case std::ios::internal: + if (print_as_decimal && (rep[0] == '+' || rep[0] == '-')) { + rep.insert(size_t{1}, count, os.fill()); + } else if ((flags & std::ios::basefield) == std::ios::hex && (flags & std::ios::showbase) && v != 0) { + rep.insert(size_t{2}, count, os.fill()); + } else { + rep.insert(size_t{0}, count, os.fill()); + } + break; + default: // std::ios::right + rep.insert(0, count, os.fill()); + break; + } + } + return os << rep; +} + +std::string int128::to_string() const { + return uint128_to_formatted_string(*this, std::ios_base::dec); +} + } // namespace numbers \ No newline at end of file diff --git a/test/integer/int128_test.cc b/test/integer/int128_test.cc index 957b5d4..5aa0cd0 100644 --- a/test/integer/int128_test.cc +++ b/test/integer/int128_test.cc @@ -1,140 +1,474 @@ #include "gtest/gtest.h" #include -#include #include "int128.hh" using namespace numbers; -TEST(Int128Test, int128Assignemnt) { - int a = 10; - int128 a128 = a; - ASSERT_EQ(a, a128); - unsigned int ua = 10; - int128 ua128 = ua; - ASSERT_EQ(ua, ua128); - - long b = 1000; - int128 b128 = b; - ASSERT_EQ(b, b128); - unsigned long ub = 1000; - int128 ub128 = ub; - ASSERT_EQ(ub, ub128); - - long long c = 1234; - int128 c128 = c; - ASSERT_EQ(c, c128); - unsigned long long uc = 1234; - int128 uc128 = uc; - ASSERT_EQ(uc, uc128); -} - -TEST(Int128Test, int128Add) { - int na = 100; - int nb = 200; - int nc = na + nb; - - int128 a = na; - int128 b = nb; - int128 c = nc; - - ASSERT_EQ(a + b, c); - ASSERT_EQ(a + b, nc); - ASSERT_EQ(a + nb, nc); - ASSERT_EQ(na + b, nc); - ASSERT_EQ(a++, na++); - ASSERT_EQ(++a, ++na); - - b += 1; - nb += 1; - ASSERT_EQ(b, nb); - - int128 max64 = std::numeric_limits::max(); - uint64_t expected = static_cast(max64) + 1; - ASSERT_EQ(max64 + 1, expected); - - int128 umax64 = std::numeric_limits::max(); - int128 ret = make_int128(1, 0); - ASSERT_EQ(umax64 + 1, ret); -} - -TEST(Int128Test, int128Add1) { - int64_t sum = 0; - int128 n = sum; - for (int64_t i = 0; i < 100000; i++) { - sum += i; - n += i; - } - ASSERT_EQ(n, sum); -} - -TEST(Int128Test, int128Add2) { - { - constexpr size_t len = 100'000; - std::vector nums(len); - std::iota(nums.begin(), nums.end(), 0); - for (size_t i = 0; i < len; i++) { - ASSERT_EQ(nums[i], i); +TEST(Int128Test, TrivalTraits) { + static_assert(std::is_trivially_default_constructible::value, + "int128 should be trivially default constructible"); + static_assert(std::is_trivially_copy_constructible::value, "int128 should be trivially copy constructible"); + static_assert(std::is_trivially_copy_assignable::value, "int128 should be trivially copy assignable"); + static_assert(std::is_trivially_destructible::value, "int128 should be trivially destructible"); +} + +typedef ::testing::Types + IntegerTypes; + +template +class Int128TraitsTest : public ::testing::Test {}; + +TYPED_TEST_SUITE(Int128TraitsTest, IntegerTypes); + +TYPED_TEST(Int128TraitsTest, ConstructAssignTest) { + static_assert(std::is_constructible::value, + "numbers::int128 must be constructible from TypeParam"); + static_assert(std::is_assignable::value, "numbers::int128 must be assignable from TypeParam"); + static_assert(!std::is_assignable::value, + "TypeParam must not be assignable from numbers::int128"); +} + +TEST(Int128Test, BoolConversion) { + EXPECT_FALSE(int128(0)); + for (int i = 0; i < 64; ++i) { + EXPECT_TRUE(make_int128(0, uint64_t(1) << i)); + } + + for (int i = 0; i < 63; ++i) { + EXPECT_TRUE(make_int128(int64_t(1) << i, 0)); + } + + EXPECT_TRUE(int128::MIN); + EXPECT_TRUE(int128::MAX); + EXPECT_EQ(int128(1), int128(true)); + EXPECT_EQ(int128(0), int128(false)); +} + +template +class Int128ConversionTest : public ::testing::Test {}; + +TYPED_TEST_SUITE(Int128ConversionTest, IntegerTypes); + +TYPED_TEST(Int128ConversionTest, Int128Conversion) { + EXPECT_EQ(TypeParam{0}, static_cast(int128(0))); + EXPECT_EQ(std::numeric_limits::min(), + static_cast(int128(std::numeric_limits::min()))); + EXPECT_EQ(std::numeric_limits::max(), + static_cast(int128(std::numeric_limits::max()))); +} + +TEST(Int128Test, MakeInt128) { + EXPECT_EQ(int128(-1), make_int128(-1, -1)); + EXPECT_EQ(int128(-31), make_int128(-1, -31)); + EXPECT_EQ(int128(std::numeric_limits::min()), make_int128(-1, std::numeric_limits::min())); + EXPECT_EQ(int128(0), make_int128(0, 0)); + EXPECT_EQ(int128(1), make_int128(0, 1)); + EXPECT_EQ(int128(345), make_int128(0, 345)); + EXPECT_EQ(int128(std::numeric_limits::max()), make_int128(0, std::numeric_limits::max())); +} + +TEST(Int128Test, int128HighLowTest) { + struct HighLowPair { + int64_t high; + uint64_t low; + }; + + HighLowPair values[]{{0, 0}, {0, 1}, {1, 0}, {1, 1}, {1234, 5678}, {-8765, 4321}}; + for (const auto &[high, low] : values) { + int128 val = make_int128(high, low); + EXPECT_EQ(low, int128_low64(val)); + EXPECT_EQ(high, int128_high64(val)); + } +} + +TEST(Int128Test, int128LimitsTest) { + EXPECT_EQ(int128::MAX, make_int128(0x7fffffffffffffff, 0xffffffffffffffff)); + EXPECT_EQ(int128::MAX, ~int128::MIN); +} + +TEST(Int128Test, int128Constexpr) { + constexpr int128 zero = int128(); + constexpr int128 one = 1; + constexpr int128 minus_two = -2; + constexpr int128 min = int128_min(); + constexpr int128 max = int128_max(); + EXPECT_EQ(zero, int128(0)); + EXPECT_EQ(one, int128(1)); + EXPECT_EQ(minus_two, make_int128(-1, -2)); + EXPECT_GT(max, one); + EXPECT_LT(min, minus_two); +} + +TEST(Int128Test, int128Comparsion) { + struct TestCase { + int128 smaller; + int128 larger; + }; + + TestCase cases[] = { + {int128(0), int128(123)}, + {make_int128(-12, 34), make_int128(12, 34)}, + {make_int128(1, 1000), make_int128(1000, 1)}, + {make_int128(1000, 1000), make_int128(1010, 1000)}, + {make_int128(-1000, 1000), make_int128(-1, 1)}, + }; + + for (const auto &[smaller, larger] : cases) { + EXPECT_TRUE(smaller < larger); + EXPECT_LT(smaller, larger); + EXPECT_TRUE(smaller <= larger); + EXPECT_LE(smaller, larger); + EXPECT_TRUE(larger > smaller); + EXPECT_GT(larger, smaller); + EXPECT_TRUE(larger >= smaller); + EXPECT_GE(larger, smaller); + EXPECT_EQ(smaller, smaller); + EXPECT_TRUE(smaller == smaller); + EXPECT_EQ(larger, larger); + EXPECT_TRUE(larger == larger); + EXPECT_NE(smaller, larger); + EXPECT_FALSE(smaller == larger); + } +} + +TEST(Int128Test, int128UnaryPlus) { + int64_t values64[] = {0, 1, 1000, 1234, std::numeric_limits::max()}; + for (auto val : values64) { + EXPECT_EQ(int128(val), +int128(val)); + EXPECT_EQ(int128(-val), +int128(-val)); + EXPECT_EQ(make_int128(val, 0), +make_int128(val, 0)); + EXPECT_EQ(make_int128(-val, 0), +make_int128(-val, 0)); + } +} + +TEST(Int128Test, int128UnaryNegation) { + int64_t values64[] = {0, 1, 1000, 1234, std::numeric_limits::max()}; + for (auto val : values64) { + EXPECT_EQ(int128(-val), -int128(val)); + EXPECT_EQ(int128(val), -int128(-val)); + EXPECT_EQ(make_int128(-val, 0), -make_int128(val, 0)); + EXPECT_EQ(make_int128(val, 0), -make_int128(-val, 0)); + } +} + +TEST(Int128Test, int128LogicNot) { + EXPECT_TRUE(!int128(0)); + for (int i = 0; i < 64; ++i) { + EXPECT_FALSE(!make_int128(0, uint64_t(1) << i)); + } + for (int i = 0; i < 63; ++i) { + EXPECT_FALSE(!make_int128(int64_t(1) << i, 0)); + } +} + +TEST(Int128Test, int128AdditionSubtraction) { + // pairs that will not cause overflow / underflow. + std::pair cases[]{ + {0, 0}, // 0, 0 + {0, 2945781290834}, // 0, + + {190835761924, 0}, // +, 0 + {0, -1204895918245}, // 0, - + {-2957928523560, 0}, // -, 0 + {8945781290834, 2945781290834}, // +, + + {-45781290834, -1204895918245}, // - ,- + {2957928523560, -2945781290834}, // +, - + {-88235237438467, 1592369524573}, // -, + + }; + + for (const auto &[first, second] : cases) { + EXPECT_EQ(int128(first + second), int128(first) + int128(second)); + EXPECT_EQ(int128(second + first), int128(second) += int128(first)); + + EXPECT_EQ(int128(first - second), int128(first) - int128(second)); + EXPECT_EQ(int128(second - first), int128(second) -= int128(first)); + + EXPECT_EQ(make_int128(second + first, 0), make_int128(second, 0) + make_int128(first, 0)); + EXPECT_EQ(make_int128(first + second, 0), make_int128(first, 0) += make_int128(second, 0)); + + EXPECT_EQ(make_int128(second - first, 0), make_int128(second, 0) - make_int128(first, 0)); + EXPECT_EQ(make_int128(first - second, 0), make_int128(first, 0) -= make_int128(second, 0)); + } + + // check postive carry + EXPECT_EQ(make_int128(31, 0), make_int128(20, 1) + make_int128(10, std::numeric_limits::max())); +} + +TEST(Int128Test, IncrementDecrement) { + int128 val = 0; + EXPECT_EQ(0, val++); + EXPECT_EQ(1, val); + EXPECT_EQ(1, val--); + EXPECT_EQ(0, val); + EXPECT_EQ(-1, --val); + EXPECT_EQ(-1, val); + EXPECT_EQ(0, ++val); + EXPECT_EQ(0, val); +} + +TEST(Int128Test, Multiplication) { + for (int i = 0; i < 64; ++i) { + for (int j = 0; j < 127 - i; ++j) { + int128 a = int128(1) << i; + int128 b = int128(1) << j; + int128 c = int128(1) << (i + j); + + EXPECT_EQ(c, a * b); + EXPECT_EQ(-c, -a * b); + EXPECT_EQ(-c, a * -b); + EXPECT_EQ(c, -a * -b); + + EXPECT_EQ(c, int128(a) *= b); + EXPECT_EQ(-c, int128(-a) *= b); + EXPECT_EQ(-c, int128(a) *= -b); + EXPECT_EQ(c, int128(-a) *= -b); } } - { - constexpr size_t len = 100; - std::vector nums(len); - std::iota(nums.begin(), nums.end(), 1); - int128 sum = std::accumulate(nums.begin(), nums.end(), int128(0)); - ASSERT_EQ(sum, 5050); - } -} - -TEST(Int128Test, int128Sub) { - long na = 100; - long nb = 2345; - long nc = na - nb; - - int128 a = na; - int128 b = nb; - int128 c = nc; - - ASSERT_EQ(a - b, c); - ASSERT_EQ(a - b, nc); - ASSERT_EQ(na - b, nc); - ASSERT_EQ(a - nb, nc); - ASSERT_EQ(na - nb, nc); - ASSERT_EQ(a--, na--); - ASSERT_EQ(--a, --na); - - b -= 201; - nb -= 201; - ASSERT_EQ(b, nb); - - int128 max64 = std::numeric_limits::max(); - uint64_t num = static_cast(max64) + 1; - ASSERT_EQ(max64, num - 1); - int128 two_power_64 = make_int128(1, 0); - ASSERT_EQ(two_power_64 - 1, int128(std::numeric_limits::max())); -} - -TEST(Int128Test, int128Mul) { - unsigned long ua = 1234; - unsigned long ub = 134083; - unsigned long ret = ua * ub; - - int128 a = ua; - int128 b = ub; - ASSERT_EQ(a * b, ret); - ASSERT_EQ(ua * b, ret); - ASSERT_EQ(a * ub, ret); -} - -TEST(Int128Test, int128Div) { - unsigned long ua = 123458; - unsigned long ub = 10; - unsigned long ret = ua / ub; - - int128 a = ua; - int128 b = ub; - ASSERT_EQ(a / b, ret); - ASSERT_EQ(ua / b, ret); - ASSERT_EQ(a / ub, ret); -} \ No newline at end of file + + // pairs will not overflow signed 64-bit multiplication + std::pair small_values[] = { + {0x5e61, 0xf29f79ca14b4}, // +, + + {0x3e033b, -0x612c0ee549}, // +, - + {-0x052c37e8, 0x7c728f0f}, // -, + + {-0x7c728f0f, -0xfb17e8}, // -, - + }; + + for (const auto &[first, second] : small_values) { + EXPECT_EQ(int128(first * second), int128(first) * int128(second)); + EXPECT_EQ(int128(first * second), int128(first) *= int128(second)); + + EXPECT_EQ(make_int128(first * second, 0), make_int128(first, 0) * int128(second)); + EXPECT_EQ(make_int128(first * second, 0), make_int128(first, 0) *= int128(second)); + } + + std::pair small_values2[] = { + {0x1bb0a110, 0x31487671}, + {0x4792784e, 0x28add7d7}, + {0x7b66553a, 0x11dff8ef}, + }; + for (const auto &[first, second] : small_values2) { + int128 a = int128(first << 32); + int128 b = int128(second << 32); + int128 c = make_int128(first * second, 0); + + EXPECT_EQ(c, a * b); + EXPECT_EQ(-c, -a * b); + EXPECT_EQ(-c, a * -b); + EXPECT_EQ(c, -a * -b); + + EXPECT_EQ(c, int128(a) *= b); + EXPECT_EQ(-c, int128(-a) *= b); + EXPECT_EQ(-c, int128(a) *= -b); + EXPECT_EQ(c, int128(-a) *= -b); + } + + int128 large_values[] = { + {make_int128(0xd66f061af02d0408, 0x727d2846cb475b53)}, + {make_int128(0x27b8d5ed6104452d, 0x03f8a33b0ee1df4f)}, + {-make_int128(0x621b6626b9e8d042, 0x27311ac99df00938)}, + {-make_int128(0x34e0656f1e95fb60, 0x4281cfd731257a47)}, + }; + for (auto val : large_values) { + EXPECT_EQ(0, 0 * val); + EXPECT_EQ(0, val * 0); + EXPECT_EQ(0, int128(0) *= val); + EXPECT_EQ(0, val *= 0); + + EXPECT_EQ(val, 1 * val); + EXPECT_EQ(val, val * 1); + EXPECT_EQ(val, int128(1) *= val); + EXPECT_EQ(val, val *= 1); + + EXPECT_EQ(-val, -1 * val); + EXPECT_EQ(-val, val * -1); + EXPECT_EQ(-val, int128(-1) * val); + EXPECT_EQ(-val, val *= -1); + } + + // Manually calculated random large value cases + EXPECT_EQ(make_int128(0xcd0efd3442219bb, 0xde47c05bcd9df6e1), + make_int128(0x7c6448, 0x3bc4285c47a9d253) * 0x1a6037537b); + EXPECT_EQ(make_int128(0x19c8b7620b507dc4, 0xfec042b71a5f29a4), + -0x3e39341147 * -make_int128(0x6a14b2, 0x5ed34cca42327b3c)); + + EXPECT_EQ(make_int128(0xcd0efd3442219bb, 0xde47c05bcd9df6e1), + make_int128(0x7c6448, 0x3bc4285c47a9d253) *= int128(0x1a6037537b)); + EXPECT_EQ(make_int128(0x19c8b7620b507dc4, 0xfec042b71a5f29a4), + int128(-0x3e39341147) *= -make_int128(0x6a14b2, 0x5ed34cca42327b3c)); +} + +TEST(Int128Test, DivisionAndModulo) { + std::pair small_pairs[] = { + {0x15f2a64138, 0x67da05}, {0x5e56d194af43045f, 0xcf1543fb99}, + {0x15e61ed052036a, -0xc8e6}, {0x88125a341e85, -0xd23fb77683}, + {-0xc06e20, 0x5a}, {-0x4f100219aea3e85d, 0xdcc56cb4efe993}, + {-0x168d629105, -0xa7}, {-0x7b44e92f03ab2375, -0x6516}, + }; + for (const auto &[first, second] : small_pairs) { + int128 dividend = first; + int128 divisor = second; + int64_t quotient = first / second; + int64_t remainder = first % second; + + EXPECT_EQ(quotient, dividend / divisor); + EXPECT_EQ(quotient, int128(dividend) /= divisor); + EXPECT_EQ(remainder, dividend % divisor); + EXPECT_EQ(remainder, int128(dividend) %= divisor); + } + + // randomly generated large values for 0, 1 and -1 + int128 values[] = { + make_int128(0x63d26ee688a962b2, 0x9e1411abda5c1d70), + make_int128(0x152f385159d6f986, 0xbf8d48ef63da395d), + -make_int128(0x3098d7567030038c, 0x14e7a8a098dc2164), + -make_int128(0x49a037aca35c809f, 0xa6a87525480ef330), + }; + for (auto val : values) { + SCOPED_TRACE(::testing::Message() << "val: " << val); + + EXPECT_EQ(0, 0 / val); + EXPECT_EQ(0, int128(0) /= val); + EXPECT_EQ(0, 0 % val); + EXPECT_EQ(0, int128(0) %= val); + + EXPECT_EQ(val, val / 1); + EXPECT_EQ(val, int128(val) /= 1); + EXPECT_EQ(0, val % 1); + EXPECT_EQ(0, int128(val) %= 1); + + EXPECT_EQ(-val, val / -1); + EXPECT_EQ(-val, int128(val) /= -1); + EXPECT_EQ(0, val % -1); + EXPECT_EQ(0, int128(val) %= -1); + } + + // Min and max + EXPECT_EQ(0, int128_max() / int128_min()); + EXPECT_EQ(int128_max(), int128_max() % int128_min()); + EXPECT_EQ(-1, int128_min() / int128_max()); + EXPECT_EQ(-1, int128_min() % int128_max()); + + // Power of two division and modulo of random large values + int128 positive_values[] = { + make_int128(0x21e1a1cc69574620, 0xe7ac447fab2fc869), + make_int128(0x32c2ff3ab89e66e8, 0x03379a613fd1ce74), + make_int128(0x6f32ca786184dcaf, 0x046f9c9ecb3a9ce1), + make_int128(0x1aeb469dd990e0ee, 0xda2740f243cd37eb), + }; + for (auto val : positive_values) { + for (int i = 0; i < 127; ++i) { + int128 power_of_two = int128(1) << i; + EXPECT_EQ(val >> i, val / power_of_two); + EXPECT_EQ(val >> i, int128(val) /= power_of_two); + EXPECT_EQ(val & (power_of_two - 1), val % power_of_two); + EXPECT_EQ(val & (power_of_two - 1), int128(val) %= power_of_two); + } + } + + // Manually calculated random large value cases + struct DivisionModCase { + int128 dividend; + int128 divisor; + int128 quotient; + int128 remainder; + }; + + DivisionModCase cases[] = { + {make_int128(0x6ada48d489007966, 0x3c9c5c98150d5d69), make_int128(0x8bc308fb, 0x8cb9cc9a3b803344), 0xc3b87e08, + make_int128(0x1b7db5e1, 0xd9eca34b7af04b49)}, + {make_int128(0xd6946511b5b, 0x4886c5c96546bf5f), -make_int128(0x263b, 0xfd516279efcfe2dc), -0x59cbabf0, + make_int128(0x622, 0xf462909155651d1f)}, + {-make_int128(0x33db734f9e8d1399, 0x8447ac92482bca4d), 0x37495078240, -make_int128(0xf01f1, 0xbc0368bf9a77eae8), + -0x21a508f404d}, + {-make_int128(0x13f837b409a07e7d, 0x7fc8e248a7d73560), -0x1b9f, make_int128(0xb9157556d724, 0xb14f635714d7563e), + -0x1ade}, + }; + for (const auto &[dividend, divisor, quotient, remainder] : cases) { + EXPECT_EQ(quotient, dividend / divisor); + EXPECT_EQ(quotient, int128(dividend) /= divisor); + EXPECT_EQ(remainder, dividend % divisor); + EXPECT_EQ(remainder, int128(dividend) %= divisor); + } +} + +TEST(Int128Test, BitwiseLogic) { + EXPECT_EQ(int128(-1), ~int128(0)); + + int128 values[] = { + 0, + -1, + 0xde400bee05c3ff6b, + make_int128(0x7f32178dd81d634a, 0), + make_int128(0xaf539057055613a, 0x7d104d7d946c2e4d), + }; + for (auto val : values) { + SCOPED_TRACE(::testing::Message() << "val: " << val); + + EXPECT_EQ(val, ~~val); + EXPECT_EQ(val, val & val); + EXPECT_EQ(val, val | val); + EXPECT_EQ(0, val ^ val); + + EXPECT_EQ(val, int128(val) &= val); + EXPECT_EQ(val, int128(val) |= val); + EXPECT_EQ(0, int128(val) ^= val); + + EXPECT_EQ(val, val | 0); + EXPECT_EQ(0, val & 0); + EXPECT_EQ(val, val ^ 0); + + EXPECT_EQ(int128(-1), val | int128(-1)); + EXPECT_EQ(val, val & int128(-1)); + EXPECT_EQ(~val, val ^ int128(-1)); + } + + std::pair pairs64[] = { + {0x7f86797f5e991af4, 0x1ee30494fb007c97}, {0x0b278282bacf01af, 0x58780e0a57a49e86}, + {0x059f266ccb93a666, 0x3d5b731bae9286f5}, {0x63c0c4820f12108c, 0x58166713c12e1c3a}, + {0x381488bb2ed2a66e, 0x2220a3eb76a3698c}, {0x2a0a0dfb81e06f21, 0x4b60585927f5523c}, + {0x555b1c3a03698537, 0x25478cd19d8e53cb}, {0x4750f6f27d779225, 0x16397553c6ff05fc}, + }; + for (const auto &[first, second] : pairs64) { + SCOPED_TRACE(::testing::Message() << "first: " << first << ", second: " << second); + + EXPECT_EQ(make_int128(~first, ~second), ~make_int128(first, second)); + EXPECT_EQ(int128(first & second), int128(first) & int128(second)); + EXPECT_EQ(int128(first | second), int128(first) | int128(second)); + EXPECT_EQ(int128(first ^ second), int128(first) ^ int128(second)); + + EXPECT_EQ(int128(first & second), int128(first) &= int128(second)); + EXPECT_EQ(int128(first | second), int128(first) |= int128(second)); + EXPECT_EQ(int128(first ^ second), int128(first) ^= int128(second)); + + EXPECT_EQ(make_int128(first & second, 0), make_int128(first, 0) & make_int128(second, 0)); + EXPECT_EQ(make_int128(first | second, 0), make_int128(first, 0) | make_int128(second, 0)); + EXPECT_EQ(make_int128(first ^ second, 0), make_int128(first, 0) ^ make_int128(second, 0)); + + EXPECT_EQ(make_int128(first & second, 0), make_int128(first, 0) &= make_int128(second, 0)); + EXPECT_EQ(make_int128(first | second, 0), make_int128(first, 0) |= make_int128(second, 0)); + EXPECT_EQ(make_int128(first ^ second, 0), make_int128(first, 0) ^= make_int128(second, 0)); + } +} + +TEST(Int128Test, BitwiseShiftTest) { + for (int i = 0; i < 64; ++i) { + for (int j = 0; j <= i; ++j) { + SCOPED_TRACE(::testing::Message() << "i: " << i << ", j: " << j); + EXPECT_EQ(uint64_t(1) << i, int128(uint64_t(1) << j) << (i - j)); + EXPECT_EQ(uint64_t(1) << i, int128(uint64_t(1) << j) <<= (i - j)); + } + } +} + +TEST(Int128Test, NumericLimits) { + static_assert(std::numeric_limits::is_specialized, "std::numeric_limits must be specialized"); + static_assert(std::numeric_limits::is_signed, "std::numeric_limits must be signed"); + static_assert(std::numeric_limits::is_integer, "std::numeric_limits must be integer"); + + EXPECT_EQ(static_cast(127 * std::log10(2)), std::numeric_limits::digits10); + EXPECT_EQ(int128_min(), std::numeric_limits::min()); + EXPECT_EQ(int128_min(), std::numeric_limits::lowest()); + EXPECT_EQ(int128_max(), std::numeric_limits::max()); +} diff --git a/test/integer/integer_test.cc b/test/integer/integer_test.cc index 22b7386..9d20add 100644 --- a/test/integer/integer_test.cc +++ b/test/integer/integer_test.cc @@ -5,324 +5,314 @@ #include "integer.hh" #include "test/utils.hh" -TEST(IntegerTest, IntegerTemplate) { - Integer test = 0; - Integer test1 = 0; - Integer test2 = 0; - Integer test3 = 0; - Integer test4 = 0; - Integer test5 = 0; - Integer test6 = 0; - Integer test7 = 0; - Integer test8 = 0; -} +using namespace numbers; TEST(IntegerTest, IntegerMinMax) { - ASSERT_THROW(-Int64::MIN, std::runtime_error); - ASSERT_EQ(Int16::MIN.saturating_neg(), Int16::MAX); - ASSERT_EQ(Int16::MAX.saturating_add(Int16::MAX), Int16::MAX); - ASSERT_EQ(Int16::MAX.saturating_sub(Int16::MAX), 0_i16); + ASSERT_THROW(-int64::MIN, std::runtime_error); + ASSERT_EQ(int16::MIN.saturating_neg(), int16::MAX); + ASSERT_EQ(int16::MAX.saturating_add(int16::MAX), int16::MAX); + ASSERT_EQ(int16::MAX.saturating_sub(int16::MAX), 0); } -TEST(IntegerTest, IntegerStringStream) { +TEST(integerTest, integerStringStream) { for (short n = 1; n < 1000; n++) { - Int16 num = n; + int16 num = n; std::ostringstream stream; stream << num; ASSERT_EQ(stream.str(), std::to_string(n)); } for (long long n = 1; n < 1000; n++) { - Int64 num = n; + int64 num = n; std::ostringstream stream; stream << num; ASSERT_EQ(stream.str(), std::to_string(n)); } } -TEST(IntegerTest, IntegerAdd) { - Int32 num = 10_i32; - Int32 num1 = 10_i32; +TEST(integerTest, integerAdd) { + int32 num = 10; + int32 num1 = 10; ASSERT_EQ(num, num1); - Int32 num2 = 20_i32; - Int32 zero_num = 0_i32; - Int32 max_num = Int32::MAX; - Int32 min_num = Int32::MIN; + int32 num2 = 20; + int32 zero_num = 0; + int32 max_num = int32::MAX; + int32 min_num = int32::MIN; ASSERT_EQ(num + num1, num2); ASSERT_EQ(zero_num + max_num, max_num); ASSERT_EQ(max_num + min_num, -1); } -TEST(IntegerTest, IntegerAddNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerAddNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); - ASSERT_EQ(num2 + num1, 20_i32); + ASSERT_EQ(num2 + num1, 20); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerWrappingAdd) { +TEST(integerTest, integerWrappingAdd) { for (int16_t n = std::numeric_limits::min(); n < std::numeric_limits::max(); n++) { - Int16 num = n; - ASSERT_EQ(num.wrapping_add(1), Int16(n + 1)); + int16 num = n; + ASSERT_EQ(num.wrapping_add(1), int16(n + 1)); } - Int16 num = Int16::MAX; - ASSERT_EQ(num.wrapping_add(1), Int16::MIN); + int16 num = int16::MAX; + ASSERT_EQ(num.wrapping_add(1), int16::MIN); constexpr int32_t min = 10; constexpr int32_t max = 90000; for (int run = 0; run < 100; run++) { int32_t a = random_integer(min, max); int32_t b = random_integer(min, max); - Int32 num_a = a; - Int32 num_b = b; - ASSERT_EQ(Int32(a + b), num_a + num_b); + int32 num_a = a; + int32 num_b = b; + ASSERT_EQ(int32(a + b), num_a + num_b); ASSERT_EQ(num_a + num_b, a + b); } } -TEST(IntegerTest, IntegerWrappingAddNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerWrappingAddNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); - ASSERT_EQ(num2.wrapping_add(num1), Int32(20)); + ASSERT_EQ(num2.wrapping_add(num1), int32(20)); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerAddByAccumulate) { +TEST(integerTest, integerAddByAccumulate) { constexpr size_t len = 100; - std::vector> nums(len); + std::vector nums(len); ASSERT_EQ(nums.size(), len); - std::iota(nums.begin(), nums.end(), Integer(1)); - ASSERT_EQ(std::accumulate(nums.begin(), nums.end(), Integer(0)), Integer(5050)); + std::iota(nums.begin(), nums.end(), int64(1)); + ASSERT_EQ(std::accumulate(nums.begin(), nums.end(), int64(0)), int64(5050)); } -TEST(IntegerTest, IntegerCheckedAdd) { +TEST(integerTest, integerCheckedAdd) { constexpr int32_t min = 10; constexpr int32_t max = 90000; for (int run = 0; run < 100; run++) { int32_t a = random_integer(min, max); int32_t b = random_integer(min, max); - Int32 num_a = a; - Int32 num_b = b; + int32 num_a = a; + int32 num_b = b; ASSERT_EQ(num_a.checked_add(num_b).value(), a + b); } { - auto num = 10_i16; - Integer num1 = Integer::MAX; + int16 num = 10; + int16 num1 = int16::MAX; ASSERT_EQ(num.checked_add(num1), std::nullopt); } { - auto num = 10_i64; - Int64 num1 = Int64::MAX; + int64 num = 10; + int64 num1 = int64::MAX; ASSERT_EQ(num.checked_add(num1), std::nullopt); } { - Integer num = -1; - Integer num1 = Integer::MIN; + int64 num = -1; + int64 num1 = int64::MIN; ASSERT_EQ(num.checked_add(num1), std::nullopt); } { - Integer num1 = Integer::MIN; + int64 num1 = int64::MIN; ASSERT_EQ(num1.checked_add(num1), std::nullopt); - Integer num2 = Integer::MAX; + int64 num2 = int64::MAX; ASSERT_EQ(num2.checked_add(num2), std::nullopt); } } -TEST(IntegerTest, IntegerCheckedAddNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerCheckedAddNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); - ASSERT_EQ(num2.checked_add(num1).value(), Int32(20)); + ASSERT_EQ(num2.checked_add(num1).value(), int32(20)); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerOverflowingAdd) { +TEST(integerTest, integerOverflowingAdd) { constexpr int32_t min = 10; constexpr int32_t max = 90000; for (int run = 0; run < 100; run++) { int32_t a = random_integer(min, max); int32_t b = random_integer(min, max); - Int32 num_a = a; - Int32 num_b = b; + int32 num_a = a; + int32 num_b = b; auto [ret, flag] = num_a.overflowing_add(num_b); ASSERT_FALSE(flag); ASSERT_EQ(ret, a + b); } { - auto num = 10_i16; - Int16 num1 = Int16::MAX; + int16 num = 10; + int16 num1 = int16::MAX; auto [ret, flag] = num.overflowing_add(num1); ASSERT_TRUE(flag); ASSERT_EQ(ret, 10 + std::numeric_limits::max()); } { - auto num = 10_i64; - Int64 num1 = Int64::MAX; + int64 num = 10; + int64 num1 = int64::MAX; auto [_, flag] = num.overflowing_add(num1); ASSERT_TRUE(flag); } { - Integer num = -1; - Integer num1 = Integer::MIN; + int64 num = -1; + int64 num1 = int64::MIN; auto [_, flag] = num.overflowing_add(num1); ASSERT_TRUE(flag); } { - Integer num1 = Integer::MIN; + int64 num1 = int64::MIN; auto [_, flag1] = num1.overflowing_add(num1); ASSERT_TRUE(flag1); } { - Integer num2 = Integer::MAX; + int64 num2 = int64::MAX; auto [_, flag2] = num2.overflowing_add(num2); ASSERT_TRUE(flag2); } } -TEST(IntegerTest, IntegerOverflowingAddNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerOverflowingAddNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto [ret, flag] = num2.overflowing_add(num1); - ASSERT_EQ(ret, Int32(20)); + ASSERT_EQ(ret, int32(20)); ASSERT_FALSE(flag); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerSaturatingAdd) { +TEST(integerTest, integerSaturatingAdd) { constexpr int32_t min = 10; constexpr int32_t max = 90000; for (int run = 0; run < 100; run++) { int32_t a = random_integer(min, max); int32_t b = random_integer(min, max); - Int32 num_a = a; - Int32 num_b = b; + int32 num_a = a; + int32 num_b = b; auto ret = num_a.saturating_add(num_b); ASSERT_EQ(ret, a + b); } for (int8_t i = 1; i < std::numeric_limits::max(); i++) { - Int8 num = Int8::MAX; - auto ret = num.saturating_add(Int8(i)); - EXPECT_EQ(ret, Int8::MAX) << static_cast(i) << '\n'; + int8 num = int8::MAX; + auto ret = num.saturating_add(int8(i)); + EXPECT_EQ(ret, int8::MAX) << static_cast(i) << '\n'; } { - auto num = 10_i16; - Integer num1 = Integer::MAX; + int16 num = 10; + int16 num1 = int16::MAX; auto ret = num.saturating_add(num1); - ASSERT_EQ(ret, Int16::MAX); + ASSERT_EQ(ret, int16::MAX); } { - auto num = 10_i64; - Int64 num1 = Int64::MAX; + int64 num = 10; + int64 num1 = int64::MAX; auto ret = num.saturating_add(num1); - ASSERT_EQ(ret, Int64::MAX); + ASSERT_EQ(ret, int64::MAX); } { - Int64 max_num = Int64::MAX; + int64 max_num = int64::MAX; auto ret = max_num.saturating_add(max_num); - ASSERT_EQ(ret, Int64::MAX); + ASSERT_EQ(ret, int64::MAX); - Int64 min_num = Int64::MIN; + int64 min_num = int64::MIN; ret = min_num.saturating_add(min_num); - ASSERT_EQ(ret, Int64::MIN); + ASSERT_EQ(ret, int64::MIN); } { - Int64 num = -1; - Int64 num1 = Int64::MIN; + int64 num = -1; + int64 num1 = int64::MIN; auto ret = num.saturating_add(num1); - ASSERT_EQ(ret, Int64::MIN); + ASSERT_EQ(ret, int64::MIN); } } -TEST(IntegerTest, IntegerSaturatingAddNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerSaturatingAddNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2.saturating_add(num1); - ASSERT_EQ(ret, Int32(20)); + ASSERT_EQ(ret, int32(20)); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerAddOverflow) { +TEST(integerTest, integerAddOverflow) { { - auto num = 10_i16; - Integer num1 = Integer::MAX; + int16 num = 10; + int16 num1 = int16::MAX; ASSERT_THROW(num + num1, std::runtime_error); } { - auto num = 10_i64; - Int64 num1 = Int64::MAX; + int64 num = 10; + int64 num1 = int64::MAX; ASSERT_THROW(num + num1, std::runtime_error); } { - Integer num = -1; - Integer num1 = Integer::MIN; + int64 num = -1; + int64 num1 = int64::MIN; ASSERT_THROW(num + num1, std::runtime_error); } } -TEST(IntegerTest, IntegerAddOverflowByAccumulate) { - std::vector nums(10); +TEST(integerTest, integerAddOverflowByAccumulate) { + std::vector nums(10); ASSERT_EQ(nums.size(), 10); - std::iota(nums.begin(), nums.end(), Int64::MAX - 11_i64); - ASSERT_THROW(std::accumulate(nums.begin(), nums.end(), 0_i64), std::runtime_error); + std::iota(nums.begin(), nums.end(), int64::MAX - 11); + ASSERT_THROW(std::accumulate(nums.begin(), nums.end(), int64(0)), std::runtime_error); } -TEST(IntegerTest, IntegerSub) { - auto num = 10_i32; - auto num1 = 10_i32; - auto num2 = 20_i32; - Int32 num3 = Int32::MAX; - auto num4 = 0_i32; +TEST(integerTest, integerSub) { + auto num = int32(10); + auto num1 = int32(10); + auto num2 = int32(20); + int32 num3 = int32::MAX; + auto num4 = int32(0); ASSERT_EQ(num2 - num1, num); ASSERT_EQ(num - num1, num4); ASSERT_EQ(num3 - num4, num3); { - Int64 num = -1; - Int64 num1 = Int64::MIN; - ASSERT_EQ(num - num1, Int64::MAX); + int64 num = -1; + int64 num1 = int64::MIN; + ASSERT_EQ(num - num1, int64::MAX); } constexpr int32_t min = 10; @@ -331,159 +321,159 @@ TEST(IntegerTest, IntegerSub) { for (int run = 0; run < 100; run++) { int32_t a = random_integer(min, max); int32_t b = random_integer(min, max); - Int32 num_a = a; - Int32 num_b = b; - ASSERT_EQ(Int32(a - b), num_a - num_b); + int32 num_a = a; + int32 num_b = b; + ASSERT_EQ(int32(a - b), num_a - num_b); ASSERT_EQ(num_a - num_b, a - b); } } -TEST(IntegerTest, IntegerSubNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerSubNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); - ASSERT_EQ(num2 - num1, Int32(0)); + ASSERT_EQ(num2 - num1, int32(0)); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerSubOverflow) { +TEST(integerTest, integerSubOverflow) { for (short n = 1; n < 1000; n++) { - Int16 num = n; - Int16 num1 = Integer::MIN; + int16 num = n; + int16 num1 = int16::MIN; ASSERT_THROW(num1 - num, std::runtime_error); } } -TEST(IntegerTest, IntegerCheckedSub) { +TEST(integerTest, integerCheckedSub) { for (short n = 1; n < 1000; n++) { - Int16 num = n; - Int16 num1 = Integer::MIN; + int16 num = n; + int16 num1 = int16::MIN; ASSERT_EQ(num1.checked_sub(num), std::nullopt); } for (int16_t n = std::numeric_limits::max(); n > std::numeric_limits::min(); n--) { - Int16 num = n; + int16 num = n; auto ret = num.checked_sub(1); - ASSERT_EQ(ret.value(), Int16(n - 1)); + ASSERT_EQ(ret.value(), int16(n - 1)); } } -TEST(IntegerTest, IntegerCheckedSubNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerCheckedSubNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); - ASSERT_EQ(num2.checked_sub(num1).value(), Int32(0)); + ASSERT_EQ(num2.checked_sub(num1).value(), int32(0)); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerOverflowingSub) { +TEST(integerTest, integerOverflowingSub) { for (short n = 1; n < 1000; n++) { - Int16 num = n; - Int16 num1 = Int16::MIN; + int16 num = n; + int16 num1 = int16::MIN; auto [ret, flag] = num1.overflowing_sub(num); ASSERT_TRUE(flag); ASSERT_EQ(ret, std::numeric_limits::min() - n); } for (int16_t n = std::numeric_limits::max(); n > std::numeric_limits::min(); n--) { - Int16 num = n; + int16 num = n; auto [ret, flag] = num.overflowing_sub(1); ASSERT_FALSE(flag); - ASSERT_EQ(ret, Int16(n - 1)); + ASSERT_EQ(ret, int16(n - 1)); } } -TEST(IntegerTest, IntegerOverflowingSubNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerOverflowingSubNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto [ret, flag] = num2.overflowing_sub(num1); - ASSERT_EQ(ret, Int32(0)); + ASSERT_EQ(ret, int32(0)); ASSERT_FALSE(flag); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerWrappingSub) { +TEST(integerTest, integerWrappingSub) { for (int16_t n = std::numeric_limits::max(); n > std::numeric_limits::min(); n--) { - Int16 num = n; - ASSERT_EQ(num.wrapping_sub(1), Int16(n - 1)); + int16 num = n; + ASSERT_EQ(num.wrapping_sub(1), int16(n - 1)); } - Int16 num = Int16::MIN; - ASSERT_EQ(num.wrapping_sub(1), Int16::MAX); + int16 num = int16::MIN; + ASSERT_EQ(num.wrapping_sub(1), int16::MAX); } -TEST(IntegerTest, IntegerWarppingSubNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerWarppingSubNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2.wrapping_sub(num1); - ASSERT_EQ(ret, Int32(0)); + ASSERT_EQ(ret, int32(0)); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerSaturatingSub) { +TEST(integerTest, integerSaturatingSub) { for (short n = 1; n <= 1000; n++) { - Int16 num = n; - Int16 num1 = Integer::MIN; + int16 num = n; + int16 num1 = int16::MIN; auto ret = num1.saturating_sub(num); - ASSERT_EQ(ret, Int16::MIN); + ASSERT_EQ(ret, int16::MIN); } for (int16_t n = std::numeric_limits::max(); n > std::numeric_limits::min(); n--) { - Int16 num = n; + int16 num = n; auto ret = num.saturating_sub(1); - ASSERT_EQ(ret, Int16(n - 1)); + ASSERT_EQ(ret, int16(n - 1)); } - Int16 min_num = Int16::MIN; + int16 min_num = int16::MIN; auto ret = min_num.saturating_sub(1); - ASSERT_EQ(ret, Int16::MIN); + ASSERT_EQ(ret, int16::MIN); - Int16 max_num = Int16::MAX; + int16 max_num = int16::MAX; ret = max_num.saturating_sub(min_num); - ASSERT_EQ(ret, Int16::MAX); + ASSERT_EQ(ret, int16::MAX); ret = min_num.saturating_sub(max_num); - ASSERT_EQ(ret, Int16::MIN); + ASSERT_EQ(ret, int16::MIN); } -TEST(IntegerTest, IntegerSaturatingSubNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerSaturatingSubNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2.saturating_sub(num1); - ASSERT_EQ(ret, Int32(0)); + ASSERT_EQ(ret, int32(0)); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerMul) { - auto num = 10_i64; - auto num1 = 100_i64; +TEST(integerTest, integerMul) { + auto num = int64(10); + auto num1 = int64(100); ASSERT_EQ(num1 * num, 1000); num = num.MAX; - num1 = 0_i64; + num1 = int64(0); ASSERT_EQ(num1 * num, 0); - num1 = 1_i64; + num1 = int64(1); ASSERT_EQ(num1 * num, num); constexpr int32_t min = 10; @@ -491,35 +481,35 @@ TEST(IntegerTest, IntegerMul) { for (int run = 0; run < 100; run++) { int32_t a = random_integer(min, max); int32_t b = random_integer(min, max); - Int32 num_a = a; - Int32 num_b = b; - ASSERT_EQ(Int32(a * b), num_a * num_b); + int32 num_a = a; + int32 num_b = b; + ASSERT_EQ(int32(a * b), num_a * num_b); ASSERT_EQ(num_a * num_b, a * b); } } -TEST(IntegerTest, IntegerMulNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerMulNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2 * num1; - ASSERT_EQ(ret, Int32(100)); + ASSERT_EQ(ret, int32(100)); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerMulOverflow) { - auto num = 10_i8; - auto num1 = 100_i8; +TEST(integerTest, integerMulOverflow) { + auto num = int8(10); + auto num1 = int8(100); ASSERT_THROW(num1 * num, std::runtime_error); } -TEST(IntegerTest, IntegerCheckedMul) { - auto num = 10_i8; - auto num1 = 100_i8; +TEST(integerTest, integerCheckedMul) { + auto num = int8(10); + auto num1 = int8(100); ASSERT_EQ(num1.checked_mul(num), std::nullopt); constexpr int32_t min = 10; @@ -527,28 +517,28 @@ TEST(IntegerTest, IntegerCheckedMul) { for (int run = 0; run < 100; run++) { int32_t a = random_integer(min, max); int32_t b = random_integer(min, max); - Int32 num_a = a; - Int32 num_b = b; + int32 num_a = a; + int32 num_b = b; ASSERT_EQ(num_a.checked_mul(num_b).value(), a * b); } } -TEST(IntegerTest, IntegerCheckedMulNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerCheckedMulNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2.checked_mul(num1); - ASSERT_EQ(ret.value(), Int32(100)); + ASSERT_EQ(ret.value(), int32(100)); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerOverflowingMul) { - auto num = 10_i8; - auto num1 = 100_i8; +TEST(integerTest, integerOverflowingMul) { + auto num = int8(10); + auto num1 = int8(100); auto [ret, flag] = num1.overflowing_mul(num); ASSERT_EQ(ret, int8_t(10 * 100)); ASSERT_TRUE(flag); @@ -558,191 +548,191 @@ TEST(IntegerTest, IntegerOverflowingMul) { for (int run = 0; run < 100; run++) { int32_t a = random_integer(min, max); int32_t b = random_integer(min, max); - Int32 num_a = a; - Int32 num_b = b; + int32 num_a = a; + int32 num_b = b; auto [ret, flag] = num_a.overflowing_mul(num_b); ASSERT_FALSE(flag); ASSERT_EQ(ret, a * b); } } -TEST(IntegerTest, IntegerOverflowingMulNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerOverflowingMulNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto [ret, flag] = num2.overflowing_mul(num1); ASSERT_FALSE(flag); - ASSERT_EQ(ret, Int32(100)); + ASSERT_EQ(ret, int32(100)); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerSaturatingMul) { - auto num = 10_i8; - auto num1 = 100_i8; +TEST(integerTest, integerSaturatingMul) { + auto num = int8(10); + auto num1 = int8(100); auto ret = num1.saturating_mul(num); - ASSERT_EQ(ret, Int8::MAX); + ASSERT_EQ(ret, int8::MAX); - Int16 max_num = Int16::MAX; - ASSERT_EQ(max_num.saturating_mul(max_num), Int16::MAX); - Int16 min_num = Int16::MIN; - Int16 num2 = Int16{-30}; - ASSERT_EQ(min_num.saturating_mul(min_num), Int16::MAX); - ASSERT_EQ(min_num.saturating_mul(num2), Int16::MAX); + int16 max_num = int16::MAX; + ASSERT_EQ(max_num.saturating_mul(max_num), int16::MAX); + int16 min_num = int16::MIN; + int16 num2 = int16{-30}; + ASSERT_EQ(min_num.saturating_mul(min_num), int16::MAX); + ASSERT_EQ(min_num.saturating_mul(num2), int16::MAX); constexpr int32_t min = 10; constexpr int32_t max = 10000; for (int run = 0; run < 100; run++) { int32_t a = random_integer(min, max); int32_t b = random_integer(min, max); - Int32 num_a = a; - Int32 num_b = b; + int32 num_a = a; + int32 num_b = b; auto ret = num_a.saturating_mul(num_b); ASSERT_EQ(ret, a * b); } } -TEST(IntegerTest, IntegerSaturatingMulNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerSaturatingMulNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2.saturating_mul(num1); - ASSERT_EQ(ret, Int32(100)); + ASSERT_EQ(ret, int32(100)); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerWrappingMul) { - Int16 min_num = Int16::MIN; - Int16 max_num = Int16::MAX; - Int16 num1 = -1; +TEST(integerTest, integerWrappingMul) { + int16 min_num = int16::MIN; + int16 max_num = int16::MAX; + int16 num1 = -1; ASSERT_EQ(min_num.wrapping_mul(num1), min_num); ASSERT_EQ(max_num.wrapping_mul(num1), min_num + 1); } -TEST(IntegerTest, IntegerWrappingMulNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerWrappingMulNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2.wrapping_mul(num1); - ASSERT_EQ(ret, Int32(100)); + ASSERT_EQ(ret, int32(100)); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerDiv) { +TEST(integerTest, integerDiv) { constexpr int32_t min = 10; constexpr int32_t max = 10000; for (int run = 0; run < 100; run++) { int32_t a = random_integer(min, max); int32_t b = random_integer(min, max); - Int32 num_a = a; - Int32 num_b = b; - ASSERT_EQ(Int32(a / b), num_a / num_b); + int32 num_a = a; + int32 num_b = b; + ASSERT_EQ(int32(a / b), num_a / num_b); EXPECT_EQ(num_a / num_b, a / b) << a << ' ' << b << '\n'; } } -TEST(IntegerTest, IntegerDivNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerDivNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2 / num1; - ASSERT_EQ(ret, Int32(1)); + ASSERT_EQ(ret, int32(1)); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerDivOverflow) { - Int8 num = -1; - Int8 num1 = Int8::MIN; +TEST(integerTest, integerDivOverflow) { + int8 num = -1; + int8 num1 = int8::MIN; ASSERT_THROW(num1 / num, std::runtime_error); } -TEST(IntegerTest, IntegerWrappingDiv) { - Int8 num = -1; - Int8 num1 = Int8::MIN; +TEST(integerTest, integerWrappingDiv) { + int8 num = -1; + int8 num1 = int8::MIN; ASSERT_EQ(num1.wrapping_div(num), num1); } -TEST(IntegerTest, IntegerWrappingDivNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerWrappingDivNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2.wrapping_div(num1); - ASSERT_EQ(ret, Int32(1)); + ASSERT_EQ(ret, int32(1)); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerCheckedDiv) { +TEST(integerTest, integerCheckedDiv) { constexpr int32_t min = 10; constexpr int32_t max = 10000; for (int run = 0; run < 100; run++) { int32_t a = random_integer(min, max); int32_t b = random_integer(min, max); - Int32 num_a = a; - Int32 num_b = b; + int32 num_a = a; + int32 num_b = b; EXPECT_EQ(num_a.checked_div(num_b).value(), a / b); } - Int8 num = -1; - Int8 num1 = Int8::MIN; + int8 num = -1; + int8 num1 = int8::MIN; ASSERT_EQ(num1.checked_div(num), std::nullopt); } -TEST(IntegerTest, IntegerCheckedDivNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerCheckedDivNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2.checked_div(num1); - ASSERT_EQ(ret.value(), Int32(1)); + ASSERT_EQ(ret.value(), int32(1)); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerOverflowingDiv) { +TEST(integerTest, integerOverflowingDiv) { constexpr int32_t min = 10; constexpr int32_t max = 10000; for (int run = 0; run < 100; run++) { int32_t a = random_integer(min, max); int32_t b = random_integer(min, max); - Int32 num_a = a; - Int32 num_b = b; + int32 num_a = a; + int32 num_b = b; auto [ret, flag] = num_a.overflowing_div(num_b); ASSERT_FALSE(flag); EXPECT_EQ(ret, a / b); } - Int8 num = -1; - Int8 num1 = Int8::MIN; + int8 num = -1; + int8 num1 = int8::MIN; auto [ret, flag] = num1.overflowing_div(num); ASSERT_TRUE(flag); ASSERT_EQ(ret, -1 * std::numeric_limits::min()); } -TEST(IntegerTest, IntegerOverflowingDivNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerOverflowingDivNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); @@ -752,28 +742,28 @@ TEST(IntegerTest, IntegerOverflowingDivNoSideEffects) { ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerSaturatingDiv) { +TEST(integerTest, integerSaturatingDiv) { constexpr int32_t min = 10; constexpr int32_t max = 10000; for (int run = 0; run < 100; run++) { int32_t a = random_integer(min, max); int32_t b = random_integer(min, max); - Int32 num_a = a; - Int32 num_b = b; + int32 num_a = a; + int32 num_b = b; auto ret = num_a.saturating_div(num_b); EXPECT_EQ(ret, a / b); } - Int8 num = -1; - Int8 num1 = Int8::MIN; + int8 num = -1; + int8 num1 = int8::MIN; auto ret = num1.saturating_div(num); - ASSERT_EQ(ret, Int8::MIN); + ASSERT_EQ(ret, int8::MIN); } -TEST(IntegerTest, IntegerSaturatingDivNoSideEffects) { - Int32 num = 10_i32; - Int32 num1 = num; - Int32 num2 = num; +TEST(integerTest, integerSaturatingDivNoSideEffects) { + int32 num = 10; + int32 num1 = num; + int32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); @@ -782,60 +772,60 @@ TEST(IntegerTest, IntegerSaturatingDivNoSideEffects) { ASSERT_EQ(num2, num); } -TEST(IntegerTest, IntegerAbs) { +TEST(integerTest, integerAbs) { for (int32_t i = 0; i <= 1000; i += 10) { - Int32 num = i; + int32 num = i; ASSERT_EQ(num.abs(), num); } for (int32_t i = -1000; i <= 0; i += 10) { - Int32 num = i; - ASSERT_EQ(num.abs(), Int32(-i)); + int32 num = i; + ASSERT_EQ(num.abs(), int32(-i)); } - Int32 max_num = Int32::MAX; + int32 max_num = int32::MAX; ASSERT_EQ(max_num.abs(), max_num); - Int32 min_num = Int32::MIN; + int32 min_num = int32::MIN; ASSERT_THROW(min_num.abs(), std::runtime_error); } -TEST(IntegerTest, IntegerAbsNoSideEffects) { - Int32 num = 10_i32; - Int32 tmp_num = num; +TEST(integerTest, integerAbsNoSideEffects) { + int32 num = 10; + int32 tmp_num = num; ASSERT_EQ(num, tmp_num); auto _ = num.abs(); ASSERT_EQ(num, tmp_num); } -TEST(IntegerTest, IntegerOverflowingAbs) { +TEST(integerTest, integerOverflowingAbs) { for (int32_t i = 0; i <= 1000; i += 10) { - Int32 num = i; + int32 num = i; auto [ret, flag] = num.overflowing_abs(); ASSERT_FALSE(flag); ASSERT_EQ(ret, num); } for (int32_t i = -1000; i <= 0; i += 10) { - Int32 num = i; + int32 num = i; auto [ret, flag] = num.overflowing_abs(); ASSERT_FALSE(flag); - ASSERT_EQ(ret, Int32(-i)); + ASSERT_EQ(ret, int32(-i)); } - Int32 max_num = Int32::MAX; + int32 max_num = int32::MAX; auto [ret, flag] = max_num.overflowing_abs(); ASSERT_FALSE(flag); ASSERT_EQ(ret, max_num); - Int32 min_num = Int32::MIN; + int32 min_num = int32::MIN; std::tie(ret, flag) = min_num.overflowing_abs(); ASSERT_TRUE(flag); ASSERT_EQ(ret, min_num); } -TEST(IntegerTest, IntegerOverflowingAbsNoSideEffects) { - Int32 num = 10_i32; - Int32 tmp_num = num; +TEST(integerTest, integerOverflowingAbsNoSideEffects) { + int32 num = 10; + int32 tmp_num = num; ASSERT_EQ(num, tmp_num); auto [_, flag] = num.overflowing_abs(); @@ -843,216 +833,216 @@ TEST(IntegerTest, IntegerOverflowingAbsNoSideEffects) { ASSERT_EQ(num, tmp_num); } -TEST(IntegerTest, IntegerSaturatingAbs) { +TEST(integerTest, integerSaturatingAbs) { for (int32_t i = 0; i <= 1000; i += 10) { - Int32 num = i; + int32 num = i; ASSERT_EQ(num.saturating_abs(), num); } for (int32_t i = -1000; i <= 0; i += 10) { - Int32 num = i; - ASSERT_EQ(num.saturating_abs(), Int32(-i)); + int32 num = i; + ASSERT_EQ(num.saturating_abs(), int32(-i)); } - Int32 max_num = Int32::MAX; + int32 max_num = int32::MAX; ASSERT_EQ(max_num.saturating_abs(), max_num); - Int32 min_num = Int32::MIN; - ASSERT_EQ(min_num.saturating_abs(), Int32::MAX); + int32 min_num = int32::MIN; + ASSERT_EQ(min_num.saturating_abs(), int32::MAX); } -TEST(IntegerTest, IntegerSaturatingAbsNoSideEffects) { - Int32 num = 10_i32; - Int32 tmp_num = num; +TEST(integerTest, integerSaturatingAbsNoSideEffects) { + int32 num = 10; + int32 tmp_num = num; ASSERT_EQ(num, tmp_num); auto _ = num.saturating_abs(); ASSERT_EQ(num, tmp_num); } -TEST(IntegerTest, IntegerCheckedAbs) { +TEST(integerTest, integerCheckedAbs) { for (int32_t i = 0; i <= 1000; i += 10) { - Int32 num = i; + int32 num = i; ASSERT_EQ(num.checked_abs(), num); } for (int32_t i = -1000; i <= 0; i += 10) { - Int32 num = i; - ASSERT_EQ(num.checked_abs(), Int32(-i)); + int32 num = i; + ASSERT_EQ(num.checked_abs(), int32(-i)); } - Int32 max_num = Int32::MAX; + int32 max_num = int32::MAX; ASSERT_EQ(max_num.checked_abs(), max_num); - Int32 min_num = Int32::MIN; + int32 min_num = int32::MIN; ASSERT_EQ(min_num.checked_abs(), std::nullopt); } -TEST(IntegerTest, IntegerCheckedAbsNoSideEffects) { - Int32 num = 10_i32; - Int32 tmp_num = num; +TEST(integerTest, integerCheckedAbsNoSideEffects) { + int32 num = 10; + int32 tmp_num = num; ASSERT_EQ(num, tmp_num); auto _ = num.checked_abs(); ASSERT_EQ(num, tmp_num); } -TEST(IntegerTest, IntegerWrappingAbs) { +TEST(integerTest, integerWrappingAbs) { for (int32_t i = 0; i <= 1000; i += 10) { - Int32 num = i; + int32 num = i; ASSERT_EQ(num.wrapping_abs(), num); } for (int32_t i = -1000; i <= 0; i += 10) { - Int32 num = i; - ASSERT_EQ(num.wrapping_abs(), Int32(-i)); + int32 num = i; + ASSERT_EQ(num.wrapping_abs(), int32(-i)); } - Int32 max_num = Int32::MAX; + int32 max_num = int32::MAX; ASSERT_EQ(max_num.wrapping_abs(), max_num); - Int32 min_num = Int32::MIN; - ASSERT_EQ(min_num.wrapping_abs(), Int32::MIN); + int32 min_num = int32::MIN; + ASSERT_EQ(min_num.wrapping_abs(), int32::MIN); } -TEST(IntegerTest, IntegerWrappingAbsNoSideEffects) { - Int32 num = 10_i32; - Int32 tmp_num = num; +TEST(integerTest, integerWrappingAbsNoSideEffects) { + int32 num = 10; + int32 tmp_num = num; ASSERT_EQ(num, tmp_num); auto _ = num.wrapping_abs(); ASSERT_EQ(num, tmp_num); } -TEST(IntegerTest, IntegerNeg) { +TEST(integerTest, integerNeg) { for (int32_t i = 0; i <= 1000; i += 10) { - Int32 num = i; - ASSERT_EQ(-num, Integer(-i)); + int32 num = i; + ASSERT_EQ(-num, int32(-i)); } for (int32_t i = -1000; i <= 0; i += 10) { - Int32 num = i; + int32 num = i; ASSERT_EQ(num.abs(), -num); } - Int32 max_num = Int32::MAX; - ASSERT_EQ(-max_num, -Int32::MAX); - Int32 min_num = Int32::MIN; + int32 max_num = int32::MAX; + ASSERT_EQ(-max_num, -int32::MAX); + int32 min_num = int32::MIN; ASSERT_THROW(-min_num, std::runtime_error); } -TEST(IntegerTest, IntegerNegNoSideEffects) { - Int32 num = 10_i32; - Int32 tmp_num = num; +TEST(integerTest, integerNegNoSideEffects) { + int32 num = 10; + int32 tmp_num = num; ASSERT_EQ(num, tmp_num); auto _ = -num; ASSERT_EQ(num, tmp_num); } -TEST(IntegerTest, IntegerCheckedNeg) { +TEST(integerTest, integerCheckedNeg) { for (int32_t i = 0; i <= 1000; i += 10) { - Int32 num = i; - ASSERT_EQ(num.checked_neg(), Integer(-i)); + int32 num = i; + ASSERT_EQ(num.checked_neg(), int32(-i)); } for (int32_t i = -1000; i <= 0; i += 10) { - Int32 num = i; + int32 num = i; ASSERT_EQ(num.checked_neg(), num.checked_abs()); } - Int32 max_num = Int32::MAX; - ASSERT_EQ(max_num.checked_neg(), Int32(-Int32::MAX)); - Int32 min_num = Int32::MIN; + int32 max_num = int32::MAX; + ASSERT_EQ(max_num.checked_neg(), int32(-int32::MAX)); + int32 min_num = int32::MIN; ASSERT_EQ(min_num.checked_neg(), std::nullopt); } -TEST(IntegerTest, IntegerCheckedNegNoSideEffects) { - Int32 num = 10_i32; - Int32 tmp_num = num; +TEST(integerTest, integerCheckedNegNoSideEffects) { + int32 num = 10; + int32 tmp_num = num; ASSERT_EQ(num, tmp_num); auto _ = num.checked_neg(); ASSERT_EQ(num, tmp_num); } -TEST(IntegerTest, IntegerOverflowingNeg) { +TEST(integerTest, integerOverflowingNeg) { for (int32_t i = 0; i <= 1000; i += 10) { - Int32 num = i; + int32 num = i; auto [ret, flag] = num.overflowing_neg(); ASSERT_FALSE(flag); - ASSERT_EQ(ret, Integer(-i)); + ASSERT_EQ(ret, int32(-i)); } for (int32_t i = -1000; i <= 0; i += 10) { - Int32 num = i; + int32 num = i; auto [ret, flag] = num.overflowing_neg(); ASSERT_FALSE(flag); ASSERT_EQ(ret, num.checked_abs()); } - Int32 max_num = Int32::MAX; + int32 max_num = int32::MAX; auto [ret, flag] = max_num.overflowing_neg(); ASSERT_FALSE(flag); - ASSERT_EQ(ret, Int32(-Int32::MAX)); - Int32 min_num = Int32::MIN; + ASSERT_EQ(ret, int32(-int32::MAX)); + int32 min_num = int32::MIN; std::tie(ret, flag) = min_num.overflowing_neg(); ASSERT_TRUE(flag); ASSERT_EQ(ret, min_num); } -TEST(IntegerTest, IntegerOverflowingNegNoSideEffects) { - Int32 num = 10_i32; - Int32 tmp_num = num; +TEST(integerTest, integerOverflowingNegNoSideEffects) { + int32 num = 10; + int32 tmp_num = num; ASSERT_EQ(num, tmp_num); auto _ = num.overflowing_neg(); ASSERT_EQ(num, tmp_num); } -TEST(IntegerTest, IntegerWrappingNeg) { +TEST(integerTest, integerWrappingNeg) { for (int32_t i = 0; i <= 1000; i += 10) { - Int32 num = i; - ASSERT_EQ(num.wrapping_neg(), Integer(-i)); + int32 num = i; + ASSERT_EQ(num.wrapping_neg(), int32(-i)); } for (int32_t i = -1000; i <= 0; i += 10) { - Int32 num = i; + int32 num = i; ASSERT_EQ(num.wrapping_neg(), num.checked_abs()); } - Int32 max_num = Int32::MAX; - ASSERT_EQ(max_num.wrapping_neg(), Int32(-Int32::MAX)); - Int32 min_num = Int32::MIN; + int32 max_num = int32::MAX; + ASSERT_EQ(max_num.wrapping_neg(), int32(-int32::MAX)); + int32 min_num = int32::MIN; ASSERT_EQ(min_num.wrapping_neg(), min_num); } -TEST(IntegerTest, IntegerWrappingNegNoSideEffects) { - Int32 num = 10_i32; - Int32 tmp_num = num; +TEST(integerTest, integerWrappingNegNoSideEffects) { + int32 num = 10; + int32 tmp_num = num; ASSERT_EQ(num, tmp_num); auto _ = num.wrapping_neg(); ASSERT_EQ(num, tmp_num); } -TEST(IntegerTest, IntegerSaturatingNeg) { +TEST(integerTest, integerSaturatingNeg) { for (int32_t i = 0; i <= 1000; i += 10) { - Int32 num = i; - ASSERT_EQ(num.saturating_neg(), Integer(-i)); + int32 num = i; + ASSERT_EQ(num.saturating_neg(), int32(-i)); } for (int32_t i = -1000; i <= 0; i += 10) { - Int32 num = i; + int32 num = i; ASSERT_EQ(num.saturating_neg(), num.checked_abs()); } - Int32 max_num = Int32::MAX; - ASSERT_EQ(max_num.saturating_neg(), Int32(-Int32::MAX)); - Int32 min_num = Int32::MIN; + int32 max_num = int32::MAX; + ASSERT_EQ(max_num.saturating_neg(), int32(-int32::MAX)); + int32 min_num = int32::MIN; ASSERT_EQ(min_num.saturating_neg(), max_num); } -TEST(IntegerTest, IntegerSaturatingNegNoSideEffects) { - Int32 num = 10_i32; - Int32 tmp_num = num; +TEST(integerTest, integerSaturatingNegNoSideEffects) { + int32 num = 10; + int32 tmp_num = num; ASSERT_EQ(num, tmp_num); auto _ = num.saturating_neg(); diff --git a/test/uinteger/uinteger_test.cc b/test/uinteger/uinteger_test.cc index c8279ff..bb729ca 100644 --- a/test/uinteger/uinteger_test.cc +++ b/test/uinteger/uinteger_test.cc @@ -5,34 +5,24 @@ #include "uinteger.hh" #include "test/utils.hh" -TEST(UintegerTest, UintegerTemplate) { - Uinteger test = 0; - Uinteger test1 = 0; - Uinteger test2 = 0; - Uinteger test3 = 0; - Uinteger test4 = 0; - Uinteger test5 = 0; - Uinteger test6 = 0; - Uinteger test7 = 0; - Uinteger test8 = 0; -} +using namespace numbers; TEST(UintegerTest, UintegerMinMax) { - ASSERT_THROW(-Uint64::MAX, std::runtime_error); - ASSERT_EQ(-Uint16::MIN, Uint16::MIN); - ASSERT_EQ(Uint16::MAX.saturating_add(Uint16::MAX), Uint16::MAX); + ASSERT_THROW(-uint64::MAX, std::runtime_error); + ASSERT_EQ(-uint16::MIN, uint16::MIN); + ASSERT_EQ(uint16::MAX.saturating_add(uint16::MAX), uint16::MAX); } TEST(UintegerTest, UintegerStringStream) { for (unsigned short n = 0; n <= 1000; n++) { - Uint16 num = n; + uint16 num = n; std::ostringstream stream; stream << num; ASSERT_EQ(stream.str(), std::to_string(n)); } for (unsigned long long n = 0; n < 1000; n++) { - Uint64 num = n; + uint64 num = n; std::ostringstream stream; stream << num; ASSERT_EQ(stream.str(), std::to_string(n)); @@ -40,14 +30,14 @@ TEST(UintegerTest, UintegerStringStream) { } TEST(UintegerTest, UintegerAdd) { - Uint32 num = 10_u32; - Uint32 num1 = 10_u32; + uint32 num = 10; + uint32 num1 = 10; ASSERT_EQ(num, num1); - Uint32 num2 = 20_u32; - Uint32 zero_num = 0_u32; - Uint32 max_num = Uint32::MAX; - Uint32 min_num = Uint32::MIN; + uint32 num2 = 20; + uint32 zero_num = 0; + uint32 max_num = uint32::MAX; + uint32 min_num = uint32::MIN; ASSERT_EQ(zero_num, min_num); ASSERT_EQ(num + num1, num2); @@ -56,45 +46,45 @@ TEST(UintegerTest, UintegerAdd) { } TEST(UintegerTest, UintegerAddNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); - ASSERT_EQ(num2 + num1, 20_u32); + ASSERT_EQ(num2 + num1, 20); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } TEST(UintegerTest, UintegerWrappingAdd) { for (uint16_t n = std::numeric_limits::min(); n < std::numeric_limits::max(); n++) { - Uint16 num = n; - ASSERT_EQ(num.wrapping_add(1), Uint16(n + 1)); + uint16 num = n; + ASSERT_EQ(num.wrapping_add(1), uint16(n + 1)); } - Uint16 num = Uint16::MAX; - ASSERT_EQ(num.wrapping_add(1), Uint16(Uint16::MIN)); + uint16 num = uint16::MAX; + ASSERT_EQ(num.wrapping_add(1), uint16(uint16::MIN)); } TEST(UintegerTest, UintegerWrappingAddNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); - ASSERT_EQ(num2.wrapping_add(num1), 20_u32); - ASSERT_EQ(num1.wrapping_add(num2), 20_u32); + ASSERT_EQ(num2.wrapping_add(num1), 20); + ASSERT_EQ(num1.wrapping_add(num2), 20); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } TEST(UintegerTest, UintegerAddByAccumulate) { constexpr size_t len = 100; - std::vector nums(len); + std::vector nums(len); ASSERT_EQ(nums.size(), len); - std::iota(nums.begin(), nums.end(), Uint64(1)); - ASSERT_EQ(std::accumulate(nums.begin(), nums.end(), Uint64(0)), Uint64(5050)); + std::iota(nums.begin(), nums.end(), uint64(1)); + ASSERT_EQ(std::accumulate(nums.begin(), nums.end(), uint64(0)), uint64(5050)); } TEST(UintegerTest, UintegerCheckedAdd) { @@ -103,20 +93,20 @@ TEST(UintegerTest, UintegerCheckedAdd) { for (int run = 0; run < 100; run++) { uint32_t a = random_integer(min, max); uint32_t b = random_integer(min, max); - Uint32 num_a = a; - Uint32 num_b = b; + uint32 num_a = a; + uint32 num_b = b; ASSERT_EQ(num_a.checked_add(num_b).value(), a + b); } { - auto num = 10_u16; - Uint16 num1 = Uint16::MAX; + uint16 num = 10; + uint16 num1 = uint16::MAX; ASSERT_EQ(num.checked_add(num1), std::nullopt); } { - auto num = 10_u64; - Uint64 num1 = Uint64::MAX; + uint64 num = 10; + uint64 num1 = uint64::MAX; ASSERT_EQ(num.checked_add(num1), std::nullopt); } @@ -129,14 +119,14 @@ TEST(UintegerTest, UintegerCheckedAdd) { } TEST(UintegerTest, UintegerCheckedAddNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); - ASSERT_EQ(num2.checked_add(num1).value(), 20_u32); - ASSERT_EQ(num1.checked_add(num2).value(), 20_u32); + ASSERT_EQ(num2.checked_add(num1).value(), 20); + ASSERT_EQ(num1.checked_add(num2).value(), 20); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } @@ -147,15 +137,15 @@ TEST(UintegerTest, UintegerOverflowingAdd) { for (int run = 0; run < 100; run++) { uint32_t a = random_integer(min, max); uint32_t b = random_integer(min, max); - Uint32 num_a = a; - Uint32 num_b = b; + uint32 num_a = a; + uint32 num_b = b; auto [ret, flag] = num_a.overflowing_add(num_b); ASSERT_FALSE(flag); ASSERT_EQ(ret, a + b); } { - auto num = 10_u16; + uint16 num = 10; Uinteger num1 = Uinteger::MAX; auto [ret, flag] = num.overflowing_add(num1); ASSERT_TRUE(flag); @@ -163,8 +153,8 @@ TEST(UintegerTest, UintegerOverflowingAdd) { } { - auto num = 10_u64; - Uint64 num1 = Uint64::MAX; + uint64 num = 10; + uint64 num1 = uint64::MAX; auto [_, flag] = num.overflowing_add(num1); ASSERT_TRUE(flag); } @@ -184,14 +174,14 @@ TEST(UintegerTest, UintegerOverflowingAdd) { } TEST(UintegerTest, UintegerOverflowingAddNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto [ret, flag] = num2.overflowing_add(num1); - ASSERT_EQ(ret, 20_u32); + ASSERT_EQ(ret, 20); ASSERT_FALSE(flag); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); @@ -203,69 +193,69 @@ TEST(UintegerTest, UintegerSaturatingAdd) { for (int run = 0; run < 100; run++) { uint32_t a = random_integer(min, max); uint32_t b = random_integer(min, max); - Uint32 num_a = a; - Uint32 num_b = b; + uint32 num_a = a; + uint32 num_b = b; auto ret = num_a.saturating_add(num_b); ASSERT_EQ(ret, a + b); } for (uint8_t i = 1; i < std::numeric_limits::max(); i++) { - Uint8 num = Uint8::MAX; - auto ret = num.saturating_add(Uint8(i)); - ASSERT_EQ(ret, Uint8(Uint8::MAX)); + uint8 num = uint8::MAX; + auto ret = num.saturating_add(uint8(i)); + ASSERT_EQ(ret, uint8(uint8::MAX)); } { - Uint64 max_num = Uint64::MAX; + uint64 max_num = uint64::MAX; auto ret = max_num.saturating_add(max_num); - ASSERT_EQ(ret, Uint64::MAX); + ASSERT_EQ(ret, uint64::MAX); - Uint64 min_num = Uint64::MIN; + uint64 min_num = uint64::MIN; ret = min_num.saturating_add(min_num); - ASSERT_EQ(ret, Uint64::MIN); + ASSERT_EQ(ret, uint64::MIN); } } TEST(UintegerTest, UintegerSaturatingAddNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2.saturating_add(num1); - ASSERT_EQ(ret, 20_u32); + ASSERT_EQ(ret, 20); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } TEST(UintegerTest, UintegerAddOverflow) { { - auto num = 10_u16; - Uint16 num1 = Uint16::MAX; + uint16 num = 10; + uint16 num1 = uint16::MAX; ASSERT_THROW(num + num1, std::runtime_error); } { - auto num = 10_u64; - Uint64 num1 = Uint64::MAX; + uint64 num = 10; + uint64 num1 = uint64::MAX; ASSERT_THROW(num + num1, std::runtime_error); } } TEST(UintegerTest, UintegerAddOverflowByAccumulate) { - std::vector nums(10); + std::vector nums(10); ASSERT_EQ(nums.size(), 10); - std::iota(nums.begin(), nums.end(), Uint64(Uint64::MAX - 11)); - ASSERT_THROW(std::accumulate(nums.begin(), nums.end(), 0_u64), std::runtime_error); + std::iota(nums.begin(), nums.end(), uint64(uint64::MAX - 11)); + ASSERT_THROW(std::accumulate(nums.begin(), nums.end(), uint64(0)), std::runtime_error); } TEST(UintegerTest, UintegerSub) { - auto num = 10_u32; - auto num1 = 10_u32; - auto num2 = 20_u32; - Uint32 num3 = Uint32::MAX; - auto num4 = 0_u32; + auto num = 10; + auto num1 = 10; + auto num2 = 20; + uint32 num3 = uint32::MAX; + auto num4 = 0; ASSERT_EQ(num2 - num1, num); ASSERT_EQ(num - num1, num4); @@ -277,70 +267,70 @@ TEST(UintegerTest, UintegerSub) { for (int run = 0; run < 100; run++) { uint32_t a = random_integer(min, max) + max; uint32_t b = random_integer(min, max); - Uint32 num_a = a; - Uint32 num_b = b; - ASSERT_EQ(Uint32(a - b), num_a - num_b); + uint32 num_a = a; + uint32 num_b = b; + ASSERT_EQ(uint32(a - b), num_a - num_b); ASSERT_EQ(num_a - num_b, a - b); } } TEST(UintegerTest, UintegerSubNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); - ASSERT_EQ(num2 - num1, 0_u32); + ASSERT_EQ(num2 - num1, 0); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } TEST(UintegerTest, UintegerSubOverflow) { for (short n = 1; n < 1000; n++) { - Uint16 num = n; - Uint16 num1 = Uint16::MIN; + uint16 num = n; + uint16 num1 = uint16::MIN; ASSERT_THROW(num1 - num, std::runtime_error); } } TEST(UintegerTest, UintegerCheckedSub) { for (short n = 1; n < 1000; n++) { - Uint16 num = n; - Uint16 num1 = Uint16::MIN; + uint16 num = n; + uint16 num1 = uint16::MIN; ASSERT_EQ(num1.checked_sub(num), std::nullopt); } for (uint16_t n = std::numeric_limits::max(); n > std::numeric_limits::min(); n--) { - Uint16 num = n; + uint16 num = n; auto ret = num.checked_sub(1); - ASSERT_EQ(ret.value(), Uint16(n - 1)); + ASSERT_EQ(ret.value(), uint16(n - 1)); } } TEST(UintegerTest, UintegerCheckedSubNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); - ASSERT_EQ(num2.checked_sub(num1).value(), 0_u32); + ASSERT_EQ(num2.checked_sub(num1).value(), 0); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } TEST(UintegerTest, UintegerOverflowingSub) { for (unsigned short n = 1; n < 1000; n++) { - Uint16 num = n; - Uint16 num1 = Uint16::MIN; + uint16 num = n; + uint16 num1 = uint16::MIN; auto [ret, flag] = num1.overflowing_sub(num); ASSERT_TRUE(flag); ASSERT_EQ(ret, std::numeric_limits::min() - n); } for (uint16_t n = std::numeric_limits::max(); n > std::numeric_limits::min(); n--) { - Uint16 num = n; + uint16 num = n; auto [ret, flag] = num.overflowing_sub(1); ASSERT_FALSE(flag); ASSERT_EQ(ret, n - 1); @@ -348,14 +338,14 @@ TEST(UintegerTest, UintegerOverflowingSub) { } TEST(UintegerTest, UintegerOverflowingSubNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto [ret, flag] = num2.overflowing_sub(num1); - ASSERT_EQ(ret, 0_u32); + ASSERT_EQ(ret, 0); ASSERT_FALSE(flag); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); @@ -363,70 +353,70 @@ TEST(UintegerTest, UintegerOverflowingSubNoSideEffects) { TEST(UintegerTest, UintegerWrappingSub) { for (uint16_t n = std::numeric_limits::max(); n > std::numeric_limits::min(); n--) { - Uint16 num = n; - ASSERT_EQ(num.wrapping_sub(1), Uint16(n - 1)); + uint16 num = n; + ASSERT_EQ(num.wrapping_sub(1), uint16(n - 1)); } - Uint16 num = Uint16::MIN; - ASSERT_EQ(num.wrapping_sub(1), Uint16(Uint16::MAX)); + uint16 num = uint16::MIN; + ASSERT_EQ(num.wrapping_sub(1), uint16(uint16::MAX)); } TEST(UintegerTest, UintegerWarppingSubNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2.wrapping_sub(num1); - ASSERT_EQ(ret, 0_u32); + ASSERT_EQ(ret, 0); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } TEST(UintegerTest, UintegerSaturatingSub) { for (unsigned short n = 1; n <= 1000; n++) { - Uint16 num = n; - Uint16 num1 = Uint16::MIN; + uint16 num = n; + uint16 num1 = uint16::MIN; auto ret = num1.saturating_sub(num); - ASSERT_EQ(ret, Uint16::MIN); + ASSERT_EQ(ret, uint16::MIN); } for (uint16_t n = std::numeric_limits::max(); n > std::numeric_limits::min(); n--) { - Uint16 num = n; + uint16 num = n; auto ret = num.saturating_sub(1); - ASSERT_EQ(ret, Uint16(n - 1)); + ASSERT_EQ(ret, uint16(n - 1)); } - Uint16 max_num = Uint16::MAX; - Uint16 min_num = Uint16::MIN; + uint16 max_num = uint16::MAX; + uint16 min_num = uint16::MIN; auto ret = max_num.saturating_sub(min_num); - ASSERT_EQ(ret, Uint16::MAX); + ASSERT_EQ(ret, uint16::MAX); ret = min_num.saturating_sub(max_num); - ASSERT_EQ(ret, Uint16::MIN); + ASSERT_EQ(ret, uint16::MIN); } TEST(UintegerTest, UintegerSaturatingSubNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2.saturating_sub(num1); - ASSERT_EQ(ret, 0_u32); + ASSERT_EQ(ret, 0); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } TEST(UintegerTest, UintegerMul) { - auto num = 10_u64; - auto num1 = 100_u64; + uint64 num = 10; + uint64 num1 = 100; ASSERT_EQ(num1 * num, 1000); - num = num.MAX; - num1 = 0_u64; + num = uint64::MAX; + num1 = 0; ASSERT_EQ(num1 * num, 0); - num1 = 1_u64; + num1 = 1; ASSERT_EQ(num1 * num, num); constexpr uint32_t min = 10; @@ -434,29 +424,29 @@ TEST(UintegerTest, UintegerMul) { for (int run = 0; run < 100; run++) { uint32_t a = random_integer(min, max); uint32_t b = random_integer(min, max); - Uint32 num_a = a; - Uint32 num_b = b; - ASSERT_EQ(Uint32(a * b), num_a * num_b); + uint32 num_a = a; + uint32 num_b = b; + ASSERT_EQ(uint32(a * b), num_a * num_b); ASSERT_EQ(num_a * num_b, a * b); } } TEST(UintegerTest, UintegerMulNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2 * num1; - ASSERT_EQ(ret, 100_u32); + ASSERT_EQ(ret, 100); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } TEST(UintegerTest, UintegerCheckedMul) { - auto num = 10_u8; - auto num1 = 100_u8; + uint8 num = 10; + uint8 num1 = 100; ASSERT_EQ(num1.checked_mul(num), std::nullopt); constexpr uint32_t min = 10; @@ -464,28 +454,28 @@ TEST(UintegerTest, UintegerCheckedMul) { for (int run = 0; run < 100; run++) { uint32_t a = random_integer(min, max); uint32_t b = random_integer(min, max); - Uint32 num_a = a; - Uint32 num_b = b; + uint32 num_a = a; + uint32 num_b = b; ASSERT_EQ(num_a.checked_mul(num_b).value(), a * b); } } TEST(UintegerTest, UintegerCheckedMulNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2.checked_mul(num1); - ASSERT_EQ(ret.value(), 100_u32); + ASSERT_EQ(ret.value(), 100); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } TEST(UintegerTest, UintegerOverflowingMul) { - auto num = 10_u8; - auto num1 = 100_u8; + uint8 num = 10; + uint8 num1 = 100; auto [ret, flag] = num1.overflowing_mul(num); ASSERT_EQ(ret, uint8_t(10 * 100)); ASSERT_TRUE(flag); @@ -495,8 +485,8 @@ TEST(UintegerTest, UintegerOverflowingMul) { for (int run = 0; run < 100; run++) { uint32_t a = random_integer(min, max); uint32_t b = random_integer(min, max); - Uint32 num_a = a; - Uint32 num_b = b; + uint32 num_a = a; + uint32 num_b = b; auto [ret, flag] = num_a.overflowing_mul(num_b); ASSERT_FALSE(flag); ASSERT_EQ(ret, a * b); @@ -504,28 +494,28 @@ TEST(UintegerTest, UintegerOverflowingMul) { } TEST(UntegerTest, UintegerOverflowingMulNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto [ret, flag] = num2.overflowing_mul(num1); ASSERT_FALSE(flag); - ASSERT_EQ(ret, 100_u32); + ASSERT_EQ(ret, 100); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } TEST(UintegerTest, UintegerSaturatingMul) { - auto num = 10_u8; - auto num1 = 100_u8; - auto ret = num1.saturating_mul(num); - ASSERT_EQ(ret, Uint8::MAX); - - Uint16 max_num = Uint16::MAX; - ASSERT_EQ(max_num.saturating_mul(max_num), Uint16::MAX); - Uint16 min_num = Uint16::MIN; + uint8 num = 10; + uint8 num1 = 100; + uint8 ret = num1.saturating_mul(num); + ASSERT_EQ(ret, uint8::MAX); + + uint16 max_num = uint16::MAX; + ASSERT_EQ(max_num.saturating_mul(max_num), uint16::MAX); + uint16 min_num = uint16::MIN; ASSERT_EQ(min_num.saturating_mul(min_num), min_num); constexpr uint32_t min = 10; @@ -533,22 +523,22 @@ TEST(UintegerTest, UintegerSaturatingMul) { for (int run = 0; run < 100; run++) { uint32_t a = random_integer(min, max); uint32_t b = random_integer(min, max); - Uint32 num_a = a; - Uint32 num_b = b; + uint32 num_a = a; + uint32 num_b = b; auto ret = num_a.saturating_mul(num_b); ASSERT_EQ(ret, a * b); } } TEST(UintegerTest, UintegerSaturatingMulNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2.saturating_mul(num1); - ASSERT_EQ(ret, 100_u32); + ASSERT_EQ(ret, 100); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } @@ -559,41 +549,41 @@ TEST(UintegerTest, UintegerDiv) { for (int run = 0; run < 100; run++) { uint32_t a = random_integer(min, max); uint32_t b = random_integer(min, max); - Uint32 num_a = a; - Uint32 num_b = b; - ASSERT_EQ(Uint32(a / b), num_a / num_b); + uint32 num_a = a; + uint32 num_b = b; + ASSERT_EQ(uint32(a / b), num_a / num_b); ASSERT_EQ(num_a / num_b, a / b); } } TEST(UintegerTest, UintegerDivNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2 / num1; - ASSERT_EQ(ret, 1_u32); + ASSERT_EQ(ret, 1); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } TEST(UintegerTest, UintegerWrappingDiv) { - Uint8 num = 10_u8; - Uint8 num1 = Uint8::MIN; + uint8 num = 10; + uint8 num1 = uint8::MIN; ASSERT_EQ(num1.wrapping_div(num), num1); } TEST(UintegerTest, UintegerWrappingDivNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2.wrapping_div(num1); - ASSERT_EQ(ret, Uint32(1)); + ASSERT_EQ(ret, uint32(1)); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } @@ -604,21 +594,21 @@ TEST(UintegerTest, UintegerCheckedDiv) { for (int run = 0; run < 100; run++) { uint32_t a = random_integer(min, max); uint32_t b = random_integer(min, max); - Uint32 num_a = a; - Uint32 num_b = b; + uint32 num_a = a; + uint32 num_b = b; EXPECT_EQ(num_a.checked_div(num_b).value(), a / b); } } TEST(UintegerTest, UintegerCheckedDivNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); auto ret = num2.checked_div(num1); - ASSERT_EQ(ret.value(), 1_u32); + ASSERT_EQ(ret.value(), 1); ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); } @@ -629,8 +619,8 @@ TEST(UintegerTest, UintegerOverflowingDiv) { for (int run = 0; run < 100; run++) { uint32_t a = random_integer(min, max); uint32_t b = random_integer(min, max); - Uint32 num_a = a; - Uint32 num_b = b; + uint32 num_a = a; + uint32 num_b = b; auto [ret, flag] = num_a.overflowing_div(num_b); ASSERT_FALSE(flag); EXPECT_EQ(ret, a / b); @@ -638,9 +628,9 @@ TEST(UintegerTest, UintegerOverflowingDiv) { } TEST(UintegerTest, UintegerOverflowingDivNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); @@ -656,17 +646,17 @@ TEST(UintegerTest, UintegerSaturatingDiv) { for (int run = 0; run < 100; run++) { uint32_t a = random_integer(min, max); uint32_t b = random_integer(min, max); - Uint32 num_a = a; - Uint32 num_b = b; + uint32 num_a = a; + uint32 num_b = b; auto ret = num_a.saturating_div(num_b); EXPECT_EQ(ret, a / b); } } TEST(UintegerTest, UintegerSaturatingDivNoSideEffects) { - Uint32 num = 10_u32; - Uint32 num1 = num; - Uint32 num2 = num; + uint32 num = 10; + uint32 num1 = num; + uint32 num2 = num; ASSERT_EQ(num1, num); ASSERT_EQ(num2, num); @@ -676,7 +666,7 @@ TEST(UintegerTest, UintegerSaturatingDivNoSideEffects) { } TEST(UintegerTest, UintegerNeg) { - Uint32 num = 0; + uint32 num = 0; ASSERT_EQ(-num, num); for (uint32_t i = 1; i <= 1000; i += 10) { @@ -686,8 +676,8 @@ TEST(UintegerTest, UintegerNeg) { } TEST(UintegerTest, UintegerNegNoSideEffects) { - Uint32 num = 10_u32; - Uint32 tmp_num = num; + uint32 num = 10; + uint32 tmp_num = num; ASSERT_EQ(num, tmp_num); ASSERT_THROW(-num, std::runtime_error); @@ -696,17 +686,17 @@ TEST(UintegerTest, UintegerNegNoSideEffects) { TEST(UintegerTest, UintegerCheckedNeg) { for (uint32_t i = 1; i <= 1000; i += 10) { - Uint32 num = i; + uint32 num = i; ASSERT_EQ(num.checked_neg(), std::nullopt); } - Uint32 min_num = Uint32::MIN; + uint32 min_num = uint32::MIN; ASSERT_EQ(min_num.checked_neg(), min_num); } TEST(UintegerTest, UintegerCheckedNegNoSideEffects) { - Uint32 num = 10_u32; - Uint32 tmp_num = num; + uint32 num = 10; + uint32 tmp_num = num; ASSERT_EQ(num, tmp_num); auto _ = num.checked_neg(); @@ -715,25 +705,25 @@ TEST(UintegerTest, UintegerCheckedNegNoSideEffects) { TEST(UintegerTest, UintegerOverflowingNeg) { for (uint32_t i = 10; i <= 1000; i += 10) { - Uint32 num = i; + uint32 num = i; auto [ret, flag] = num.overflowing_neg(); ASSERT_TRUE(flag); ASSERT_EQ(ret, -i); } - Uint32 max_num = Uint32::MAX; + uint32 max_num = uint32::MAX; auto [ret, flag] = max_num.overflowing_neg(); ASSERT_TRUE(flag); ASSERT_EQ(ret, -std::numeric_limits::max()); - Uint32 min_num = Uint32::MIN; + uint32 min_num = uint32::MIN; std::tie(ret, flag) = min_num.overflowing_neg(); ASSERT_FALSE(flag); ASSERT_EQ(ret, min_num); } TEST(UintegerTest, UintegerOverflowingNegNoSideEffects) { - Uint32 num = 10_u32; - Uint32 tmp_num = num; + uint32 num = 10; + uint32 tmp_num = num; ASSERT_EQ(num, tmp_num); auto _ = num.overflowing_neg(); @@ -742,19 +732,19 @@ TEST(UintegerTest, UintegerOverflowingNegNoSideEffects) { TEST(UintegerTest, UintegerWrappingNeg) { for (uint32_t i = 10; i <= 1000; i += 10) { - Uint32 num = i; + uint32 num = i; ASSERT_EQ(num.wrapping_neg(), -i); } - Uint32 max_num = Uint32::MAX; + uint32 max_num = uint32::MAX; ASSERT_EQ(max_num.wrapping_neg(), -std::numeric_limits::max()); - Uint32 min_num = Uint32::MIN; + uint32 min_num = uint32::MIN; ASSERT_EQ(min_num.wrapping_neg(), min_num); } TEST(UintegerTest, UintegerWrappingNegNoSideEffects) { - Uint32 num = 10_u32; - Uint32 tmp_num = num; + uint32 num = 10; + uint32 tmp_num = num; ASSERT_EQ(num, tmp_num); auto _ = num.wrapping_neg();