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 | #include "gate/tests.hpp"
#include "gate/strings.hpp"
#include "gate/mathematics.hpp"
using namespace gate;
GATEXX_TEST_UNIT(String)
{
{
static String const empty;
GATEXX_TEST_CHECK(empty.empty() == true);
GATEXX_TEST_CHECK(!empty == true);
gate_string_t const& strref = *empty;
GATEXX_TEST_CHECK(gate_string_is_empty(&strref));
}
static StaticString const a = "A";
static StaticString const b = "B";<--- Shadowed declaration<--- Shadowed declaration
{
StaticString b(a);<--- Shadow variable
GATEXX_TEST_CHECK_EQUAL(b, a);
b = a;
GATEXX_TEST_CHECK_EQUAL(b, a);
}
{
String c1 = a + b;
String c2 = a.c_str() + b;
String c3 = a + b.c_str();
GATEXX_TEST_CHECK(a < b);
GATEXX_TEST_CHECK(a <= b);
GATEXX_TEST_CHECK(b > a);
GATEXX_TEST_CHECK(b >= a);
GATEXX_TEST_CHECK(a != b);
static StaticString const strReal = "12.34";
GATEXX_TEST_CHECK(math::isZero(strReal.parseReal() - 12.34));
GATEXX_TEST_CHECK_EQUAL(strReal.parseInt(), 12);
}
{
static StaticString const strHex = "AB";
gate::uint64_t n = 0;
GATEXX_TEST_CHECK_EQUAL(strHex.parseHex(n), 2);
GATEXX_TEST_CHECK_EQUAL(n, 0xab);
}
{
String f = String::createFilled(3, 'b');
GATEXX_TEST_REQUIRE_EQUAL(f.length(), 3);
GATEXX_TEST_CHECK_EQUAL(f.at(0), 'b');
for(String::const_iterator it = f.begin(); it != f.end(); ++it)
{
GATEXX_TEST_CHECK_EQUAL(*it, 'b');
}
}
{
String n = String::createIntNum(123);
GATEXX_TEST_CHECK_EQUAL(n, "123");
}
{
String n = String::createRealNum(123.456, 2);
GATEXX_TEST_CHECK_EQUAL(n, "123.45");
}
static StaticString const strLowerABC = "abc";
{
gate_string_t s1 = *strLowerABC.c_impl();
gate_string_t s2 = GATE_STRING_INIT_EMPTY;
String::swap(s1, s2);
GATEXX_TEST_CHECK(gate_string_equals_str(&s2, strLowerABC.c_str()));
GATEXX_TEST_CHECK(gate_string_is_empty(&s1));
}
{
static StaticString const strLowerABCDEF = "abcdef";
static StaticString const b = "bc";<--- Shadow variable
static StaticString const c = "x";
GATEXX_TEST_CHECK_EQUAL(strLowerABCDEF.positionOf(b), 1);
GATEXX_TEST_CHECK_EQUAL(strLowerABCDEF.positionOf(b[0]), 1);
GATEXX_TEST_CHECK_EQUAL(strLowerABCDEF.positionOfLast(b), 1);
GATEXX_TEST_CHECK_EQUAL(strLowerABCDEF.positionOfLast(b[0]), 1);
GATEXX_TEST_CHECK_EQUAL(strLowerABCDEF.positionOf(c), String::npos);
GATEXX_TEST_CHECK_EQUAL(strLowerABCDEF.positionOfLast(c), String::npos);
GATEXX_TEST_CHECK_EQUAL(strLowerABCDEF.findFirstOf(b), 1);
GATEXX_TEST_CHECK_EQUAL(strLowerABCDEF.findFirstNotOf(b), 0);
GATEXX_TEST_CHECK_EQUAL(strLowerABCDEF.findLastOf(b), 2);
GATEXX_TEST_CHECK_EQUAL(strLowerABCDEF.findLastNotOf(b), 5);
GATEXX_TEST_CHECK_EQUAL(strLowerABCDEF.substr(1, 2), b);
GATEXX_TEST_CHECK_EQUAL(strLowerABCDEF.left(4), "abcd");
GATEXX_TEST_CHECK_EQUAL(strLowerABCDEF.left(20), strLowerABCDEF);
GATEXX_TEST_CHECK_EQUAL(strLowerABCDEF.right(4), "cdef");
GATEXX_TEST_CHECK_EQUAL(strLowerABCDEF.right(20), strLowerABCDEF);
static StaticString const strAbcNlDef = "abc\ndef";
{
static StaticString nl = strAbcNlDef;
String const l1 = nl.readLine();
GATEXX_TEST_CHECK_EQUAL(l1, strLowerABC);
}
static StaticString const strLowerDEF = "def";
{
String remaining;
String const l1 = strAbcNlDef.readLine(remaining);
GATEXX_TEST_CHECK_EQUAL(l1, strLowerABC);
GATEXX_TEST_CHECK_EQUAL(remaining, strLowerDEF);
}
{
static StaticString n1 = "abc def";
String tok, tail;
GATEXX_TEST_CHECK_EQUAL(n1.readToken(tok, tail), true);
GATEXX_TEST_CHECK_EQUAL(tok, strLowerABC);
GATEXX_TEST_CHECK_EQUAL(tail, " def");
String tok2 = tail.readToken();
GATEXX_TEST_CHECK_EQUAL(tok2, strLowerDEF);
}
String const a_upper = strLowerABCDEF.toUpper();
GATEXX_TEST_CHECK_EQUAL(a_upper, "ABCDEF");
String const a_lower = a_upper.toLower();
GATEXX_TEST_CHECK_EQUAL(a_lower, strLowerABCDEF);
static StaticString const whitespace_str = " \t\r\nabc \t\r\n";
GATEXX_TEST_CHECK_EQUAL(whitespace_str.ltrim(), "abc \t\r\n");
GATEXX_TEST_CHECK_EQUAL(whitespace_str.rtrim(), " \t\r\nabc");
GATEXX_TEST_CHECK_EQUAL(whitespace_str.trim(), strLowerABC);
String const a_copy = strLowerABCDEF.copy();
GATEXX_TEST_CHECK_NOT_EQUAL(strLowerABCDEF.c_str(), a_copy.c_str());
GATEXX_TEST_CHECK_EQUAL(strLowerABCDEF, a_copy);
String const a_dup = strLowerABCDEF.duplicate();
GATEXX_TEST_CHECK_EQUAL(strLowerABCDEF.c_str(), a_dup.c_str());
GATEXX_TEST_CHECK_EQUAL(strLowerABCDEF, a_dup);
GATEXX_TEST_CHECK(a_upper.compareIC(strLowerABCDEF) == 0);
GATEXX_TEST_CHECK(a_upper.compareIC(b) < 0);
GATEXX_TEST_CHECK(strLowerABCDEF.compare(strLowerABCDEF) == 0);
GATEXX_TEST_CHECK(strLowerABCDEF.compareIC("AA") > 0);
GATEXX_TEST_CHECK(strLowerABCDEF.startsWith("abc"));
GATEXX_TEST_CHECK(strLowerABCDEF.startsWithIC("ABC"));
GATEXX_TEST_CHECK(strLowerABCDEF.startsWithIC(StaticString("AB")));
GATEXX_TEST_CHECK(!strLowerABCDEF.startsWith(b));
GATEXX_TEST_CHECK(strLowerABCDEF.startsWith('a'));
GATEXX_TEST_CHECK(!strLowerABCDEF.startsWith('b'));
GATEXX_TEST_CHECK(strLowerABCDEF.endsWith("def"));
GATEXX_TEST_CHECK(strLowerABCDEF.endsWithIC("DEF"));
GATEXX_TEST_CHECK(strLowerABCDEF.endsWithIC(StaticString("EF")));
GATEXX_TEST_CHECK(!strLowerABCDEF.endsWith(b));
GATEXX_TEST_CHECK(strLowerABCDEF.endsWith('f'));
GATEXX_TEST_CHECK(!strLowerABCDEF.endsWith('e'));
GATEXX_TEST_CHECK(strLowerABCDEF.equals(strLowerABCDEF));
GATEXX_TEST_CHECK(strLowerABCDEF.equals("abcdef"));
GATEXX_TEST_CHECK(strLowerABCDEF.equalsIC("ABCDEF"));
GATEXX_TEST_CHECK(strLowerABCDEF.equalsIC(a_upper));
GATEXX_TEST_CHECK(strLowerABCDEF.like("ab*f"));
GATEXX_TEST_CHECK(!strLowerABCDEF.like("b*f"));
GATEXX_TEST_CHECK(!strLowerABCDEF.like("a*e"));
GATEXX_TEST_CHECK(strLowerABCDEF.likeOneOf("a*x;bf;ab*ef"));
}
{ // parse numbers
{
static StaticString const strNum = "12345xyz";
gate::uint64_t ui64 = 0;
GATEXX_TEST_CHECK_EQUAL(strNum.parseNum(ui64), 5);
GATEXX_TEST_CHECK_EQUAL(ui64, 12345);
}
{
static StaticString const strNum2 = "-12345xyz";
gate::int64_t i64 = 0;
GATEXX_TEST_CHECK_EQUAL(strNum2.parseNum(i64), 6);
GATEXX_TEST_CHECK_EQUAL(i64, -12345);
}
{
static StaticString const strNum5 = "+12345xyz";
gate::int64_t i64 = 0;
GATEXX_TEST_CHECK_EQUAL(strNum5.parseNum(i64), 6);
GATEXX_TEST_CHECK_EQUAL(i64, 12345);
}
{
static StaticString const strNum3 = "-123.456xyz";
gate::real32_t r32 = 0.0f;
GATEXX_TEST_CHECK_EQUAL(strNum3.parseNum(r32), 8);
GATEXX_TEST_CHECK(math::isZero(r32 + 123.456f));
}
{
static StaticString const strNum4 = "-123.456xyz";
gate::real64_t r64 = 0.0f;
GATEXX_TEST_CHECK_EQUAL(strNum4.parseNum(r64), 8);
GATEXX_TEST_CHECK(math::isZero(r64 + 123.456));
}
{
static StaticString const strHex = StaticString("deadbeef", 8);
String strBin = strHex.parseHex();
GATEXX_TEST_CHECK_EQUAL(strBin, "\xde\xad\xbe\xef");
GATEXX_TEST_CHECK_EQUAL(strBin.toHex(), strHex);
}
{
static StaticString const tokens = "abc:=def";
String head, tail;
GATEXX_TEST_CHECK_EQUAL(tokens.parse(":=", 0, &head, &tail, false), 5);
GATEXX_TEST_CHECK_EQUAL(head, "abc");
GATEXX_TEST_CHECK_EQUAL(tail, "def");
GATEXX_TEST_CHECK_EQUAL(tokens.parse(":=", 0, &head, &tail, true), 5);
GATEXX_TEST_CHECK_EQUAL(head, "abc");
GATEXX_TEST_CHECK_EQUAL(tail, ":=def");
}
{
static StaticString const strOct = "377";
GATEXX_TEST_CHECK_EQUAL(strOct.parseOctByte(), 255);
}
}
{
static char_16_t const text[] = { 'H', 'e', 'l', 'l', 'o' };
GATEXX_TEST_CHECK_NOTHROW(String::createFrom(text, 5));
}
{
static char_32_t const text[] = { 'H', 'e', 'l', 'l', 'o' };
GATEXX_TEST_CHECK_NOTHROW(String::createFrom(text, 5));
}
{
static String const strEmpty;
static StaticString const strTrue = "true";
static StaticString const strOn = "on";
static StaticString const strYes = "yes";
static StaticString const str1 = "1";
static StaticString const strFalse = "false";
static StaticString const strOff = "off";
static StaticString const strNo = "no";
static StaticString const str0 = "0";
GATEXX_TEST_CHECK_EQUAL(strEmpty.parseBool(), false);
GATEXX_TEST_CHECK_EQUAL(strTrue.parseBool(), true);
GATEXX_TEST_CHECK_EQUAL(strOn.parseBool(), true);
GATEXX_TEST_CHECK_EQUAL(strYes.parseBool(), true);
GATEXX_TEST_CHECK_EQUAL(str1.parseBool(), true);
GATEXX_TEST_CHECK_EQUAL(strFalse.parseBool(), false);
GATEXX_TEST_CHECK_EQUAL(strOff.parseBool(), false);
GATEXX_TEST_CHECK_EQUAL(strNo.parseBool(), false);
GATEXX_TEST_CHECK_EQUAL(str0.parseBool(), false);
}
{
char buffer[1024];
gate::size_t buflen = sizeof(buffer);
{
static gate_bool_t const v = true;
GATEXX_TEST_CHECK(String::printValue(buffer, buflen, GATE_PRINT_BOOL, &v) > 0);
}
{
static gate_uint8_t const v = 123;
GATEXX_TEST_CHECK(String::printValue(buffer, buflen, GATE_PRINT_UI8, &v) > 0);
}
{
static gate_uint16_t const v = 123;
GATEXX_TEST_CHECK(String::printValue(buffer, buflen, GATE_PRINT_UI16, &v) > 0);
}
{
static gate_uint32_t const v = 123;
GATEXX_TEST_CHECK(String::printValue(buffer, buflen, GATE_PRINT_UI32, &v) > 0);
}
{
static gate_uint64_t const v = 123;
GATEXX_TEST_CHECK(String::printValue(buffer, buflen, GATE_PRINT_UI64, &v) > 0);
}
{
static gate_int8_t const v = 123;
GATEXX_TEST_CHECK(String::printValue(buffer, buflen, GATE_PRINT_I8, &v) > 0);
}
{
static gate_int16_t const v = 123;
GATEXX_TEST_CHECK(String::printValue(buffer, buflen, GATE_PRINT_I16, &v) > 0);
}
{
static gate_int32_t const v = 123;
GATEXX_TEST_CHECK(String::printValue(buffer, buflen, GATE_PRINT_I32, &v) > 0);
}
{
static gate_int64_t const v = 123;
GATEXX_TEST_CHECK(String::printValue(buffer, buflen, GATE_PRINT_I64, &v) > 0);
}
}
{
static const StaticString lines = "Hello\nWorld";
gate_size_t parsed;
String line1;
parsed = lines.parseLine(line1);
GATEXX_TEST_CHECK_EQUAL(parsed, 6);
GATEXX_TEST_CHECK_EQUAL(line1, "Hello");
const String tail = lines.substr(parsed);
String line2;
parsed = tail.parseLine(line2);
GATEXX_TEST_CHECK_EQUAL(parsed, 5);
GATEXX_TEST_CHECK_EQUAL(line2, "World");
static String const empty;
String line3;
parsed = empty.parseLine(line3);
GATEXX_TEST_CHECK_EQUAL(parsed, 0);
}
}
GATEXX_TEST_UNIT(CstrBuffer)
{
static StaticString const text = "Hello World";
CstrBuffer buffer(text.c_str(), text.length(), true);
CstrBuffer buffer2(text);
GATEXX_TEST_CHECK(buffer.c_impl() != NULL);
GATEXX_TEST_CHECK_EQUAL(text.length(), buffer2.length());
GATEXX_TEST_CHECK_NOT_EQUAL(buffer2.get(), NULL);
GATEXX_TEST_CHECK_NOT_EQUAL(buffer.getLocalBuffer(), NULL);
static wchar_t const wtext[] = L"Hello";
CstrBuffer buffer3(wtext, 5);
GATEXX_TEST_CHECK(buffer3.get() != NULL);
}
GATEXX_TEST_UNIT(CstrBuffer16)
{
static char_16_t const text16[] = { 'H', 'e', 'l', 'l', 'o' };
static StaticString const text = "Hello";
CstrBuffer16 buffer(text16, sizeof(text16) / sizeof(text16[0]), true);
CstrBuffer16 buffer2(text);
GATEXX_TEST_CHECK(buffer.c_impl() != NULL);
GATEXX_TEST_CHECK_EQUAL(text.length(), buffer2.length());
GATEXX_TEST_CHECK_NOT_EQUAL(buffer2.get(), NULL);
GATEXX_TEST_CHECK_NOT_EQUAL(buffer.getLocalBuffer(), NULL);
}
GATEXX_TEST_UNIT(CstrBuffer32)
{
static char_32_t const text32[] = { 'H', 'e', 'l', 'l', 'o' };
static StaticString const text = "Hello";
CstrBuffer32 buffer(text32, sizeof(text32) / sizeof(text32[0]), true);
CstrBuffer32 buffer2(text);
GATEXX_TEST_CHECK(buffer.c_impl() != NULL);
GATEXX_TEST_CHECK_EQUAL(text.length(), buffer2.length());
GATEXX_TEST_CHECK_NOT_EQUAL(buffer2.get(), NULL);
GATEXX_TEST_CHECK_NOT_EQUAL(buffer.getLocalBuffer(), NULL);
}
static void testBufferPrintValue(char* buffer, gate::size_t buffersize, gate::enumint_t type, void const* data, char const* expected)
{
const gate_size_t expectedLen = String::length(expected);
gate_size_t printLen = String::printValue(buffer, buffersize, type, data);
GATEXX_TEST_CHECK_EQUAL(printLen, expectedLen);
GATEXX_TEST_CHECK_EQUAL(gate_str_compare(buffer, printLen, expected, expectedLen), 0);
}
GATEXX_TEST_UNIT(PrintValues)
{
char buf[128];
gate_size_t blen = sizeof(buf);
testBufferPrintValue(buf, blen, GATE_PRINT_CR, NULL, "\r");
testBufferPrintValue(buf, blen, GATE_PRINT_LF, NULL, "\n");
testBufferPrintValue(buf, blen, GATE_PRINT_CRLF, NULL, "\r\n");
testBufferPrintValue(buf, blen, GATE_PRINT_NEWLINE, NULL, GATE_STR_NEWLINE);
testBufferPrintValue(buf, blen, GATE_PRINT_CSTR, "Test", "Test");
const gate::bool_t b = true; testBufferPrintValue(buf, blen, GATE_PRINT_BOOL, &b, "true");
const gate::bool_t b2 = false; testBufferPrintValue(buf, blen, GATE_PRINT_BOOL, &b2, "false");
const gate::int8_t i8 = 1; testBufferPrintValue(buf, blen, GATE_PRINT_I8, &i8, "1");
const gate::int16_t i16 = 2; testBufferPrintValue(buf, blen, GATE_PRINT_I16, &i16, "2");
const gate::int32_t i32 = 3; testBufferPrintValue(buf, blen, GATE_PRINT_I32, &i32, "3");
const gate::int64_t i64 = 4; testBufferPrintValue(buf, blen, GATE_PRINT_I64, &i64, "4");
const gate::uint8_t ui8 = 0x11; testBufferPrintValue(buf, blen, GATE_PRINT_UI8, &ui8, "17"); testBufferPrintValue(buf, blen, GATE_PRINT_H8, &ui8, "11");
const gate::uint16_t ui16 = 0x12; testBufferPrintValue(buf, blen, GATE_PRINT_UI16, &ui16, "18"); testBufferPrintValue(buf, blen, GATE_PRINT_H16, &ui16, "0012");
const gate::uint32_t ui32 = 0x13; testBufferPrintValue(buf, blen, GATE_PRINT_UI32, &ui32, "19"); testBufferPrintValue(buf, blen, GATE_PRINT_H32, &ui32, "00000013");
const gate::uint64_t ui64 = 0x14; testBufferPrintValue(buf, blen, GATE_PRINT_UI64, &ui64, "20"); testBufferPrintValue(buf, blen, GATE_PRINT_H64, &ui64, "0000000000000014");
const gate::real32_t r32 = 0.1234561f; testBufferPrintValue(buf, blen, GATE_PRINT_R32, &r32, "0.123456");
const gate::real64_t r64 = 0.1234561; testBufferPrintValue(buf, blen, GATE_PRINT_R64, &r64, "0.123456");
}
|