based

Opinionated utility library
git clone git://git.dimitrijedobrota.com/based.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

enum_test.cpp (2339B)


0 #define CATCH_CONFIG_RUNTIME_STATIC_REQUIRE
2 #include "based/enum/enum.hpp"
4 #include <catch2/catch_test_macros.hpp>
6 #include "based/concepts/comparable/equality.hpp"
7 #include "based/concepts/comparable/greater.hpp"
8 #include "based/concepts/comparable/greater_equal.hpp"
9 #include "based/concepts/comparable/less.hpp"
10 #include "based/concepts/comparable/less_equal.hpp"
11 #include "based/concepts/is/invocable.hpp"
12 #include "based/concepts/is/same.hpp"
13 #include "based/types/types.hpp"
15 struct test
16 {
17 BASED_DECLARE_ENUM(var, based::bu8, 0, a, b, c)
19 [[nodiscard]] int get_var(var::enum_type req) const;
21 private:
22 int m_a = 1;
23 int m_b = 2;
24 int m_c = 3;
25 };
27 BASED_DEFINE_ENUM_CLASS(test, var, based::bu8, 0, a, b, c)
29 inline int test::get_var(var::enum_type req) const
30 {
31 switch (req()) {
32 case var::a():
33 return m_a;
34 case var::b():
35 return m_b;
36 case var::c():
37 return m_c;
38 default:
39 return -1;
40 }
41 }
43 TEST_CASE("types", "[enum/enum]")
44 {
45 STATIC_REQUIRE(requires { typename test::var; });
46 STATIC_REQUIRE(requires { test::var::a; });
47 STATIC_REQUIRE(requires { test::var::b; });
48 STATIC_REQUIRE(requires { test::var::c; });
49 STATIC_REQUIRE(test::var::enum_type::size == 3);
50 STATIC_REQUIRE(test::var::a() == 0);
51 STATIC_REQUIRE(test::var::b() == 1);
52 STATIC_REQUIRE(test::var::c() == 2);
53 }
55 TEST_CASE("safety", "[enum/enum]")
56 {
57 const test crnt;
59 REQUIRE(crnt.get_var(test::var::a) == 1);
60 REQUIRE(crnt.get_var(test::var::b) == 2);
61 REQUIRE(crnt.get_var(test::var::c) == 3);
63 REQUIRE(!based::Invocable<decltype(&test::get_var), based::bu8>);
64 REQUIRE(!based::Invocable<decltype(&test::get_var), int>);
65 }
67 TEST_CASE("names", "[enum/enum]")
68 {
69 REQUIRE(std::strcmp(test::var::enum_type::names[test::var::a], "a") == 0);
70 REQUIRE(std::strcmp(test::var::enum_type::names[test::var::b], "b") == 0);
71 REQUIRE(std::strcmp(test::var::enum_type::names[test::var::c], "c") == 0);
72 }
74 TEST_CASE("operations", "[enum/enum]")
75 {
76 using based::SameAs;
78 STATIC_REQUIRE(based::EqualityComparable<test::var::enum_type>);
79 STATIC_REQUIRE(!based::LessComparable<test::var::enum_type>);
80 STATIC_REQUIRE(!based::GreaterComparable<test::var::enum_type>);
81 STATIC_REQUIRE(!based::LessEqualComparable<test::var::enum_type>);
82 STATIC_REQUIRE(!based::GreaterEqualComparable<test::var::enum_type>);
83 }