lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <float.h> |
| 2 | #include <math.h> |
| 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | #include <wchar.h> |
| 6 | #include <libc-internal.h> |
| 7 | |
| 8 | static int |
| 9 | t1 (void) |
| 10 | { |
| 11 | int n = -1; |
| 12 | sscanf ("abc ", "abc %n", &n); |
| 13 | printf ("t1: count=%d\n", n); |
| 14 | |
| 15 | return n != 5; |
| 16 | } |
| 17 | |
| 18 | static int |
| 19 | t2 (void) |
| 20 | { |
| 21 | int result = 0; |
| 22 | int n; |
| 23 | long N; |
| 24 | int retval; |
| 25 | #define SCAN(INPUT, FORMAT, VAR, EXP_RES, EXP_VAL) \ |
| 26 | VAR = -1; \ |
| 27 | retval = sscanf (INPUT, FORMAT, &VAR); \ |
| 28 | printf ("sscanf (\"%s\", \"%s\", &x) => %d, x = %ld\n", \ |
| 29 | INPUT, FORMAT, retval, (long int) VAR); \ |
| 30 | result |= retval != EXP_RES || VAR != EXP_VAL |
| 31 | |
| 32 | /* This function is testing corner cases of the scanf format string, |
| 33 | so they do not all conform to -Wformat's expectations. */ |
| 34 | DIAG_PUSH_NEEDS_COMMENT; |
| 35 | DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat"); |
| 36 | DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat-extra-args"); |
| 37 | |
| 38 | SCAN ("12345", "%ld", N, 1, 12345); |
| 39 | SCAN ("12345", "%llllld", N, 0, -1); |
| 40 | SCAN ("12345", "%LLLLLd", N, 0, -1); |
| 41 | SCAN ("test ", "%*s%n", n, 0, 4); |
| 42 | SCAN ("test ", "%2*s%n", n, 0, -1); |
| 43 | SCAN ("12 ", "%l2d", n, 0, -1); |
| 44 | SCAN ("12 ", "%2ld", N, 1, 12); |
| 45 | |
| 46 | n = -1; |
| 47 | N = -1; |
| 48 | retval = sscanf ("1 1", "%d %Z", &n, &N); |
| 49 | printf ("sscanf (\"1 1\", \"%%d %%Z\", &n, &N) => %d, n = %d, N = %ld\n", \ |
| 50 | retval, n, N); \ |
| 51 | result |= retval != 1 || n != 1 || N != -1; |
| 52 | |
| 53 | DIAG_POP_NEEDS_COMMENT; |
| 54 | |
| 55 | return result; |
| 56 | } |
| 57 | |
| 58 | static int |
| 59 | t3 (void) |
| 60 | { |
| 61 | char buf[80]; |
| 62 | wchar_t wbuf[80]; |
| 63 | int result = 0; |
| 64 | int retval; |
| 65 | |
| 66 | retval = sprintf (buf, "%p", (char *) NULL); |
| 67 | result |= retval != 5 || strcmp (buf, "(nil)") != 0; |
| 68 | |
| 69 | retval = swprintf (wbuf, sizeof (wbuf) / sizeof (wbuf[0]), |
| 70 | L"%p", (char *) NULL); |
| 71 | result |= retval != 5 || wcscmp (wbuf, L"(nil)") != 0; |
| 72 | |
| 73 | return result; |
| 74 | } |
| 75 | |
| 76 | volatile double qnanval; |
| 77 | volatile long double lqnanval; |
| 78 | /* A sNaN is only guaranteed to be representable in variables with static (or |
| 79 | thread-local) storage duration. */ |
| 80 | static volatile double snanval = __builtin_nans (""); |
| 81 | static volatile double msnanval = -__builtin_nans (""); |
| 82 | static volatile long double lsnanval = __builtin_nansl (""); |
| 83 | static volatile long double lmsnanval = -__builtin_nansl (""); |
| 84 | volatile double infval; |
| 85 | volatile long double linfval; |
| 86 | |
| 87 | |
| 88 | static int |
| 89 | F (void) |
| 90 | { |
| 91 | char buf[80]; |
| 92 | wchar_t wbuf[40]; |
| 93 | int result = 0; |
| 94 | |
| 95 | qnanval = NAN; |
| 96 | |
| 97 | snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G", |
| 98 | qnanval, qnanval, qnanval, qnanval, |
| 99 | qnanval, qnanval, qnanval, qnanval); |
| 100 | result |= strcmp (buf, "nan NAN nan NAN nan NAN nan NAN") != 0; |
| 101 | printf ("expected \"nan NAN nan NAN nan NAN nan NAN\", got \"%s\"\n", buf); |
| 102 | |
| 103 | snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G", |
| 104 | -qnanval, -qnanval, -qnanval, -qnanval, |
| 105 | -qnanval, -qnanval, -qnanval, -qnanval); |
| 106 | result |= strcmp (buf, "-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0; |
| 107 | printf ("expected \"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got \"%s\"\n", |
| 108 | buf); |
| 109 | |
| 110 | snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G", |
| 111 | snanval, snanval, snanval, snanval, |
| 112 | snanval, snanval, snanval, snanval); |
| 113 | result |= strcmp (buf, "nan NAN nan NAN nan NAN nan NAN") != 0; |
| 114 | printf ("expected \"nan NAN nan NAN nan NAN nan NAN\", got \"%s\"\n", buf); |
| 115 | |
| 116 | snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G", |
| 117 | msnanval, msnanval, msnanval, msnanval, |
| 118 | msnanval, msnanval, msnanval, msnanval); |
| 119 | result |= strcmp (buf, "-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0; |
| 120 | printf ("expected \"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got \"%s\"\n", |
| 121 | buf); |
| 122 | |
| 123 | infval = DBL_MAX * DBL_MAX; |
| 124 | |
| 125 | snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G", |
| 126 | infval, infval, infval, infval, infval, infval, infval, infval); |
| 127 | result |= strcmp (buf, "inf INF inf INF inf INF inf INF") != 0; |
| 128 | printf ("expected \"inf INF inf INF inf INF inf INF\", got \"%s\"\n", buf); |
| 129 | |
| 130 | snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G", |
| 131 | -infval, -infval, -infval, -infval, |
| 132 | -infval, -infval, -infval, -infval); |
| 133 | result |= strcmp (buf, "-inf -INF -inf -INF -inf -INF -inf -INF") != 0; |
| 134 | printf ("expected \"-inf -INF -inf -INF -inf -INF -inf -INF\", got \"%s\"\n", |
| 135 | buf); |
| 136 | |
| 137 | swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G", |
| 138 | qnanval, qnanval, qnanval, qnanval, |
| 139 | qnanval, qnanval, qnanval, qnanval); |
| 140 | result |= wcscmp (wbuf, L"nan NAN nan NAN nan NAN nan NAN") != 0; |
| 141 | printf ("expected L\"nan NAN nan NAN nan NAN nan NAN\", got L\"%S\"\n", wbuf); |
| 142 | |
| 143 | swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G", |
| 144 | -qnanval, -qnanval, -qnanval, -qnanval, |
| 145 | -qnanval, -qnanval, -qnanval, -qnanval); |
| 146 | result |= wcscmp (wbuf, L"-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0; |
| 147 | printf ("expected L\"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got L\"%S\"\n", |
| 148 | wbuf); |
| 149 | |
| 150 | swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G", |
| 151 | snanval, snanval, snanval, snanval, |
| 152 | snanval, snanval, snanval, snanval); |
| 153 | result |= wcscmp (wbuf, L"nan NAN nan NAN nan NAN nan NAN") != 0; |
| 154 | printf ("expected L\"nan NAN nan NAN nan NAN nan NAN\", got L\"%S\"\n", wbuf); |
| 155 | |
| 156 | swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G", |
| 157 | msnanval, msnanval, msnanval, msnanval, |
| 158 | msnanval, msnanval, msnanval, msnanval); |
| 159 | result |= wcscmp (wbuf, L"-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0; |
| 160 | printf ("expected L\"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got L\"%S\"\n", |
| 161 | wbuf); |
| 162 | |
| 163 | swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G", |
| 164 | infval, infval, infval, infval, infval, infval, infval, infval); |
| 165 | result |= wcscmp (wbuf, L"inf INF inf INF inf INF inf INF") != 0; |
| 166 | printf ("expected L\"inf INF inf INF inf INF inf INF\", got L\"%S\"\n", wbuf); |
| 167 | |
| 168 | swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G", |
| 169 | -infval, -infval, -infval, -infval, |
| 170 | -infval, -infval, -infval, -infval); |
| 171 | result |= wcscmp (wbuf, L"-inf -INF -inf -INF -inf -INF -inf -INF") != 0; |
| 172 | printf ("expected L\"-inf -INF -inf -INF -inf -INF -inf -INF\", got L\"%S\"\n", |
| 173 | wbuf); |
| 174 | |
| 175 | lqnanval = NAN; |
| 176 | |
| 177 | snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG", |
| 178 | lqnanval, lqnanval, lqnanval, lqnanval, |
| 179 | lqnanval, lqnanval, lqnanval, lqnanval); |
| 180 | result |= strcmp (buf, "nan NAN nan NAN nan NAN nan NAN") != 0; |
| 181 | printf ("expected \"nan NAN nan NAN nan NAN nan NAN\", got \"%s\"\n", buf); |
| 182 | |
| 183 | snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG", |
| 184 | -lqnanval, -lqnanval, -lqnanval, -lqnanval, |
| 185 | -lqnanval, -lqnanval, -lqnanval, -lqnanval); |
| 186 | result |= strcmp (buf, "-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0; |
| 187 | printf ("expected \"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got \"%s\"\n", |
| 188 | buf); |
| 189 | |
| 190 | snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG", |
| 191 | lsnanval, lsnanval, lsnanval, lsnanval, |
| 192 | lsnanval, lsnanval, lsnanval, lsnanval); |
| 193 | result |= strcmp (buf, "nan NAN nan NAN nan NAN nan NAN") != 0; |
| 194 | printf ("expected \"nan NAN nan NAN nan NAN nan NAN\", got \"%s\"\n", buf); |
| 195 | |
| 196 | snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG", |
| 197 | lmsnanval, lmsnanval, lmsnanval, lmsnanval, |
| 198 | lmsnanval, lmsnanval, lmsnanval, lmsnanval); |
| 199 | result |= strcmp (buf, "-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0; |
| 200 | printf ("expected \"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got \"%s\"\n", |
| 201 | buf); |
| 202 | |
| 203 | linfval = LDBL_MAX * LDBL_MAX; |
| 204 | |
| 205 | snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG", |
| 206 | linfval, linfval, linfval, linfval, |
| 207 | linfval, linfval, linfval, linfval); |
| 208 | result |= strcmp (buf, "inf INF inf INF inf INF inf INF") != 0; |
| 209 | printf ("expected \"inf INF inf INF inf INF inf INF\", got \"%s\"\n", buf); |
| 210 | |
| 211 | snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG", |
| 212 | -linfval, -linfval, -linfval, -linfval, |
| 213 | -linfval, -linfval, -linfval, -linfval); |
| 214 | result |= strcmp (buf, "-inf -INF -inf -INF -inf -INF -inf -INF") != 0; |
| 215 | printf ("expected \"-inf -INF -inf -INF -inf -INF -inf -INF\", got \"%s\"\n", |
| 216 | buf); |
| 217 | |
| 218 | swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), |
| 219 | L"%La %LA %Le %LE %Lf %LF %Lg %LG", |
| 220 | lqnanval, lqnanval, lqnanval, lqnanval, |
| 221 | lqnanval, lqnanval, lqnanval, lqnanval); |
| 222 | result |= wcscmp (wbuf, L"nan NAN nan NAN nan NAN nan NAN") != 0; |
| 223 | printf ("expected L\"nan NAN nan NAN nan NAN nan NAN\", got L\"%S\"\n", wbuf); |
| 224 | |
| 225 | swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), |
| 226 | L"%La %LA %Le %LE %Lf %LF %Lg %LG", |
| 227 | -lqnanval, -lqnanval, -lqnanval, -lqnanval, |
| 228 | -lqnanval, -lqnanval, -lqnanval, -lqnanval); |
| 229 | result |= wcscmp (wbuf, L"-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0; |
| 230 | printf ("expected L\"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got L\"%S\"\n", |
| 231 | wbuf); |
| 232 | |
| 233 | swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), |
| 234 | L"%La %LA %Le %LE %Lf %LF %Lg %LG", |
| 235 | lsnanval, lsnanval, lsnanval, lsnanval, |
| 236 | lsnanval, lsnanval, lsnanval, lsnanval); |
| 237 | result |= wcscmp (wbuf, L"nan NAN nan NAN nan NAN nan NAN") != 0; |
| 238 | printf ("expected L\"nan NAN nan NAN nan NAN nan NAN\", got L\"%S\"\n", wbuf); |
| 239 | |
| 240 | swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), |
| 241 | L"%La %LA %Le %LE %Lf %LF %Lg %LG", |
| 242 | lmsnanval, lmsnanval, lmsnanval, lmsnanval, |
| 243 | lmsnanval, lmsnanval, lmsnanval, lmsnanval); |
| 244 | result |= wcscmp (wbuf, L"-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0; |
| 245 | printf ("expected L\"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got L\"%S\"\n", |
| 246 | wbuf); |
| 247 | |
| 248 | swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), |
| 249 | L"%La %LA %Le %LE %Lf %LF %Lg %LG", |
| 250 | linfval, linfval, linfval, linfval, |
| 251 | linfval, linfval, linfval, linfval); |
| 252 | result |= wcscmp (wbuf, L"inf INF inf INF inf INF inf INF") != 0; |
| 253 | printf ("expected L\"inf INF inf INF inf INF inf INF\", got L\"%S\"\n", wbuf); |
| 254 | |
| 255 | swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), |
| 256 | L"%La %LA %Le %LE %Lf %LF %Lg %LG", |
| 257 | -linfval, -linfval, -linfval, -linfval, |
| 258 | -linfval, -linfval, -linfval, -linfval); |
| 259 | result |= wcscmp (wbuf, L"-inf -INF -inf -INF -inf -INF -inf -INF") != 0; |
| 260 | printf ("expected L\"-inf -INF -inf -INF -inf -INF -inf -INF\", got L\"%S\"\n", |
| 261 | wbuf); |
| 262 | |
| 263 | return result; |
| 264 | } |
| 265 | |
| 266 | int |
| 267 | main (int argc, char *argv[]) |
| 268 | { |
| 269 | int result = 0; |
| 270 | |
| 271 | result |= t1 (); |
| 272 | result |= t2 (); |
| 273 | result |= t3 (); |
| 274 | result |= F (); |
| 275 | |
| 276 | result |= fflush (stdout) == EOF; |
| 277 | |
| 278 | return result; |
| 279 | } |