| 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 2 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, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * 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, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 18 | * USA |
| 19 | */ |
| 20 | #include "test.h" |
| 21 | |
| 22 | static void test_printf(void) { |
| 23 | char c; |
| 24 | short s; |
| 25 | int i; |
| 26 | long l; |
| 27 | long long ll; |
| 28 | intmax_t m; |
| 29 | ssize_t ssz; |
| 30 | ptrdiff_t p; |
| 31 | char *cp; |
| 32 | char buffer[16]; |
| 33 | FILE *fp; |
| 34 | |
| 35 | check_string(do_printf("%d", 999), "999"); |
| 36 | check_string(do_printf("%d", -999), "-999"); |
| 37 | check_string(do_printf("%i", 999), "999"); |
| 38 | check_string(do_printf("%i", -999), "-999"); |
| 39 | check_string(do_printf("%u", 999), "999"); |
| 40 | check_string(do_printf("%2u", 999), "999"); |
| 41 | check_string(do_printf("%10u", 999), " 999"); |
| 42 | check_string(do_printf("%-10u", 999), "999 "); |
| 43 | check_string(do_printf("%010u", 999), "0000000999"); |
| 44 | check_string(do_printf("%-10d", -999), "-999 "); |
| 45 | check_string(do_printf("%-010d", -999), "-999 "); /* "-" beats "0" */ |
| 46 | check_string(do_printf("%66u", 999), " 999"); |
| 47 | check_string(do_printf("%o", 999), "1747"); |
| 48 | check_string(do_printf("%#o", 999), "01747"); |
| 49 | check_string(do_printf("%#o", 0), "0"); |
| 50 | check_string(do_printf("%x", 999), "3e7"); |
| 51 | check_string(do_printf("%#x", 999), "0x3e7"); |
| 52 | check_string(do_printf("%#X", 999), "0X3E7"); |
| 53 | check_string(do_printf("%#x", 0), "0"); |
| 54 | check_string(do_printf("%hd", (short)999), "999"); |
| 55 | check_string(do_printf("%hhd", (short)99), "99"); |
| 56 | check_string(do_printf("%ld", 100000L), "100000"); |
| 57 | check_string(do_printf("%lld", 10000000000LL), "10000000000"); |
| 58 | check_string(do_printf("%qd", 10000000000LL), "10000000000"); |
| 59 | check_string(do_printf("%jd", (intmax_t)10000000000LL), "10000000000"); |
| 60 | check_string(do_printf("%zd", (ssize_t)2000000000), "2000000000"); |
| 61 | check_string(do_printf("%td", (ptrdiff_t)2000000000), "2000000000"); |
| 62 | check_string(do_printf("%hu", (short)999), "999"); |
| 63 | check_string(do_printf("%hhu", (short)99), "99"); |
| 64 | check_string(do_printf("%lu", 100000L), "100000"); |
| 65 | check_string(do_printf("%llu", 10000000000LL), "10000000000"); |
| 66 | check_string(do_printf("%ju", (uintmax_t)10000000000LL), "10000000000"); |
| 67 | check_string(do_printf("%zu", (size_t)2000000000), "2000000000"); |
| 68 | check_string(do_printf("%tu", (ptrdiff_t)2000000000), "2000000000"); |
| 69 | check_string(do_printf("%p", (void *)0x100), "0x100"); |
| 70 | check_string(do_printf("%s", "wibble"), "wibble"); |
| 71 | check_string(do_printf("%s-%s", "wibble", "wobble"), "wibble-wobble"); |
| 72 | check_string(do_printf("%10s", "wibble"), " wibble"); |
| 73 | check_string(do_printf("%010s", "wibble"), " wibble"); /* 0 ignored for %s */ |
| 74 | check_string(do_printf("%-10s", "wibble"), "wibble "); |
| 75 | check_string(do_printf("%2s", "wibble"), "wibble"); |
| 76 | check_string(do_printf("%.2s", "wibble"), "wi"); |
| 77 | check_string(do_printf("%.2s", "w"), "w"); |
| 78 | check_string(do_printf("%4.2s", "wibble"), " wi"); |
| 79 | check_string(do_printf("%c", 'a'), "a"); |
| 80 | check_string(do_printf("%4c", 'a'), " a"); |
| 81 | check_string(do_printf("%-4c", 'a'), "a "); |
| 82 | check_string(do_printf("%*c", 0, 'a'), "a"); |
| 83 | check_string(do_printf("x%hhny", &c), "xy"); |
| 84 | insist(c == 1); |
| 85 | check_string(do_printf("xx%hnyy", &s), "xxyy"); |
| 86 | insist(s == 2); |
| 87 | check_string(do_printf("xxx%nyyy", &i), "xxxyyy"); |
| 88 | insist(i == 3); |
| 89 | check_string(do_printf("xxxx%lnyyyy", &l), "xxxxyyyy"); |
| 90 | insist(l == 4); |
| 91 | check_string(do_printf("xxxxx%llnyyyyy", &ll), "xxxxxyyyyy"); |
| 92 | insist(ll == 5); |
| 93 | check_string(do_printf("xxxxxx%jnyyyyyy", &m), "xxxxxxyyyyyy"); |
| 94 | insist(m == 6); |
| 95 | check_string(do_printf("xxxxxxx%znyyyyyyy", &ssz), "xxxxxxxyyyyyyy"); |
| 96 | insist(ssz == 7); |
| 97 | check_string(do_printf("xxxxxxxx%tnyyyyyyyy", &p), "xxxxxxxxyyyyyyyy"); |
| 98 | insist(p == 8); |
| 99 | check_string(do_printf("%*d", 5, 99), " 99"); |
| 100 | check_string(do_printf("%*d", -5, 99), "99 "); |
| 101 | check_string(do_printf("%.*d", 5, 99), "00099"); |
| 102 | check_string(do_printf("%.*d", -5, 99), "99"); |
| 103 | check_string(do_printf("%.0d", 0), ""); |
| 104 | check_string(do_printf("%.d", 0), ""); |
| 105 | check_string(do_printf("%.d", 0), ""); |
| 106 | check_string(do_printf("%%"), "%"); |
| 107 | check_string(do_printf("wibble"), "wibble"); |
| 108 | insist(do_printf("%") == 0); |
| 109 | insist(do_printf("%=") == 0); |
| 110 | i = byte_asprintf(&cp, "xyzzy %d", 999); |
| 111 | insist(i == 9); |
| 112 | check_string(cp, "xyzzy 999"); |
| 113 | i = byte_snprintf(buffer, sizeof buffer, "xyzzy %d", 999); |
| 114 | insist(i == 9); |
| 115 | check_string(buffer, "xyzzy 999"); |
| 116 | i = byte_snprintf(buffer, sizeof buffer, "%*d", 32, 99); |
| 117 | insist(i == 32); |
| 118 | check_string(buffer, " "); |
| 119 | { |
| 120 | /* bizarre workaround for compiler checking of format strings */ |
| 121 | char f[] = "xyzzy %"; |
| 122 | i = byte_asprintf(&cp, f); |
| 123 | insist(i == -1); |
| 124 | } |
| 125 | |
| 126 | fp = tmpfile(); |
| 127 | insist(byte_fprintf(fp, "%10s\n", "wibble") == 11); |
| 128 | rewind(fp); |
| 129 | insist(fgets(buffer, sizeof buffer, fp) == buffer); |
| 130 | check_string(buffer, " wibble\n"); |
| 131 | fclose(fp); |
| 132 | } |
| 133 | |
| 134 | TEST(printf); |
| 135 | |
| 136 | /* |
| 137 | Local Variables: |
| 138 | c-basic-offset:2 |
| 139 | comment-column:40 |
| 140 | fill-column:79 |
| 141 | indent-tabs-mode:nil |
| 142 | End: |
| 143 | */ |