root/maint/gnulib/tests/unistdio/test-ulc-printf1.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. test_xfunction

   1 /* Test of ulc_v[a]s[n]printf() functions.
   2    Copyright (C) 2007, 2009-2021 Free Software Foundation, Inc.
   3 
   4    This program is free software: you can redistribute it and/or modify
   5    it under the terms of the GNU General Public License as published by
   6    the Free Software Foundation; either version 3 of the License, or
   7    (at your option) any later version.
   8 
   9    This program is distributed in the hope that it will be useful,
  10    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12    GNU General Public License for more details.
  13 
  14    You should have received a copy of the GNU General Public License
  15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  16 
  17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
  18 
  19 static void
  20 test_xfunction (char * (*my_xasprintf) (const char *, ...))
     /* [previous][next][first][last][top][bottom][index][help] */
  21 {
  22   /* Test support of size specifiers as in C99.  */
  23 
  24   {
  25     char *result =
  26       my_xasprintf ("%ju %d", (uintmax_t) 12345671, 33, 44, 55);
  27     ASSERT (result != NULL);
  28     ASSERT (strcmp (result, "12345671 33") == 0);
  29     free (result);
  30   }
  31 
  32   {
  33     char *result =
  34       my_xasprintf ("%zu %d", (size_t) 12345672, 33, 44, 55);
  35     ASSERT (result != NULL);
  36     ASSERT (strcmp (result, "12345672 33") == 0);
  37     free (result);
  38   }
  39 
  40   {
  41     char *result =
  42       my_xasprintf ("%tu %d", (ptrdiff_t) 12345673, 33, 44, 55);
  43     ASSERT (result != NULL);
  44     ASSERT (strcmp (result, "12345673 33") == 0);
  45     free (result);
  46   }
  47 
  48   {
  49     char *result =
  50       my_xasprintf ("%Lg %d", (long double) 1.5, 33, 44, 55);
  51     ASSERT (result != NULL);
  52     ASSERT (strcmp (result, "1.5 33") == 0);
  53     free (result);
  54   }
  55 
  56   /* Test the support of the 'U' conversion specifier for Unicode strings.  */
  57 
  58   {
  59     static const uint8_t unicode_string[] = "Hello";
  60     {
  61       char *result =
  62         my_xasprintf ("%U %d", unicode_string, 33, 44, 55);
  63       ASSERT (result != NULL);
  64       ASSERT (strcmp (result, "Hello 33") == 0);
  65       free (result);
  66     }
  67     { /* Width.  */
  68       char *result =
  69         my_xasprintf ("%10U %d", unicode_string, 33, 44, 55);
  70       ASSERT (result != NULL);
  71       ASSERT (strcmp (result, "     Hello 33") == 0);
  72       free (result);
  73     }
  74     { /* FLAG_LEFT.  */
  75       char *result =
  76         my_xasprintf ("%-10U %d", unicode_string, 33, 44, 55);
  77       ASSERT (result != NULL);
  78       ASSERT (strcmp (result, "Hello      33") == 0);
  79       free (result);
  80     }
  81     { /* FLAG_ZERO: no effect.  */
  82       char *result =
  83         my_xasprintf ("%010U %d", unicode_string, 33, 44, 55);
  84       ASSERT (result != NULL);
  85       ASSERT (strcmp (result, "     Hello 33") == 0);
  86       free (result);
  87     }
  88   }
  89 
  90   {
  91     static const uint16_t unicode_string[] = { 'H', 'e', 'l', 'l', 'o', 0 };
  92     {
  93       char *result =
  94         my_xasprintf ("%lU %d", unicode_string, 33, 44, 55);
  95       ASSERT (result != NULL);
  96       ASSERT (strcmp (result, "Hello 33") == 0);
  97       free (result);
  98     }
  99     { /* Width.  */
 100       char *result =
 101         my_xasprintf ("%10lU %d", unicode_string, 33, 44, 55);
 102       ASSERT (result != NULL);
 103       ASSERT (strcmp (result, "     Hello 33") == 0);
 104       free (result);
 105     }
 106     { /* FLAG_LEFT.  */
 107       char *result =
 108         my_xasprintf ("%-10lU %d", unicode_string, 33, 44, 55);
 109       ASSERT (result != NULL);
 110       ASSERT (strcmp (result, "Hello      33") == 0);
 111       free (result);
 112     }
 113     { /* FLAG_ZERO: no effect.  */
 114       char *result =
 115         my_xasprintf ("%010lU %d", unicode_string, 33, 44, 55);
 116       ASSERT (result != NULL);
 117       ASSERT (strcmp (result, "     Hello 33") == 0);
 118       free (result);
 119     }
 120   }
 121 
 122   {
 123     static const uint32_t unicode_string[] = { 'H', 'e', 'l', 'l', 'o', 0 };
 124     {
 125       char *result =
 126         my_xasprintf ("%llU %d", unicode_string, 33, 44, 55);
 127       ASSERT (result != NULL);
 128       ASSERT (strcmp (result, "Hello 33") == 0);
 129       free (result);
 130     }
 131     { /* Width.  */
 132       char *result =
 133         my_xasprintf ("%10llU %d", unicode_string, 33, 44, 55);
 134       ASSERT (result != NULL);
 135       ASSERT (strcmp (result, "     Hello 33") == 0);
 136       free (result);
 137     }
 138     { /* FLAG_LEFT.  */
 139       char *result =
 140         my_xasprintf ("%-10llU %d", unicode_string, 33, 44, 55);
 141       ASSERT (result != NULL);
 142       ASSERT (strcmp (result, "Hello      33") == 0);
 143       free (result);
 144     }
 145     { /* FLAG_ZERO: no effect.  */
 146       char *result =
 147         my_xasprintf ("%010llU %d", unicode_string, 33, 44, 55);
 148       ASSERT (result != NULL);
 149       ASSERT (strcmp (result, "     Hello 33") == 0);
 150       free (result);
 151     }
 152   }
 153 
 154   /* Test the support of the 's' conversion specifier for strings.  */
 155 
 156   {
 157     char *result =
 158       my_xasprintf ("Mr. %s %d", "Ronald Reagan", 33, 44, 55);
 159     ASSERT (result != NULL);
 160     ASSERT (strcmp (result, "Mr. Ronald Reagan 33") == 0);
 161     free (result);
 162   }
 163 
 164   { /* Width.  */
 165     char *result =
 166       my_xasprintf ("Mr. %20s %d", "Ronald Reagan", 33, 44, 55);
 167     ASSERT (result != NULL);
 168     ASSERT (strcmp (result, "Mr.        Ronald Reagan 33") == 0);
 169     free (result);
 170   }
 171 
 172   { /* FLAG_LEFT.  */
 173     char *result =
 174       my_xasprintf ("Mr. %-20s %d", "Ronald Reagan", 33, 44, 55);
 175     ASSERT (result != NULL);
 176     ASSERT (strcmp (result, "Mr. Ronald Reagan        33") == 0);
 177     free (result);
 178   }
 179 
 180   { /* FLAG_ZERO: no effect.  */
 181     char *result =
 182       my_xasprintf ("Mr. %020s %d", "Ronald Reagan", 33, 44, 55);
 183     ASSERT (result != NULL);
 184     ASSERT (strcmp (result, "Mr.        Ronald Reagan 33") == 0);
 185     free (result);
 186   }
 187 
 188   /* Test the support of the 'a' and 'A' conversion specifier for hexadecimal
 189      output of floating-point numbers.  */
 190 
 191   { /* A positive number.  */
 192     char *result =
 193       my_xasprintf ("%a %d", 3.1416015625, 33, 44, 55);
 194     ASSERT (result != NULL);
 195     ASSERT (strcmp (result, "0x1.922p+1 33") == 0
 196             || strcmp (result, "0x3.244p+0 33") == 0
 197             || strcmp (result, "0x6.488p-1 33") == 0
 198             || strcmp (result, "0xc.91p-2 33") == 0);
 199     free (result);
 200   }
 201 
 202   { /* Width.  */
 203     char *result =
 204       my_xasprintf ("%10a %d", 1.75, 33, 44, 55);
 205     ASSERT (result != NULL);
 206     ASSERT (strcmp (result, "  0x1.cp+0 33") == 0
 207             || strcmp (result, "  0x3.8p-1 33") == 0
 208             || strcmp (result, "    0x7p-2 33") == 0
 209             || strcmp (result, "    0xep-3 33") == 0);
 210     free (result);
 211   }
 212 
 213   { /* Small precision.  */
 214     char *result =
 215       my_xasprintf ("%.10a %d", 1.75, 33, 44, 55);
 216     ASSERT (result != NULL);
 217     ASSERT (strcmp (result, "0x1.c000000000p+0 33") == 0
 218             || strcmp (result, "0x3.8000000000p-1 33") == 0
 219             || strcmp (result, "0x7.0000000000p-2 33") == 0
 220             || strcmp (result, "0xe.0000000000p-3 33") == 0);
 221     free (result);
 222   }
 223 
 224   { /* Large precision.  */
 225     char *result =
 226       my_xasprintf ("%.50a %d", 1.75, 33, 44, 55);
 227     ASSERT (result != NULL);
 228     ASSERT (strcmp (result, "0x1.c0000000000000000000000000000000000000000000000000p+0 33") == 0
 229             || strcmp (result, "0x3.80000000000000000000000000000000000000000000000000p-1 33") == 0
 230             || strcmp (result, "0x7.00000000000000000000000000000000000000000000000000p-2 33") == 0
 231             || strcmp (result, "0xe.00000000000000000000000000000000000000000000000000p-3 33") == 0);
 232     free (result);
 233   }
 234 
 235   { /* A positive number.  */
 236     char *result =
 237       my_xasprintf ("%La %d", 3.1416015625L, 33, 44, 55);
 238     ASSERT (result != NULL);
 239     ASSERT (strcmp (result, "0x1.922p+1 33") == 0
 240             || strcmp (result, "0x3.244p+0 33") == 0
 241             || strcmp (result, "0x6.488p-1 33") == 0
 242             || strcmp (result, "0xc.91p-2 33") == 0);
 243     free (result);
 244   }
 245 
 246   { /* Width.  */
 247     char *result =
 248       my_xasprintf ("%10La %d", 1.75L, 33, 44, 55);
 249     ASSERT (result != NULL);
 250     ASSERT (strcmp (result, "  0x1.cp+0 33") == 0
 251             || strcmp (result, "  0x3.8p-1 33") == 0
 252             || strcmp (result, "    0x7p-2 33") == 0
 253             || strcmp (result, "    0xep-3 33") == 0);
 254     free (result);
 255   }
 256 
 257   { /* Small precision.  */
 258     char *result =
 259       my_xasprintf ("%.10La %d", 1.75L, 33, 44, 55);
 260     ASSERT (result != NULL);
 261     ASSERT (strcmp (result, "0x1.c000000000p+0 33") == 0
 262             || strcmp (result, "0x3.8000000000p-1 33") == 0
 263             || strcmp (result, "0x7.0000000000p-2 33") == 0
 264             || strcmp (result, "0xe.0000000000p-3 33") == 0);
 265     free (result);
 266   }
 267 
 268   { /* Large precision.  */
 269     char *result =
 270       my_xasprintf ("%.50La %d", 1.75L, 33, 44, 55);
 271     ASSERT (result != NULL);
 272     ASSERT (strcmp (result, "0x1.c0000000000000000000000000000000000000000000000000p+0 33") == 0
 273             || strcmp (result, "0x3.80000000000000000000000000000000000000000000000000p-1 33") == 0
 274             || strcmp (result, "0x7.00000000000000000000000000000000000000000000000000p-2 33") == 0
 275             || strcmp (result, "0xe.00000000000000000000000000000000000000000000000000p-3 33") == 0);
 276     free (result);
 277   }
 278 
 279   /* Test the support of the %f format directive.  */
 280 
 281   { /* A positive number.  */
 282     char *result =
 283       my_xasprintf ("%f %d", 12.75, 33, 44, 55);
 284     ASSERT (result != NULL);
 285     ASSERT (strcmp (result, "12.750000 33") == 0);
 286     free (result);
 287   }
 288 
 289   { /* Width.  */
 290     char *result =
 291       my_xasprintf ("%10f %d", 1.75, 33, 44, 55);
 292     ASSERT (result != NULL);
 293     ASSERT (strcmp (result, "  1.750000 33") == 0);
 294     free (result);
 295   }
 296 
 297   { /* Precision.  */
 298     char *result =
 299       my_xasprintf ("%.f %d", 1234.0, 33, 44, 55);
 300     ASSERT (result != NULL);
 301     ASSERT (strcmp (result, "1234 33") == 0);
 302     free (result);
 303   }
 304 
 305   { /* A positive number.  */
 306     char *result =
 307       my_xasprintf ("%Lf %d", 12.75L, 33, 44, 55);
 308     ASSERT (result != NULL);
 309     ASSERT (strcmp (result, "12.750000 33") == 0);
 310     free (result);
 311   }
 312 
 313   { /* Width.  */
 314     char *result =
 315       my_xasprintf ("%10Lf %d", 1.75L, 33, 44, 55);
 316     ASSERT (result != NULL);
 317     ASSERT (strcmp (result, "  1.750000 33") == 0);
 318     free (result);
 319   }
 320 
 321   { /* Precision.  */
 322     char *result =
 323       my_xasprintf ("%.Lf %d", 1234.0L, 33, 44, 55);
 324     ASSERT (result != NULL);
 325     ASSERT (strcmp (result, "1234 33") == 0);
 326     free (result);
 327   }
 328 
 329   /* Test the support of the %F format directive.  */
 330 
 331   { /* A positive number.  */
 332     char *result =
 333       my_xasprintf ("%F %d", 12.75, 33, 44, 55);
 334     ASSERT (result != NULL);
 335     ASSERT (strcmp (result, "12.750000 33") == 0);
 336     free (result);
 337   }
 338 
 339   { /* Precision.  */
 340     char *result =
 341       my_xasprintf ("%.F %d", 1234.0, 33, 44, 55);
 342     ASSERT (result != NULL);
 343     ASSERT (strcmp (result, "1234 33") == 0);
 344     free (result);
 345   }
 346 
 347   { /* A positive number.  */
 348     char *result =
 349       my_xasprintf ("%LF %d", 12.75L, 33, 44, 55);
 350     ASSERT (result != NULL);
 351     ASSERT (strcmp (result, "12.750000 33") == 0);
 352     free (result);
 353   }
 354 
 355   { /* Precision.  */
 356     char *result =
 357       my_xasprintf ("%.LF %d", 1234.0L, 33, 44, 55);
 358     ASSERT (result != NULL);
 359     ASSERT (strcmp (result, "1234 33") == 0);
 360     free (result);
 361   }
 362 
 363   /* Test the support of the %e format directive.  */
 364 
 365   { /* A positive number.  */
 366     char *result =
 367       my_xasprintf ("%e %d", 12.75, 33, 44, 55);
 368     ASSERT (result != NULL);
 369     ASSERT (strcmp (result, "1.275000e+01 33") == 0
 370             || strcmp (result, "1.275000e+001 33") == 0);
 371     free (result);
 372   }
 373 
 374   { /* Width.  */
 375     char *result =
 376       my_xasprintf ("%15e %d", 1.75, 33, 44, 55);
 377     ASSERT (result != NULL);
 378     ASSERT (strcmp (result, "   1.750000e+00 33") == 0
 379             || strcmp (result, "  1.750000e+000 33") == 0);
 380     free (result);
 381   }
 382 
 383   { /* Precision.  */
 384     char *result =
 385       my_xasprintf ("%.e %d", 1234.0, 33, 44, 55);
 386     ASSERT (result != NULL);
 387     ASSERT (strcmp (result, "1e+03 33") == 0
 388             || strcmp (result, "1e+003 33") == 0);
 389     free (result);
 390   }
 391 
 392   { /* A positive number.  */
 393     char *result =
 394       my_xasprintf ("%Le %d", 12.75L, 33, 44, 55);
 395     ASSERT (result != NULL);
 396     ASSERT (strcmp (result, "1.275000e+01 33") == 0);
 397     free (result);
 398   }
 399 
 400   { /* Width.  */
 401     char *result =
 402       my_xasprintf ("%15Le %d", 1.75L, 33, 44, 55);
 403     ASSERT (result != NULL);
 404     ASSERT (strcmp (result, "   1.750000e+00 33") == 0);
 405     free (result);
 406   }
 407 
 408   { /* Precision.  */
 409     char *result =
 410       my_xasprintf ("%.Le %d", 1234.0L, 33, 44, 55);
 411     ASSERT (result != NULL);
 412     ASSERT (strcmp (result, "1e+03 33") == 0);
 413     free (result);
 414   }
 415 
 416   /* Test the support of the %g format directive.  */
 417 
 418   { /* A positive number.  */
 419     char *result =
 420       my_xasprintf ("%g %d", 12.75, 33, 44, 55);
 421     ASSERT (result != NULL);
 422     ASSERT (strcmp (result, "12.75 33") == 0);
 423     free (result);
 424   }
 425 
 426   { /* Width.  */
 427     char *result =
 428       my_xasprintf ("%10g %d", 1.75, 33, 44, 55);
 429     ASSERT (result != NULL);
 430     ASSERT (strcmp (result, "      1.75 33") == 0);
 431     free (result);
 432   }
 433 
 434   { /* Precision.  */
 435     char *result =
 436       my_xasprintf ("%.g %d", 1234.0, 33, 44, 55);
 437     ASSERT (result != NULL);
 438     ASSERT (strcmp (result, "1e+03 33") == 0
 439             || strcmp (result, "1e+003 33") == 0);
 440     free (result);
 441   }
 442 
 443   { /* A positive number.  */
 444     char *result =
 445       my_xasprintf ("%Lg %d", 12.75L, 33, 44, 55);
 446     ASSERT (result != NULL);
 447     ASSERT (strcmp (result, "12.75 33") == 0);
 448     free (result);
 449   }
 450 
 451   { /* Width.  */
 452     char *result =
 453       my_xasprintf ("%10Lg %d", 1.75L, 33, 44, 55);
 454     ASSERT (result != NULL);
 455     ASSERT (strcmp (result, "      1.75 33") == 0);
 456     free (result);
 457   }
 458 
 459   { /* Precision.  */
 460     char *result =
 461       my_xasprintf ("%.Lg %d", 1234.0L, 33, 44, 55);
 462     ASSERT (result != NULL);
 463     ASSERT (strcmp (result, "1e+03 33") == 0);
 464     free (result);
 465   }
 466 
 467   /* Test the support of the %n format directive.  */
 468 
 469   {
 470     int count = -1;
 471     char *result =
 472       my_xasprintf ("%d %n", 123, &count, 33, 44, 55);
 473     ASSERT (result != NULL);
 474     ASSERT (strcmp (result, "123 ") == 0);
 475     ASSERT (count == 4);
 476     free (result);
 477   }
 478 
 479   /* Test the support of the POSIX/XSI format strings with positions.  */
 480 
 481   {
 482     char *result =
 483       my_xasprintf ("%2$d %1$d", 33, 55);
 484     ASSERT (result != NULL);
 485     ASSERT (strcmp (result, "55 33") == 0);
 486     free (result);
 487   }
 488 
 489   /* Test the support of the grouping flag.  */
 490 
 491   {
 492     char *result =
 493       my_xasprintf ("%'d %d", 1234567, 99);
 494     ASSERT (result != NULL);
 495     ASSERT (result[strlen (result) - 1] == '9');
 496     free (result);
 497   }
 498 }

/* [previous][next][first][last][top][bottom][index][help] */