-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
1057 lines (968 loc) · 38.2 KB
/
test.c
File metadata and controls
1057 lines (968 loc) · 38.2 KB
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include "./smallunit.h"
#define FMT_IMPLEMENTATION
//#define FMT_BIN_GROUP_NIBBLES
#define FMT_DEFAULT_FLOAT_PRECISION -1
//#define FMT_FAST_DISPLAY_WIDTH
#include "fmt.h"
#define STRINGIFY_2(x) #x
#define STRINGIFY(x) STRINGIFY_2(x)
enum { STRING_BUF_SIZE = 256 };
static char* get_string_buf() {
static char buf[STRING_BUF_SIZE];
return buf;
}
bool compact_flag = false;
bool stop_on_failure_flag = false;
////////////////////////////////////////////////////////////////////////////////
// Expect formatted string
////////////////////////////////////////////////////////////////////////////////
static bool expect_check(
int source_line, const char *expected, const char *got, int written
) {
if (strcmp(expected, got) != 0) {
//size_t diff;
//for (diff = 0; expected[diff] == got[diff] && got[diff]; ++diff) {}
//const char *part2 = got + diff;
//fmt_eprintln(
// " Line {}:\n mismatch:\n expected: \"{}\"\n got: \"{:.{}}\x1b[31m{}\x1b[0m\"",
// source_line, expected, got, diff, part2
//);
fmt_eprintln(
" Line {}:\n mismatch:\n expected: \"{}\"\n got: \"{}\"",
source_line, expected, got
);
return false;
}
const int expect_written = strlen(expected);
if (written != expect_written) {
fmt_eprintln(
" Line {}:\n wrong number of bytes written reported:\n"
" expected: {}\n got: {}\n string: \"{}\"",
source_line, expect_written, written, got
);
return false;
}
return true;
}
static bool expect_impl(
int source_line, const char *expected, const char *fmt, int arg_count, ...
) {
char *const buf = get_string_buf();
memset(buf, 0, STRING_BUF_SIZE);
va_list ap;
va_start(ap, arg_count);
fmt_String_Writer writer = fmt_sw_new(buf, STRING_BUF_SIZE);
const int written = fmt_va_write((fmt_Writer *)&writer, fmt, arg_count, ap);
va_end(ap);
return expect_check(source_line, expected, buf, written);
}
#define expect(_expected, _fmt, ...) \
do { \
if (!expect_impl( \
__LINE__, \
_expected, \
_fmt, \
FMT_VA_ARG_COUNT(__VA_ARGS__) \
__VA_OPT__(, FMT_ARGS(__VA_ARGS__)) \
)) { \
su_fail(); \
} \
} while(0)
static bool expect_time_impl(
int source_line, const char *expected, const char *fmt, const struct tm *datetime
) {
char *const buf = get_string_buf();
memset(buf, 0, STRING_BUF_SIZE);
fmt_String_Writer writer = fmt_sw_new(buf, STRING_BUF_SIZE);
const int written = fmt_write_time((fmt_Writer *)&writer, fmt, datetime);
return expect_check(source_line, expected, buf, written);
}
#define expect_time(_expected, _fmt, _tm) \
do { \
if (!expect_time_impl(__LINE__, _expected, _fmt, _tm)) { \
su_fail(); \
} \
} while (0)
////////////////////////////////////////////////////////////////////////////////
// Expect format specifier
////////////////////////////////////////////////////////////////////////////////
static fmt_String expect_spec_get_check_impl(int number, const char *checks) {
const char *before, *after;
if (number) {
int i;
// note: postfix incement, will point after the comma
for (before = checks, i = 0; i < number; i += *before++ == ',') {}
} else {
// points after the opening parenthesis
before = checks + 1;
}
after = strchr(before, ',');
if (!after) {
after = before + strlen(before) - 1;
}
// comma_after either points at the comma after or the closing parenthesis now
while (*before == ' ') {
++before;
}
while (*after == ' ') {
--after;
}
return (fmt_String) {
.data = (char *)before,
.size = (size_t)(after - before),
.capacity = 0,
};
}
/// Get the text for the `number`th check from the variadic arguments
#define expect_spec_get_check(number, ...) \
expect_spec_get_check_impl((number), STRINGIFY((__VA_ARGS__)))
static const char * my_parse_specifier(
fmt_Format_Specifier *out,
fmt_Type_Id type,
const char *format_specifier,
int *arg_count,
...
) {
va_list ap;
va_start(ap, arg_count);
const char *const end = fmt__parse_specifier(
format_specifier, out, type, 1, arg_count, FMT__VA_LIST_REF(ap)
);
va_end(ap);
return end;
}
#define expect_spec(_type, _format, ...) \
do { \
fmt_Format_Specifier spec; \
fmt__format_specifier_default(&spec); \
int arg_count = FMT_VA_ARG_COUNT(__VA_ARGS__); \
const char *end = my_parse_specifier( \
&spec, \
_type, \
_format, \
&arg_count \
__VA_OPT__(, FMT_ARGS(__VA_ARGS__)) \
); \
su_assert_eq(*end, '\0'); \
su_assert_eq(arg_count, 0); \
expect_spec_2
#define expect_spec_2(...) \
bool checks[] = { __VA_ARGS__ }; \
const int count = sizeof(checks) / sizeof(*checks); \
for (int i = 0; i < count; ++i) { \
if (!checks[i]) { \
fmt_eprintln( \
"format specifier check failed near line {}: {}", \
__LINE__, \
expect_spec_get_check(i, __VA_ARGS__) \
); \
su_fail(); \
} \
} \
} while (0)
////////////////////////////////////////////////////////////////////////////////
// Expect time format specifier
////////////////////////////////////////////////////////////////////////////////
#define expect_time_spec(_format) \
do { \
fmt_Format_Specifier spec; \
fmt__time_format_specifier_default(&spec, 0); \
const char *end = fmt__parse_time_specifier( \
_format, \
&spec, \
1 \
); \
su_assert_eq(*end, '\0'); \
expect_time_spec_2
#define expect_time_spec_2(...) \
bool checks[] = { __VA_ARGS__ }; \
const int count = sizeof(checks) / sizeof(*checks); \
for (int i = 0; i < count; ++i) { \
if (!checks[i]) { \
fmt_eprintln( \
"time format specifier check failed near line {}: {}", \
__LINE__, \
expect_spec_get_check(i, __VA_ARGS__) \
); \
su_fail(); \
} \
} \
} while (0)
////////////////////////////////////////////////////////////////////////////////
// Panic
////////////////////////////////////////////////////////////////////////////////
static bool expect_panic_impl(
int source_line, const char *message, const char *format, int arg_count, ...
) {
char *const buf = get_string_buf();
int pipe_ends[2];
pipe(pipe_ends);
const int rx = pipe_ends[0];
const int tx = pipe_ends[1];
const int pid = fork();
switch (pid) {
case -1:
fmt_panic("fork failed");
case 0:
dup2(tx, STDERR_FILENO);
close(rx);
va_list ap;
va_start(ap, arg_count);
fmt_va_sprint(buf, STRING_BUF_SIZE, format, arg_count, ap);
va_end(ap);
close(tx);
exit(EXIT_SUCCESS);
}
close(tx);
int stat = -1;
waitpid(pid, &stat, 0);
if (stat == 0) {
fmt_eprintln(
" Line {}:\n expected call to panic but it didn't", source_line
);
return false;
}
const ssize_t n = read(rx, buf, STRING_BUF_SIZE);
buf[n - 1] = '\0';
close(rx);
// I have no idea why but inserts a newline between the fmt__panic_loc
// and fmt_va_write calls here. This does not happen when printing to
// the terminal.
for (int i = 0; i < n; ++i) {
if (buf[i] == '\n') {
memmove(buf + i, buf + i + 1, n - i);
}
}
// skip "file:line: "
char *start = strchr(strchr(buf, ':') + 1, ':') + 2;
if (memcmp(start, message, strlen(message))) {
fmt_eprintln(
" Line {}:\n panic message mismatch:\n expected: \"{}\"\n got: \"{}\"",
source_line, message, start
);
return false;
}
return true;
}
/// Checks if a call panics and it's panic message after the source location
/// starts with the given message.
#define expect_panic(_message, _format, ...) \
do { \
if (!expect_panic_impl( \
__LINE__, \
_message, \
_format, \
FMT_VA_ARG_COUNT(__VA_ARGS__) \
__VA_OPT__(, FMT_ARGS(__VA_ARGS__)) \
)) { \
su_fail(); \
} \
} while(0)
////////////////////////////////////////////////////////////////////////////////
// Unicode test cases
////////////////////////////////////////////////////////////////////////////////
typedef struct {
const fmt_char8_t *data;
fmt_char32_t codepoint;
int size;
} Unicode_Test_Case;
const Unicode_Test_Case unicode_test_cases[] = {
{(const fmt_char8_t *)"a", U'a', 1},
{(const fmt_char8_t *)"Ä", U'Ä', 2},
{(const fmt_char8_t *)"가", U'가', 3},
{(const fmt_char8_t *)"💚", U'💚', 4},
};
const int unicode_test_case_count = sizeof(unicode_test_cases) / sizeof(Unicode_Test_Case);
////////////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////////////
static int format_specifier_parsing_tests() {
void *su__skip = &&skip;
int su__status = SU_PASS;
expect_spec(fmt__TYPE_INT, "{x}")(
spec.type == 'x'
);
expect_spec(fmt__TYPE_INT, "{:{}^+#{}..{}?}", 'a', 123, 456)(
spec.fill == 'a',
spec.align == fmt_ALIGN_CENTER,
spec.sign == fmt_SIGN_ALWAYS,
spec.alternate_form == true,
spec.group == '.',
spec.precision == 456,
spec.debug == true
);
expect_spec(fmt__TYPE_INT, "{:.}")(
spec.group == '.'
);
expect_spec(fmt__TYPE_INT, "{:.?}")(
spec.group == '.'
);
expect_spec(fmt__TYPE_INT, "{:.1?}")(
spec.precision == 1,
spec.debug == true,
);
expect_spec(fmt__TYPE_INT, "{:{}<0}", 'a')(
spec.fill == '0',
spec.align == fmt_ALIGN_AFTER_SIGN,
);
fmt_Format_Specifier def;
fmt__format_specifier_default(&def);
expect_spec(fmt__TYPE_INT, "{:?}")(
spec.group != '?',
spec.debug == true
);
skip:
return su__status;
}
static int time_format_specifier_parsing_tests() {
void *su__skip = &&skip;
int su__status = SU_PASS;
expect_time_spec("{H}")(
spec.type == 'H'
);
expect_time_spec("{M: <.2}")(
spec.type == 'M',
spec.fill == ' ',
spec.align == fmt_ALIGN_LEFT,
spec.precision == 2,
);
skip:
return su__status;
}
su_module_d(internal_functions, "internal functions", {
su_compact = compact_flag;
su_stop_on_failure = stop_on_failure_flag;
su_test("argument count", {
su_assert_eq(FMT_VA_ARG_COUNT(), 0);
su_assert_eq(FMT_VA_ARG_COUNT(1), 1);
su_assert_eq(FMT_VA_ARG_COUNT(1, 2), 2);
su_assert_eq(FMT_VA_ARG_COUNT(,), 2);
})
su_test("integer width", {
su_assert_eq(fmt__unsigned_width(1000, 10), 4);
su_assert_eq(fmt__unsigned_width(0x1000, 16), 4);
su_assert_eq(fmt__unsigned_width(0b1000, 2), 4);
su_assert_eq(fmt__unsigned_width(01000, 8), 4);
})
su_test("display width", {
su_assert_eq(fmt__utf8_width_and_length("Hello", -1, -1).first, 5);
su_assert_eq(fmt__utf8_width_and_length("Hello\n", -1, -1).first, 5);
su_assert_eq(fmt__utf8_width_and_length("안녕", -1, -1).first, 4);
su_assert_eq(fmt__utf8_width_and_length("안녕", -1, 1).first, 2);
su_assert_eq(fmt__utf8_width_and_length("Hello", -1, -1).second, 5);
su_assert_eq(fmt__utf8_width_and_length("안녕", -1, -1).second, 2);
su_assert_eq(fmt__utf16_width_and_length(u"Hello", -1, -1).first, 5);
su_assert_eq(fmt__utf16_width_and_length(u"안녕", -1, -1).first, 4);
su_assert_eq(fmt__utf16_width_and_length(u"안녕", -1, 1).first, 2);
su_assert_eq(fmt__utf16_width_and_length(u"\U00012345", -1, -1).first, 1);
su_assert_eq(fmt__utf16_width_and_length(u"\U00012345", -1, -1).second, 1);
su_assert_eq(fmt__utf32_width(U"Hello", -1), 5);
su_assert_eq(fmt__utf32_width(U"안녕", -1), 4);
su_assert_eq(fmt__utf32_width(U"안녕", 1), 2);
#ifndef FMT_FAST_DISPLAY_WIDTH
su_assert_eq(fmt__display_width(u'\u0303'), 0);
#endif
su_assert_eq(fmt__debug_char_width('a', false), 1);
su_assert_eq(fmt__debug_char_width('\n', false), 2);
su_assert_eq(fmt__debug_char_width('\n', true), 2);
su_assert_eq(fmt__debug_char_width('\'', false), 1);
su_assert_eq(fmt__debug_char_width('\'', true), 2);
})
su_test("utf8 encode and decode", {
// ENCODE
char data[4];
for (
const Unicode_Test_Case
*c = unicode_test_cases,
*end = unicode_test_cases + unicode_test_case_count;
c != end;
++c
) {
su_assert_eq(fmt__utf8_encode(c->codepoint, data), c->size);
su_assert(memcmp(data, c->data, c->size) == 0);
}
// DECODE
fmt_char32_t codepoint;
for (
const Unicode_Test_Case
*c = unicode_test_cases,
*end = unicode_test_cases + unicode_test_case_count;
c != end;
++c
) {
su_assert_eq(fmt__utf8_decode(c->data, &codepoint), c->size);
su_assert_eq(codepoint, c->codepoint);
}
})
su_test("format specifier parsing", {
// Something about the 2-part macro with the variadic arguments
// irritates the smallunit macros so this needs to be in a separate
// function.
su__status = format_specifier_parsing_tests();
})
su_test("time format specifier parsing", {
// as above
su__status = time_format_specifier_parsing_tests();
})
#ifndef __cplusplus
su_test("strftime format string translation", {
enum { COUNT = 3};
const char **sources = ((const char*[COUNT]){
"%H",
"%%",
"{a}",
});
const char **expected = ((const char*[COUNT]){
"{H}",
"%",
// Closing curly braces are not currently escaped
//"{{a}}"
"{{a}"
});
char buf[6];
for (int i = 0; i < COUNT; ++i) {
fmt_translate_strftime(sources[i], buf, sizeof(buf));
if (strcmp(buf, expected[i]) != 0) {
fmt_eprintln("\"{}\" != \"{}\"", buf, expected[i]);
su_fail();
}
}
})
#endif
})
su_module_d(basic_printing, "basic printing", {
su_compact = compact_flag;
su_stop_on_failure = stop_on_failure_flag;
su_test("characters", {
expect("a", "{}", (char)'a');
expect("a", "{c}", u'a');
expect("a", "{c}", U'a');
expect("a", "{c}", L'a');
#ifdef __cplusplus
expect("a", "{}", L'a');
#endif
expect("가", "{c}", u'가');
expect("a", "{c}", 97);
expect("�", "{c}", -97);
expect("�", "{c}", FMT_MAX_CODEPOINT + 1);
})
su_test("strings", {
expect("hello", "{}", "hello");
expect("hello", "{}", u"hello");
expect("hello", "{}", U"hello");
expect("hello", "{}", L"hello");
expect("안녕", "{}", U"안녕");
})
su_test("booleans", {
bool t = true;
bool f = false;
expect("true", "{}", t);
expect("false", "{}", f);
expect("true", "{B}", 1 != 2);
})
su_test("integers", {
expect("0", "{}", 0);
expect("0", "{b}", 0);
expect("0", "{x}", 0);
expect("0", "{X}", 0);
expect("0", "{o}", 0);
expect("123", "{}", 123);
expect("123", "{d}", 123);
expect("123", "{i}", 123);
expect("-123", "{}", -123);
expect("123abc", "{x}", 0x123abc);
expect("-123abc", "{x}", -0x123abc);
expect("123ABC", "{X}", 0x123abc);
expect("-123ABC", "{X}", -0x123abc);
expect("11011", "{b}", 0b11011);
expect("-11011", "{b}", -0b11011);
expect("644", "{o}", 0644);
expect("-644", "{o}", -0644);
expect("18446744073709551615", "{}", 18446744073709551615ULL);
expect("1111111111111111111111111111111111111111111111111111111111111111", "{b}", 18446744073709551615ULL);
expect("1777777777777777777777", "{o}", 18446744073709551615ULL);
expect("ffffffffffffffff", "{x}", 18446744073709551615ULL);
expect("FFFFFFFFFFFFFFFF", "{X}", 18446744073709551615ULL);
expect("$12.00", "{$}", 12);
expect("-$10.00", "{$}", -10);
})
su_test("floats", {
expect("-2", "{}", -2.0);
expect("-1", "{}", -1.0);
expect("-0.5", "{}", -0.5);
expect("-0.1", "{}", -0.1);
expect("-0.01", "{}", -0.01);
expect("0", "{}", -0.0);
expect("0", "{}", 0.0);
expect("0.01", "{}", 0.01);
expect("0.1", "{}", 0.1);
expect("0.5", "{}", 0.5);
expect("1", "{}", 1.0);
expect("2", "{}", 2.0);
expect("3.141", "{}", 3.141);
expect("0.0123456789", "{:.10}", 0.0123456789);
expect("-3.141", "{:.3}", -3.141);
expect("73786976294838210000", "{}", 0x1p66);
expect("inf", "{}", INFINITY);
expect("INF", "{F}", INFINITY);
expect("nan", "{}", NAN);
expect("NAN", "{F}", NAN);
expect("-inf", "{}", -INFINITY);
expect("-INF", "{F}", -INFINITY);
expect("-nan", "{}", -NAN);
expect("-NAN", "{F}", -NAN);
expect("inf", "{}", HUGE_VAL);
expect("1e0", "{e}", 1.0);
expect("1e3", "{e}", 1000.0);
expect("1e-2", "{e}", 0.01);
expect("7.378697629483821e19", "{e}", 0x1p66);
expect("12.000%", "{%:.3}", 0.12);
expect("12.30000%", "{%:.5}", 0.123);
expect("50%", "{%:.0}", 0.5);
expect("3.14159", "{g}", 3.14159265359);
expect("314.159", "{g}", 314.159265359);
expect("31415.9", "{g}", 31415.9265359);
expect("3.14159e6", "{g}", 3141592.65359);
expect("3.14159e8", "{g}", 314159265.359);
expect("$3.14", "{$}", 3.141);
expect("-$1.00", "{$}", -1.0);
expect("1e-10", "{}", 1e-10);
expect("1e-7", "{}", 1e-7);
expect("0.000001", "{}", 1e-6);
expect("10", "{}", 1e1);
expect("10000000000", "{}", 1e10);
expect("100000000000000000000", "{}", 1e20);
expect("1e21", "{}", 1e21);
expect("1e100", "{}", 1e100);
})
su_test("pointers", {
const void *p = (void *)0x123;
const char *s = (char *)0x123;
const int *i = (int *)0x123;
expect("123", "{}", p);
expect("123", "{p}", s);
expect("123", "{p}", i);
expect("abc", "{}", (void*)0xabc);
expect("ABC", "{P}", (void*)0xabc);
expect("ABC", "{P}", (char*)0xabc);
})
su_test("fmt_String", {
char data[] = "Hello World";
fmt_String string = ((fmt_String) {
.data = data,
.size = sizeof(data) - 1,
.capacity = 0,
});
expect("Hello World", "{}", string);
string.size = 5;
expect("Hello World", "{} World", string);
string = fmt_format("World");
expect("Hello World", "Hello {}", string.take);
})
su_test("escaping", {
expect("{d}", "{{d}");
// turns out we don't need to escape in the format specifier, but we
// probably should require it? (TODO)
expect("{{{12{345", "{d:{>9{}", 12345);
expect_time("{H}", "{{H}", NULL);
expect("{H}", "{%{{H}%}", (struct tm *)NULL);
expect("{H}", "{%{}}", (struct tm *)NULL, "{{H}");
})
})
su_module(formatting, {
su_compact = compact_flag;
su_stop_on_failure = stop_on_failure_flag;
su_test("alignment (and width)", {
expect("Hello ", "{:9}", "Hello");
expect("Hello ", "{:<9}", "Hello");
expect(" Hello", "{:>9}", "Hello");
expect(" Hello ", "{:^9}", "Hello");
expect(" a ", "{:^5}", (char)'a');
expect(" 123 ", "{:^7}", 123);
expect("- 123", "{:=7}", -123);
expect(" true ", "{:^8}", (bool)true);
expect(" 2 ", "{:^7}", 2.0);
expect(" 2.1 ", "{:^7}", 2.1);
expect("2e2 ", "{e:<9}", 200.0);
expect(" 2e2", "{e:>9}", 200.0);
expect(" 2e2 ", "{e:^9}", 200.0);
expect("$1.20 ", "{$:<9}", 1.2);
expect(" $1.20", "{$:>9}", 1.2);
expect(" $1.20 ", "{$:^9}", 1.2);
expect("- $1.20", "{$:=9}", -1.2);
})
su_test("precision", {
expect("3.141000", "{:.6}", 3.141);
expect("3", "{:.0}", 3.141);
expect("1.2e3", "{e:.1}", 1234.0);
expect("inf", "{:.1}", INFINITY);
expect("java", "{:.4}", "javascript");
expect("안녕", "{:.$2}", "안녕하세요");
// Booleans are treated as strings
// The cast to bool is needed for C11 builds
expect("t", "{:.1}", (bool)true);
expect("3.14", "{g:.3}", 3.14159265359);
expect("314", "{g:.3}", 314.159265359);
expect("3.14e4", "{g:.3}", 31415.9265359);
expect("3.14e6", "{g:.3}", 3141592.65359);
expect("3.14e8", "{g:.3}", 314159265.359);
expect("3e8", "{g:.0}", 314159265.359);
})
su_test("grouping", {
expect("100", "{:'}", 100);
expect("1'000", "{:'}", 1000);
expect("10'000'000", "{:'}", 10000000);
expect("1'2345'6789", "{x:'}", 0x123456789);
expect("AB'CDEF", "{X:'}", 0xabcdef);
#ifdef FMT_BIN_GROUP_NIBBLES
expect("101'0101'0101", "{b:'}", 0b010101010101);
#else
expect("101'01010101", "{b:'}", 0b010101010101);
#endif
expect("777'644", "{o:'}", 0777644);
expect("1ä000", "{:ä}", 1000);
expect("1", "{:'}", 1.0);
expect("100", "{:'}", 100.0);
expect("1'000", "{:'}", 1000.0);
expect("10'000", "{:'}", 10000.0);
expect("100'000", "{:'}", 100000.0);
expect("1'000'000", "{:'}", 1000000.0);
expect(" 1'000", "{:>7'}", 1000);
expect(" 1'000", "{:>7'}", 1000.0);
})
su_test("field width, filling, and alignment", {
expect("10000", "{:0<5}", 100);
expect("00100", "{:0>5}", 100);
expect("0-100", "{:0>5}", -100);
expect("-0100", "{:05}", -100);
expect("-0100", "{:0=5}", -100);
expect("- 100", "{:=5}", -100);
expect(" 2 ", "{:^5.0}", 2.0);
expect("- 2", "{:=7}", -2.0);
expect("äääabc", "{:ä>6}", "abc");
expect("äääabc", "{:{}>6}", "abc", u'ä');
expect(" 1.234", "{:>7.}", 1234);
expect(" inf ", "{:^7}", INFINITY);
expect(" -inf ", "{:^8}", -INFINITY);
#ifndef FMT_FAST_DISPLAY_WIDTH
expect(" ã ", "{:^3}", "a\u0303");
#else
// Make sure we get the correct wrong result
expect("ã ", "{:^3}", "a\u0303");
#endif
})
su_test("alternate form", {
expect("0b101", "{b:#}", 0b101);
expect("0o123", "{o:#}", 00123);
expect("0xabc", "{x:#}", 0xabc);
expect("0XABC", "{X:#}", 0xabc);
expect("0xabc", "{p:#}", (void*)0xabc);
expect("0XABC", "{P:#}", (void*)0xabc);
expect("yes", "{:#}", (bool)true);
expect("no", "{:#}", (bool)false);
})
su_test("signing", {
expect("+123", "{:+}", 123);
expect(" 123", "{: }", 123);
expect("+2", "{:+}", 2.0);
expect(" 2", "{: }", 2.0);
expect("+$1.20", "{$:+}", 1.2);
expect(" $1.20", "{$: }", 1.2);
})
su_test("debug format", {
expect("'a'", "{c:?}", 'a');
expect("'\\0'", "{c:?}", '\0');
expect("'\\t'", "{c:?}", '\t');
expect("'\\r'", "{c:?}", '\r');
expect("'\\n'", "{c:?}", '\n');
expect("'\\\\'", "{c:?}", '\\');
expect("'\\''", "{c:?}", '\'');
expect("'\"'", "{c:?}", '"');
expect("'\\U00110000'", "{c:?}", FMT_MAX_CODEPOINT + 1);
// UTF-8
expect("a\"a\"a", "a{:?}a", "a");
expect("a\"\\0\"a", "a{:.1?}a", "\0");
expect("a\"\\t\"a", "a{:?}a", "\t");
expect("a\"\\r\"a", "a{:?}a", "\r");
expect("a\"\\n\"a", "a{:?}a", "\n");
expect("a\"\\\\\"a", "a{:?}a", "\\");
expect("a\"'\"a", "a{:?}a", "'");
expect("a\"\\\"\"a", "a{:?}a", "\"");
expect("\"aaa\"", "{:?}", "aaa");
expect("\"a\\0a\"", "{:.3?}", "a\0a");
expect("\"a\\ta\"", "{:?}", "a\ta");
expect("\"a\\ra\"", "{:?}", "a\ra");
expect("\"a\\na\"", "{:?}", "a\na");
expect("\"a\\\\a\"", "{:?}", "a\\a");
expect("\"a'a\"", "{:?}", "a'a");
expect("\"a\\\"a\"", "{:?}", "a\"a");
expect("\"\\0\\0\\0\"", "{:.3?}", "\0\0\0");
expect("\"\\t\\t\\t\"", "{:?}", "\t\t\t");
expect("\"\\r\\r\\r\"", "{:?}", "\r\r\r");
expect("\"\\n\\n\\n\"", "{:?}", "\n\n\n");
expect("\"\\\\\\\\\\\\\"", "{:?}", "\\\\\\");
expect("\"\\\"\\\"\\\"\"", "{:?}", "\"\"\"");
// UTF-16
expect("a\"a\"a", "a{:?}a", u"a");
expect("a\"\\0\"a", "a{:.1?}a", u"\0");
expect("a\"\\t\"a", "a{:?}a", u"\t");
expect("a\"\\r\"a", "a{:?}a", u"\r");
expect("a\"\\n\"a", "a{:?}a", u"\n");
expect("a\"\\\\\"a", "a{:?}a", u"\\");
expect("a\"'\"a", "a{:?}a", u"'");
expect("a\"\\\"\"a", "a{:?}a", u"\"");
expect("\"aaa\"", "{:?}", u"aaa");
expect("\"a\\0a\"", "{:.3?}", u"a\0a");
expect("\"a\\ta\"", "{:?}", u"a\ta");
expect("\"a\\ra\"", "{:?}", u"a\ra");
expect("\"a\\na\"", "{:?}", u"a\na");
expect("\"a\\\\a\"", "{:?}", u"a\\a");
expect("\"a'a\"", "{:?}", u"a'a");
expect("\"a\\\"a\"", "{:?}", u"a\"a");
expect("\"\\0\\0\\0\"", "{:.3?}", u"\0\0\0");
expect("\"\\t\\t\\t\"", "{:?}", u"\t\t\t");
expect("\"\\r\\r\\r\"", "{:?}", u"\r\r\r");
expect("\"\\n\\n\\n\"", "{:?}", u"\n\n\n");
expect("\"\\\\\\\\\\\\\"", "{:?}", u"\\\\\\");
expect("\"\\\"\\\"\\\"\"", "{:?}", u"\"\"\"");
// UTF-32
expect("a\"a\"a", "a{:?}a", U"a");
expect("a\"\\0\"a", "a{:.1?}a", U"\0");
expect("a\"\\t\"a", "a{:?}a", U"\t");
expect("a\"\\r\"a", "a{:?}a", U"\r");
expect("a\"\\n\"a", "a{:?}a", U"\n");
expect("a\"\\\\\"a", "a{:?}a", U"\\");
expect("a\"'\"a", "a{:?}a", U"'");
expect("a\"\\\"\"a", "a{:?}a", U"\"");
expect("\"aaa\"", "{:?}", U"aaa");
expect("\"a\\0a\"", "{:.3?}", U"a\0a");
expect("\"a\\ta\"", "{:?}", U"a\ta");
expect("\"a\\ra\"", "{:?}", U"a\ra");
expect("\"a\\na\"", "{:?}", U"a\na");
expect("\"a\\\\a\"", "{:?}", U"a\\a");
expect("\"a'a\"", "{:?}", U"a'a");
expect("\"a\\\"a\"", "{:?}", U"a\"a");
expect("\"\\0\\0\\0\"", "{:.3?}", U"\0\0\0");
expect("\"\\t\\t\\t\"", "{:?}", U"\t\t\t");
expect("\"\\r\\r\\r\"", "{:?}", U"\r\r\r");
expect("\"\\n\\n\\n\"", "{:?}", U"\n\n\n");
expect("\"\\\\\\\\\\\\\"", "{:?}", U"\\\\\\");
expect("\"\\\"\\\"\\\"\"", "{:?}", U"\"\"\"");
// Width calculation
expect(" '\\n' ", "{c:^8?}", '\n');
expect(" \"\\n\" ", "{:^8?}", "\n");
expect(" \"\\n\" ", "{:^8?}", u"\n");
expect(" \"\\n\" ", "{:^8?}", U"\n");
// Alternate format
expect("\\n", "{:#?}", "\n");
expect("\\n", "{c:#?}", '\n');
expect(" \\n ", "{c:^#6?}", '\n');
expect(" \\n ", "{:^#6?}", "\n");
expect(" \\n ", "{:^#6?}", u"\n");
expect(" \\n ", "{:^#6?}", U"\n");
expect("\"'", "{:#?}", "\"'");
expect("'", "{c:#?}", '\'');
expect("\"'", "{:#?}", u"\"'");
expect("\"'", "{:#?}", U"\"'");
expect(" \"' ", "{:^#6?}", "\"'");
expect(" ' ", "{c:^#5?}", '\'');
expect(" \"' ", "{:^#6?}", u"\"'");
expect(" \"' ", "{:^#6?}", U"\"'");
})
})
su_module(datetime, {
su_compact = compact_flag;
su_stop_on_failure = stop_on_failure_flag;
struct tm datetime_value;
memset(&datetime_value, 0, sizeof(struct tm));
datetime_value.tm_sec = 1;
datetime_value.tm_min = 2;
datetime_value.tm_hour = 3;
datetime_value.tm_mday = 4;
datetime_value.tm_mon = 5;
datetime_value.tm_year = 123;
datetime_value.tm_wday = 6;
datetime_value.tm_yday = 7;
datetime_value.tm_isdst = false; // daylight savings
datetime_value.tm_zone = (char *)"Timezone name";
datetime_value.tm_gmtoff = -14400; // -4 hours
const struct tm *datetime = &datetime_value;
su_test("basic time printing", {
expect_time("Sat Jun 4 03:02:01 2023", "{a} {b} {d:0} {H}:{M}:{S} {Y}", datetime);
expect_time("008", "{j}", datetime);
expect_time("23", "{y}", datetime);
expect_time("7", "{u}", datetime);
expect_time("6", "{w}", datetime);
expect_time("04", "{d}", datetime);
expect_time("03:02:01 AM", "{r}", datetime);
expect_time("03:02", "{R}", datetime);
expect_time("03:02:01", "{T}", datetime);
expect_time("-0400", "{z}", datetime);
expect_time("Timezone name", "{Z}", datetime);
})
su_test("12 hour clock", {
int old = datetime_value.tm_hour;
datetime_value.tm_hour = 0;
expect_time("12 AM", "{I} {p}", datetime);
datetime_value.tm_hour = 1;
expect_time("01 am", "{I} {P}", datetime);
datetime_value.tm_hour = 11;
expect_time("11 AM", "{I} {p}", datetime);
datetime_value.tm_hour = 12;
expect_time("12 pm", "{I} {P}", datetime);
datetime_value.tm_hour = 13;
expect_time("01 PM", "{I} {p}", datetime);
datetime_value.tm_hour = 23;
expect_time("11 pm", "{I} {P}", datetime);
datetime_value.tm_hour = old;
})
su_test("embedded time strings", {
expect("Hello Sat Jun 4 03:02:01 2023 World", "Hello {} World", datetime);
expect("Hello 03:02:01 World", "Hello {%{}} World", datetime, "{H}:{M}:{S}");
expect("Hello 03:02:01 World", "Hello {%{H}:{M}:{S}%} World", datetime);
})
su_test("formatting", {
expect_time(" Sat", "{a:>7}", datetime);
expect_time("....Sat", "{a:.>7}", datetime);
expect_time("Satu", "{A:.4}", datetime);
})
su_test("embedded formatting", {
expect(" 03:02 ", "{%{H}:{M}%:^9}", datetime);
expect(" Satu ", "{%{A:.4}%:^8}", datetime);
expect(" Satu ", "{%{A}%:^8.4}", datetime);
expect("::Sat::", "{%{a}%::^7}", datetime);
expect("::Sat Jun 4 03:02:01 2023::", "{::^27}", datetime);
})
su_test("strftime compatibility", {
setlocale(LC_TIME, "POSIX");
// Omitted: DEGgklnOtUVW%
//
// The 'u' field is also omitted, the standard says: "Replaced by the
// weekday as a decimal number [1,7], with 1 representing Monday.", but
// strftime seems to use the range [0,6] like the 'w' field for some
// reason.
#ifdef __APPLE__
// The %P specifier is a GNU extension
// TODO: %z gives me a different result from strftime than on Linux?
const char specifiers[] = "aAbBcCdeFHIjMprRsSwxXyYZ";
#else
const char specifiers[] = "aAbBcCdeFHIjMpPrRsSwxXyYzZ";
#endif
char strftime_format[] = "%_";
char fmt_format[] = "{_}";
enum { SIZE = 64};
char strftime_result[SIZE];
char fmt_result[SIZE];
int written;
for (const char *s = specifiers; *s; ++s) {
strftime_format[1] = *s;
fmt_format[1] = *s;
strftime(strftime_result, SIZE, strftime_format, datetime);
written = fmt_format_time_to(fmt_result, SIZE, fmt_format, datetime);
if (!expect_check(__LINE__, strftime_result, fmt_result, written)) {
fmt_eprintln(" field: {}", fmt_format);
su_fail();
}
}
})
})
su_module(panics, {
su_compact = compact_flag;
su_stop_on_failure = stop_on_failure_flag;
su_test("argument count", {
expect_panic("arguments exhausted at specifier 1", "{}");
expect_panic("3 arguments left", "", 1, 2, 3);
expect_panic("arguments exhausted at fill character", "{:{}>2}", 1);
expect_panic("arguments exhausted at width", "{:{}}", 1);
expect_panic("arguments exhausted at precision", "{:.{}}", 1);
expect_panic("arguments exhausted at parameterized time format", "{%{}}", (struct tm *)NULL);
})
su_test("format specifier format", {
expect_panic("overflow in width", "{:>99999999999}", 1);
expect_panic("expected : or } after display type", "{d{}", 1);
expect_panic("undelimited time format string", "{%{{H}}", (struct tm *)NULL);
})
su_test("conversion specifiers", {
expect_panic("invalid display type", "{s}", 1);
expect_panic("invalid display type", "{d}", "a");
expect_panic("invalid display type", "{p}", 1.2);
long i = 0;
expect_panic("unimplemented argument type", "{c}", &i);
expect_panic("invalid display type", "{g}", (char)'a');
})
su_test("overflow", {
expect_panic("string writer overflow", "{:>999}", 'a');
})
su_test("parameter types", {
expect_panic("expected integer type", "{:{}}", 1, "width");
expect_panic("expected unsigned value", "{:{}}", 1, -1);
expect_panic("expected character type", "{:{}>1}", 1, 1.0);
})
})
su_module(writers, {
su_test("allocating writer", {
enum { COUNT = 1000 };
fmt_Allocating_String_Writer writer = fmt_aw_new();
for (int i = 0; i < COUNT; ++i) {