-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmicro_format_tests.cpp
490 lines (409 loc) · 13.4 KB
/
micro_format_tests.cpp
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
#include <string>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include <boost/locale.hpp>
#include "../micro_format.hpp"
static const std::string error_str = "{{error}}";
template <typename ... Args>
void test_eq(const std::string &desired, const char *format_str, const Args& ... args)
{
char result[256] = {};
mf::format(result, format_str, args...);
assert(desired == result);
}
template <typename ... Args>
void test_eq_unicode(const std::string& desired, const char* format_str, const Args& ... args)
{
std::wstring wstr;
auto add_wide_char_cb = [](void* data, mf::WideChar chr)
{
auto* str = (std::wstring*)data;
str->push_back(chr);
return true;
};
auto wide_chars_count = mf::format_u8(add_wide_char_cb, &wstr, format_str, args...);
auto desired_w = boost::locale::conv::utf_to_utf<wchar_t>(desired);
assert(desired_w == wstr);
assert(wide_chars_count == desired_w.size());
}
void test_cmp_printf(const char* format_str, double value)
{
char result[256] = {};
std::string fmt1 = "{:";
fmt1.append(format_str);
fmt1.append("}");
mf::format(result, fmt1.c_str(), value);
std::string fmt2 = "%";
fmt2.append(format_str);
fmt2.append("f");
char printf_buffer[256] = {};
sprintf_s(printf_buffer, fmt2.c_str(), value);
int res = strcmp(result, printf_buffer);
if (res != 0)
{
char long_printf_result[256] = {};
sprintf_s(long_printf_result, "%.20f", value);
char debug_result[256] = {};
mf::format(debug_result, fmt1.c_str(), value);
}
assert(res == 0);
}
static void test_common()
{
test_eq("", "");
test_eq("Simple text", "Simple text");
test_eq("Simple text arg Another text", "Simple text {} Another text", "arg");
}
static void test_types()
{
test_eq("12345", "{}", (int32_t)12345);
test_eq("12345", "{}", (uint32_t)12345);
test_eq("12345", "{}", (int64_t)12345);
test_eq("12345", "{}", (uint64_t)12345);
test_eq("12345", "{}", (int16_t)12345);
test_eq("12345", "{}", (uint16_t)12345);
volatile int64_t vi64 = 12345;
test_eq("12345", "{}", vi64);
volatile uint64_t vui64 = 12345;
test_eq("12345", "{}", vui64);
volatile int32_t vi32 = 12345;
test_eq("12345", "{}", vi32);
volatile uint32_t vui32 = 12345;
test_eq("12345", "{}", vui32);
volatile int16_t vi16 = 12345;
test_eq("12345", "{}", vi16);
volatile uint16_t uvi16 = 12345;
test_eq("12345", "{}", uvi16);
}
static void test_integer()
{
// decimal
test_eq("42", "{}", 42);
test_eq("42", "{:d}", 42);
test_eq("-42", "{}", -42);
test_eq("+42", "{:+}", 42);
test_eq("-42", "{:+}", -42);
test_eq("+42", "{:+}", 42U);
test_eq("42", "{:-}", 42);
test_eq("-42", "{:-}", -42);
test_eq(" 42", "{:5}", 42);
test_eq(" -42", "{:5}", -42);
test_eq(" +42", "{:+5}", 42);
test_eq(" -42", "{:+5}", -42);
test_eq("00042", "{:05}", 42);
test_eq("-0042", "{:05}", -42);
test_eq("+0042", "{:+05}", 42);
test_eq("000123", "{:06}", 123);
test_eq("-00123", "{:06}", -123);
test_eq("-00123", "{:+06}", -123);
test_eq("+00123", "{:+06}", 123);
test_eq(" 00123", "{: 06}", 123);
test_eq("-00123", "{: 06}", -123);
test_eq(" 123", "{: }", 123);
test_eq("-123", "{: }", -123);
#ifdef MICRO_FORMAT_INT64
test_eq("18446744073709551615", "{:}", UINT64_MAX);
test_eq("9223372036854775807", "{:}", INT64_MAX);
test_eq("-9223372036854775808", "{:}", INT64_MIN);
#endif
test_eq("4294967295", "{:}", UINT32_MAX);
test_eq("2147483647", "{:}", INT32_MAX);
test_eq("-2147483648", "{:}", INT32_MIN);
// hex
test_eq("a", "{:x}", 0xa);
test_eq("A", "{:X}", 0xa);
test_eq("5533", "{:x}", 0x5533);
test_eq("0x5533", "{:#x}", 0x5533);
test_eq("0X5533", "{:#X}", 0x5533);
test_eq("0X553A", "{:#X}", 0x553A);
test_eq("-0x5533", "{:#x}", -0x5533);
test_eq(" 0x123", "{:#7x}", 0x123);
test_eq(" -0x123", "{:#7x}", -0x123);
test_eq("-0x0123", "{:#07x}", -0x123);
test_eq("0x00123", "{:#07x}", 0x123);
test_eq("0000123", "{:07x}", 0x123);
test_eq("123 ", "{:<7x}", 0x123);
test_eq("123 ", "{:<7x}", 0x123);
test_eq("0x123 ", "{:<#7x}", 0x123);
test_eq(" 123 ", "{:^7x}", 0x123);
test_eq(" 123 ", "{:^8x}", 0x123);
test_eq(" -123 ", "{:^8x}", -0x123);
#ifdef MICRO_FORMAT_INT64
test_eq("ffffffffffffffff", "{:x}", UINT64_MAX);
#endif
test_eq("ffffffff", "{:x}", UINT32_MAX);
// binary
test_eq("1", "{:b}", 1);
test_eq("1", "{:B}", 1);
test_eq("0b1", "{:#b}", 1);
test_eq("0B1", "{:#B}", 1);
test_eq("11001100", "{:b}", 0b11001100);
test_eq("-11001100", "{:b}", -0b11001100);
test_eq("0b11001100", "{:#b}", 0b11001100);
test_eq("-0b11001100", "{:#b}", -0b11001100);
test_eq("0011001100", "{:010b}", 0b11001100);
test_eq(" 11001100", "{:10b}", 0b11001100);
#ifdef MICRO_FORMAT_INT64
test_eq("1111111111111111111111111111111111111111111111111111111111111111", "{:b}", UINT64_MAX);
#endif
test_eq("11111111111111111111111111111111", "{:b}", UINT32_MAX);
// octal
test_eq("1234567", "{:o}", 01234567);
test_eq("01234567", "{:#o}", 01234567);
test_eq(" 01234567", "{:#10o}", 01234567);
test_eq(" 1234567", "{:10o}", 01234567);
test_eq(" -1234567", "{:10o}", -01234567);
#ifdef MICRO_FORMAT_INT64
test_eq("1777777777777777777777", "{:o}", UINT64_MAX);
#endif
test_eq("37777777777", "{:o}", UINT32_MAX);
// to char
test_eq("AB", "{:c}{:c}", 65, 66);
// errors
test_eq(error_str, "{:s}", 123);
test_eq(error_str, "{:f}", 123);
// x64
test_eq("1000000000000", "{}", 1000'000'000'000ULL);
test_eq("-1000000000000", "{}", -1000'000'000'000);
test_eq("FFFFFFFFFFFFFFFF", "{:X}", 0xFFFFFFFFFFFFFFFFULL);
}
static void test_bool()
{
test_eq("true", "{}", true);
test_eq("false", "{}", false);
test_eq("true", "{:s}", true);
test_eq("false", "{:s}", false);
test_eq("true ", "{:6}", true);
test_eq("false ", "{:6}", false);
test_eq(" true", "{:>6}", true);
test_eq(" false", "{:>6}", false);
test_eq("1", "{:d}", true);
test_eq("0", "{:d}", false);
test_eq("1", "{:x}", true);
test_eq("0", "{:x}", false);
test_eq("0x1", "{:#x}", true);
test_eq("0x0", "{:#x}", false);
test_eq("0b1", "{:#b}", true);
test_eq("0b0", "{:#b}", false);
test_eq(error_str, "{:c}", true);
test_eq(error_str, "{:f}", true);
}
static void test_str()
{
test_eq("str", "{}", "str");
test_eq("str", "{:s}", "str");
test_eq("str ", "{:7}", "str");
test_eq(" str", "{:>7}", "str");
test_eq(" str ", "{:^7}", "str");
test_eq(error_str, "{:c}", "str");
test_eq(error_str, "{:f}", "str");
test_eq(error_str, "{:d}", "str");
test_eq(error_str, "{:x}", "str");
test_eq(error_str, "{:X}", "str");
test_eq(error_str, "{:o}", "str");
test_eq(error_str, "{:b}", "str");
test_eq(error_str, "{:B}", "str");
}
static void test_char()
{
test_eq("A", "{}", 'A');
test_eq("A", "{:c}", 'A');
test_eq("A ", "{:6}", 'A');
test_eq(" A", "{:>6}", 'A');
test_eq("65", "{:d}", 'A');
test_eq("0x41", "{:#x}", 'A');
test_eq("0101", "{:#o}", 'A');
test_eq("0b1000001", "{:#b}", 'A');
test_eq(error_str, "{:s}", 'E');
test_eq(error_str, "{:f}", 'E');
}
static void test_float()
{
// float
test_eq("1.200000", "{}", 1.2f);
test_eq("-1.200000", "{}", -1.2f);
test_eq("1.200000", "{:f}", 1.2f);
test_eq("1.2", "{:.1}", 1.2f);
test_eq("-1.2", "{:.1}", -1.2f);
test_eq("1", "{:.0}", 1.2f);
test_eq("-1", "{:.0}", -1.2f);
test_eq(" 1", "{:6.0}", 1.2f);
test_eq(" -1", "{:6.0}", -1.2f);
test_eq(" 1.2", "{:6.1}", 1.2f);
test_eq(" -1.2", "{:6.1}", -1.2f);
test_eq(" 1.2", "{:>6.1}", 1.2f);
test_eq(" -1.2", "{:>6.1}", -1.2f);
test_eq("-1.2 ", "{:<6.1}", -1.2f);
test_eq("1.2 ", "{:<6.1}", 1.2f);
test_eq(" -1.2 ", "{:^6.1}", -1.2f);
test_eq("+1.2", "{:+.1}", 1.2f);
test_eq("-1.2", "{:+.1}", -1.2f);
test_eq(" 1.2", "{: .1}", 1.2f);
test_eq("-1.2", "{: .1}", -1.2f);
test_eq("nan", "{}", NAN);
test_eq("nan", "{:f}", NAN);
test_eq("NAN", "{:F}", NAN);
test_eq(" nan", "{:5}", NAN);
test_eq("nan ", "{:<5}", NAN);
test_eq("inf", "{}", INFINITY);
test_eq("inf", "{:f}", INFINITY);
test_eq("INF", "{:F}", INFINITY);
test_eq("-inf", "{}", -INFINITY);
test_eq("+inf", "{:+}", INFINITY);
test_eq("-inf", "{:+}", -INFINITY);
test_eq("+INF", "{:+F}", INFINITY);
test_eq("-INF", "{:+F}", -INFINITY);
test_eq(" inf", "{: }", INFINITY);
test_eq("-inf", "{: }", -INFINITY);
test_eq(" inf", "{:5}", INFINITY);
test_eq(" -inf", "{:5}", -INFINITY);
test_eq("inf ", "{:<5}", INFINITY);
test_eq("-inf ", "{:<5}", -INFINITY);
test_eq("1000.0", "{:.1}", 1000.0f);
test_eq("3210.9", "{:.1}", 3210.9f);
test_eq("7654.3", "{:.1}", 7654.3f);
test_eq("10.0", "{:.1}", 10.0f);
test_eq("100.0", "{:.1}", 100.0f);
test_eq("1000.0", "{:.1}", 1000.0f);
test_eq("10000.0", "{:.1}", 10000.0f);
test_eq("100000.0", "{:.1}", 100000.0f);
test_eq("1000000.0", "{:.1}", 1000000.0f);
test_eq("10000000.0", "{:.1}", 10000000.0f);
test_eq("100000000.0", "{:.1}", 100000000.0f);
test_eq("1000000000.0", "{:.1}", 1000000000.0f);
test_eq("10000000000.0", "{:.1}", 10000000000.0f);
// double
test_eq("1.200000", "{}", 1.2);
test_eq("-1.200000", "{}", -1.2);
test_eq("9.999999", "{}", 9.999999);
test_eq("0.999999", "{}", 0.999999);
test_eq("10000000000.0", "{:.1}", 10000000000.0);
test_eq("1000000000000000.0", "{:.1}", 1000000000000000.0);
test_eq("100000000000000000000.0", "{:.1}", 100000000000000000000.0);
test_eq("10000000000000000000000.0", "{:.1}", 10000000000000000000000.0);
// compalre with printf
for (int64_t i = -10'000; i < 1'000'000; i += 11)
{
double value = i / 1003.123;
test_cmp_printf(".13", value);
}
for (int64_t i = 1'000'000; i < 1'000'000'000; i += 10003)
{
double value = i / 1003.321;
test_cmp_printf(".13", value);
}
for (int64_t i = -10'000; i < 1'000'000'000; i += 10003)
{
double value = i / 13.777;
test_cmp_printf(".11", value);
}
// errors
test_eq(error_str, "{:s}", 123.0f);
test_eq(error_str, "{:s}", 123.0);
test_eq(error_str, "{:c}", 123.0f);
test_eq(error_str, "{:c}", 123.0);
test_eq(error_str, "{:d}", 123.0f);
test_eq(error_str, "{:d}", 123.0);
test_eq(error_str, "{:x}", 123.0f);
test_eq(error_str, "{:x}", 123.0);
test_eq(error_str, "{:X}", 123.0f);
test_eq(error_str, "{:X}", 123.0);
test_eq(error_str, "{:o}", 123.0f);
test_eq(error_str, "{:o}", 123.0);
test_eq(error_str, "{:b}", 123.0f);
test_eq(error_str, "{:b}", 123.0);
test_eq(error_str, "{:B}", 123.0f);
test_eq(error_str, "{:B}", 123.0);
}
static void test_arg_pos()
{
test_eq("1234", "{}{}{}{}", 1, 2, 3, 4);
test_eq("1234", "{0}{1}{2}{3}", 1, 2, 3, 4);
test_eq("1144", "{0}{0}{3}{3}", 1, 2, 3, 4);
test_eq("text", "text", 1, 2, 3, 4);
test_eq("4321", "{3}{2}{1}{0}", 1, 2, 3, 4);
test_eq("1"+error_str+"1", "{0}{1}{0}", 1);
}
static void test_individual_functions()
{
char buffer[256] = {};
mf::format_dec(buffer, 777);
assert(strcmp(buffer, "777") == 0);
mf::format_dec(buffer, -12345);
assert(strcmp(buffer, "-12345") == 0);
mf::format_dec(buffer, 12345);
assert(strcmp(buffer, "12345") == 0);
mf::format_hex(buffer, 0xabcd4321);
assert(strcmp(buffer, "abcd4321") == 0);
mf::format_bin(buffer, 0b111100111000110);
assert(strcmp(buffer, "111100111000110") == 0);
mf::format_float(buffer, -1234.56789, 3);
assert(strcmp(buffer, "-1234.568") == 0);
mf::format_float(buffer, INFINITY, 3);
assert(strcmp(buffer, "inf") == 0);
mf::format_float(buffer, -INFINITY, 3);
assert(strcmp(buffer, "-inf") == 0);
mf::format_float(buffer, NAN, 3);
assert(strcmp(buffer, "nan") == 0);
}
static void test_print_to_buffer()
{
char buffer1[3] = {0, 1, 2};
auto printed = mf::format_dec(buffer1, 2, -12345);
assert(printed == 1);
assert(buffer1[0] == '-');
assert(buffer1[1] == 0);
assert(buffer1[2] == 2);
char buffer2[3] = { 0, 1, 2 };
printed = mf::format_dec(buffer2, 2, 12345U);
assert(printed == 1);
assert(buffer2[0] == '1');
assert(buffer2[1] == 0);
assert(buffer2[2] == 2);
char buffer3[7] = { 0, 1, 2, 3, 4, 5, 6 };
printed = mf::format_float(buffer3, 6, -1.123456789, 10);
assert(printed == 5);
assert(buffer3[0] == '-');
assert(buffer3[1] == '1');
assert(buffer3[2] == '.');
assert(buffer3[3] == '1');
assert(buffer3[4] == '2');
assert(buffer3[5] == 0);
assert(buffer3[6] == 6);
char buffer4[7] = { 0, 1, 2, 3, 4, 5, 6 };
printed = mf::format(buffer4, 6, "{}{}{}{}{}{}{}{}{}{}", 10, 20, 30, 40, 50, 60, 70);
assert(printed == 5);
assert(buffer4[0] == '1');
assert(buffer4[1] == '0');
assert(buffer4[2] == '2');
assert(buffer4[3] == '0');
assert(buffer4[4] == '3');
assert(buffer4[5] == 0);
assert(buffer4[6] == 6);
}
static void test_utf8()
{
// correct sequenses
test_eq_unicode(u8"Русский текст", u8"Русский текст");
test_eq_unicode(u8"日本語テキスト", u8"日本語テキスト");
test_eq_unicode(u8"Русский текст 日本語テキスト", u8"Русский текст {}", u8"日本語テキスト");
test_eq_unicode(u8"-Русский текст-日本語テキスト-", "-{}-{}-", u8"Русский текст", u8"日本語テキスト");
// wrong sequenses
test_eq_unicode("before ? after", "before \xC0\xC1 after");
}
int main()
{
test_common();
test_types();
test_integer();
test_bool();
test_str();
test_char();
test_float();
test_arg_pos();
test_individual_functions();
test_print_to_buffer();
test_utf8();
}