@@ -21,10 +21,41 @@ TEST_CASE("Casting arby::Nat with value higher than UINT_MAX to uintmax_t throws
2121 CHECK_THROWS_AS ((uintmax_t )value, std::range_error);
2222}
2323
24- TEST_CASE (" Casting arby::Nat to long double " , " [casting]" ) {
24+ TEMPLATE_TEST_CASE (" Casting arby::Nat to various floating-point types " , " [casting]" , float , double , long double ) {
2525 auto value = GENERATE (take (1000 , random ((uintmax_t )0 , std::numeric_limits<uintmax_t >::max ())));
2626
27- CHECK ((long double )arby::Nat (value) == (long double )value);
27+ // float is so imprecise for large values that we need to cast the input too
28+ CHECK ((TestType)arby::Nat::from_float ((TestType)value) == (TestType)value);
29+ }
30+
31+ TEMPLATE_TEST_CASE (
32+ " Casting arby::Nat with value higher than max to other type throws range_error" , " [casting]" ,
33+ std::uint8_t , std::int8_t , std::uint16_t , std::int16_t , std::uint32_t , std::int32_t
34+ ) {
35+ arby::Nat value = arby::Nat ((uintmax_t )std::numeric_limits<TestType>::max ()) + 1 ;
36+
37+ CHECK_THROWS_AS ((TestType)value, std::range_error);
38+ }
39+
40+ TEST_CASE (" Casting arby::Nat to other type - uint8_t" , " [casting]" ,) {
41+ auto value = GENERATE (take (1000 , random ((std::uint16_t )0 , (std::uint16_t )std::numeric_limits<std::uint8_t >::max ())));
42+
43+ CHECK ((std::uint8_t )arby::Nat ((uintmax_t )value) == (std::uint8_t )value);
44+ }
45+
46+ TEST_CASE (" Casting arby::Nat to other type - int8_t" , " [casting]" ,) {
47+ auto value = GENERATE (take (1000 , random ((std::int16_t )0 , (std::int16_t )std::numeric_limits<std::int8_t >::max ())));
48+
49+ CHECK ((std::int8_t )arby::Nat ((uintmax_t )value) == (std::int8_t )value);
50+ }
51+
52+ TEMPLATE_TEST_CASE (
53+ " Casting arby::Nat to other type" , " [casting]" ,
54+ std::uint16_t , std::int16_t , std::uint32_t , std::int32_t
55+ ) {
56+ auto value = GENERATE (take (1000 , random ((TestType)0 , std::numeric_limits<TestType>::max ())));
57+
58+ CHECK ((TestType)arby::Nat ((uintmax_t )value) == (TestType)value);
2859}
2960
3061TEST_CASE (" arby::Nat::from_float() with negative value throws std::domain_error" ) {
0 commit comments