jaulib v1.3.6
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
test_enumutil.cpp
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright Gothel Software e.K.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * This Source Code Form is subject to the terms of the MIT License
8 * If a copy of the MIT was not distributed with this
9 * file, You can obtain one at https://opensource.org/license/mit/.
10 */
11#include <cassert>
12#include <cstring>
13#include <string_view>
14
15#include <jau/enum_util.hpp>
16#include <jau/file_util.hpp>
17#include <jau/test/catch2_ext.hpp>
18
19// Define the `enum class` yourself ...
20enum class test_type1_t : uint8_t {
21 none = 0, // <no value item denoting no value
22 one = 1,
23 two = 2,
25};
26// and add the `enum class` support functions
29
30// Define the `enum class` yourself ...
31enum class test_type2_t : uint8_t {
32 none = 0, // <no value item denoting no value
33 one, // <first value
34 two, // <second value
35 three // <third value
36};
37// and add the `enum class` support functions
40
41// Define the `enum class` yourself ...
42enum class test_type3_t : uint8_t {
43 none = 0, // <no value item denoting no value
44 one = 1 << 0,
45 two = 1 << 1,
46 three = 1 << 2
47};
48// and add the `enum class` support functions
51
52namespace jau::fs {
57}
58
59template<typename enum_info_t>
60void test_enum_info(size_t size)
61{
62 using namespace jau::enums;
63
64 typedef typename enum_info_t::iterator iterator;
65 typedef typename enum_info_t::value_type enum_t;
66 const enum_info_t& ei = enum_info_t::get();
67 std::cout << ei << std::endl;
68 std::cout << "Enum type: " << ei.name() << std::endl;
69 size_t i=0;
70 for(iterator iter = ei.begin(); iter != ei.end(); ++iter, ++i) {
71 enum_t ev = *iter;
72 std::cout << "#" << i << ": " << ev << ", value: " << std::to_string( *ev ) << std::endl;
73 }
74 REQUIRE( size == enum_info_t::size() );
75}
76
77TEST_CASE( "Enum Class Value Type Test 10", "[enum][type]" ) {
78 {
79 using namespace jau::enums;
80
81 static_assert( true == is_enum<test_type1_t::one>() );
82 static_assert( true == is_enum<test_type1_t::two>() );
83 static_assert( true == is_enum<test_type1_t::three>() );
84 static_assert( "test_type1_t::one" == long_name<test_type1_t::one>() );
85 static_assert( "test_type1_t::two" == long_name<test_type1_t::two>() );
86 static_assert( "test_type1_t::three" == long_name<test_type1_t::three>() );
87 static_assert( "one" == name<test_type1_t::one>() );
88 static_assert( "two" == name<test_type1_t::two>() );
89 static_assert( "three" == name<test_type1_t::three>() );
90
91 REQUIRE( "test_type1_t::one" == long_name<test_type1_t::one>() );
92 REQUIRE( "one" == name<test_type1_t::one>() );
93 REQUIRE( true == is_enum<test_type1_t::one>() );
94 {
95 // std::string_view *res = fill_names<test_type1_t::one, test_type1_t::two, test_type1_t::three>();
97 for(std::string_view sv : nt.names) {
98 std::cout << "NameTable: val -> string: " << sv << std::endl;
99 REQUIRE( false == sv.empty() );
100 }
102 for(test_type1_t v : vt.values) {
103 std::cout << "ValueTable: val: " << static_cast<int>(v) << std::endl;
104 }
105 }
106 }
107
108 {
109 static_assert( 4 == test_type2_t_info_t::size() );
110 static_assert( "one" == name(test_type2_t::one) );
111 static_assert( "test_type2_t::one" == long_name(test_type2_t::one) );
112
113 REQUIRE( "one" == name(test_type2_t::one) );
114 REQUIRE( "test_type2_t::one" == long_name(test_type2_t::one) );
115 REQUIRE( "one" == to_string(test_type2_t::one) );
116 }
117 {
118 static_assert( 4 == test_type3_t_info_t::size() );
119 static_assert( "one" == name(test_type3_t::one) );
120 // static_assert( "one" == to_string(test_type3_t::one) );
121 static_assert( "test_type3_t::one" == long_name(test_type3_t::one) );
122 REQUIRE( "one" == name(test_type3_t::one) );
123 REQUIRE( "test_type3_t::one" == long_name(test_type3_t::one) );
124
125 REQUIRE( "[one]" == to_string(test_type3_t::one) );
126
127 {
128 using namespace jau::enums;
129
130 REQUIRE( "[one, two]" == to_string(test_type3_t::one | test_type3_t::two) );
131 REQUIRE( "[one, two, three]" == to_string(test_type3_t::one | test_type3_t::two | test_type3_t::three) );
132 }
133 }
134 {
140
141 }
142}
143
144namespace test::local {
145 // Define the `enum class` yourself ...
146 enum class test_type4_t : uint8_t {
147 none = 0, // <no value item denoting no value
148 one = 1 << 0,
149 two = 1 << 1,
150 three = 1 << 2
151 };
152 // and add the `enum class` support functions
155
156 // Define the `enum class` yourself ...
157 enum class test_type5_t : uint8_t {
158 none = 0, // <no value item denoting no value
159 one = 10,
160 two = 20,
161 three = 30
162 };
163 // and add the `enum class` support functions
166}
167
168TEST_CASE( "Enum Class Value Type Test 11", "[enum][type]" ) {
169 {
170 using namespace test::local;
171 static_assert( 3 == test_type4_t_info_t::size() );
172 static_assert( "one" == name(test_type4_t::one) );
173 // static_assert( "one" == to_string(test_type4_t::one) );
174 static_assert( "test_type4_t::one" == long_name(test_type4_t::one) );
175 REQUIRE( "one" == name(test_type4_t::one) );
176 REQUIRE( "test_type4_t::one" == long_name(test_type4_t::one) );
177
178 REQUIRE( "[one]" == to_string(test_type4_t::one) );
179
180 {
181 using namespace jau::enums;
182 REQUIRE( "[one, two]" == to_string(test_type4_t::one | test_type4_t::two) );
183 REQUIRE( "[one, two, three]" == to_string(test_type4_t::one | test_type4_t::two | test_type4_t::three) );
184 }
185 }
186 {
187 using namespace test::local;
188
189 static_assert( 3 == test_type5_t_info_t::size() );
190 static_assert( "one" == name(test_type5_t::one) );
191 static_assert( "test_type5_t::one" == long_name(test_type5_t::one) );
192 REQUIRE( "one" == name(test_type5_t::one) );
193 REQUIRE( "test_type5_t::one" == long_name(test_type5_t::one) );
194
195 REQUIRE( "one" == to_string(test_type5_t::one) );
196
197 using namespace jau::enums;
198 static_assert( 10 == number(test_type5_t::one) );
199 static_assert( 20 == *test_type5_t::two );
200 static_assert( 30 == *test_type5_t::three );
201 }
202}
std::string to_string(const endian_t v) noexcept
Return std::string representation of the given endian.
consteval_cxx20 std::string_view name() noexcept
constexpr std::underlying_type_t< E > number(const E v) noexcept
consteval_cxx20 NameTable< Vargs... > get_names() noexcept
#define JAU_MAKE_ENUM_STRING(type,...)
constexpr ValueTable< std::common_type_t< Args... >, sizeof...(Args)> get_values(Args... args) noexcept
#define JAU_MAKE_BITFIELD_ENUM_STRING(type,...)
consteval_cxx20 bool is_enum() noexcept
Definition enum_util.hpp:84
consteval_cxx20 std::string_view long_name() noexcept
Definition enum_util.hpp:99
#define JAU_MAKE_ENUM_INFO(type,...)
fmode_t
Generic file type and POSIX protection mode bits as used in file_stats, touch(), mkdir() etc.
mountflags_linux
Flag bit values for mount() flags under GNU/Linux.
@ no_access
Type: Entity gives no access to user, exclusive bit.
@ link
Type: Entity is a symbolic link, might be in combination with file or dir, fifo, chr,...
@ sock
Type: Entity is a socket, might be in combination with link.
@ none
No mode bit set.
@ chr
Type: Entity is a character device, might be in combination with link.
@ dir
Type: Entity is a directory, might be in combination with link.
@ file
Type: Entity is a file, might be in combination with link.
@ blk
Type: Entity is a block device, might be in combination with link.
@ not_existing
Type: Entity does not exist, exclusive bit.
@ fifo
Type: Entity is a fifo/pipe, might be in combination with link.
Author: Sven Gothel sgothel@jausoft.com Copyright Gothel Software e.K.
Definition enum_util.hpp:65
Author: Sven Gothel sgothel@jausoft.com Copyright (c) 2022 Gothel Software e.K.
Definition file_util.hpp:41
void test_enum_info(size_t size)
TEST_CASE("Enum Class Value Type Test 10", "[enum][type]")
test_type1_t
Author: Sven Gothel sgothel@jausoft.com Copyright Gothel Software e.K.
test_type3_t
test_type2_t