Line data Source code
1 : // Copyright (c) 2019-2022 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/basic-xml
4 : // contact@m2osw.com
5 : //
6 : // This program is free software: you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation, either version 3 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 :
19 : // self
20 : //
21 : #include "catch_main.h"
22 :
23 :
24 : // basic-xml
25 : //
26 : #include <basic-xml/exception.h>
27 : #include <basic-xml/type.h>
28 :
29 :
30 :
31 6 : CATCH_TEST_CASE("types", "[type][valid]")
32 : {
33 8 : CATCH_START_SECTION("is_alpha")
34 : {
35 257 : for(int c(0); c < 256; ++c)
36 : {
37 256 : if((c >= 'a' && c <= 'z')
38 230 : || (c >= 'A' && c <= 'Z')
39 204 : || c == '_')
40 : {
41 53 : CATCH_REQUIRE(basic_xml::is_alpha(c));
42 : }
43 : else
44 : {
45 203 : CATCH_REQUIRE_FALSE(basic_xml::is_alpha(c));
46 : }
47 : }
48 : }
49 : CATCH_END_SECTION()
50 :
51 8 : CATCH_START_SECTION("is_digit")
52 : {
53 257 : for(int c(0); c < 256; ++c)
54 : {
55 256 : if((c >= '0' && c <= '9')
56 246 : || c == '-')
57 : {
58 11 : CATCH_REQUIRE(basic_xml::is_digit(c));
59 : }
60 : else
61 : {
62 245 : CATCH_REQUIRE_FALSE(basic_xml::is_digit(c));
63 : }
64 : }
65 : }
66 : CATCH_END_SECTION()
67 :
68 8 : CATCH_START_SECTION("is_space")
69 : {
70 257 : for(int c(0); c < 256; ++c)
71 : {
72 256 : switch(c)
73 : {
74 6 : case ' ':
75 : case '\t':
76 : case '\v':
77 : case '\f':
78 : case '\n':
79 : case '\r':
80 6 : CATCH_REQUIRE(basic_xml::is_space(c));
81 6 : break;
82 :
83 250 : default:
84 250 : CATCH_REQUIRE_FALSE(basic_xml::is_space(c));
85 250 : break;
86 :
87 : }
88 : }
89 : }
90 : CATCH_END_SECTION()
91 :
92 8 : CATCH_START_SECTION("is_token -- last cannot be '-'")
93 : {
94 11 : for(int count(0); count < 10; ++count)
95 : {
96 20 : std::string token;
97 10 : int max(rand() % 10 + 10);
98 10 : bool more(true);
99 171 : for(int len(0); len < max || more; ++len)
100 : {
101 161 : more = false;
102 161 : switch(rand() % (token.empty() ? 3 : 5))
103 : {
104 35 : case 0:
105 35 : token += static_cast<char>(rand() % 26 + 'a');
106 35 : break;
107 :
108 34 : case 1:
109 34 : token += static_cast<char>(rand() % 26 + 'A');
110 34 : break;
111 :
112 29 : case 2:
113 29 : token += '_';
114 29 : break;
115 :
116 32 : case 3:
117 32 : token += static_cast<char>(rand() % 10 + '0');
118 32 : break;
119 :
120 31 : case 4:
121 31 : token += '-';
122 31 : more = true; // do not end with '-'
123 31 : break;
124 :
125 : }
126 : }
127 :
128 10 : CATCH_REQUIRE(basic_xml::is_token(token));
129 : }
130 : }
131 : CATCH_END_SECTION()
132 4 : }
133 :
134 :
135 :
136 6 : CATCH_TEST_CASE("invalid_tokens", "[type][invalid]")
137 : {
138 8 : CATCH_START_SECTION("is_token -- empty")
139 : {
140 1 : CATCH_REQUIRE_FALSE(basic_xml::is_token(std::string()));
141 : }
142 : CATCH_END_SECTION()
143 :
144 8 : CATCH_START_SECTION("is_token -- 1st must be alpha")
145 : {
146 256 : for(int c(1); c < 256; ++c)
147 : {
148 255 : if(basic_xml::is_alpha(c))
149 : {
150 53 : continue;
151 : }
152 :
153 404 : std::string token;
154 202 : token += static_cast<char>(c);
155 :
156 202 : CATCH_REQUIRE_FALSE(basic_xml::is_token(token));
157 :
158 202 : token += static_cast<char>(rand() % 26 + 'a');
159 202 : CATCH_REQUIRE_FALSE(basic_xml::is_token(token));
160 :
161 202 : token[1] = static_cast<char>(rand() % 26 + 'A');
162 202 : CATCH_REQUIRE_FALSE(basic_xml::is_token(token));
163 :
164 202 : token[1] = '_';
165 202 : CATCH_REQUIRE_FALSE(basic_xml::is_token(token));
166 :
167 202 : token[1] = static_cast<char>(rand() % 10 + '0');
168 202 : CATCH_REQUIRE_FALSE(basic_xml::is_token(token));
169 :
170 202 : token[1] = '-';
171 202 : token += 'g';
172 202 : CATCH_REQUIRE_FALSE(basic_xml::is_token(token));
173 : }
174 : }
175 : CATCH_END_SECTION()
176 :
177 8 : CATCH_START_SECTION("is_token -- 2nd+ must be alpha or digit")
178 : {
179 256 : for(int c(1); c < 256; ++c)
180 : {
181 510 : if(basic_xml::is_alpha(c)
182 255 : || basic_xml::is_digit(c))
183 : {
184 64 : continue;
185 : }
186 :
187 382 : std::string token;
188 191 : token += static_cast<char>(rand() % 26 + 'a');
189 :
190 191 : token += static_cast<char>(c);
191 191 : CATCH_REQUIRE_FALSE(basic_xml::is_token(token));
192 :
193 191 : token[0] = static_cast<char>(rand() % 26 + 'A');
194 191 : CATCH_REQUIRE_FALSE(basic_xml::is_token(token));
195 :
196 191 : token[0] = '_';
197 191 : CATCH_REQUIRE_FALSE(basic_xml::is_token(token));
198 : }
199 : }
200 : CATCH_END_SECTION()
201 :
202 8 : CATCH_START_SECTION("is_token -- last cannot be '-'")
203 : {
204 11 : for(int count(0); count < 10; ++count)
205 : {
206 20 : std::string token;
207 10 : int max(rand() % 10 + 10);
208 129 : for(int len(0); len < max; ++len)
209 : {
210 119 : switch(rand() % (token.empty() ? 3 : 5))
211 : {
212 30 : case 0:
213 30 : token += static_cast<char>(rand() % 26 + 'a');
214 30 : break;
215 :
216 25 : case 1:
217 25 : token += static_cast<char>(rand() % 26 + 'A');
218 25 : break;
219 :
220 29 : case 2:
221 29 : token += '_';
222 29 : break;
223 :
224 11 : case 3:
225 11 : token += static_cast<char>(rand() % 10 + '0');
226 11 : break;
227 :
228 24 : case 4:
229 24 : token += '-';
230 24 : break;
231 :
232 : }
233 : }
234 :
235 10 : token += '-';
236 10 : CATCH_REQUIRE_FALSE(basic_xml::is_token(token));
237 :
238 : // make sure the token is actually valid if not ending with '-'
239 : // (testing the test)
240 : //
241 10 : token += 'q';
242 10 : CATCH_REQUIRE(basic_xml::is_token(token));
243 : }
244 : }
245 : CATCH_END_SECTION()
246 10 : }
247 :
248 :
249 :
250 : // vim: ts=4 sw=4 et
|