jaulib v1.4.1
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
14#include <jau/enum_util.hpp>
15#include <jau/io/file_util.hpp>
16#include <jau/test/catch2_ext.hpp>
17
18// Define the `enum class` yourself ...
19enum class test_type1_t : uint8_t {
20 none = 0, // <no value item denoting no value
21 one = 1,
22 two = 2,
24};
25// and add the `enum class` support functions
28
29// Define the `enum class` yourself ...
30enum class test_type2_t : uint8_t {
31 none = 0, // <no value item denoting no value
32 one, // <first value
33 two, // <second value
34 three // <third value
35};
36// and add the `enum class` support functions
39
40// Define the `enum class` yourself ...
41enum class test_type3_t : uint8_t {
42 none = 0, // <no value item denoting no value
43 one = 1 << 0,
44 two = 1 << 1,
45 three = 1 << 2
46};
47// and add the `enum class` support functions
50
51namespace jau::io::fs {
56}
57
58template<typename enum_info_t>
59static void test_enum_info(size_t size)
60{
61 using namespace jau::enums;
62
63 typedef typename enum_info_t::iterator iterator;
64 typedef typename enum_info_t::value_type enum_t;
65 const enum_info_t& ei = enum_info_t::get();
66 std::cout << ei << std::endl;
67 std::cout << "Enum type: " << ei.name() << std::endl;
68 size_t i=0;
69 for(iterator iter = ei.begin(); iter != ei.end(); ++iter, ++i) {
70 enum_t ev = *iter;
71 std::cout << "#" << i << ": " << ev << ", value: " << std::to_string( *ev ) << std::endl;
72 }
73 REQUIRE( size == enum_info_t::size() );
74}
75
76TEST_CASE( "Enum Class Value Type Test 10", "[enum][type]" ) {
77 {
78 using namespace jau::enums;
79
80 static_assert( true == is_enum<test_type1_t::one>() );
81 static_assert( true == is_enum<test_type1_t::two>() );
82 static_assert( true == is_enum<test_type1_t::three>() );
83 static_assert( "test_type1_t::one" == long_name<test_type1_t::one>() );
84 static_assert( "test_type1_t::two" == long_name<test_type1_t::two>() );
85 static_assert( "test_type1_t::three" == long_name<test_type1_t::three>() );
86 static_assert( "one" == name<test_type1_t::one>() );
87 static_assert( "two" == name<test_type1_t::two>() );
88 static_assert( "three" == name<test_type1_t::three>() );
89
90 REQUIRE( "test_type1_t::one" == long_name<test_type1_t::one>() );
91 REQUIRE( "one" == name<test_type1_t::one>() );
92 REQUIRE( true == is_enum<test_type1_t::one>() );
93 {
94 // std::string_view *res = fill_names<test_type1_t::one, test_type1_t::two, test_type1_t::three>();
96 for(std::string_view sv : nt.names) {
97 std::cout << "NameTable: val -> string: " << sv << std::endl;
98 REQUIRE( false == sv.empty() );
99 }
101 for(test_type1_t v : vt.values) {
102 std::cout << "ValueTable: val: " << static_cast<int>(v) << std::endl;
103 }
104 }
105 }
106
107 {
108 static_assert( 4 == test_type2_t_info_t::size() );
109 static_assert( "one" == name(test_type2_t::one) );
110 static_assert( "test_type2_t::one" == long_name(test_type2_t::one) );
111
112 REQUIRE( "one" == name(test_type2_t::one) );
113 REQUIRE( "test_type2_t::one" == long_name(test_type2_t::one) );
114 REQUIRE( "one" == to_string(test_type2_t::one) );
115 }
116 {
117 static_assert( 4 == test_type3_t_info_t::size() );
118 static_assert( "one" == name(test_type3_t::one) );
119 // static_assert( "one" == to_string(test_type3_t::one) );
120 static_assert( "test_type3_t::one" == long_name(test_type3_t::one) );
121 REQUIRE( "one" == name(test_type3_t::one) );
122 REQUIRE( "test_type3_t::one" == long_name(test_type3_t::one) );
123
124 REQUIRE( "[one]" == to_string(test_type3_t::one) );
125
126 {
127 using namespace jau::enums;
128
129 REQUIRE( "[one, two]" == to_string(test_type3_t::one | test_type3_t::two) );
130 REQUIRE( "[one, two, three]" == to_string(test_type3_t::one | test_type3_t::two | test_type3_t::three) );
131 }
132 }
133 {
139
140 }
141}
142
143namespace test::local {
144 // Define the `enum class` yourself ...
145 enum class test_type4_t : uint8_t {
146 none = 0, // <no value item denoting no value
147 one = 1 << 0,
148 two = 1 << 1,
149 three = 1 << 2
150 };
151 // and add the `enum class` support functions
154
155 // Define the `enum class` yourself ...
156 enum class test_type5_t : uint8_t {
157 none = 0, // <no value item denoting no value
158 one = 10,
159 two = 20,
160 three = 30
161 };
162 // and add the `enum class` support functions
165}
166
167TEST_CASE( "Enum Class Value Type Test 11", "[enum][type]" ) {
168 {
169 using namespace test::local;
170 static_assert( 3 == test_type4_t_info_t::size() );
171 static_assert( "one" == name(test_type4_t::one) );
172 // static_assert( "one" == to_string(test_type4_t::one) );
173 static_assert( "test_type4_t::one" == long_name(test_type4_t::one) );
174 REQUIRE( "one" == name(test_type4_t::one) );
175 REQUIRE( "test_type4_t::one" == long_name(test_type4_t::one) );
176
177 REQUIRE( "[one]" == to_string(test_type4_t::one) );
178
179 {
180 using namespace jau::enums;
181 REQUIRE( "[one, two]" == to_string(test_type4_t::one | test_type4_t::two) );
182 REQUIRE( "[one, two, three]" == to_string(test_type4_t::one | test_type4_t::two | test_type4_t::three) );
183 }
184 }
185 {
186 using namespace test::local;
187
188 static_assert( 3 == test_type5_t_info_t::size() );
189 static_assert( "one" == name(test_type5_t::one) );
190 static_assert( "test_type5_t::one" == long_name(test_type5_t::one) );
191 REQUIRE( "one" == name(test_type5_t::one) );
192 REQUIRE( "test_type5_t::one" == long_name(test_type5_t::one) );
193
194 REQUIRE( "one" == to_string(test_type5_t::one) );
195
196 using namespace jau::enums;
197 static_assert( 10 == number(test_type5_t::one) );
198 static_assert( 20 == *test_type5_t::two );
199 static_assert( 30 == *test_type5_t::three );
200 }
201}
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,...)
mountflags_linux
Flag bit values for mount() flags under GNU/Linux.
fmode_t
Generic file type and POSIX protection mode bits as used in file_stats, touch(), mkdir() etc.
@ 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
std::string to_string(const bit_order_t v) noexcept
Return std::string representation of the given bit_order_t.
TEST_CASE("Enum Class Value Type Test 10", "[enum][type]")
static void test_enum_info(size_t size)
test_type1_t
Author: Sven Gothel sgothel@jausoft.com Copyright Gothel Software e.K.
test_type3_t
test_type2_t