lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Test program for %a printf formats. */ |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | |
| 6 | #ifndef WIDE |
| 7 | # define STR_LEN strlen |
| 8 | # define STR_CMP strcmp |
| 9 | # define SPRINT snprintf |
| 10 | # define CHAR_T char |
| 11 | # define PRINT printf |
| 12 | # define L_(Str) Str |
| 13 | # define S "%s" |
| 14 | #else |
| 15 | # define STR_LEN wcslen |
| 16 | # define SPRINT swprintf |
| 17 | # define STR_CMP wcscmp |
| 18 | # define CHAR_T wchar_t |
| 19 | # define PRINT wprintf |
| 20 | # define L_(Str) L##Str |
| 21 | # define S "%ls" |
| 22 | #endif |
| 23 | |
| 24 | struct testcase |
| 25 | { |
| 26 | double value; |
| 27 | const CHAR_T *fmt; |
| 28 | const CHAR_T *expect; |
| 29 | }; |
| 30 | |
| 31 | static const struct testcase testcases[] = |
| 32 | { |
| 33 | { 0x0.0030p+0, L_("%a"), L_("0x1.8p-11") }, |
| 34 | { 0x0.0040p+0, L_("%a"), L_("0x1p-10") }, |
| 35 | { 0x0.0030p+0, L_("%040a"), L_("0x00000000000000000000000000000001.8p-11") }, |
| 36 | { 0x0.0040p+0, L_("%040a"), L_("0x0000000000000000000000000000000001p-10") }, |
| 37 | { 0x0.0040p+0, L_("%40a"), L_(" 0x1p-10") }, |
| 38 | { 0x0.0040p+0, L_("%#40a"), L_(" 0x1.p-10") }, |
| 39 | { 0x0.0040p+0, L_("%-40a"), L_("0x1p-10 ") }, |
| 40 | { 0x0.0040p+0, L_("%#-40a"), L_("0x1.p-10 ") }, |
| 41 | { 0x0.0030p+0, L_("%040e"), L_("00000000000000000000000000007.324219e-04") }, |
| 42 | { 0x0.0040p+0, L_("%040e"), L_("00000000000000000000000000009.765625e-04") }, |
| 43 | }; |
| 44 | |
| 45 | |
| 46 | static int |
| 47 | do_test (void) |
| 48 | { |
| 49 | const struct testcase *t; |
| 50 | int result = 0; |
| 51 | |
| 52 | for (t = testcases; |
| 53 | t < &testcases[sizeof testcases / sizeof testcases[0]]; |
| 54 | ++t) |
| 55 | { |
| 56 | CHAR_T buf[1024]; |
| 57 | int n = SPRINT (buf, sizeof buf / sizeof (buf[0]), t->fmt, t->value); |
| 58 | if (n != STR_LEN (t->expect) || STR_CMP (buf, t->expect) != 0) |
| 59 | { |
| 60 | PRINT (L_("" S "\tExpected \"" S "\" (%Zu)\n\tGot \"" |
| 61 | S "\" (%d, %Zu)\n"), |
| 62 | t->fmt, t->expect, STR_LEN (t->expect), |
| 63 | buf, n, STR_LEN (buf)); |
| 64 | result = 1; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | #define TEST_FUNCTION do_test () |
| 72 | #include "../test-skeleton.c" |