-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_suite.cpp
More file actions
1285 lines (1205 loc) · 40.5 KB
/
test_suite.cpp
File metadata and controls
1285 lines (1205 loc) · 40.5 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
/*
Copyright (c) 2015, 2016, 2020, 2023, 2024 Andreas F. Borchert
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
test suite for "printf.hpp" with test cases where
std::printf and fmt::printf are expected to deliver
identical results
*/
#include <algorithm>
#include <cerrno>
#include <clocale>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <cwchar>
#include <limits>
#include <sstream>
#include <string>
#include <type_traits>
#include "printf.hpp"
#ifdef __INTEL_COMPILER
/* disable warnings of the Intel compiler for out of range values
as they are intentional */
#pragma warning(disable:265)
#endif
static unsigned int testcases = 0;
static unsigned int successful = 0;
static unsigned int warnings = 0;
static unsigned int skipped = 0;
static unsigned int broken = 0; /* std::printf is broken */
static unsigned int cpp_broken = 0; /* iostreams are broken */
template<typename... Values>
int sprint(char* buffer, std::size_t size,
const char* format, Values&&... values) {
#if defined(__clang__)
#pragma clang diagnostic ignored "-Wformat-security"
#endif
#if defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wformat-security"
#endif
return std::snprintf(buffer, size, format, std::forward<Values>(values)...);
}
template<typename... Values>
int sprint(wchar_t* buffer, std::size_t size,
const wchar_t* format, Values&&... values) {
if (buffer && size > 0) {
return std::swprintf(buffer, size, format,
std::forward<Values>(values)...);
} else {
/* unfortunately the behaviour of swprintf differs
from that of snprintf, i.e. if size == 0, just -1
is returned instead of computing the required buffer
size */
wchar_t buf[1024]; // should be sufficient for our tests
return std::swprintf(buf, sizeof buf/sizeof buf[0], format,
std::forward<Values>(values)...);
}
}
int compare(const char* s1, const char* s2) {
return std::strcmp(s1, s2);
}
int compare(const wchar_t* s1, const wchar_t* s2) {
return std::wcscmp(s1, s2);
}
template<typename... Values>
int print(const char* format, Values&&... values) {
return fmt::printf(std::cout, format, std::forward<Values>(values)...);
}
template<typename... Values>
int print(const wchar_t* format, Values&&... values) {
return fmt::printf(std::wcout, format, std::forward<Values>(values)...);
}
void diff_analysis(bool implementation_defined,
const char* format, int count1, int count2,
const char* buf1, const char* buf2) {
if (implementation_defined) {
print("implementation-dependent test for \"%s\" differs:\n", format);
} else {
print("test for \"%s\" fails:\n", format);
}
if (count1 == count2) {
print(" fmt delivers: '%s'\n", buf1);
print(" std delivers: '%s'\n", buf2);
} else {
print(" fmt delivers: '%s' (%d)\n", buf1, count1);
print(" std delivers: '%s' (%d)\n", buf2, count2);
}
}
void diff_analysis(bool implementation_defined,
const wchar_t* format, int count1, int count2,
const wchar_t* buf1, const wchar_t* buf2) {
if (implementation_defined) {
print(L"implementation-dependent test for L\"%s\" differs:\n", format);
} else {
print(L"test for L\"%s\" fails:\n", format);
}
if (count1 == count2) {
print(L" fmt delivers: '%s'\n", buf1);
print(L" std delivers: '%s'\n", buf2);
} else {
print(L" fmt delivers: '%s' (%d)\n", buf1, count1);
print(L" std delivers: '%s' (%d)\n", buf2, count2);
}
}
void buffer_overrun(const char* format, int len, int index) {
print("buffer overrun for \"%s\" and len = %d at index = %d\n",
format, len, index);
}
void buffer_overrun(const wchar_t* format, int len, int index) {
print(L"buffer overrun for \"%s\" and len = %d at index = %d\n",
format, len, index);
}
void count_mismatch(bool implementation_defined,
const char* format, int count1, int count2) {
if (implementation_defined) {
print("implementation-dependent test for \"%s\" differs,", format);
} else {
print("test for \"%s\" fails,", format);
}
print(" fmt returns %d, std returns %d\n",
count1, count2);
}
void count_mismatch(bool implementation_defined,
const wchar_t* format, int count1, int count2) {
if (implementation_defined) {
print(L"implementation-dependent test for \"%s\" differs,", format);
} else {
print(L"test for \"%s\" fails,", format);
}
print(L" fmt returns %d, std returns %d\n",
count1, count2);
}
void errno_mismatch(bool implementation_defined,
const char* format, int err1, int err2) {
if (implementation_defined) {
print("implementation-dependent test for \"%s\" differs,", format);
} else {
print("test for \"%s\" fails,", format);
}
print(" fmt sets errno to %d, std sets errno to %d\n",
err1, err2);
}
void errno_mismatch(bool implementation_defined,
const wchar_t* format, int err1, int err2) {
if (implementation_defined) {
print(L"implementation-dependent test for \"%s\" differs,", format);
} else {
print(L"test for \"%s\" fails,", format);
}
print(L" fmt sets errno to %d, std sets errno to %d\n",
err1, err2);
}
void offset_mismatch(const char* format, int off1, int off2) {
print("test for \"%s\" fails,", format);
print(" fmt sets offset to %d, std sets offset to %d\n",
off1, off2);
}
void offset_mismatch(const wchar_t* format, int off1, int off2) {
print(L"test for \"%s\" fails,", format);
print(L" fmt sets offset to %d, std sets offset to %d\n",
off1, off2);
}
void print_values(int) {
}
#if __cplusplus < 201703L
/* an output operator for nullptr_t is provided since C++17
but neither for C++11 nor C++14;
however clang++ defines this operator also for C++11 and C++14
beginning from at least clang version 12 */
#if defined(__clang__) && defined(_LIBCPP_VERSION)
#if _LIBCPP_VERSION < 12000
#define DEFINE_OUTPUT_OPERATOR_FOR_NULLPTR
#endif
#else
#define DEFINE_OUTPUT_OPERATOR_FOR_NULLPTR
#endif
#ifdef DEFINE_OUTPUT_OPERATOR_FOR_NULLPTR
std::ostream& operator<<(std::ostream& out, std::nullptr_t) {
out << "nullptr";
return out;
}
#endif
#endif
template<typename Value, typename... Values>
void print_values(int index, Value value, Values&&... values) {
std::cout << " argument #" << index << ": '" << value << "'" << std::endl;
print_values(index + 1, std::forward<Values>(values)...);
}
template<typename CharT, typename... Values>
bool check_printf(const CharT* expected, const CharT* format,
Values&&... values) {
int count = sprint(nullptr, 0, format, std::forward<Values>(values)...);
bool ok = false;
if (count >= 0) {
CharT* buf = new CharT[count + 1];
sprint(buf, count + 1, format, std::forward<Values>(values)...);
ok = compare(expected, buf) == 0;
if (!ok) {
fmt::printf(
"std::printf deviates from standard for \"%s\": \"%.*s\"\n",
format, count, buf);
}
delete[] buf;
} else if (!ok) {
fmt::printf("std::printf from standard for \"%s\"\n", format);
}
return ok;
}
/* older C++ libraries do not support hexfloat
which is required to support %a etc. */
bool check_hexfloat() {
std::ostringstream os;
os.setf(std::ios_base::scientific | std::ios_base::fixed,
std::ios_base::floatfield);
os << std::numeric_limits<double>::max();
std::string osstr(os.str());
return osstr == "0x1.fffffffffffffp+1023";
}
template<typename CharT, typename... Values>
bool general_testcase(bool implementation_defined,
bool with_offset, int& offset,
bool to_string, int string_len,
const CharT* format, Values&&... values) {
++testcases;
std::basic_ostringstream<CharT> os;
errno = 0;
int off1 = 0; int off2 = 0;
int count1 = 0;
if (to_string) {
CharT* buf = new CharT[string_len + 8];
std::fill_n(buf, string_len + 8, 42);
count1 = fmt::snprintf(buf, string_len, format, std::forward<Values>(values)...);
if (count1 > 0) {
int i = 0;
while (i < string_len) {
if (!buf[i]) break;
++i;
}
if (i < string_len) {
for (i = string_len; i < string_len + 8; ++i) {
if (buf[i] != 42) {
buffer_overrun(format, string_len, i);
print_values(1, values...);
return false;
}
}
os << buf;
}
}
delete[] buf;
} else {
count1 = fmt::printf(os, format, std::forward<Values>(values)...);
}
if (with_offset) off1 = offset;
int err1 = errno;
errno = 0;
int count2 = sprint(nullptr, 0, format, std::forward<Values>(values)...);
if (with_offset) off2 = offset;
int err2 = errno;
if (count1 < 0 || count2 < 0) {
if (count1 != count2) {
count_mismatch(implementation_defined, format, count1, count2);
print_values(1, values...);
if (implementation_defined) ++warnings;
return false;
}
if (err1 != err2) {
errno_mismatch(implementation_defined, format, err1, err2);
print_values(1, values...);
if (implementation_defined) ++warnings;
return false;
}
++successful;
return true;
}
/* compare the resulting strings */
int buflen, len;
if (to_string) {
buflen = string_len + 1; len = string_len;
} else {
buflen = len = count2 + 1;
}
CharT* buf = new CharT[buflen] {};
sprint(buf, len, format, values...);
std::basic_string<CharT> osstr(os.str());
bool ok = compare(buf, osstr.c_str()) == 0;
if (!ok) {
diff_analysis(implementation_defined, format, count1, count2,
osstr.c_str(), buf);
if (implementation_defined) ++warnings;
}
delete[] buf;
if (with_offset && off1 != off2) {
offset_mismatch(format, off1, off2);
ok = false;
}
if (!ok) print_values(1, values...);
if (ok) ++successful;
return ok;
}
template<typename CharT, typename... Values>
bool testcase(const CharT* format, Values&&... values) {
int offset = 0;
return general_testcase(/* implementation defined = */ false,
/* with offset = */ false, /* unused */ offset,
/* to_string = */ false, /* string_len = */ 0,
format, std::forward<Values>(values)...);
}
template<typename CharT, typename... Values>
bool implementation_dependent_testcase(const CharT* format,
Values&&... values) {
int offset = 0;
return general_testcase(/* implementation defined = */ true,
/* with offset = */ false, /* unused */ offset,
/* to_string = */ false, /* string_len = */ 0,
format, std::forward<Values>(values)...);
}
template<typename CharT, typename... Values>
bool testcase_with_offset(int& offset,
const CharT* format, Values&&... values) {
return general_testcase(/* implementation defined = */ false,
/* with offset = */ true, offset,
/* to_string = */ false, /* string_len = */ 0,
format, std::forward<Values>(values)...);
}
template<typename CharT, typename... Values>
bool testcase_for_snprintf(int string_len,
const CharT* format, Values&&... values) {
int offset = 0;
return general_testcase(/* implementation defined = */ false,
/* with offset = */ false, /* unused */ offset,
/* to_string = */ true, /* string_len = */ string_len,
format, std::forward<Values>(values)...);
}
template<typename CharT, typename... Values>
bool special_testcase(int expected_count,
const CharT* expected_string,
const CharT* format, Values&&... values) {
++testcases;
bool ok = true;
std::basic_ostringstream<CharT> os;
auto count = fmt::printf(os, format, std::forward<Values>(values)...);
if ((count < 0 || expected_count < 0) && count != expected_count) {
print("special test for \"%s\" fails,", format);
print(" fmt returns %d but we expected %d\n", count, expected_count);
print_values(1, values...);
return false;
}
if (count != expected_count) {
ok = false;
}
std::basic_string<CharT> fmt_str(os.str());
std::basic_string<CharT> expected_str(expected_string);
if (fmt_str != expected_str) {
ok = false;
}
if (ok) {
++successful; return true;
} else {
print("special test for \"%s\" fails:\n", format);
if (count != expected_count) {
print(" fmt returns: %d\n", count);
print(" expected return value: %d\n", expected_count);
}
print(" fmt delivers: \"%s\"\n", fmt_str);
print(" expected: \"%s\"\n", expected_str);
print_values(1, values...);
return false;
}
}
/* for tests of the support for output operators */
template<typename T>
struct TestObject {
TestObject() : object() {
}
TestObject(T&& val) : object(val) {
}
T object;
};
template<typename CharT, typename Traits, typename T>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& out, const TestObject<T>& t) {
out << t.object << "/TO";
return out;
}
template<typename CharT, typename Traits, typename T>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& out, const TestObject<T>* tp) {
out << tp->object << "/TO";
return out;
}
void run_tests() {
testcase("Hello world!");
testcase("%% %% %%%%");
/* test with many arguments of different types */
testcase("%d %u %lg %c %s", 27, 13u, 2.3, 'x', "Hello");
char c_values[] = {'a', 'A', '.', '/', ' ', '\t', '\n', '\0',
std::numeric_limits<char>::min(), std::numeric_limits<char>::max()};
for (auto val: c_values) {
testcase("%c", val);
testcase("%c%c", val, val);
testcase("%c %c", val, val);
testcase("%8c", val);
testcase("%-8c", val);
}
testcase("%c", 65);
for (bool val: {false, true}) {
testcase("%d", val);
testcase("%3d", val);
testcase("%03d", val);
}
signed char sc_values[] = {'a', 'A', '.', '/', ' ', -1,
std::numeric_limits<signed char>::min(),
std::numeric_limits<signed char>::max()};
for (auto val: sc_values) {
testcase("%hhd", val);
testcase("%hhd%hhd", val, val);
testcase("%hhd %hhd", val, val);
testcase("%8hhd", val);
testcase("%-8hhd", val);
}
unsigned char uc_values[] = {'a', 'A', '.', '/', ' ',
std::numeric_limits<unsigned char>::min(),
std::numeric_limits<unsigned char>::max()};
for (auto val: uc_values) {
testcase("%hhu", val);
testcase("%hhu%hhu", val, val);
testcase("%hhu %hhu", val, val);
testcase("%8hhu", val);
testcase("%-8hhu", val);
}
short int si_values[] = {0, 1234, -1234,
std::numeric_limits<short int>::min(),
std::numeric_limits<short int>::max()};
for (auto val: si_values) {
testcase("%hd", val);
testcase("%8hd", val);
testcase("%-8hd", val);
testcase("%+8hd", val);
testcase(" %hd ", val);
testcase("%hd%hd", val, val);
}
int i_values[] = {0, -12, 42, 117, 1234, -1234,
std::numeric_limits<int>::min(), std::numeric_limits<int>::max()};
for (auto val: i_values) {
testcase("%d", val);
testcase("%1d", val);
testcase("%2d", val);
testcase("%3d", val);
testcase("%4d", val);
testcase("%5d", val);
testcase("%6d", val);
testcase("%7d", val);
testcase("%8d", val);
testcase("%9d", val);
testcase("%10d", val);
testcase("%11d", val);
testcase("%12d", val);
testcase("%0d", val);
testcase("%01d", val);
testcase("%02d", val);
testcase("%03d", val);
testcase("%04d", val);
testcase("%05d", val);
testcase("%06d", val);
testcase("%07d", val);
testcase("%08d", val);
testcase("%09d", val);
testcase("%010d", val);
testcase("%011d", val);
testcase("%012d", val);
testcase("%-d", val);
testcase("%-1d", val);
testcase("%-2d", val);
testcase("%-3d", val);
testcase("%-4d", val);
testcase("%-5d", val);
testcase("%-6d", val);
testcase("%-7d", val);
testcase("%-8d", val);
testcase("%-9d", val);
testcase("%-10d", val);
testcase("%-11d", val);
testcase("%-12d", val);
testcase("%+d", val);
testcase("%+1d", val);
testcase("%+2d", val);
testcase("%+3d", val);
testcase("%+4d", val);
testcase("%+5d", val);
testcase("%+6d", val);
testcase("%+7d", val);
testcase("%+8d", val);
testcase("%+9d", val);
testcase("%+10d", val);
testcase("%+11d", val);
testcase("%+12d", val);
testcase("%0-8d", val);
testcase("%0+8d", val);
testcase("% d", val);
testcase("% 1d", val);
testcase("% 2d", val);
testcase("% 3d", val);
testcase("% 4d", val);
testcase("% 5d", val);
testcase("% 6d", val);
testcase("% 7d", val);
testcase("% 8d", val);
testcase("% 9d", val);
testcase("% 10d", val);
testcase("% 11d", val);
testcase("% 12d", val);
testcase("%.4d", val);
testcase("%8.4d", val);
testcase("% .4d", val);
testcase("% 8.4d", val);
testcase("% -4d", val);
testcase("% -.4d", val);
testcase("% 8.4d", val);
testcase("% -8.4d", val);
testcase("%+.4d", val);
testcase("%+8.4d", val);
testcase("%+ .4d", val);
testcase("%+ 8.4d", val);
testcase("%+ -4d", val);
testcase("%+ -.4d", val);
testcase("%+ 8.4d", val);
testcase("%+ -8.4d", val);
testcase(" %d ", val);
testcase("%d%d", val, val);
}
long int li_values[] = {0, 1234, -1234,
std::numeric_limits<long int>::min(),
std::numeric_limits<long int>::max()};
for (auto val: li_values) {
testcase("%ld", val);
testcase("%8ld", val);
testcase("%-8ld", val);
testcase("%+8ld", val);
testcase(" %ld ", val);
testcase("%ld%ld", val, val);
}
long long int lli_values[] = {0, 1234, -1234,
std::numeric_limits<long long int>::min(),
std::numeric_limits<long long int>::max()};
for (auto val: lli_values) {
testcase("%lld", val);
testcase("%16lld", val);
testcase("%-16lld", val);
testcase("%+16lld", val);
testcase(" %lld ", val);
testcase("%lld%lld", val, val);
}
std::intmax_t im_values[] = {0, 17, -13, 1234, -1234,
std::numeric_limits<std::intmax_t>::min(),
std::numeric_limits<std::intmax_t>::max()};
for (auto val: im_values) {
testcase("%jd", val);
testcase("%16jd", val);
testcase("%-16jd", val);
testcase("%+16jd", val);
testcase(" %jd ", val);
testcase("%jd%jd", val, val);
}
using ssize_t = std::make_signed<std::size_t>::type;
ssize_t ssize_values[] = {0, 17, -13, 1234, -1234,
sizeof(ssize_t),
std::numeric_limits<ssize_t>::min(),
std::numeric_limits<ssize_t>::max()};
for (auto val: ssize_values) {
testcase("%zd", val);
testcase("%16zd", val);
testcase("%-16zd", val);
testcase("%+16zd", val);
testcase(" %zd ", val);
testcase("%zd%zd", val, val);
}
std::ptrdiff_t ptrdiff_values[] = {0, 17, -13, 1234, -1234,
&ptrdiff_values[4] - &ptrdiff_values[0],
&ptrdiff_values[0] - &ptrdiff_values[4],
std::numeric_limits<std::ptrdiff_t>::min(),
std::numeric_limits<std::ptrdiff_t>::max()};
for (auto val: ptrdiff_values) {
testcase("%td", val);
testcase("%16td", val);
testcase("%-16td", val);
testcase("%+16td", val);
testcase(" %td ", val);
testcase("%td%td", val, val);
}
unsigned int ui_values[] = {1, 42, 117, 1234, 2048,
std::numeric_limits<unsigned int>::min(),
std::numeric_limits<unsigned int>::max()};
for (auto val: ui_values) {
testcase("%u", val);
testcase("%8u", val);
testcase("%-8u", val);
testcase("%+8u", val);
testcase("%o", val);
testcase("%x", val);
testcase("%X", val);
testcase(" %u ", val);
testcase("%u%u", val, val);
testcase("%#o", val);
testcase("%#.4o", val);
testcase("%#x", val);
testcase("%#.4x", val);
testcase("%#o", val);
testcase("%#.4o", val);
testcase("%#x", val);
testcase("%#.4x", val);
}
unsigned long int uli_values[] = {1234,
std::numeric_limits<unsigned long int>::min(),
std::numeric_limits<unsigned long int>::max()};
for (auto val: uli_values) {
testcase("%lu", val);
testcase("%8lu", val);
testcase("%-8lu", val);
testcase("%+8lu", val);
testcase("%lo", val);
testcase("%lx", val);
testcase("%lX", val);
testcase(" %lu ", val);
testcase("%lu%lu", val, val);
}
unsigned long long int ulli_values[] = {1234,
std::numeric_limits<unsigned long long int>::min(),
std::numeric_limits<unsigned long long int>::max()};
for (auto val: ulli_values) {
testcase("%llu", val);
testcase("%16llu", val);
testcase("%-16llu", val);
testcase("%+16llu", val);
testcase("%llo", val);
testcase("%llx", val);
testcase("%llX", val);
testcase(" %llu ", val);
testcase("%llu%llu", val, val);
}
std::uintmax_t uim_values[] = {0, 17, 1234,
std::numeric_limits<std::uintmax_t>::min(),
std::numeric_limits<std::uintmax_t>::max()};
for (auto val: uim_values) {
testcase("%ju", val);
testcase("%16ju", val);
testcase("%-16ju", val);
testcase("%+16ju", val);
testcase("%jo", val);
testcase("%jx", val);
testcase("%jX", val);
testcase(" %ju ", val);
testcase("%ju%ju", val, val);
}
std::size_t size_values[] = {0, 17, 1234,
sizeof(uim_values),
std::numeric_limits<std::size_t>::min(),
std::numeric_limits<std::size_t>::max()};
for (auto val: size_values) {
testcase("%zu", val);
testcase("%16zu", val);
testcase("%-16zu", val);
testcase("%+16zu", val);
testcase("%zo", val);
testcase("%zx", val);
testcase("%zX", val);
testcase(" %zu ", val);
testcase("%zu%zu", val, val);
}
using uptrdiff_t = std::make_unsigned<std::ptrdiff_t>::type;
uptrdiff_t uptrdiff_values[] = {0, 17, 1234,
std::numeric_limits<uptrdiff_t>::min(),
std::numeric_limits<uptrdiff_t>::max()};
for (auto val: uptrdiff_values) {
testcase("%tu", val);
testcase("%16tu", val);
testcase("%-16tu", val);
testcase("%+16tu", val);
testcase("%to", val);
testcase("%016to", val);
testcase("%tx", val);
testcase("%tX", val);
testcase(" %tu ", val);
testcase("%tu%tu", val, val);
}
bool nan_works = check_printf(" NAN", "% F", std::nanf("1"));
if (!nan_works) ++broken;
bool hexfloat_works = check_hexfloat();
if (!hexfloat_works) {
++cpp_broken;
}
bool uppercase_inf_works = check_printf("INF", "%E",
std::numeric_limits<float>::max() * 2);
if (!uppercase_inf_works) {
++broken;
}
float f_values[] = {0, -0.0f, -1, 42, 1234.5678f, 1.25E-10f, 3E+10f,
std::numeric_limits<float>::min() / 2,
std::numeric_limits<float>::max() * 2,
std::numeric_limits<float>::min(),
std::numeric_limits<float>::max(),
std::numeric_limits<float>::lowest(),
std::numeric_limits<float>::epsilon(),
std::sqrt(-1.0f), std::nanf("1"), -std::nanf("1")};
for (auto val: f_values) {
if (!nan_works && !std::isfinite(val)) continue;
testcase("%f", val);
testcase("%e", val);
testcase("%g", val);
if (uppercase_inf_works || std::isfinite(val)) {
testcase("%F", val);
testcase("%E", val);
testcase("%G", val);
}
if (hexfloat_works) {
testcase("%a", val);
// testcase("%.0a", val);
// testcase("%.2a", val);
if (uppercase_inf_works || std::isfinite(val)) {
testcase("%A", val);
}
}
testcase("%10.2f", val);
testcase("%10.2e", val);
testcase("%10.2g", val);
testcase("%#g", val);
testcase("%0g", val);
testcase("%01g", val);
testcase("%02g", val);
testcase("%03g", val);
testcase("%04g", val);
testcase("%05g", val);
testcase("%06g", val);
testcase("%07g", val);
testcase("%08g", val);
testcase("%09g", val);
testcase("%010g", val);
testcase("%011g", val);
testcase("%012g", val);
testcase("%-g", val);
testcase("%-1g", val);
testcase("%-2g", val);
testcase("%-3g", val);
testcase("%-4g", val);
testcase("%-5g", val);
testcase("%-6g", val);
testcase("%-7g", val);
testcase("%-8g", val);
testcase("%-9g", val);
testcase("%-10g", val);
testcase("%-11g", val);
testcase("%-12g", val);
testcase("%+g", val);
testcase("%+1g", val);
testcase("%+2g", val);
testcase("%+3g", val);
testcase("%+4g", val);
testcase("%+5g", val);
testcase("%+6g", val);
testcase("%+7g", val);
testcase("%+8g", val);
testcase("%+9g", val);
testcase("%+10g", val);
testcase("%+11g", val);
testcase("%+12g", val);
testcase("%0-8g", val);
testcase("%0+8g", val);
testcase("% f", val);
testcase("% 1f", val);
testcase("% 2f", val);
testcase("% 3f", val);
testcase("% 4f", val);
testcase("% 5f", val);
testcase("% 6f", val);
testcase("% 7f", val);
testcase("% 8f", val);
testcase("% 9f", val);
testcase("% 10f", val);
testcase("% 11f", val);
testcase("% 12f", val);
testcase("% e", val);
testcase("% 1e", val);
testcase("% 2e", val);
testcase("% 3e", val);
testcase("% 4e", val);
testcase("% 5e", val);
testcase("% 6e", val);
testcase("% 7e", val);
testcase("% 8e", val);
testcase("% 9e", val);
testcase("% 10e", val);
testcase("% 11e", val);
testcase("% 12e", val);
testcase("% g", val);
testcase("% 1g", val);
testcase("% 2g", val);
testcase("% 3g", val);
testcase("% 4g", val);
testcase("% 5g", val);
testcase("% 6g", val);
testcase("% 7g", val);
testcase("% 8g", val);
testcase("% 9g", val);
testcase("% 10g", val);
testcase("% 11g", val);
testcase("% 12g", val);
testcase("% 8.4f", val);
testcase("% -8.4f", val);
}
#ifdef __SUNPRO_CC
// suppress the harmless warning regarding the intended overflow
// of std::numeric_limits<double>::max() * 2
#pragma error_messages (off, mulfovfl)
#endif
double d_values[] = {-0.0, 1234.5678, 1.25E-10, 3E+10,
std::numeric_limits<double>::min() / 2,
std::numeric_limits<double>::max() * 2,
std::numeric_limits<double>::min(),
std::numeric_limits<double>::max(),
std::numeric_limits<double>::lowest(),
std::numeric_limits<double>::epsilon(),
std::sqrt(-1.0), std::nan("1"), -std::nan("1")};
for (auto val: d_values) {
if (!nan_works && !std::isfinite(val)) continue;
testcase("%lf", val);
testcase("%le", val);
testcase("%lg", val);
if (uppercase_inf_works || std::isfinite(val)) {
testcase("%lF", val);
testcase("%lE", val);
testcase("%lG", val);
}
if (hexfloat_works) {
testcase("%la", val);
if (uppercase_inf_works || std::isfinite(val)) {
testcase("%lA", val);
}
}
testcase("%10.2lf", val);
testcase("%10.2le", val);
testcase("%10.2lg", val);
testcase("%#lg", val);
testcase("% lf", val);
testcase("% 1lf", val);
testcase("% 2lf", val);
testcase("% 3lf", val);
testcase("% 4lf", val);
testcase("% 5lf", val);
testcase("% 6lf", val);
testcase("% 7lf", val);
testcase("% 8lf", val);
testcase("% 9lf", val);
testcase("% 10lf", val);
testcase("% 11lf", val);
testcase("% 12lf", val);
testcase("% le", val);
testcase("% 1le", val);
testcase("% 2le", val);
testcase("% 3le", val);
testcase("% 4le", val);
testcase("% 5le", val);
testcase("% 6le", val);
testcase("% 7le", val);
testcase("% 8le", val);
testcase("% 9le", val);
testcase("% 10le", val);
testcase("% 11le", val);
testcase("% 12le", val);
testcase("% lg", val);
testcase("% 1lg", val);
testcase("% 2lg", val);
testcase("% 3lg", val);
testcase("% 4lg", val);
testcase("% 5lg", val);
testcase("% 6lg", val);
testcase("% 7lg", val);
testcase("% 8lg", val);
testcase("% 9lg", val);
testcase("% 10lg", val);
testcase("% 11lg", val);
testcase("% 12lg", val);
}
long double ld_values[] = {-0.0L, 1234.5678L, 1.25E-10L, 3E+10L,
std::numeric_limits<long double>::min() / 2,
std::numeric_limits<long double>::max() * 2,
std::numeric_limits<long double>::min(),
std::numeric_limits<long double>::max(),
std::numeric_limits<long double>::lowest(),
std::numeric_limits<long double>::epsilon(),
std::sqrt(-1.0L), std::nanl("1"), -std::nanl("1")};
for (auto val: ld_values) {
if (!nan_works && !std::isfinite(val)) continue;
testcase("%Lf", val);
testcase("%Le", val);
testcase("%Lg", val);
if (uppercase_inf_works || std::isfinite(val)) {
testcase("%LF", val);
testcase("%LE", val);
testcase("%LG", val);
}
if (hexfloat_works) {
testcase("%La", val);
if (uppercase_inf_works || std::isfinite(val)) {
testcase("%LA", val);
}
}
testcase("%10.2Lf", val);
testcase("%10.2Le", val);
testcase("%10.2Lg", val);
testcase("%#Lf", val);
testcase("% Lf", val);
testcase("% 1Lf", val);
testcase("% 2Lf", val);
testcase("% 3Lf", val);
testcase("% 4Lf", val);
testcase("% 5Lf", val);
testcase("% 6Lf", val);
testcase("% 7Lf", val);
testcase("% 8Lf", val);
testcase("% 9Lf", val);
testcase("% 10Lf", val);
testcase("% 11Lf", val);