Skip to content

Commit ffef4b8

Browse files
committed
✅ Add tests for to_underlying with arithmetic types
Problem: - `to_underlying` works with many types, but is only tested with enums and `int`. Solution: - Add template tests for more integral and floating-point types.
1 parent a95b340 commit ffef4b8

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

test/to_underlying.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <stdx/type_traits.hpp>
22

3+
#include <catch2/catch_template_test_macros.hpp>
34
#include <catch2/catch_test_macros.hpp>
45

56
#include <type_traits>
@@ -25,8 +26,18 @@ TEST_CASE("to_underlying values", "[type_traits]") {
2526
CHECK(stdx::to_underlying(ScopedEnum::Value5) == 5);
2627
}
2728

28-
TEST_CASE("to_underlying works on integral types", "[type_traits]") {
29-
constexpr int x = 0;
30-
CHECK(std::is_same_v<decltype(stdx::to_underlying(x)), int>);
31-
CHECK(stdx::to_underlying(x) == 0);
29+
TEMPLATE_TEST_CASE("to_underlying works on integral types", "[type_traits]",
30+
bool, char, signed char, unsigned char, short int,
31+
unsigned short int, int, unsigned int, long int,
32+
unsigned long int) {
33+
constexpr TestType x{};
34+
CHECK(std::is_same_v<decltype(stdx::to_underlying(x)), TestType>);
35+
CHECK(stdx::to_underlying(x) == TestType{});
36+
}
37+
38+
TEMPLATE_TEST_CASE("to_underlying works on floating point types",
39+
"[type_traits]", float, double) {
40+
constexpr TestType x{};
41+
CHECK(std::is_same_v<decltype(stdx::to_underlying(x)), TestType>);
42+
CHECK(stdx::to_underlying(x) == TestType{});
3243
}

0 commit comments

Comments
 (0)