diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-11-28 23:53:55 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-12-20 15:35:58 +0100 |
commit | 27c3969e69f5525ccb0bc462a615a14142b5f178 (patch) | |
tree | a11ebc28cb6c2609d5e04913f63d5c1ba1181e0e /asl/tests | |
parent | 3dc9bc3a6cefa30c553c6ec21b1545db98e26b6d (diff) |
Add float utilities, & float formatting with dragonbox
Diffstat (limited to 'asl/tests')
-rw-r--r-- | asl/tests/float_tests.cpp | 23 | ||||
-rw-r--r-- | asl/tests/format_tests.cpp | 27 | ||||
-rw-r--r-- | asl/tests/meta_tests.cpp | 6 | ||||
-rw-r--r-- | asl/tests/string_view_tests.cpp | 32 |
4 files changed, 85 insertions, 3 deletions
diff --git a/asl/tests/float_tests.cpp b/asl/tests/float_tests.cpp new file mode 100644 index 0000000..981279f --- /dev/null +++ b/asl/tests/float_tests.cpp @@ -0,0 +1,23 @@ +#include <asl/float.hpp>
+
+#include <asl/testing/testing.hpp>
+
+ASL_TEST(is_infinity)
+{
+ ASL_TEST_EXPECT(!asl::is_infinity(0.0F));
+ ASL_TEST_EXPECT(!asl::is_infinity(-25.0F));
+ ASL_TEST_EXPECT(asl::is_infinity(45.0F / 0.0F));
+ ASL_TEST_EXPECT(asl::is_infinity(-45.0F / 0.0F));
+ ASL_TEST_EXPECT(asl::is_infinity(asl::infinity<float>()));
+ ASL_TEST_EXPECT(asl::is_infinity(-asl::infinity<double>()));
+}
+
+ASL_TEST(is_nan)
+{
+ ASL_TEST_EXPECT(!asl::is_nan(0.0F));
+ ASL_TEST_EXPECT(!asl::is_nan(-25.0F));
+ ASL_TEST_EXPECT(!asl::is_nan(45.0F / 0.0F));
+ ASL_TEST_EXPECT(asl::is_nan(asl::nan<float>()));
+ ASL_TEST_EXPECT(asl::is_nan(asl::nan<double>()));
+}
+
diff --git a/asl/tests/format_tests.cpp b/asl/tests/format_tests.cpp index dd3db1c..906fce6 100644 --- a/asl/tests/format_tests.cpp +++ b/asl/tests/format_tests.cpp @@ -1,6 +1,7 @@ #include "asl/format.hpp"
#include "asl/testing/testing.hpp"
#include "asl/allocator.hpp"
+#include "asl/float.hpp"
static_assert(asl::formattable<decltype("Hello")>);
@@ -110,11 +111,31 @@ ASL_TEST(format_floats) sink.reset();
asl::format(&sink, "{} {} {}", 0.0F, 1.0, 2.0F);
- ASL_TEST_EXPECT(sink.str() == "0.000000 1.000000 2.000000"_sv);
+ ASL_TEST_EXPECT(sink.str() == "0 1 2"_sv);
+
+ sink.reset();
+ asl::format(&sink, "{} {} {}", 0.1F, 0.001F, 0.123F);
+ ASL_TEST_EXPECT(sink.str() == "0.1 0.001 0.123"_sv);
+
+ sink.reset();
+ asl::format(&sink, "{} {}", 1.25F, -22.3);
+ ASL_TEST_EXPECT(sink.str() == "1.25 -22.3"_sv);
+
+ sink.reset();
+ asl::format(&sink, "{}", 1e32);
+ ASL_TEST_EXPECT(sink.str() == "100000000000000000000000000000000"_sv);
+
+ sink.reset();
+ asl::format(&sink, "{}", 123e-8);
+ ASL_TEST_EXPECT(sink.str() == "0.00000123"_sv);
+
+ sink.reset();
+ asl::format(&sink, "{} {}", asl::infinity<float>(), -asl::infinity<double>());
+ ASL_TEST_EXPECT(sink.str() == "Infinity -Infinity"_sv);
sink.reset();
- asl::format(&sink, "{} {}", 10.25F, -22.3);
- ASL_TEST_EXPECT(sink.str() == "10.250000 -22.300000"_sv);
+ asl::format(&sink, "{}", asl::nan<float>());
+ ASL_TEST_EXPECT(sink.str() == "NaN"_sv);
}
ASL_TEST(format_boolean)
diff --git a/asl/tests/meta_tests.cpp b/asl/tests/meta_tests.cpp index 354cea6..fcafe1c 100644 --- a/asl/tests/meta_tests.cpp +++ b/asl/tests/meta_tests.cpp @@ -203,3 +203,9 @@ static_assert(asl::is_const<const int>); static_assert(!asl::is_const<const int*>);
static_assert(asl::is_const<int* const>);
+static_assert(asl::is_floating_point<float>);
+static_assert(asl::is_floating_point<const float>);
+static_assert(asl::is_floating_point<volatile double>);
+static_assert(!asl::is_floating_point<const float&>);
+static_assert(!asl::is_floating_point<int>);
+static_assert(!asl::is_floating_point<C>);
diff --git a/asl/tests/string_view_tests.cpp b/asl/tests/string_view_tests.cpp index 1340247..708877d 100644 --- a/asl/tests/string_view_tests.cpp +++ b/asl/tests/string_view_tests.cpp @@ -58,6 +58,38 @@ ASL_TEST(substr2) ASL_TEST_ASSERT(s2.size() == 0); } +ASL_TEST(first) +{ + asl::string_view s1 = "abcd"; + + asl::string_view s2 = s1.first(0); + ASL_TEST_ASSERT(s2.size() == 0); + + s2 = s1.first(2); + ASL_TEST_ASSERT(s2.size() == 2); + ASL_TEST_EXPECT(asl::memcmp(s2.data(), "ab", 2) == 0); + + s2 = s1.first(4); + ASL_TEST_ASSERT(s2.size() == 4); + ASL_TEST_EXPECT(asl::memcmp(s2.data(), "abcd", 4) == 0); +} + +ASL_TEST(last) +{ + asl::string_view s1 = "abcd"; + + asl::string_view s2 = s1.last(0); + ASL_TEST_ASSERT(s2.size() == 0); + + s2 = s1.last(2); + ASL_TEST_ASSERT(s2.size() == 2); + ASL_TEST_EXPECT(asl::memcmp(s2.data(), "cd", 2) == 0); + + s2 = s1.last(4); + ASL_TEST_ASSERT(s2.size() == 4); + ASL_TEST_EXPECT(asl::memcmp(s2.data(), "abcd", 4) == 0); +} + ASL_TEST(equal) { ASL_TEST_EXPECT("abc"_sv == "abc"_sv); |