-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathenum_examples.cpp
97 lines (76 loc) · 2.61 KB
/
enum_examples.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*******************************************************************************
* This file is part of the "https://github.com/blackmatov/enum.hpp"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2019-2023, by Matvey Cherevko ([email protected])
******************************************************************************/
#include <enum.hpp/enum.hpp>
#include "enum_tests.hpp"
#include <string>
#include <iostream>
namespace
{
ENUM_HPP_CLASS_DECL(color, unsigned,
(red = 0xFF0000)
(green = 0x00FF00)
(blue = 0x0000FF)
(white = red | green | blue))
ENUM_HPP_REGISTER_TRAITS(color)
}
namespace some_namespace
{
ENUM_HPP_CLASS_DECL(color, unsigned,
(red = 0xFF0000)
(green = 0x00FF00)
(blue = 0x0000FF)
(white = red | green | blue))
ENUM_HPP_REGISTER_TRAITS(color)
}
namespace external_ns
{
enum class external_enum : unsigned short {
a = 10,
b,
c = a + b
};
ENUM_HPP_TRAITS_DECL(external_enum,
(a)
(b)
(c))
ENUM_HPP_REGISTER_TRAITS(external_enum)
}
TEST_CASE("examples") {
SUBCASE("traits using") {
// size
static_assert(color_traits::size == 4);
// to_underlying
static_assert(color_traits::to_underlying(color::white) == 0xFFFFFF);
// to_string
static_assert(color_traits::to_string(color::red) == "red");
static_assert(color_traits::to_string(color(42)) == std::nullopt);
// from_string
static_assert(color_traits::from_string("green") == color::green);
static_assert(color_traits::from_string("error") == std::nullopt);
// to_index
static_assert(color_traits::to_index(color::blue) == 2u);
static_assert(color_traits::to_index(color(42)) == std::nullopt);
// from_index
static_assert(color_traits::from_index(2) == color::blue);
static_assert(color_traits::from_index(42) == std::nullopt);
// names
for ( std::string_view n : color_traits::names ) {
std::cout << n << ",";
} // stdout: red,green,blue,
}
SUBCASE("generic context") {
using color = some_namespace::color;
// to string
static_assert(enum_hpp::to_string(color::red) == "red");
// from string
static_assert(enum_hpp::from_string<color>("red") == color::red);
}
SUBCASE("external enums") {
using ee = external_ns::external_enum;
static_assert(enum_hpp::to_string(ee::a) == "a");
static_assert(enum_hpp::from_string<ee>("c") == ee::c);
}
}