|
1 | 1 | #include <gtest/gtest.h> |
2 | 2 |
|
3 | | -import mcpplibs.templates; |
| 3 | +import mcpplibs.primitive; |
4 | 4 |
|
5 | | -TEST(TemplatesTest, HelloMcpp) { |
6 | | - testing::internal::CaptureStdout(); |
7 | | - mcpplibs::templates::hello_mcpp(); |
8 | | - std::string output = testing::internal::GetCapturedStdout(); |
9 | | - EXPECT_EQ(output, "hello mcpp!\n"); |
| 5 | +namespace { |
| 6 | + |
| 7 | +struct UserInteger { |
| 8 | + int value; |
| 9 | +}; |
| 10 | + |
| 11 | +struct NotRegistered { |
| 12 | + int value; |
| 13 | +}; |
| 14 | + |
| 15 | +struct BadRep { |
| 16 | + int value; |
| 17 | +}; |
| 18 | + |
| 19 | +struct BadKind { |
| 20 | + int value; |
| 21 | +}; |
| 22 | + |
| 23 | +} // namespace |
| 24 | + |
| 25 | +template <> |
| 26 | +struct mcpplibs::primitive::underlying::traits<UserInteger> { |
| 27 | + using value_type = UserInteger; |
| 28 | + using rep_type = int; |
| 29 | + |
| 30 | + static constexpr bool enabled = true; |
| 31 | + static constexpr auto kind = mcpplibs::primitive::underlying::category::integer; |
| 32 | + |
| 33 | + static constexpr rep_type to_rep(value_type value) noexcept { return value.value; } |
| 34 | + |
| 35 | + static constexpr value_type from_rep(rep_type value) noexcept { return UserInteger{value}; } |
| 36 | + |
| 37 | + static constexpr bool is_valid_rep(rep_type) noexcept { return true; } |
| 38 | +}; |
| 39 | + |
| 40 | +template <> |
| 41 | +struct mcpplibs::primitive::underlying::traits<BadRep> { |
| 42 | + using value_type = BadRep; |
| 43 | + using rep_type = BadRep; |
| 44 | + |
| 45 | + static constexpr bool enabled = true; |
| 46 | + static constexpr auto kind = mcpplibs::primitive::underlying::category::integer; |
| 47 | + |
| 48 | + static constexpr rep_type to_rep(value_type value) noexcept { return value; } |
| 49 | + |
| 50 | + static constexpr value_type from_rep(rep_type value) noexcept { return value; } |
| 51 | + |
| 52 | + static constexpr bool is_valid_rep(rep_type) noexcept { return true; } |
| 53 | +}; |
| 54 | + |
| 55 | +template <> |
| 56 | +struct mcpplibs::primitive::underlying::traits<BadKind> { |
| 57 | + using value_type = BadKind; |
| 58 | + using rep_type = int; |
| 59 | + |
| 60 | + static constexpr bool enabled = true; |
| 61 | + static constexpr auto kind = mcpplibs::primitive::underlying::category::floating; |
| 62 | + |
| 63 | + static constexpr rep_type to_rep(value_type value) noexcept { return value.value; } |
| 64 | + |
| 65 | + static constexpr value_type from_rep(rep_type value) noexcept { return BadKind{value}; } |
| 66 | + |
| 67 | + static constexpr bool is_valid_rep(rep_type) noexcept { return true; } |
| 68 | +}; |
| 69 | + |
| 70 | +TEST(PrimitiveTraitsTest, StandardTypeConcepts) { |
| 71 | + EXPECT_TRUE((mcpplibs::primitive::std_integer<int>)); |
| 72 | + EXPECT_TRUE((mcpplibs::primitive::std_integer<long long>)); |
| 73 | + EXPECT_FALSE((mcpplibs::primitive::std_integer<bool>)); |
| 74 | + EXPECT_FALSE((mcpplibs::primitive::std_integer<char>)); |
| 75 | + |
| 76 | + EXPECT_TRUE((mcpplibs::primitive::std_floating<float>)); |
| 77 | + EXPECT_TRUE((mcpplibs::primitive::std_floating<double>)); |
| 78 | + EXPECT_TRUE((mcpplibs::primitive::std_floating<long double>)); |
| 79 | + |
| 80 | + EXPECT_TRUE((mcpplibs::primitive::std_bool<bool>)); |
| 81 | + EXPECT_FALSE((mcpplibs::primitive::std_bool<int>)); |
| 82 | + |
| 83 | + EXPECT_TRUE((mcpplibs::primitive::std_char<char>)); |
| 84 | + EXPECT_TRUE((mcpplibs::primitive::std_char<char8_t>)); |
| 85 | + EXPECT_TRUE((mcpplibs::primitive::std_char<wchar_t>)); |
| 86 | + |
| 87 | + EXPECT_TRUE((mcpplibs::primitive::std_underlying_type<int>)); |
| 88 | + EXPECT_TRUE((mcpplibs::primitive::std_underlying_type<double>)); |
| 89 | + EXPECT_FALSE((mcpplibs::primitive::std_underlying_type<void*>)); |
| 90 | +} |
| 91 | + |
| 92 | +TEST(PrimitiveTraitsTest, UnderlyingTraitsDefaultsAndCustomRegistration) { |
| 93 | + EXPECT_TRUE((mcpplibs::primitive::underlying_type<int>)); |
| 94 | + EXPECT_EQ(mcpplibs::primitive::underlying::traits<int>::kind, |
| 95 | + mcpplibs::primitive::underlying::category::integer); |
| 96 | + |
| 97 | + EXPECT_TRUE((mcpplibs::primitive::underlying_type<UserInteger>)); |
| 98 | + EXPECT_EQ(mcpplibs::primitive::underlying::traits<UserInteger>::to_rep(UserInteger{7}), 7); |
| 99 | + |
| 100 | + EXPECT_FALSE((mcpplibs::primitive::underlying_type<NotRegistered>)); |
| 101 | + EXPECT_FALSE((mcpplibs::primitive::underlying::traits<NotRegistered>::enabled)); |
| 102 | +} |
| 103 | + |
| 104 | +TEST(PrimitiveTraitsTest, UnderlyingTypeRequiresValidRepTypeAndCategoryConsistency) { |
| 105 | + EXPECT_TRUE((mcpplibs::primitive::underlying::traits<BadRep>::enabled)); |
| 106 | + EXPECT_FALSE((mcpplibs::primitive::underlying_type<BadRep>)); |
| 107 | + |
| 108 | + EXPECT_TRUE((mcpplibs::primitive::underlying::traits<BadKind>::enabled)); |
| 109 | + EXPECT_FALSE((mcpplibs::primitive::underlying_type<BadKind>)); |
10 | 110 | } |
11 | 111 |
|
12 | 112 | int main(int argc, char **argv) { |
|
0 commit comments