00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #undef NDEBUG
00011 #include <limits.h>
00012 #include <stdarg.h>
00013 #include <stddef.h>
00014 #include <stdlib.h>
00015 #include <stdio.h>
00016 #include <string.h>
00017 #include "threads/test.h"
00018
00019
00020 static int failure_cnt;
00021
00022 static void
00023 checkf (const char *expect, const char *format, ...)
00024 {
00025 char output[128];
00026 va_list args;
00027
00028 printf ("\"%s\" -> \"%s\": ", format, expect);
00029
00030 va_start (args, format);
00031 vsnprintf (output, sizeof output, format, args);
00032 va_end (args);
00033
00034 if (strcmp (expect, output))
00035 {
00036 printf ("\nFAIL: actual output \"%s\"\n", output);
00037 failure_cnt++;
00038 }
00039 else
00040 printf ("okay\n");
00041 }
00042
00043
00044 void
00045 test (void)
00046 {
00047 printf ("Testing formats:");
00048
00049
00050
00051 checkf ("1", "%'d", 1);
00052 checkf ("12", "%'d", 12);
00053 checkf ("123", "%'d", 123);
00054 checkf ("1,234", "%'d", 1234);
00055 checkf ("12,345", "%'d", 12345);
00056 checkf ("123,456", "%'ld", 123456L);
00057 checkf ("1,234,567", "%'ld", 1234567L);
00058 checkf ("12,345,678", "%'ld", 12345678L);
00059 checkf ("123,456,789", "%'ld", 123456789L);
00060 checkf ("1,234,567,890", "%'ld", 1234567890L);
00061 checkf ("12,345,678,901", "%'lld", 12345678901LL);
00062 checkf ("123,456,789,012", "%'lld", 123456789012LL);
00063 checkf ("1,234,567,890,123", "%'lld", 1234567890123LL);
00064 checkf ("12,345,678,901,234", "%'lld", 12345678901234LL);
00065 checkf ("123,456,789,012,345", "%'lld", 123456789012345LL);
00066 checkf ("1,234,567,890,123,456", "%'lld", 1234567890123456LL);
00067 checkf ("12,345,678,901,234,567", "%'lld", 12345678901234567LL);
00068 checkf ("123,456,789,012,345,678", "%'lld", 123456789012345678LL);
00069 checkf ("1,234,567,890,123,456,789", "%'lld", 1234567890123456789LL);
00070
00071
00072
00073 checkf ("-1", "%'d", -1);
00074 checkf ("-12", "%'d", -12);
00075 checkf ("-123", "%'d", -123);
00076 checkf ("-1,234", "%'d", -1234);
00077 checkf ("-12,345", "%'d", -12345);
00078 checkf ("-123,456", "%'ld", -123456L);
00079 checkf ("-1,234,567", "%'ld", -1234567L);
00080 checkf ("-12,345,678", "%'ld", -12345678L);
00081 checkf ("-123,456,789", "%'ld", -123456789L);
00082 checkf ("-1,234,567,890", "%'ld", -1234567890L);
00083 checkf ("-12,345,678,901", "%'lld", -12345678901LL);
00084 checkf ("-123,456,789,012", "%'lld", -123456789012LL);
00085 checkf ("-1,234,567,890,123", "%'lld", -1234567890123LL);
00086 checkf ("-12,345,678,901,234", "%'lld", -12345678901234LL);
00087 checkf ("-123,456,789,012,345", "%'lld", -123456789012345LL);
00088 checkf ("-1,234,567,890,123,456", "%'lld", -1234567890123456LL);
00089 checkf ("-12,345,678,901,234,567", "%'lld", -12345678901234567LL);
00090 checkf ("-123,456,789,012,345,678", "%'lld", -123456789012345678LL);
00091 checkf ("-1,234,567,890,123,456,789", "%'lld", -1234567890123456789LL);
00092
00093
00094 checkf (" 0", "%5d", 0);
00095 checkf ("0 ", "%-5d", 0);
00096 checkf (" +0", "%+5d", 0);
00097 checkf ("+0 ", "%+-5d", 0);
00098 checkf (" 0", "% 5d", 0);
00099 checkf ("00000", "%05d", 0);
00100 checkf (" ", "%5.0d", 0);
00101 checkf (" 00", "%5.2d", 0);
00102 checkf ("0", "%d", 0);
00103
00104 checkf (" 1", "%5d", 1);
00105 checkf ("1 ", "%-5d", 1);
00106 checkf (" +1", "%+5d", 1);
00107 checkf ("+1 ", "%+-5d", 1);
00108 checkf (" 1", "% 5d", 1);
00109 checkf ("00001", "%05d", 1);
00110 checkf (" 1", "%5.0d", 1);
00111 checkf (" 01", "%5.2d", 1);
00112 checkf ("1", "%d", 1);
00113
00114 checkf (" -1", "%5d", -1);
00115 checkf ("-1 ", "%-5d", -1);
00116 checkf (" -1", "%+5d", -1);
00117 checkf ("-1 ", "%+-5d", -1);
00118 checkf (" -1", "% 5d", -1);
00119 checkf ("-0001", "%05d", -1);
00120 checkf (" -1", "%5.0d", -1);
00121 checkf (" -01", "%5.2d", -1);
00122 checkf ("-1", "%d", -1);
00123
00124 checkf ("12345", "%5d", 12345);
00125 checkf ("12345", "%-5d", 12345);
00126 checkf ("+12345", "%+5d", 12345);
00127 checkf ("+12345", "%+-5d", 12345);
00128 checkf (" 12345", "% 5d", 12345);
00129 checkf ("12345", "%05d", 12345);
00130 checkf ("12345", "%5.0d", 12345);
00131 checkf ("12345", "%5.2d", 12345);
00132 checkf ("12345", "%d", 12345);
00133
00134 checkf ("123456", "%5d", 123456);
00135 checkf ("123456", "%-5d", 123456);
00136 checkf ("+123456", "%+5d", 123456);
00137 checkf ("+123456", "%+-5d", 123456);
00138 checkf (" 123456", "% 5d", 123456);
00139 checkf ("123456", "%05d", 123456);
00140 checkf ("123456", "%5.0d", 123456);
00141 checkf ("123456", "%5.2d", 123456);
00142 checkf ("123456", "%d", 123456);
00143
00144
00145 checkf (" 0", "%5u", 0);
00146 checkf (" 0", "%5o", 0);
00147 checkf (" 0", "%5x", 0);
00148 checkf (" 0", "%5X", 0);
00149 checkf (" 0", "%#5o", 0);
00150 checkf (" 0", "%#5x", 0);
00151 checkf (" 0", "%#5X", 0);
00152 checkf (" 00000000", "%#10.8x", 0);
00153
00154 checkf (" 1", "%5u", 1);
00155 checkf (" 1", "%5o", 1);
00156 checkf (" 1", "%5x", 1);
00157 checkf (" 1", "%5X", 1);
00158 checkf (" 01", "%#5o", 1);
00159 checkf (" 0x1", "%#5x", 1);
00160 checkf (" 0X1", "%#5X", 1);
00161 checkf ("0x00000001", "%#10.8x", 1);
00162
00163 checkf ("123456", "%5u", 123456);
00164 checkf ("361100", "%5o", 123456);
00165 checkf ("1e240", "%5x", 123456);
00166 checkf ("1E240", "%5X", 123456);
00167 checkf ("0361100", "%#5o", 123456);
00168 checkf ("0x1e240", "%#5x", 123456);
00169 checkf ("0X1E240", "%#5X", 123456);
00170 checkf ("0x0001e240", "%#10.8x", 123456);
00171
00172
00173 checkf ("foobar", "%c%c%c%c%c%c", 'f', 'o', 'o', 'b', 'a', 'r');
00174 checkf (" left-right ", "%6s%s%-7s", "left", "-", "right");
00175 checkf ("trim", "%.4s", "trimoff");
00176 checkf ("%%", "%%%%");
00177
00178
00179 checkf (" abcdefgh", "%9s", "abcdefgh");
00180 checkf ("36657730000", "%- o", (unsigned) 036657730000);
00181 checkf ("4139757568", "%- u", (unsigned) 4139757568UL);
00182 checkf ("f6bfb000", "%- x", (unsigned) 0xf6bfb000);
00183 checkf ("36657730000", "%-to", (ptrdiff_t) 036657730000);
00184 checkf ("4139757568", "%-tu", (ptrdiff_t) 4139757568UL);
00185 checkf ("-155209728", "%-zi", (size_t) -155209728);
00186 checkf ("-155209728", "%-zd", (size_t) -155209728);
00187 checkf ("036657730000", "%+#o", (unsigned) 036657730000);
00188 checkf ("0xf6bfb000", "%+#x", (unsigned) 0xf6bfb000);
00189 checkf ("-155209728", "% zi", (size_t) -155209728);
00190 checkf ("-155209728", "% zd", (size_t) -155209728);
00191 checkf ("4139757568", "% tu", (ptrdiff_t) 4139757568UL);
00192 checkf ("036657730000", "% #o", (unsigned) 036657730000);
00193 checkf ("0xf6bfb000", "% #x", (unsigned) 0xf6bfb000);
00194 checkf ("0xf6bfb000", "%# x", (unsigned) 0xf6bfb000);
00195 checkf ("-155209728", "%#zd", (size_t) -155209728);
00196 checkf ("-155209728", "%0zi", (size_t) -155209728);
00197 checkf ("4,139,757,568", "%'tu", (ptrdiff_t) 4139757568UL);
00198 checkf ("-155,209,728", "%-'d", -155209728);
00199 checkf ("-155209728", "%.zi", (size_t) -155209728);
00200 checkf ("-155209728", "%zi", (size_t) -155209728);
00201 checkf ("-155209728", "%zd", (size_t) -155209728);
00202 checkf ("-155209728", "%+zi", (size_t) -155209728);
00203
00204 if (failure_cnt == 0)
00205 printf ("\nstdio: PASS\n");
00206 else
00207 printf ("\nstdio: FAIL: %d tests failed\n", failure_cnt);
00208 }