Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
test_token_fsm.cpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#include <cassert>
25#include <cinttypes>
26#include <cstring>
27
29
30#include <jau/token_fsm.hpp>
31
32using namespace jau::int_literals;
33
35 typedef jau::lang::token_fsm<uint16_t> token_fsm_u17;
36
37 std::vector<token_fsm_u17::token_value_t> tkey_words = {
38 { 1, "on" },
39 { 2, "one" },
40 { 3, "oneworld" },
41 { 4, "onward" },
42 { 5, "hello" }
43 };
44
45 token_fsm_u17 token( jau::lang::ascii26_alphabet(), tkey_words );
46 fprintf(stderr, "token: %s\n", token.fsm_to_string(26).c_str());
47 fprintf(stderr, "token: %s\n", token.to_string().c_str());
48 REQUIRE( false == token.empty() );
49 REQUIRE( 5 == token.count() );
50
51 REQUIRE( true == token.add( { 6, "heaven" } ) );
52 REQUIRE( 6 == token.count() );
53 REQUIRE( false == token.empty() );
54
55 for(size_t count=0; count < tkey_words.size(); ++count) {
56 token_fsm_u17::uint_t res = token.get(tkey_words[count].value);
57 fprintf(stderr, "%2zu: %s -> %zu (token)\n", count, std::string(tkey_words[count].value).c_str(), (size_t)res);
58 REQUIRE( tkey_words[count].name == res );
59 }
60 REQUIRE( 6 == token.get("heaven") );
61 {
62 std::string haystack = "012345 hello aa"; // [7..12[
63 token_fsm_u17::result_t res = token.find(haystack); // [7..12[
64 fprintf(stderr, "find '%s' -> %s\n", haystack.c_str(), res.to_string().c_str());
65 REQUIRE( tkey_words[4].name == res.token_name );
66 REQUIRE( 7 == res.source_begin );
67 REQUIRE( 12 == res.source_last );
68 }
69 {
70 std::string haystack = "012345 hello"; // [7..12[
71 token_fsm_u17::result_t res = token.find(haystack); // [7..12[
72 fprintf(stderr, "find '%s' -> %s\n", haystack.c_str(), res.to_string().c_str());
73 REQUIRE( tkey_words[4].name == res.token_name );
74 REQUIRE( 7 == res.source_begin );
75 REQUIRE( 12 == res.source_last );
76 }
77}
78
80 std::vector<std::string_view> skey_words = {
81 "alignas", // C++11
82 "alignof", // C++11
83 "and",
84 "and_eq",
85 "asm",
86 "atomic_cancel", // TM TS
87 "atomic_commit", // TM TS
88 "atomic_noexcept", // TM TS
89 "auto",
90 "bitand",
91 "bitor",
92 "bool",
93 "break",
94 "case",
95 "catch",
96 "char",
97 "char8_t", // C++20
98 "char16_t", // C++11
99 "char32_t", // C++11
100 "class",
101 "compl",
102 "concept", // C++20
103 "const",
104 "consteval", // C++20
105 "constexpr", // C++11
106 "constinit", // C++20
107 "const_cast",
108 "continue",
109 "co_await", // C++20
110 "co_return", // C++20
111 "co_yield", // C++20
112 "decltype", // C++11
113 "default",
114 "delete",
115 "do",
116 "double",
117 "dynamic_cast",
118 "else",
119 "enum",
120 "explicit",
121 "export",
122 "extern",
123 "false",
124 "float",
125 "for",
126 "friend",
127 "goto",
128 "if",
129 "inline",
130 "int",
131 "long",
132 "mutable",
133 "namespace",
134 "new",
135 "noexcept", // C++11
136 "not",
137 "not_eq",
138 "nullptr", // C++11
139 "operator",
140 "or",
141 "or_eq",
142 "private",
143 "protected",
144 "public",
145 "reflexpr", // reflection TS
146 "register",
147 "reinterpret_cast",
148 "requires", // C++20
149 "return",
150 "short",
151 "signed",
152 "sizeof",
153 "static",
154 "static_assert", // C++11
155 "static_cast",
156 "struct",
157 "switch",
158 "synchronized", // TM TS
159 "template",
160 "this",
161 "thread_local", // C++11
162 "throw",
163 "true",
164 "try",
165 "typedef",
166 "typeid",
167 "typename",
168 "union",
169 "unsigned",
170 "using",
171 "virtual",
172 "void",
173 "volatile",
174 "wchar_t",
175 "while",
176 "xor",
177 "xor_eq" };
178
179 typedef jau::lang::token_fsm<uint16_t> token_fsm_u17;
180
181 std::vector<token_fsm_u17::token_value_t> tkey_words;
182 for(size_t i=0; i<skey_words.size(); ++i ) {
183 tkey_words.push_back( { static_cast<token_fsm_u17::uint_t>( i+1 ) /* token */, skey_words[i] /* value */ } );
184 }
185
186 token_fsm_u17 token( jau::lang::ascii69_alphabet(), tkey_words );
187 // fprintf(stderr, "cpp_token: %s\n", token.fsm_to_string(26).c_str());
188 fprintf(stderr, "cpp_token: %s\n", token.to_string().c_str());
189 REQUIRE( false == token.empty() );
190
191 for(size_t count=0; count < tkey_words.size(); ++count) {
192 token_fsm_u17::uint_t res = token.get(tkey_words[count].value);
193 fprintf(stderr, "%2zu: %s -> %zu (token)\n", count, std::string(tkey_words[count].value).c_str(), (size_t)res);
194 REQUIRE( tkey_words[count].name == res );
195 }
196}
197
Case insensitive ASCII base 26 alphabet with ASCII code-point sorting order.
Definition: token_fsm.hpp:187
Case insensitive ASCII base 69 alphabet with ASCII code-point sorting order.
Definition: token_fsm.hpp:158
METHOD_AS_TEST_CASE(test00_hello, "00_hello")
void test00_hello()
void test10_cpp_token()