aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven Göthel <[email protected]>2024-03-02 21:30:11 +0100
committerSven Göthel <[email protected]>2024-03-02 21:30:11 +0100
commit09ae42f5add46dbf15808c6d410ce19597d35ac6 (patch)
tree893f5f52dae1ea54567852e01fa96ab8ba0d0ac5
parent373d83e413508c5a66535ff68a0e1b7546f8763d (diff)
big_int_t: Fix & test free math function wrapper (min, max, clamp ..)
-rw-r--r--include/jau/mp/big_int_t.hpp21
-rw-r--r--test/test_big_int01.hpp62
2 files changed, 57 insertions, 26 deletions
diff --git a/include/jau/mp/big_int_t.hpp b/include/jau/mp/big_int_t.hpp
index 81aa8a4..9d91311 100644
--- a/include/jau/mp/big_int_t.hpp
+++ b/include/jau/mp/big_int_t.hpp
@@ -1557,27 +1557,26 @@ namespace jau {
inline mp::big_int_t abs(mp::big_int_t x) noexcept { return x.abs(); }
inline mp::big_int_t pow(mp::big_int_t b, mp::big_int_t e) { return b.pow(e); }
- constexpr const mp::big_int_t& min(const mp::big_int_t& x, const mp::big_int_t& y) noexcept {
+ inline const mp::big_int_t& min(const mp::big_int_t& x, const mp::big_int_t& y) noexcept {
return x < y ? x : y;
}
- constexpr const mp::big_int_t& max(const mp::big_int_t& x, const mp::big_int_t& y) noexcept {
+ inline const mp::big_int_t& max(const mp::big_int_t& x, const mp::big_int_t& y) noexcept {
return x > y ? x : y;
}
- constexpr const mp::big_int_t& clamp(const mp::big_int_t& x, const mp::big_int_t& min_val, const mp::big_int_t& max_val) noexcept {
+ inline const mp::big_int_t& clamp(const mp::big_int_t& x, const mp::big_int_t& min_val, const mp::big_int_t& max_val) noexcept {
return min(max(x, min_val), max_val);
}
- constexpr mp::big_int_t& min(mp::big_int_t& x, mp::big_int_t& y) noexcept {
+ inline mp::big_int_t& min(mp::big_int_t& x, mp::big_int_t& y) noexcept {
return x < y ? x : y;
}
- constexpr mp::big_int_t max(mp::big_int_t& x, mp::big_int_t& y) noexcept {
+ inline mp::big_int_t& max(mp::big_int_t& x, mp::big_int_t& y) noexcept {
return x > y ? x : y;
}
- constexpr mp::big_int_t& clamp(mp::big_int_t& x, mp::big_int_t& min_val, mp::big_int_t& max_val) noexcept {
+ inline mp::big_int_t& clamp(mp::big_int_t& x, mp::big_int_t& min_val, mp::big_int_t& max_val) noexcept {
return min(max(x, min_val), max_val);
}
-
inline mp::big_int_t gcd(const mp::big_int_t& a, const mp::big_int_t& b) noexcept {
mp::big_int_t a_ = abs(a);
mp::big_int_t b_ = abs(b);
@@ -1589,9 +1588,11 @@ namespace jau {
return a_;
}
- std::ostream& operator<<(std::ostream& out, const mp::big_int_t& v) {
+ /**@}*/
+}
+
+namespace std {
+ inline std::ostream& operator<<(std::ostream& out, const jau::mp::big_int_t& v) {
return out << v.to_dec_string();
}
-
- /**@}*/
}
diff --git a/test/test_big_int01.hpp b/test/test_big_int01.hpp
index 0aeba20..f268634 100644
--- a/test/test_big_int01.hpp
+++ b/test/test_big_int01.hpp
@@ -113,6 +113,30 @@ TEST_CASE( "MP Big Int Test 01", "[big_int_t][arithmetic][math]" ) {
std::cout << "big_int a*b:: " << ab.to_dec_string(true) << std::endl;
std::cout << "big_int a*b:: " << ab.to_hex_string(true) << std::endl;
}
+ {
+ big_int_t zero, ten(10), thirty(30), forty(40);
+ REQUIRE( zero < ten );
+ REQUIRE( zero < thirty );
+ REQUIRE( ten < thirty );
+
+ REQUIRE( ten > zero );
+ REQUIRE( thirty > ten );
+ REQUIRE( thirty > zero );
+
+ REQUIRE( ten <= ten );
+ REQUIRE( ten <= thirty );
+
+ REQUIRE( thirty >= thirty );
+ REQUIRE( thirty >= ten );
+
+ REQUIRE( thirty == thirty );
+ REQUIRE( thirty != ten );
+
+ REQUIRE( zero == min(zero, ten) );
+ REQUIRE( ten == max(zero, ten) );
+ REQUIRE( ten == clamp(zero, ten, thirty) );
+ REQUIRE( thirty == clamp(forty, ten, thirty) );
+ }
}
TEST_CASE( "MP Big Int Test 02", "[big_int_t][arithmetic][math]" ) {
REQUIRE( big_int_t(10) == big_int_t( 5) + big_int_t(5) );
@@ -135,13 +159,15 @@ TEST_CASE( "MP Big Int Dec Test 10", "[big_int_t][inout][math]" ) {
};
big_int_t a(a_u8, sizeof(a_u8), lb_endian::little);
- std::cout << "big_int a:: " << a.to_dec_string(true) << std::endl;
- std::cout << "big_int a:: " << a.to_hex_string(true) << std::endl;
+ std::cout << "big_int zero:: " << a.to_dec_string(true) << std::endl;
+ std::cout << "big_int zero:: " << a.to_hex_string(true) << std::endl;
REQUIRE( 23 == sizeof(a_u8) );
- for(size_t i=0; i<sizeof(a_u8); ++i) {
- std::string s1;
- jau::byteHexString(s1, a.byte_at(i), true);
- std::cout << "a.buf[" << std::to_string(i) << "]: 0x" << s1 << std::endl;
+ if ( false ) {
+ for(size_t i=0; i<sizeof(a_u8); ++i) {
+ std::string s1;
+ jau::byteHexString(s1, a.byte_at(i), true);
+ std::cout << "zero.buf[" << std::to_string(i) << "]: 0x" << s1 << std::endl;
+ }
}
REQUIRE( sizeof(a_u8)*8 == a.bits() );
REQUIRE( sizeof(a_u8) == a.bytes() );
@@ -152,10 +178,12 @@ TEST_CASE( "MP Big Int Dec Test 10", "[big_int_t][inout][math]" ) {
buf[i] = 0;
}
REQUIRE( sizeof(a_u8) == a.binary_encode(buf, sizeof(buf), jau::lb_endian::little) );
- for(size_t i=0; i<sizeof(a_u8); ++i) {
- std::string s1;
- jau::byteHexString(s1, buf[i], true);
- std::cout << "le.buf[" << std::to_string(i) << "]: 0x" << s1 << std::endl;
+ if ( false ) {
+ for(size_t i=0; i<sizeof(a_u8); ++i) {
+ std::string s1;
+ jau::byteHexString(s1, buf[i], true);
+ std::cout << "le.buf[" << std::to_string(i) << "]: 0x" << s1 << std::endl;
+ }
}
for(size_t i=0; i<sizeof(a_u8); ++i) {
REQUIRE( (uint32_t)a_u8[i] == (uint32_t)buf[i] );
@@ -170,10 +198,12 @@ TEST_CASE( "MP Big Int Dec Test 10", "[big_int_t][inout][math]" ) {
buf[i] = 0;
}
REQUIRE( sizeof(a_u8) == a.binary_encode(buf, sizeof(buf), jau::lb_endian::big) );
- for(size_t i=0; i<sizeof(a_u8); ++i) {
- std::string s1;
- jau::byteHexString(s1, buf[i], true);
- std::cout << "be.buf[" << std::to_string(i) << "]: 0x" << s1 << std::endl;
+ if ( false ) {
+ for(size_t i=0; i<sizeof(a_u8); ++i) {
+ std::string s1;
+ jau::byteHexString(s1, buf[i], true);
+ std::cout << "be.buf[" << std::to_string(i) << "]: 0x" << s1 << std::endl;
+ }
}
for(size_t i=0; i<sizeof(a_u8); ++i) {
REQUIRE( (uint32_t)a_u8[sizeof(a_u8)-i-1] == (uint32_t)buf[i] );
@@ -193,8 +223,8 @@ TEST_CASE( "MP Big Int Dec Test 11", "[big_int_t][inout][math]" ) {
};
big_int_t a("0xffeeddccbbaa998877665544332211fedcba9876543210");
- std::cout << "big_int a:: " << a.to_dec_string(true) << std::endl;
- std::cout << "big_int a:: " << a.to_hex_string(true) << std::endl;
+ std::cout << "big_int zero:: " << a.to_dec_string(true) << std::endl;
+ std::cout << "big_int zero:: " << a.to_hex_string(true) << std::endl;
REQUIRE( 23 == sizeof(a_u8) );
REQUIRE( sizeof(a_u8)*8 == a.bits() );
REQUIRE( sizeof(a_u8) == a.bytes() );