1 /*
2  * Copyright 2010-2019 Branimir Karadzic. All rights reserved.
3  * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
4  */
5 
6 #include "test.h"
7 #include <bx/filepath.h>
8 #include <bx/string.h>
9 #include <bx/handlealloc.h>
10 #include <bx/sort.h>
11 #include <string>
12 
13 bx::AllocatorI* g_allocator;
14 
15 TEST_CASE("stringPrintfTy", "")
16 {
17 	std::string test;
18 	bx::stringPrintf(test, "printf into std::string.");
19 	REQUIRE(0 == bx::strCmp(bx::StringView(test), "printf into std::string.") );
20 }
21 
22 TEST_CASE("prettify", "")
23 {
24 	char tmp[1024];
25 	prettify(tmp, BX_COUNTOF(tmp), 4000, bx::Units::Kilo);
26 	REQUIRE(0 == bx::strCmp(tmp, "4.00 kB") );
27 
28 	prettify(tmp, BX_COUNTOF(tmp), 4096, bx::Units::Kibi);
29 	REQUIRE(0 == bx::strCmp(tmp, "4.00 KiB") );
30 }
31 
32 TEST_CASE("chars", "")
33 {
34 	for (char ch = 'A'; ch <= 'Z'; ++ch)
35 	{
36 		REQUIRE(!bx::isLower(ch) );
37 		REQUIRE(!bx::isNumeric(ch) );
38 		REQUIRE(bx::isUpper(ch) );
39 		REQUIRE(bx::isAlpha(ch) );
40 		REQUIRE(bx::isAlphaNum(ch) );
41 		REQUIRE(bx::isLower(bx::toLower(ch) ) );
42 	}
43 }
44 
45 TEST_CASE("strLen", "")
46 {
47 	const char* test = "test";
48 
49 	REQUIRE(0 == bx::strLen(test, 0) );
50 	REQUIRE(2 == bx::strLen(test, 2) );
51 	REQUIRE(4 == bx::strLen(test, INT32_MAX) );
52 }
53 
54 TEST_CASE("strCopy", "")
55 {
56 	char dst[128];
57 	size_t num;
58 
59 	num = bx::strCopy(dst, 1, "blah");
60 	REQUIRE(num == 0);
61 
62 	num = bx::strCopy(dst, 3, "blah", 3);
63 	REQUIRE(0 == bx::strCmp(dst, "bl") );
64 	REQUIRE(num == 2);
65 
66 	num = bx::strCopy(dst, sizeof(dst), "blah", 3);
67 	REQUIRE(0 == bx::strCmp(dst, "bla") );
68 	REQUIRE(num == 3);
69 
70 	num = bx::strCopy(dst, sizeof(dst), "blah");
71 	REQUIRE(0 == bx::strCmp(dst, "bl", 2) );
72 	REQUIRE(0 == bx::strCmp(dst, "blah") );
73 	REQUIRE(num == 4);
74 }
75 
76 TEST_CASE("strCat", "")
77 {
78 	char dst[128] = { '\0' };
79 
80 	REQUIRE(0 == bx::strCat(dst, 1, "cat") );
81 
82 	REQUIRE(4 == bx::strCopy(dst, 5, "copy") );
83 	REQUIRE(3 == bx::strCat(dst, 8, "cat") );
84 	REQUIRE(0 == bx::strCmp(dst, "copycat") );
85 	REQUIRE(0 == bx::strCmp(dst, "copy", 4) );
86 
87 	REQUIRE(1 == bx::strCat(dst, BX_COUNTOF(dst), "------", 1) );
88 	REQUIRE(3 == bx::strCat(dst, BX_COUNTOF(dst), "cat") );
89 	REQUIRE(0 == bx::strCmp(dst, "copycat-cat") );
90 }
91 
92 TEST_CASE("strCmp", "")
93 {
94 	REQUIRE(0  < bx::strCmp("abvgd", "abv") );
95 	REQUIRE(0  < bx::strCmp("abvgd", "") );
96 	REQUIRE(0  > bx::strCmp("", "abvgd") );
97 	REQUIRE(0 != bx::strCmp(".tar.gz", ".") );
98 	REQUIRE(0 != bx::strCmp("meh", "meh/") );
99 }
100 
101 TEST_CASE("strCmpI", "")
102 {
103 	REQUIRE(0 == bx::strCmpI("test", "test") );
104 	REQUIRE(0 == bx::strCmpI("test", "testestes", 4) );
105 	REQUIRE(0 == bx::strCmpI("testestes", "test", 4) );
106 	REQUIRE(0 != bx::strCmpI("preprocess", "platform") );
107 
108 	const char* abvgd = "abvgd";
109 	const char* abvgx = "abvgx";
110 	const char* empty = "";
111 	REQUIRE(0 == bx::strCmpI(abvgd, abvgd) );
112 	REQUIRE(0 == bx::strCmpI(abvgd, abvgx, 4) );
113 	REQUIRE(0 == bx::strCmpI(empty, empty) );
114 
115 	REQUIRE(0 >  bx::strCmpI(abvgd, abvgx) );
116 	REQUIRE(0 >  bx::strCmpI(empty, abvgd) );
117 
118 	REQUIRE(0 <  bx::strCmpI(abvgx, abvgd) );
119 	REQUIRE(0 <  bx::strCmpI(abvgd, empty) );
120 }
121 
122 TEST_CASE("strCmpV", "")
123 {
124 	REQUIRE(0 == bx::strCmpV("test", "test") );
125 	REQUIRE(0 == bx::strCmpV("test", "testestes", 4) );
126 	REQUIRE(0 == bx::strCmpV("testestes", "test", 4) );
127 	REQUIRE(0 != bx::strCmpV("preprocess", "platform") );
128 
129 	const char* abvgd = "abvgd";
130 	const char* abvgx = "abvgx";
131 	const char* empty = "";
132 	REQUIRE(0 == bx::strCmpV(abvgd, abvgd) );
133 	REQUIRE(0 == bx::strCmpV(abvgd, abvgx, 4) );
134 	REQUIRE(0 == bx::strCmpV(empty, empty) );
135 
136 	REQUIRE(0 >  bx::strCmpV(abvgd, abvgx) );
137 	REQUIRE(0 >  bx::strCmpV(empty, abvgd) );
138 
139 	REQUIRE(0 <  bx::strCmpV(abvgx, abvgd) );
140 	REQUIRE(0 <  bx::strCmpV(abvgd, empty) );
141 }
142 
strCmpV(const void * _lhs,const void * _rhs)143 static int32_t strCmpV(const void* _lhs, const void* _rhs)
144 {
145 	const char* lhs = *(const char**)_lhs;
146 	const char* rhs = *(const char**)_rhs;
147 	int32_t result = bx::strCmpV(lhs, rhs);
148 	return result;
149 }
150 
151 TEST_CASE("strCmpV sort", "")
152 {
153 	const char* test[] =
154 	{
155 		"test_1.txt",
156 		"test_10.txt",
157 		"test_100.txt",
158 		"test_15.txt",
159 		"test_11.txt",
160 		"test_23.txt",
161 		"test_3.txt",
162 	};
163 
164 	const char* expected[] =
165 	{
166 		"test_1.txt",
167 		"test_3.txt",
168 		"test_10.txt",
169 		"test_11.txt",
170 		"test_15.txt",
171 		"test_23.txt",
172 		"test_100.txt",
173 	};
174 
175 	BX_STATIC_ASSERT(BX_COUNTOF(test) == BX_COUNTOF(expected) );
176 
177 	bx::quickSort(test, BX_COUNTOF(test), sizeof(const char*), strCmpV);
178 
179 	for (uint32_t ii = 0; ii < BX_COUNTOF(test); ++ii)
180 	{
181 		REQUIRE(0 == bx::strCmp(test[ii], expected[ii]) );
182 	}
183 }
184 
185 TEST_CASE("strRFind", "")
186 {
187 	const char* test = "test";
188 	REQUIRE(bx::strRFind(bx::StringView(test, 0), 's').isEmpty() );
189 	REQUIRE(bx::strRFind(bx::StringView(test, 1), 's').isEmpty() );
190 	REQUIRE(&test[2] == bx::strRFind(test, 's').getPtr() );
191 	REQUIRE(&test[3] == bx::strRFind(test, 't').getPtr() );
192 }
193 
194 TEST_CASE("strFindI", "")
195 {
196 	const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
197 
198 	REQUIRE(bx::strFindI(bx::StringView(test, 8), "quick").isEmpty() );
199 	REQUIRE(bx::strFindI(test, "quick1").isEmpty() );
200 	REQUIRE(&test[4] == bx::strFindI(bx::StringView(test, 9), "quick").getPtr() );
201 	REQUIRE(&test[4] == bx::strFindI(test, "quick").getPtr() );
202 }
203 
204 TEST_CASE("strFind", "")
205 {
206 	{
207 		const char* test = "test";
208 
209 		REQUIRE(bx::strFind(bx::StringView(test, 0), 's').isEmpty() );
210 		REQUIRE(bx::strFind(bx::StringView(test, 2), 's').isEmpty() );
211 		REQUIRE(&test[2] == bx::strFind(test, 's').getPtr() );
212 	}
213 
214 	{
215 		const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
216 
217 		REQUIRE(bx::strFind(bx::StringView(test, 8), "quick").isEmpty() );
218 		REQUIRE(bx::strFind(test, "quick1").isEmpty() );
219 		REQUIRE(bx::strFind(bx::StringView(test, 9), "quick").isEmpty() );
220 		REQUIRE(bx::strFind(test, "quick").isEmpty() );
221 
222 		REQUIRE(bx::strFind(bx::StringView(test, 8), "Quick").isEmpty() );
223 		REQUIRE(bx::strFind(test, "Quick1").isEmpty() );
224 		REQUIRE(&test[4] == bx::strFind(bx::StringView(test, 9), "Quick").getPtr() );
225 		REQUIRE(&test[4] == bx::strFind(test, "Quick").getPtr() );
226 
227 		REQUIRE(bx::strFind("vgd", 'a').isEmpty() );
228 	}
229 }
230 
231 TEST_CASE("strSkip", "")
232 {
233 	const bx::StringView t0("   test X");
234 
235 	const bx::StringView t1 = bx::strLTrimSpace(t0);
236 	REQUIRE(0 == bx::strCmp(t1, "test", 4) );
237 
238 	const bx::StringView t2 = bx::strLTrimNonSpace(t1);
239 	REQUIRE(0 == bx::strCmp(t2, " X", 2) );
240 
241 	const bx::StringView t3("test");
242 
243 	const bx::StringView t4 = bx::strLTrimNonSpace(t3);
244 	REQUIRE(t4.getTerm() == t4.getPtr() );
245 }
246 
247 template<typename Ty>
testToStringS(Ty _value,const char * _expected,char _separator='\\0')248 static bool testToStringS(Ty _value, const char* _expected, char _separator = '\0')
249 {
250 	char tmp[1024];
251 	int32_t num = bx::toString(tmp, BX_COUNTOF(tmp), _value, 10, _separator);
252 	int32_t len = (int32_t)bx::strLen(_expected);
253 	if (0 == bx::strCmp(tmp, _expected)
254 	&&  num == len)
255 	{
256 		return true;
257 	}
258 
259 	printf("result '%s' (%d), expected '%s' (%d)\n", tmp, num, _expected, len);
260 	return false;
261 }
262 
263 TEST_CASE("toString intXX_t/uintXX_t", "")
264 {
265 	REQUIRE(testToStringS(0,          "0") );
266 	REQUIRE(testToStringS(-256,       "-256") );
267 	REQUIRE(testToStringS(INT32_MAX,  "2147483647") );
268 	REQUIRE(testToStringS(UINT32_MAX, "4294967295") );
269 	REQUIRE(testToStringS(INT64_MAX,  "9223372036854775807") );
270 	REQUIRE(testToStringS(UINT64_MAX, "18446744073709551615") );
271 
272 	REQUIRE(testToStringS(0,          "0", ',') );
273 	REQUIRE(testToStringS(-256,       "-256", ',') );
274 	REQUIRE(testToStringS(INT32_MAX,  "2,147,483,647", ',') );
275 	REQUIRE(testToStringS(UINT32_MAX, "4,294,967,295", ',') );
276 	REQUIRE(testToStringS(INT64_MAX,  "9,223,372,036,854,775,807", ',') );
277 	REQUIRE(testToStringS(UINT64_MAX, "18,446,744,073,709,551,615", ',') );
278 }
279 
280 template<typename Ty>
testToString(Ty _value,const char * _expected)281 static bool testToString(Ty _value, const char* _expected)
282 {
283 	char tmp[1024];
284 	int32_t num = bx::toString(tmp, BX_COUNTOF(tmp), _value);
285 	int32_t len = (int32_t)bx::strLen(_expected);
286 	if (0 == bx::strCmp(tmp, _expected)
287 	&&  num == len)
288 	{
289 		return true;
290 	}
291 
292 	printf("result '%s' (%d), expected '%s' (%d)\n", tmp, num, _expected, len);
293 	return false;
294 }
295 
296 TEST_CASE("toString double", "")
297 {
298 	REQUIRE(testToString(0.0,                     "0.0") );
299 	REQUIRE(testToString(-0.0,                    "-0.0") );
300 	REQUIRE(testToString(1.0,                     "1.0") );
301 	REQUIRE(testToString(-1.0,                    "-1.0") );
302 	REQUIRE(testToString(1.2345,                  "1.2345") );
303 	REQUIRE(testToString(1.2345678,               "1.2345678") );
304 	REQUIRE(testToString(0.123456789012,          "0.123456789012") );
305 	REQUIRE(testToString(1234567.8,               "1234567.8") );
306 	REQUIRE(testToString(-79.39773355813419,      "-79.39773355813419") );
307 	REQUIRE(testToString(0.000001,                "0.000001") );
308 	REQUIRE(testToString(0.0000001,               "1e-7") );
309 	REQUIRE(testToString(1e30,                    "1e30") );
310 	REQUIRE(testToString(1.234567890123456e30,    "1.234567890123456e30") );
311 	REQUIRE(testToString(-5e-324,                 "-5e-324") );
312 	REQUIRE(testToString(2.225073858507201e-308,  "2.225073858507201e-308") );
313 	REQUIRE(testToString(2.2250738585072014e-308, "2.2250738585072014e-308") );
314 	REQUIRE(testToString(1.7976931348623157e308,  "1.7976931348623157e308") );
315 	REQUIRE(testToString(0.00000123123123,        "0.00000123123123") );
316 	REQUIRE(testToString(0.000000123123123,       "1.23123123e-7") );
317 	REQUIRE(testToString(123123.123,              "123123.123") );
318 	REQUIRE(testToString(1231231.23,              "1231231.23") );
319 	REQUIRE(testToString(0.000000000123123,       "1.23123e-10") );
320 	REQUIRE(testToString(0.0000000001,            "1e-10") );
321 	REQUIRE(testToString(-270.000000,             "-270.0") );
322 	REQUIRE(testToString(2.225073858507201e-308,  "2.225073858507201e-308") );
323 	REQUIRE(testToString(-79.39773355813419,      "-79.39773355813419") );
324 }
325 
326 template<typename Ty>
testFromString(Ty _value,const char * _input)327 static bool testFromString(Ty _value, const char* _input)
328 {
329 	char tmp[1024];
330 	bx::toString(tmp, BX_COUNTOF(tmp), _value);
331 
332 	Ty lhs;
333 	bx::fromString(&lhs, tmp);
334 
335 	Ty rhs;
336 	bx::fromString(&rhs, _input);
337 
338 	if (lhs == rhs)
339 	{
340 		return true;
341 	}
342 
343 	printf("result '%f', input '%s'\n", _value, _input);
344 	return false;
345 }
346 
347 TEST_CASE("fromString float", "")
348 {
349 	REQUIRE(testFromString<float>(std::numeric_limits<float>::min(),    "1.175494351e-38") );
350 	REQUIRE(testFromString<float>(std::numeric_limits<float>::lowest(), "-3.402823466e+38") );
351 	REQUIRE(testFromString<float>(std::numeric_limits<float>::max(),    "3.402823466e+38") );
352 }
353 
354 TEST_CASE("fromString double", "")
355 {
356 	REQUIRE(testFromString<double>(0.0,                     "0.0") );
357 	REQUIRE(testFromString<double>(-0.0,                    "-0.0") );
358 	REQUIRE(testFromString<double>(1.0,                     "1.0") );
359 	REQUIRE(testFromString<double>(-1.0,                    "-1.0") );
360 	REQUIRE(testFromString<double>(1.2345,                  "1.2345") );
361 	REQUIRE(testFromString<double>(1.2345678,               "1.2345678") );
362 	REQUIRE(testFromString<double>(0.123456789012,          "0.123456789012") );
363 	REQUIRE(testFromString<double>(123456.789,              "123456.789") );
364 	REQUIRE(testFromString<double>(1234567.8,               "1234567.8") );
365 	REQUIRE(testFromString<double>(-79.39773355813419,      "-79.39773355813419") );
366 	REQUIRE(testFromString<double>(0.000001,                "0.000001") );
367 	REQUIRE(testFromString<double>(0.0000001,               "1e-7") );
368 	REQUIRE(testFromString<double>(1e30,                    "1e30") );
369 	REQUIRE(testFromString<double>(1.234567890123456e30,    "1.234567890123456e30") );
370 	REQUIRE(testFromString<double>(-5e-324,                 "-5e-324") );
371 	REQUIRE(testFromString<double>(2.225073858507201e-308,  "2.225073858507201e-308") );
372 	REQUIRE(testFromString<double>(2.2250738585072014e-308, "2.2250738585072014e-308") );
373 	REQUIRE(testFromString<double>(1.7976931348623157e308,  "1.7976931348623157e308") );
374 	REQUIRE(testFromString<double>(0.00000123123123,        "0.00000123123123") );
375 	REQUIRE(testFromString<double>(0.000000123123123,       "1.23123123e-7") );
376 	REQUIRE(testFromString<double>(123123.123,              "123123.123") );
377 	REQUIRE(testFromString<double>(1231231.23,              "1231231.23") );
378 	REQUIRE(testFromString<double>(0.000000000123123,       "1.23123e-10") );
379 	REQUIRE(testFromString<double>(0.0000000001,            "1e-10") );
380 	REQUIRE(testFromString<double>(-270.000000,             "-270.0") );
381 	REQUIRE(testFromString<double>(2.2250738585072011e-308, "2.2250738585072011e-308") ); // https://web.archive.org/web/20181112222123/https://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
382 	REQUIRE(testFromString<double>(2.2250738585072009e-308, "2.2250738585072009e-308") ); // Max subnormal double
383 	REQUIRE(testFromString<double>(4.9406564584124654e-324, "4.9406564584124654e-324") ); // Min denormal
384 	REQUIRE(testFromString<double>(1.7976931348623157e+308, "1.7976931348623157e+308") ); // Max double
385 
386 //  warning: magnitude of floating-point constant too small for type 'double'; minimum is 4.9406564584124654E-324
387 //	REQUIRE(testFromString<double>(1e-10000,                "0.0") );                     // Must underflow
388 //	integer literal is too large to be represented in any integer type
389 //	REQUIRE(testFromString<double>(18446744073709551616,    "18446744073709551616.0") );  // 2^64 (max of uint64_t + 1, force to use double)
390 //	REQUIRE(testFromString<double>(-9223372036854775809,    "-9223372036854775809.0") );  // -2^63 - 1(min of int64_t + 1, force to use double)
391 
392 	REQUIRE(testFromString<double>(0.9868011474609375,      "0.9868011474609375") );      // https://github.com/miloyip/rapidjson/issues/120
393 	REQUIRE(testFromString<double>(123e34,                  "123e34") );
394 	REQUIRE(testFromString<double>(45913141877270640000.0,  "45913141877270640000.0") );
395 	REQUIRE(testFromString<double>(std::numeric_limits<double>::min(),    "2.2250738585072014e-308") );
396 	REQUIRE(testFromString<double>(std::numeric_limits<double>::lowest(), "-1.7976931348623158e+308") );
397 	REQUIRE(testFromString<double>(std::numeric_limits<double>::max(),    "1.7976931348623158e+308") );
398 }
399 
testFromString(int32_t _value,const char * _input)400 static bool testFromString(int32_t _value, const char* _input)
401 {
402 	char tmp[1024];
403 	bx::toString(tmp, BX_COUNTOF(tmp), _value);
404 
405 	double lhs;
406 	bx::fromString(&lhs, tmp);
407 
408 	double rhs;
409 	bx::fromString(&rhs, _input);
410 
411 	if (lhs == rhs)
412 	{
413 		return true;
414 	}
415 
416 	printf("result '%d', input '%s'\n", _value, _input);
417 	return false;
418 }
419 
420 TEST_CASE("fromString int32_t", "")
421 {
422 	REQUIRE(testFromString(1389,   "1389") );
423 	REQUIRE(testFromString(1389,   "  1389") );
424 	REQUIRE(testFromString(1389,   "+1389") );
425 	REQUIRE(testFromString(-1389,  "-1389") );
426 	REQUIRE(testFromString(-1389,  " -1389") );
427 	REQUIRE(testFromString(555333, "555333") );
428 	REQUIRE(testFromString(-21,    "-021") );
429 }
430 
431 TEST_CASE("StringView", "")
432 {
433 	bx::StringView sv("test");
434 	REQUIRE(4 == sv.getLength() );
435 
436 	bx::DefaultAllocator crt;
437 	g_allocator = &crt;
438 
439 	typedef bx::StringT<&g_allocator> String;
440 
441 	String st(sv);
442 	REQUIRE(4 == st.getLength() );
443 
444 	st.append("test");
445 	REQUIRE(8 == st.getLength() );
446 
447 	st.append(bx::StringView("test", 2) );
448 	REQUIRE(10 == st.getLength() );
449 
450 	REQUIRE(0 == bx::strCmp(st.getPtr(), "testtestte") );
451 
452 	st.clear();
453 	REQUIRE(0 == st.getLength() );
454 	REQUIRE(4 == sv.getLength() );
455 
456 	st.append("test");
457 	REQUIRE(4 == st.getLength() );
458 
459 	sv.clear();
460 	REQUIRE(0 == sv.getLength() );
461 }
462 
463 TEST_CASE("Trim", "")
464 {
465 	REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "ab"), "vgd") );
466 	REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "vagbd"), "abvgd") );
467 	REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "vgd"), "abvgd") );
468 	REQUIRE(0 == bx::strCmp(bx::strLTrim("/555333/podmac/", "/"), "555333/podmac/") );
469 
470 	REQUIRE(0 == bx::strCmp(bx::strRTrim("abvgd", "vagbd"), "abvgd") );
471 	REQUIRE(0 == bx::strCmp(bx::strRTrim("abvgd", "abv"), "abvgd") );
472 	REQUIRE(0 == bx::strCmp(bx::strRTrim("/555333/podmac/", "/"), "/555333/podmac") );
473 
474 	REQUIRE(0 == bx::strCmp(bx::strTrim("abvgd", "da"), "bvg") );
475 	REQUIRE(0 == bx::strCmp(bx::strTrim("<1389>", "<>"), "1389") );
476 	REQUIRE(0 == bx::strCmp(bx::strTrim("/555333/podmac/", "/"), "555333/podmac") );
477 
478 	REQUIRE(0 == bx::strCmp(bx::strTrim("abvgd", ""), "abvgd") );
479 	REQUIRE(0 == bx::strCmp(bx::strTrim(" \t a b\tv g d \t ", " \t"), "a b\tv g d") );
480 
481 	bx::FilePath uri("/555333/podmac/");
482 	REQUIRE(0 == bx::strCmp(bx::strTrim(uri.getPath(), "/"), "555333/podmac") );
483 }
484 
485 TEST_CASE("strWord", "")
486 {
487 	REQUIRE(bx::strWord(" abvgd-1389.0").isEmpty() );
488 	REQUIRE(0 == bx::strCmp(bx::strWord("abvgd-1389.0"), "abvgd") );
489 }
490 
491 TEST_CASE("strFindBlock", "")
492 {
493 	const bx::StringView test0("{ { {} {} abvgd; {} } }");
494 	const bx::StringView test1(test0, 1);
495 
496 	bx::StringView result = bx::strFindBlock(test1, '{', '}');
497 	REQUIRE(19 == result.getLength() );
498 }
499