| 1 | /* |
| 2 | * This file is part of DisOrder. |
| 3 | * Copyright (C) 2005, 2007, 2008 Richard Kettlewell |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #include "test.h" |
| 19 | |
| 20 | /* launder a string constant to stop gcc warnings */ |
| 21 | static const char *L(const char *s) { |
| 22 | return s; |
| 23 | } |
| 24 | |
| 25 | static void test_printf(void) { |
| 26 | char c; |
| 27 | short s; |
| 28 | int i; |
| 29 | long l; |
| 30 | long long ll; |
| 31 | intmax_t m; |
| 32 | ssize_t ssz; |
| 33 | ptrdiff_t p; |
| 34 | char *cp; |
| 35 | char buffer[16]; |
| 36 | FILE *fp; |
| 37 | |
| 38 | check_string(do_printf("%d", 999), "999"); |
| 39 | check_string(do_printf("%d", -999), "-999"); |
| 40 | check_string(do_printf("%+d", 999), "+999"); |
| 41 | check_string(do_printf("%+d", -999), "-999"); |
| 42 | check_string(do_printf("%i", 999), "999"); |
| 43 | check_string(do_printf("%i", -999), "-999"); |
| 44 | check_string(do_printf("%u", 999), "999"); |
| 45 | check_string(do_printf("%2u", 999), "999"); |
| 46 | check_string(do_printf("%10u", 999), " 999"); |
| 47 | check_string(do_printf("%-10u", 999), "999 "); |
| 48 | check_string(do_printf("%010u", 999), "0000000999"); |
| 49 | check_string(do_printf("%-10d", -999), "-999 "); |
| 50 | check_string(do_printf("%-010d", -999), "-999 "); /* "-" beats "0" */ |
| 51 | check_string(do_printf("%66u", 999), " 999"); |
| 52 | check_string(do_printf("%o", 999), "1747"); |
| 53 | check_string(do_printf("%#o", 999), "01747"); |
| 54 | check_string(do_printf("%#o", 0), "0"); |
| 55 | check_string(do_printf("%x", 999), "3e7"); |
| 56 | check_string(do_printf("%#x", 999), "0x3e7"); |
| 57 | check_string(do_printf("%#X", 999), "0X3E7"); |
| 58 | check_string(do_printf("%#x", 0), "0"); |
| 59 | check_string(do_printf("%hd", (short)999), "999"); |
| 60 | check_string(do_printf("%hhd", (short)99), "99"); |
| 61 | check_string(do_printf("%ld", 100000L), "100000"); |
| 62 | check_string(do_printf("%lld", 10000000000LL), "10000000000"); |
| 63 | check_string(do_printf("%qd", 10000000000LL), "10000000000"); |
| 64 | check_string(do_printf("%jd", (intmax_t)10000000000LL), "10000000000"); |
| 65 | check_string(do_printf("%zd", (ssize_t)2000000000), "2000000000"); |
| 66 | check_string(do_printf("%td", (ptrdiff_t)2000000000), "2000000000"); |
| 67 | check_string(do_printf("%hu", (short)999), "999"); |
| 68 | check_string(do_printf("%hhu", (short)99), "99"); |
| 69 | check_string(do_printf("%lu", 100000L), "100000"); |
| 70 | check_string(do_printf("%llu", 10000000000LL), "10000000000"); |
| 71 | check_string(do_printf("%ju", (uintmax_t)10000000000LL), "10000000000"); |
| 72 | check_string(do_printf("%zu", (size_t)2000000000), "2000000000"); |
| 73 | check_string(do_printf("%tu", (ptrdiff_t)2000000000), "2000000000"); |
| 74 | check_string(do_printf("%p", (void *)0x100), "0x100"); |
| 75 | check_string(do_printf("%s", "wibble"), "wibble"); |
| 76 | check_string(do_printf("%s-%s", "wibble", "wobble"), "wibble-wobble"); |
| 77 | check_string(do_printf("%10s", "wibble"), " wibble"); |
| 78 | check_string(do_printf("%010s", "wibble"), " wibble"); /* 0 ignored for %s */ |
| 79 | check_string(do_printf("%-10s", "wibble"), "wibble "); |
| 80 | check_string(do_printf("%2s", "wibble"), "wibble"); |
| 81 | check_string(do_printf("%.2s", "wibble"), "wi"); |
| 82 | check_string(do_printf("%.2s", "w"), "w"); |
| 83 | check_string(do_printf("%4.2s", "wibble"), " wi"); |
| 84 | check_string(do_printf("%c", 'a'), "a"); |
| 85 | check_string(do_printf("%4c", 'a'), " a"); |
| 86 | check_string(do_printf("%-4c", 'a'), "a "); |
| 87 | check_string(do_printf("%*c", 0, 'a'), "a"); |
| 88 | check_string(do_printf("x%hhny", &c), "xy"); |
| 89 | insist(c == 1); |
| 90 | check_string(do_printf("xx%hnyy", &s), "xxyy"); |
| 91 | insist(s == 2); |
| 92 | check_string(do_printf("xxx%nyyy", &i), "xxxyyy"); |
| 93 | insist(i == 3); |
| 94 | check_string(do_printf("xxxx%lnyyyy", &l), "xxxxyyyy"); |
| 95 | insist(l == 4); |
| 96 | check_string(do_printf("xxxxx%llnyyyyy", &ll), "xxxxxyyyyy"); |
| 97 | insist(ll == 5); |
| 98 | check_string(do_printf("xxxxxx%jnyyyyyy", &m), "xxxxxxyyyyyy"); |
| 99 | insist(m == 6); |
| 100 | check_string(do_printf("xxxxxxx%znyyyyyyy", &ssz), "xxxxxxxyyyyyyy"); |
| 101 | insist(ssz == 7); |
| 102 | check_string(do_printf("xxxxxxxx%tnyyyyyyyy", &p), "xxxxxxxxyyyyyyyy"); |
| 103 | insist(p == 8); |
| 104 | check_string(do_printf("%*d", 5, 99), " 99"); |
| 105 | check_string(do_printf("%*d", -5, 99), "99 "); |
| 106 | check_string(do_printf("%.*d", 5, 99), "00099"); |
| 107 | check_string(do_printf("%.*d", -5, 99), "99"); |
| 108 | check_string(do_printf("%.0d", 0), ""); |
| 109 | check_string(do_printf("%.d", 0), ""); |
| 110 | check_string(do_printf("%.d", 0), ""); |
| 111 | check_string(do_printf("%%"), "%"); |
| 112 | check_string(do_printf("wibble"), "wibble"); |
| 113 | insist(do_printf("%") == 0); |
| 114 | insist(do_printf("%=") == 0); |
| 115 | i = byte_asprintf(&cp, "xyzzy %d", 999); |
| 116 | insist(i == 9); |
| 117 | check_string(cp, "xyzzy 999"); |
| 118 | i = byte_snprintf(buffer, sizeof buffer, "xyzzy %d", 999); |
| 119 | insist(i == 9); |
| 120 | check_string(buffer, "xyzzy 999"); |
| 121 | i = byte_snprintf(buffer, sizeof buffer, "%*d", 32, 99); |
| 122 | insist(i == 32); |
| 123 | check_string(buffer, " "); |
| 124 | { |
| 125 | /* bizarre workaround for compiler checking of format strings */ |
| 126 | char f[] = "xyzzy %"; |
| 127 | i = byte_asprintf(&cp, f); |
| 128 | insist(i == -1); |
| 129 | } |
| 130 | |
| 131 | fp = tmpfile(); |
| 132 | insist(byte_fprintf(fp, "%10s\n", "wibble") == 11); |
| 133 | rewind(fp); |
| 134 | insist(fgets(buffer, sizeof buffer, fp) == buffer); |
| 135 | check_string(buffer, " wibble\n"); |
| 136 | fclose(fp); |
| 137 | check_integer(byte_snprintf(buffer, sizeof buffer, |
| 138 | "%18446744073709551616d", 10), -1); |
| 139 | check_integer(byte_snprintf(buffer, sizeof buffer, |
| 140 | "%.18446744073709551616d", 10), -1); |
| 141 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%hs"), ""), -1); |
| 142 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%qs"), ""), -1); |
| 143 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%js"), ""), -1); |
| 144 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%zs"), ""), -1); |
| 145 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%ts"), ""), -1); |
| 146 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%Ls"), ""), -1); |
| 147 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%hp"), (void *)0), -1); |
| 148 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%lp"), (void *)0), -1); |
| 149 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%qp"), (void *)0), -1); |
| 150 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%jp"), (void *)0), -1); |
| 151 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%zp"), (void *)0), -1); |
| 152 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%tp"), (void *)0), -1); |
| 153 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%Lp"), (void *)0), -1); |
| 154 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%h%")), -1); |
| 155 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%l%")), -1); |
| 156 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%q%")), -1); |
| 157 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%j%")), -1); |
| 158 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%z%")), -1); |
| 159 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%t%")), -1); |
| 160 | check_integer(byte_snprintf(buffer, sizeof buffer, L("%L%")), -1); |
| 161 | check_integer(byte_snprintf(buffer, sizeof buffer, "%2147483647s%2147483647s", "", ""), -1); |
| 162 | check_integer(byte_sinkprintf(sink_error(), ""), 0); |
| 163 | check_integer(byte_sinkprintf(sink_error(), "%5s", ""), -1); |
| 164 | check_integer(byte_sinkprintf(sink_error(), "%d", 0), -1); |
| 165 | check_integer(byte_sinkprintf(sink_error(), "%d", 1), -1); |
| 166 | check_integer(byte_sinkprintf(sink_error(), "%2d", 0), -1); |
| 167 | check_integer(byte_sinkprintf(sink_error(), "%d", -1), -1); |
| 168 | check_integer(byte_sinkprintf(sink_error(), "%#x", 10), -1); |
| 169 | check_integer(byte_sinkprintf(sink_error(), "%-d", 0), -1); |
| 170 | check_integer(byte_sinkprintf(sink_error(), "%-d", 1), -1); |
| 171 | check_integer(byte_sinkprintf(sink_error(), "%-2d", 0), -1); |
| 172 | check_integer(byte_sinkprintf(sink_error(), "%-d", -1), -1); |
| 173 | check_integer(byte_sinkprintf(sink_error(), "%-#x", 10), -1); |
| 174 | |
| 175 | } |
| 176 | |
| 177 | TEST(printf); |
| 178 | |
| 179 | /* |
| 180 | Local Variables: |
| 181 | c-basic-offset:2 |
| 182 | comment-column:40 |
| 183 | fill-column:79 |
| 184 | indent-tabs-mode:nil |
| 185 | End: |
| 186 | */ |