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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
|
/*
* libfud
* Copyright 2024 Dominick Allen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "fud_algorithm.hpp"
#include "fud_allocator.hpp"
#include "fud_array.hpp"
#include "fud_string.hpp"
#include "fud_utf8.hpp"
#include "fud_utf8_iterator.hpp"
#include "fud_vector.hpp"
#include "test_common.hpp"
// #include "fud_format.hpp"
// #include "fud_span.hpp"
#include "gtest/gtest.h"
namespace fud {
constexpr size_t validAsciiSize = INT8_MAX + 1;
constexpr size_t invalidAsciiSize = UINT8_MAX + 1 - validAsciiSize;
constexpr size_t numControlChars = 33;
constexpr char printableCharOffset = 0x20;
constexpr auto invalidAscii = FudUtf8::invalidAsciiCode.character();
auto invalidAsciiGenerator()
{
return Iota<uint16_t>{validAsciiSize, 1, invalidAsciiSize};
}
template <typename T>
auto toUtf8(T letter)
{
return FudUtf8::make(static_cast<utf8>(letter));
}
template <typename T>
auto toLetter(T letter)
{
return static_cast<char>(letter);
}
TEST(Utf8Test, Utf8Creation)
{
const Array<utf8, 4> threeByte = {THREE_BYTE};
FudUtf8 utf8Point{FudUtf8::make(threeByte)};
ASSERT_NE(utf8Point.data(), nullptr);
ASSERT_EQ(utf8Point.size(), 3);
ASSERT_NE(utf8Point.hash(), -1);
const Array<utf8, 4> asciiLetter = {'A'};
utf8Point = FudUtf8::make(asciiLetter);
ASSERT_NE(utf8Point.data(), nullptr);
ASSERT_EQ(utf8Point.size(), 1);
const Array<utf8, 4> twoByte = {TWO_BYTE};
utf8Point = FudUtf8::make(twoByte);
ASSERT_NE(utf8Point.data(), nullptr);
ASSERT_EQ(utf8Point.size(), 2);
ASSERT_NE(utf8Point.hash(), -1);
Array<utf8, 4> fourByte = {
static_cast<utf8>(FOUR_BYTE[0]),
static_cast<utf8>(FOUR_BYTE[1]),
static_cast<utf8>(FOUR_BYTE[2]),
static_cast<utf8>(FOUR_BYTE[3])};
utf8Point = FudUtf8::make(fourByte);
ASSERT_NE(utf8Point.data(), nullptr);
ASSERT_EQ(utf8Point.size(), 4);
ASSERT_NE(utf8Point.hash(), -1);
const Array<utf8, 4> invalidBytes = {0xFF, 0xFF, 0xFF, 0xFF};
utf8Point = FudUtf8::make(invalidBytes);
ASSERT_EQ(utf8Point.data(), nullptr);
ASSERT_EQ(utf8Point.size(), 0);
ASSERT_EQ(utf8Point.hash(), -1);
}
TEST(Utf8Test, Utf8MultiByte)
{
Array<utf8, sizeof(MULTI_BYTE_LITERAL)> data{MULTI_BYTE_LITERAL};
constexpr size_t bufSize = data.size();
EXPECT_EQ(data[bufSize - 1], '\0');
class FixedAllocator final : public Allocator {
private:
Array<utf8, bufSize> m_memory{};
size_t m_allocated{0};
public:
virtual ~FixedAllocator() override final = default;
virtual Result<void*, FudStatus> allocate(size_t bytes, size_t alignment) override final
{
static_cast<void>(alignment);
if (bytes > m_memory.size() - m_allocated) {
return FudStatus::AllocFailure;
}
auto* data = m_memory.data() + m_allocated;
m_allocated += bytes;
return data;
}
virtual FudStatus deallocate(void* pointer, size_t bytes) override final
{
static_cast<void>(pointer);
static_cast<void>(bytes);
return FudStatus::Success;
}
virtual bool isEqual(const Allocator& rhs) const override final
{
return &rhs == this;
}
};
FixedAllocator fixedAllocator;
auto stringBufferRes{String::makeFromCString(MULTI_BYTE_LITERAL, &fixedAllocator)};
ASSERT_TRUE(stringBufferRes.isOkay());
auto stringBuffer{stringBufferRes.takeOkay()};
EXPECT_EQ(stringBuffer.size(), bufSize);
EXPECT_EQ(stringBuffer.size(), sizeof(data));
EXPECT_EQ(stringBuffer.length(), bufSize - 1);
EXPECT_TRUE(stringBuffer.nullTerminated());
EXPECT_TRUE(stringBuffer.valid());
ASSERT_TRUE(stringBuffer.utf8Valid());
Utf8Iterator utf8Iter{stringBuffer};
auto characterOpt = utf8Iter.next();
ASSERT_TRUE(characterOpt.has_value());
// MULTI_BYTE_LITERAL "test今日素敵はですねƩ®😀z"
const Array<FudUtf8, 16> multiByteCharacters{
FudUtf8::make(Utf8Variant{Ascii{'t'}}),
FudUtf8::make(Utf8Variant{Ascii{'e'}}),
FudUtf8::make(Utf8Variant{Ascii{'s'}}),
FudUtf8::make(Utf8Variant{Ascii{'t'}}),
FudUtf8::from(StringView{sizeof("今"), "今"}, 0),
FudUtf8::from(StringView{sizeof("日"), "日"}, 0),
FudUtf8::from(StringView{sizeof("素"), "素"}, 0),
FudUtf8::from(StringView{sizeof("敵"), "敵"}, 0),
FudUtf8::from(StringView{sizeof("は"), "は"}, 0),
FudUtf8::from(StringView{sizeof("で"), "で"}, 0),
FudUtf8::from(StringView{sizeof("す"), "す"}, 0),
FudUtf8::from(StringView{sizeof("ね"), "ね"}, 0),
FudUtf8::from(StringView{sizeof("Ʃ"), "Ʃ"}, 0),
FudUtf8::from(StringView{sizeof("®"), "®"}, 0),
FudUtf8::from(StringView{sizeof("😀"), "😀"}, 0),
FudUtf8::make(Utf8Variant{Ascii{'z'}}),
};
size_t idx = 0;
while (characterOpt.has_value()) {
auto character = *characterOpt;
if (character != FudUtf8{Utf8Variant{Ascii{'\0'}}}) {
EXPECT_TRUE(character.size() >= 1);
ASSERT_LT(idx, multiByteCharacters.size());
EXPECT_EQ(character.size(), multiByteCharacters[idx].size());
EXPECT_EQ(character, multiByteCharacters[idx]);
EXPECT_TRUE(multiByteCharacters[idx].valid());
if (character != multiByteCharacters[idx]) {
printf("idx = %zu, %.*s\n", idx, static_cast<int>(character.size()), character.data());
}
idx++;
}
characterOpt = utf8Iter.next();
}
utf8Iter.reset();
ASSERT_TRUE(utf8Iter.next().has_value());
FudUtf8 invalid = FudUtf8::invalidAscii();
ASSERT_FALSE(invalid.valid());
ASSERT_EQ(invalid.size(), 0);
ASSERT_EQ(invalid.data(), nullptr);
ASSERT_EQ(invalid.hash(), -1);
}
TEST(Utf8Test, Utf8IsAscii)
{
ASSERT_FALSE(charIsAscii(invalidAscii));
Iota<int16_t> charIota{0, 1, validAsciiSize};
ASSERT_TRUE(allOf([&]() -> Option<char> { return charIota().map(toLetter<int16_t>); }, charIsAscii));
Iota<int16_t> invalidCharIota{validAsciiSize, 1, invalidAsciiSize};
ASSERT_FALSE(anyOf([&]() -> Option<char> { return invalidCharIota().map(toLetter<int16_t>); }, charIsAscii));
FudUtf8 unicode{FudUtf8::invalidAscii()};
ASSERT_FALSE(utf8IsAscii(unicode));
charIota.set(0);
ASSERT_TRUE(allOf([&]() -> Option<FudUtf8> { return charIota().map(toUtf8<int16_t>); }, utf8IsAscii));
invalidCharIota.set(invalidAsciiSize);
ASSERT_FALSE(anyOf([&]() -> Option<FudUtf8> { return invalidCharIota().map(toUtf8<int16_t>); }, utf8IsAscii));
}
template <typename T, size_t Size = SIZE_MAX>
struct SpanGenerator {
Span<T, Size> span;
size_t index{0};
void reset()
{
index = 0;
}
Option<T> operator()()
{
if (index < span.size()) {
index++;
return span[index - 1];
}
return NullOpt;
}
};
TEST(Utf8Test, Utf8IsAlphanumeric)
{
constexpr size_t numAlphanumericChars = 26 * 2 + 10;
Array<char, numAlphanumericChars + 1> alphanumericCharLiteral{ALPHA_NUMERIC_CHARS};
Array<char, numAlphanumericChars> alphanumericChars{};
copyMem<numAlphanumericChars>(alphanumericChars, alphanumericCharLiteral);
ASSERT_TRUE(allOf(alphanumericChars.span(), charIsAlphanumeric));
constexpr size_t numNonAlphanumericChars = validAsciiSize - numAlphanumericChars;
Vector<char> nonAlphanumericChars{};
for (char idx = 0; idx < INT8_MAX; ++idx) {
if (!charIsAlphanumeric(idx)) {
ASSERT_EQ(nonAlphanumericChars.pushBack(idx), FudStatus::Success);
}
}
auto nonAlphanumericSpan{nonAlphanumericChars.span().takeOkay()};
ASSERT_FALSE(anyOf(nonAlphanumericSpan, charIsAlphanumeric));
auto invalidAsciiChars = invalidAsciiGenerator();
ASSERT_FALSE(anyOf(invalidAsciiChars, charIsAlphanumeric));
ASSERT_FALSE(utf8IsAlphanumeric(FudUtf8{Ascii{invalidAscii}}));
auto iotaGenerator = invalidAsciiGenerator();
auto generator = generate(
[]() { return Array<utf8, invalidAsciiSize>{}; },
[&]() { return iotaGenerator().map([](auto val) { return static_cast<utf8>(val); }); });
SpanGenerator<char, alphanumericChars.size()> alphanumericGenerator{alphanumericChars.span()};
auto utf8AlphanumericGenerator = [&]() { return alphanumericGenerator().map(toUtf8<uint16_t>); };
ASSERT_TRUE(allOf(utf8AlphanumericGenerator, utf8IsAlphanumeric));
SpanGenerator<char> nonAlphanumericGenerator{nonAlphanumericChars.span().takeOkay()};
auto utf8NonAlphanumericGenerator = [&]() { return nonAlphanumericGenerator().map(toUtf8<uint16_t>); };
ASSERT_FALSE(anyOf(utf8NonAlphanumericGenerator, utf8IsAlphanumeric));
}
TEST(Utf8Test, Utf8IsAlpha)
{
constexpr size_t numAlphaChars = sizeof(ALPHA_CHARS) - 1;
Array<char, numAlphaChars + 1> alphaCharLiteral{ALPHA_CHARS};
Array<char, numAlphaChars> alphaChars{};
copyMem<numAlphaChars>(alphaChars, alphaCharLiteral);
ASSERT_TRUE(allOf(alphaChars.span(), charIsAlpha));
constexpr size_t numNonAlphanumericChars = validAsciiSize - numAlphaChars;
Vector<char> nonAlphaChars{};
for (char idx = 0; idx < INT8_MAX; ++idx) {
if (!charIsAlphanumeric(idx)) {
ASSERT_EQ(nonAlphaChars.pushBack(idx), FudStatus::Success);
}
}
ASSERT_FALSE(anyOf(nonAlphaChars.span().takeOkay(), charIsAlpha));
auto invalidAsciiChars = invalidAsciiGenerator();
ASSERT_FALSE(anyOf(invalidAsciiChars, charIsAlpha));
ASSERT_FALSE(utf8IsAlpha(FudUtf8{Ascii{invalidAscii}}));
SpanGenerator<char, alphaChars.size()> alphaGenerator{alphaChars.span()};
auto utf8AlphaGenerator = [&]() { return alphaGenerator().map(toUtf8<uint16_t>); };
ASSERT_TRUE(allOf(utf8AlphaGenerator, utf8IsAlpha));
SpanGenerator<char> nonAlphaGenerator{nonAlphaChars.span().takeOkay()};
auto utf8NonAlphaGenerator = [&]() { return nonAlphaGenerator().map(toUtf8<uint16_t>); };
ASSERT_FALSE(anyOf(utf8NonAlphaGenerator, utf8IsAlpha));
}
TEST(Utf8Test, Utf8IsLower)
{
constexpr size_t numLowerChars = 26;
Array<char, numLowerChars + 1> lowerCharLiteral{LOWERCASE_CHARS};
Array<char, numLowerChars> lowerChars{};
copyMem<numLowerChars>(lowerChars, lowerCharLiteral);
ASSERT_TRUE(allOf(lowerChars.span(), charIsLowercase));
constexpr size_t numNonLowerChars = validAsciiSize - numLowerChars;
Vector<char> nonLowerChars{};
for (char idx = 0; idx < INT8_MAX; ++idx) {
if (!charIsLowercase(idx)) {
ASSERT_EQ(nonLowerChars.pushBack(idx), FudStatus::Success);
}
}
ASSERT_FALSE(anyOf(nonLowerChars.span().takeOkay(), charIsLowercase));
auto invalidAsciiChars = invalidAsciiGenerator();
ASSERT_FALSE(anyOf(invalidAsciiChars, charIsLowercase));
ASSERT_FALSE(utf8IsLowercase(FudUtf8{Ascii{invalidAscii}}));
SpanGenerator<char, lowerChars.size()> lowerGenerator{lowerChars.span()};
auto utf8LowerGenerator = [&]() { return lowerGenerator().map(toUtf8<uint16_t>); };
ASSERT_TRUE(allOf(utf8LowerGenerator, utf8IsLowercase));
SpanGenerator<char> nonLowerGenerator{nonLowerChars.span().takeOkay()};
auto utf8NonLowerGenerator = [&]() { return nonLowerGenerator().map(toUtf8<uint16_t>); };
ASSERT_FALSE(anyOf(utf8NonLowerGenerator, utf8IsLowercase));
}
TEST(Utf8Test, Utf8IsUpper)
{
constexpr size_t numUpperChars = 26;
Array<char, numUpperChars + 1> upperCharLiteral{UPPERCASE_CHARS};
Array<char, numUpperChars> upperChars{};
copyMem<numUpperChars>(upperChars, upperCharLiteral);
ASSERT_TRUE(allOf(upperChars.span(), charIsUppercase));
constexpr size_t numNonUpperChars = validAsciiSize - numUpperChars;
Vector<char> nonUpperChars{};
for (char idx = 0; idx < INT8_MAX; ++idx) {
if (!charIsUppercase(idx)) {
ASSERT_EQ(nonUpperChars.pushBack(idx), FudStatus::Success);
}
}
ASSERT_FALSE(anyOf(nonUpperChars.span().takeOkay(), charIsUppercase));
auto invalidAsciiChars = invalidAsciiGenerator();
ASSERT_FALSE(anyOf(invalidAsciiChars, charIsUppercase));
ASSERT_FALSE(utf8IsUppercase(FudUtf8{Ascii{invalidAscii}}));
SpanGenerator<char, upperChars.size()> upperGenerator{upperChars.span()};
auto utf8UpperGenerator = [&]() { return upperGenerator().map(toUtf8<uint16_t>); };
ASSERT_TRUE(allOf(utf8UpperGenerator, utf8IsUppercase));
SpanGenerator<char> nonUpperGenerator{nonUpperChars.span().takeOkay()};
auto utf8NonUpperGenerator = [&]() { return nonUpperGenerator().map(toUtf8<uint16_t>); };
ASSERT_FALSE(anyOf(utf8NonUpperGenerator, utf8IsUppercase));
}
TEST(Utf8Test, Utf8IsDigit)
{
constexpr size_t numDigitChars = 10;
Array<char, numDigitChars + 1> digitCharLiteral{DECIMAL_CHARS};
Array<char, numDigitChars> digitChars{};
copyMem<numDigitChars>(digitChars, digitCharLiteral);
ASSERT_TRUE(allOf(digitChars.span(), charIsDigit));
constexpr size_t numNonDigitChars = validAsciiSize - numDigitChars;
Vector<char> nonDigitChars{};
for (char idx = 0; idx < INT8_MAX; ++idx) {
if (!charIsDigit(idx)) {
ASSERT_EQ(nonDigitChars.pushBack(idx), FudStatus::Success);
}
}
ASSERT_FALSE(anyOf(nonDigitChars.span().takeOkay(), charIsDigit));
auto invalidAsciiChars = invalidAsciiGenerator();
ASSERT_FALSE(anyOf(invalidAsciiChars, charIsDigit));
ASSERT_FALSE(utf8IsDigit(FudUtf8{Ascii{invalidAscii}}));
SpanGenerator<char, digitChars.size()> digitGenerator{digitChars.span()};
auto utf8DigitGenerator = [&]() { return digitGenerator().map(toUtf8<uint16_t>); };
ASSERT_TRUE(allOf(utf8DigitGenerator, utf8IsDigit));
SpanGenerator<char> nonDigitGenerator{nonDigitChars.span().takeOkay()};
auto utf8NonDigitGenerator = [&]() { return nonDigitGenerator().map(toUtf8<uint16_t>); };
ASSERT_FALSE(anyOf(utf8NonDigitGenerator, utf8IsDigit));
}
TEST(Utf8Test, Utf8IsHexDigit)
{
constexpr size_t numHexDigitChars = 6 * 2 + 10;
Array<char, numHexDigitChars + 1> hexDigitCharLiteral{"abcdefABCDEF0123456789"};
Array<char, numHexDigitChars> hexDigitChars{};
copyMem<numHexDigitChars>(hexDigitChars, hexDigitCharLiteral);
ASSERT_TRUE(allOf(hexDigitChars.span(), charIsHexDigit));
constexpr size_t numNonHexDigitChars = validAsciiSize - numHexDigitChars;
Vector<char> nonHexDigitChars{};
for (char idx = 0; idx < INT8_MAX; ++idx) {
if (!charIsHexDigit(idx)) {
ASSERT_EQ(nonHexDigitChars.pushBack(idx), FudStatus::Success);
}
}
ASSERT_FALSE(anyOf(nonHexDigitChars.span().takeOkay(), charIsHexDigit));
auto invalidAsciiChars = invalidAsciiGenerator();
ASSERT_FALSE(anyOf(invalidAsciiChars, charIsHexDigit));
ASSERT_FALSE(utf8IsHexDigit(FudUtf8{Ascii{invalidAscii}}));
SpanGenerator<char, hexDigitChars.size()> hexDigitGenerator{hexDigitChars.span()};
auto utf8HexDigitGenerator = [&]() { return hexDigitGenerator().map(toUtf8<uint16_t>); };
ASSERT_TRUE(allOf(utf8HexDigitGenerator, utf8IsHexDigit));
SpanGenerator<char> nonHexDigitGenerator{nonHexDigitChars.span().takeOkay()};
auto utf8NonHexDigitGenerator = [&]() { return nonHexDigitGenerator().map(toUtf8<uint16_t>); };
ASSERT_FALSE(anyOf(utf8NonHexDigitGenerator, utf8IsHexDigit));
}
TEST(Utf8Test, Utf8IsControl)
{
Iota<char> controlArrayGenerator{0, 1, numControlChars};
auto controlChars = generate([]() { return Array<char, numControlChars>{}; }, controlArrayGenerator);
constexpr const char deleteChar = 0x7F;
controlChars.back() = deleteChar;
ASSERT_TRUE(allOf(controlChars.span(), charIsControl));
constexpr size_t numNonControlChars = INT8_MAX + 1 - numControlChars;
Vector<char> nonControlChars{};
ASSERT_EQ(nonControlChars.reserve(numNonControlChars), FudStatus::Success);
for (auto idx = numControlChars - 1; idx < deleteChar; ++idx) {
ASSERT_EQ(nonControlChars.pushBack(idx), FudStatus::Success);
}
ASSERT_FALSE(anyOf(nonControlChars.span().takeOkay(), charIsControl));
ASSERT_TRUE(allOf(nonControlChars.span().takeOkay(), charIsAscii));
auto invalidAsciiChars = invalidAsciiGenerator();
ASSERT_FALSE(anyOf(invalidAsciiChars, charIsControl));
ASSERT_FALSE(utf8IsControl(FudUtf8{Ascii{invalidAscii}}));
SpanGenerator<char, controlChars.size()> controlGenerator{controlChars.span()};
auto utf8ControlGenerator = [&]() { return controlGenerator().map(toUtf8<uint16_t>); };
ASSERT_TRUE(allOf(utf8ControlGenerator, utf8IsControl));
SpanGenerator<char> nonControlGenerator{nonControlChars.span().takeOkay()};
auto utf8NonControlGenerator = [&]() { return nonControlGenerator().map(toUtf8<uint16_t>); };
ASSERT_FALSE(anyOf(utf8NonControlGenerator, utf8IsControl));
}
TEST(Utf8Test, Utf8IsGraphical)
{
constexpr size_t numGraphicalChars = sizeof(GRAPHICAL_CHARS) - 1;
Array<char, numGraphicalChars + 1> graphicalCharLiteral{GRAPHICAL_CHARS};
Array<char, numGraphicalChars> graphicalChars{};
copyMem<numGraphicalChars>(graphicalChars, graphicalCharLiteral);
ASSERT_TRUE(allOf(graphicalChars.span(), charIsGraphical));
constexpr size_t numNonGraphicalChars = validAsciiSize - numGraphicalChars;
Vector<char> nonGraphicalChars{};
ASSERT_EQ(nonGraphicalChars.reserve(numNonGraphicalChars), FudStatus::Success);
for (uint8_t idx = 0; idx < INT8_MAX + 1; ++idx) {
if (!charIsGraphical(static_cast<char>(idx))) {
ASSERT_EQ(nonGraphicalChars.pushBack(static_cast<char>(idx)), FudStatus::Success);
}
}
ASSERT_FALSE(anyOf(nonGraphicalChars.span().takeOkay(), charIsGraphical));
ASSERT_TRUE(allOf(nonGraphicalChars.span().takeOkay(), charIsAscii));
ASSERT_EQ(nonGraphicalChars.size() + graphicalChars.size(), INT8_MAX + 1);
auto invalidAsciiChars = invalidAsciiGenerator();
ASSERT_FALSE(anyOf(invalidAsciiChars, charIsGraphical));
ASSERT_FALSE(utf8IsGraphical(FudUtf8{Ascii{invalidAscii}}));
SpanGenerator<char, graphicalChars.size()> graphicalGenerator{graphicalChars.span()};
auto utf8GraphicalGenerator = [&]() { return graphicalGenerator().map(toUtf8<uint16_t>); };
ASSERT_TRUE(allOf(utf8GraphicalGenerator, utf8IsGraphical));
SpanGenerator<char> nonGraphicalGenerator{nonGraphicalChars.span().takeOkay()};
auto utf8NonGraphicalGenerator = [&]() { return nonGraphicalGenerator().map(toUtf8<uint16_t>); };
ASSERT_FALSE(anyOf(utf8NonGraphicalGenerator, utf8IsGraphical));
}
TEST(Utf8Test, Utf8IsSpace)
{
constexpr size_t numSpaceChars = sizeof(SPACE_CHARS) - 1;
Array<char, numSpaceChars + 1> spaceCharLiteral{SPACE_CHARS};
Array<char, numSpaceChars> spaceChars{};
copyMem<numSpaceChars>(spaceChars, spaceCharLiteral);
ASSERT_TRUE(allOf(spaceChars.span(), charIsSpace));
constexpr size_t numNonSpaceChars = validAsciiSize - numSpaceChars;
Vector<char> nonSpaceChars{};
ASSERT_EQ(nonSpaceChars.reserve(numNonSpaceChars), FudStatus::Success);
for (uint8_t idx = 0; idx < INT8_MAX + 1; ++idx) {
if (!charIsSpace(static_cast<char>(idx))) {
ASSERT_EQ(nonSpaceChars.pushBack(static_cast<char>(idx)), FudStatus::Success);
}
}
ASSERT_FALSE(anyOf(nonSpaceChars.span().takeOkay(), charIsSpace));
ASSERT_TRUE(allOf(nonSpaceChars.span().takeOkay(), charIsAscii));
ASSERT_EQ(nonSpaceChars.size() + spaceChars.size(), INT8_MAX + 1);
auto invalidAsciiChars = invalidAsciiGenerator();
ASSERT_FALSE(anyOf(invalidAsciiChars, charIsSpace));
ASSERT_FALSE(utf8IsSpace(FudUtf8{Ascii{invalidAscii}}));
SpanGenerator<char, spaceChars.size()> spaceGenerator{spaceChars.span()};
auto utf8SpaceGenerator = [&]() { return spaceGenerator().map(toUtf8<uint16_t>); };
ASSERT_TRUE(allOf(utf8SpaceGenerator, utf8IsSpace));
SpanGenerator<char> nonSpaceGenerator{nonSpaceChars.span().takeOkay()};
auto utf8NonSpaceGenerator = [&]() { return nonSpaceGenerator().map(toUtf8<uint16_t>); };
ASSERT_FALSE(anyOf(utf8NonSpaceGenerator, utf8IsSpace));
}
TEST(Utf8Test, Utf8IsBlank)
{
constexpr size_t numBlankChars = sizeof(BLANK_CHARS) - 1;
Array<char, numBlankChars + 1> blankCharLiteral{BLANK_CHARS};
Array<char, numBlankChars> blankChars{};
copyMem<numBlankChars>(blankChars, blankCharLiteral);
ASSERT_TRUE(allOf(blankChars.span(), charIsBlank));
constexpr size_t numNonBlankChars = validAsciiSize - numBlankChars;
Vector<char> nonBlankChars{};
ASSERT_EQ(nonBlankChars.reserve(numNonBlankChars), FudStatus::Success);
for (uint8_t idx = 0; idx < INT8_MAX + 1; ++idx) {
if (!charIsBlank(static_cast<char>(idx))) {
ASSERT_EQ(nonBlankChars.pushBack(static_cast<char>(idx)), FudStatus::Success);
}
}
ASSERT_FALSE(anyOf(nonBlankChars.span().takeOkay(), charIsBlank));
ASSERT_TRUE(allOf(nonBlankChars.span().takeOkay(), charIsAscii));
ASSERT_EQ(nonBlankChars.size() + blankChars.size(), INT8_MAX + 1);
auto invalidAsciiChars = invalidAsciiGenerator();
ASSERT_FALSE(anyOf(invalidAsciiChars, charIsBlank));
ASSERT_FALSE(utf8IsBlank(FudUtf8{Ascii{invalidAscii}}));
SpanGenerator<char, blankChars.size()> blankGenerator{blankChars.span()};
auto utf8BlankGenerator = [&]() { return blankGenerator().map(toUtf8<uint16_t>); };
ASSERT_TRUE(allOf(utf8BlankGenerator, utf8IsBlank));
SpanGenerator<char> nonBlankGenerator{nonBlankChars.span().takeOkay()};
auto utf8NonBlankGenerator = [&]() { return nonBlankGenerator().map(toUtf8<uint16_t>); };
ASSERT_FALSE(anyOf(utf8NonBlankGenerator, utf8IsBlank));
}
TEST(Utf8Test, Utf8IsPrintable)
{
constexpr size_t numPrintableChars = sizeof(PRINTABLE_CHARS) - 1;
Array<char, numPrintableChars + 1> printableCharLiteral{PRINTABLE_CHARS};
Array<char, numPrintableChars> printableChars{};
copyMem<numPrintableChars>(printableChars, printableCharLiteral);
ASSERT_TRUE(allOf(printableChars.span(), charIsPrintable));
constexpr size_t numNonPrintableChars = validAsciiSize - numPrintableChars;
Vector<char> nonPrintableChars{};
ASSERT_EQ(nonPrintableChars.reserve(numNonPrintableChars), FudStatus::Success);
for (uint8_t idx = 0; idx < INT8_MAX + 1; ++idx) {
if (!charIsPrintable(static_cast<char>(idx))) {
ASSERT_EQ(nonPrintableChars.pushBack(static_cast<char>(idx)), FudStatus::Success);
}
}
ASSERT_FALSE(anyOf(nonPrintableChars.span().takeOkay(), charIsPrintable));
ASSERT_TRUE(allOf(nonPrintableChars.span().takeOkay(), charIsAscii));
ASSERT_EQ(nonPrintableChars.size() + printableChars.size(), INT8_MAX + 1);
auto invalidAsciiChars = invalidAsciiGenerator();
ASSERT_FALSE(anyOf(invalidAsciiChars, charIsPrintable));
ASSERT_FALSE(utf8IsPrintable(FudUtf8{Ascii{invalidAscii}}));
SpanGenerator<char, printableChars.size()> printableGenerator{printableChars.span()};
auto utf8PrintableGenerator = [&]() { return printableGenerator().map(toUtf8<uint16_t>); };
ASSERT_TRUE(allOf(utf8PrintableGenerator, utf8IsPrintable));
SpanGenerator<char> nonPrintableGenerator{nonPrintableChars.span().takeOkay()};
auto utf8NonPrintableGenerator = [&]() { return nonPrintableGenerator().map(toUtf8<uint16_t>); };
ASSERT_FALSE(anyOf(utf8NonPrintableGenerator, utf8IsPrintable));
}
TEST(Utf8Test, Utf8IsPunctuation)
{
constexpr size_t numPunctuationChars = sizeof(PUNCTUATION_CHARS) - 1;
Array<char, numPunctuationChars + 1> punctuationCharLiteral{PUNCTUATION_CHARS};
Array<char, numPunctuationChars> punctuationChars{};
copyMem<numPunctuationChars>(punctuationChars, punctuationCharLiteral);
ASSERT_TRUE(allOf(punctuationChars.span(), charIsPunctuation));
constexpr size_t numNonPunctuationChars = validAsciiSize - numPunctuationChars;
Vector<char> nonPunctuationChars{};
ASSERT_EQ(nonPunctuationChars.reserve(numNonPunctuationChars), FudStatus::Success);
for (uint8_t idx = 0; idx < INT8_MAX + 1; ++idx) {
if (!charIsPunctuation(static_cast<char>(idx))) {
ASSERT_EQ(nonPunctuationChars.pushBack(static_cast<char>(idx)), FudStatus::Success);
}
}
ASSERT_FALSE(anyOf(nonPunctuationChars.span().takeOkay(), charIsPunctuation));
ASSERT_TRUE(allOf(nonPunctuationChars.span().takeOkay(), charIsAscii));
ASSERT_EQ(nonPunctuationChars.size() + punctuationChars.size(), INT8_MAX + 1);
auto invalidAsciiChars = invalidAsciiGenerator();
ASSERT_FALSE(anyOf(invalidAsciiChars, charIsPunctuation));
ASSERT_FALSE(utf8IsPunctuation(FudUtf8{Ascii{invalidAscii}}));
SpanGenerator<char, punctuationChars.size()> punctuationGenerator{punctuationChars.span()};
auto utf8PunctuationGenerator = [&]() { return punctuationGenerator().map(toUtf8<uint16_t>); };
ASSERT_TRUE(allOf(utf8PunctuationGenerator, utf8IsPunctuation));
SpanGenerator<char> nonPunctuationGenerator{nonPunctuationChars.span().takeOkay()};
auto utf8NonPunctuationGenerator = [&]() { return nonPunctuationGenerator().map(toUtf8<uint16_t>); };
ASSERT_FALSE(anyOf(utf8NonPunctuationGenerator, utf8IsPunctuation));
}
} // namespace fud
|