root/maint/gnulib/tests/unistdio/test-u8-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 u8_v[a]s[n]printf() function.
   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 (uint8_t * (*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     uint8_t *result =
  26       my_xasprintf ("%ju %d", (uintmax_t) 12345671, 33, 44, 55);
  27     static const uint8_t expected[] = "12345671 33";
  28     ASSERT (result != NULL);
  29     ASSERT (u8_strcmp (result, expected) == 0);
  30     free (result);
  31   }
  32 
  33   {
  34     uint8_t *result =
  35       my_xasprintf ("%zu %d", (size_t) 12345672, 33, 44, 55);
  36     static const uint8_t expected[] = "12345672 33";
  37     ASSERT (result != NULL);
  38     ASSERT (u8_strcmp (result, expected) == 0);
  39     free (result);
  40   }
  41 
  42   {
  43     uint8_t *result =
  44       my_xasprintf ("%tu %d", (ptrdiff_t) 12345673, 33, 44, 55);
  45     static const uint8_t expected[] = "12345673 33";
  46     ASSERT (result != NULL);
  47     ASSERT (u8_strcmp (result, expected) == 0);
  48     free (result);
  49   }
  50 
  51   {
  52     uint8_t *result =
  53       my_xasprintf ("%Lg %d", (long double) 1.5, 33, 44, 55);
  54     static const uint8_t expected[] = "1.5 33";
  55     ASSERT (result != NULL);
  56     ASSERT (u8_strcmp (result, expected) == 0);
  57     free (result);
  58   }
  59 
  60   /* Test the support of the 'U' conversion specifier for Unicode strings.  */
  61 
  62   {
  63     static const uint8_t unicode_string[] = "Hello";
  64     {
  65       uint8_t *result =
  66         my_xasprintf ("%U %d", unicode_string, 33, 44, 55);
  67       static const uint8_t expected[] = "Hello 33";
  68       ASSERT (result != NULL);
  69       ASSERT (u8_strcmp (result, expected) == 0);
  70       free (result);
  71     }
  72     { /* Width.  */
  73       uint8_t *result =
  74         my_xasprintf ("%10U %d", unicode_string, 33, 44, 55);
  75       static const uint8_t expected[] = "     Hello 33";
  76       ASSERT (result != NULL);
  77       ASSERT (u8_strcmp (result, expected) == 0);
  78       free (result);
  79     }
  80     { /* FLAG_LEFT.  */
  81       uint8_t *result =
  82         my_xasprintf ("%-10U %d", unicode_string, 33, 44, 55);
  83       static const uint8_t expected[] = "Hello      33";
  84       ASSERT (result != NULL);
  85       ASSERT (u8_strcmp (result, expected) == 0);
  86       free (result);
  87     }
  88     { /* FLAG_ZERO: no effect.  */
  89       uint8_t *result =
  90         my_xasprintf ("%010U %d", unicode_string, 33, 44, 55);
  91       static const uint8_t expected[] = "     Hello 33";
  92       ASSERT (result != NULL);
  93       ASSERT (u8_strcmp (result, expected) == 0);
  94       free (result);
  95     }
  96   }
  97 
  98   {
  99     static const uint16_t unicode_string[] = { 'H', 'e', 'l', 'l', 'o', 0 };
 100     {
 101       uint8_t *result =
 102         my_xasprintf ("%lU %d", unicode_string, 33, 44, 55);
 103       static const uint8_t expected[] = "Hello 33";
 104       ASSERT (result != NULL);
 105       ASSERT (u8_strcmp (result, expected) == 0);
 106       free (result);
 107     }
 108     { /* Width.  */
 109       uint8_t *result =
 110         my_xasprintf ("%10lU %d", unicode_string, 33, 44, 55);
 111       static const uint8_t expected[] = "     Hello 33";
 112       ASSERT (result != NULL);
 113       ASSERT (u8_strcmp (result, expected) == 0);
 114       free (result);
 115     }
 116     { /* FLAG_LEFT.  */
 117       uint8_t *result =
 118         my_xasprintf ("%-10lU %d", unicode_string, 33, 44, 55);
 119       static const uint8_t expected[] = "Hello      33";
 120       ASSERT (result != NULL);
 121       ASSERT (u8_strcmp (result, expected) == 0);
 122       free (result);
 123     }
 124     { /* FLAG_ZERO: no effect.  */
 125       uint8_t *result =
 126         my_xasprintf ("%010lU %d", unicode_string, 33, 44, 55);
 127       static const uint8_t expected[] = "     Hello 33";
 128       ASSERT (result != NULL);
 129       ASSERT (u8_strcmp (result, expected) == 0);
 130       free (result);
 131     }
 132   }
 133 
 134   {
 135     static const uint32_t unicode_string[] = { 'H', 'e', 'l', 'l', 'o', 0 };
 136     {
 137       uint8_t *result =
 138         my_xasprintf ("%llU %d", unicode_string, 33, 44, 55);
 139       static const uint8_t expected[] = "Hello 33";
 140       ASSERT (result != NULL);
 141       ASSERT (u8_strcmp (result, expected) == 0);
 142       free (result);
 143     }
 144     { /* Width.  */
 145       uint8_t *result =
 146         my_xasprintf ("%10llU %d", unicode_string, 33, 44, 55);
 147       static const uint8_t expected[] = "     Hello 33";
 148       ASSERT (result != NULL);
 149       ASSERT (u8_strcmp (result, expected) == 0);
 150       free (result);
 151     }
 152     { /* FLAG_LEFT.  */
 153       uint8_t *result =
 154         my_xasprintf ("%-10llU %d", unicode_string, 33, 44, 55);
 155       static const uint8_t expected[] = "Hello      33";
 156       ASSERT (result != NULL);
 157       ASSERT (u8_strcmp (result, expected) == 0);
 158       free (result);
 159     }
 160     { /* FLAG_ZERO: no effect.  */
 161       uint8_t *result =
 162         my_xasprintf ("%010llU %d", unicode_string, 33, 44, 55);
 163       static const uint8_t expected[] = "     Hello 33";
 164       ASSERT (result != NULL);
 165       ASSERT (u8_strcmp (result, expected) == 0);
 166       free (result);
 167     }
 168   }
 169 
 170   /* Test the support of the 's' conversion specifier for strings.  */
 171 
 172   {
 173     uint8_t *result =
 174       my_xasprintf ("Mr. %s %d", "Ronald Reagan", 33, 44, 55);
 175     static const uint8_t expected[] = "Mr. Ronald Reagan 33";
 176     ASSERT (result != NULL);
 177     ASSERT (u8_strcmp (result, expected) == 0);
 178     free (result);
 179   }
 180 
 181   { /* Width.  */
 182     uint8_t *result =
 183       my_xasprintf ("Mr. %20s %d", "Ronald Reagan", 33, 44, 55);
 184     static const uint8_t expected[] = "Mr.        Ronald Reagan 33";
 185     ASSERT (result != NULL);
 186     ASSERT (u8_strcmp (result, expected) == 0);
 187     free (result);
 188   }
 189 
 190   { /* FLAG_LEFT.  */
 191     uint8_t *result =
 192       my_xasprintf ("Mr. %-20s %d", "Ronald Reagan", 33, 44, 55);
 193     static const uint8_t expected[] = "Mr. Ronald Reagan        33";
 194     ASSERT (result != NULL);
 195     ASSERT (u8_strcmp (result, expected) == 0);
 196     free (result);
 197   }
 198 
 199   { /* FLAG_ZERO: no effect.  */
 200     uint8_t *result =
 201       my_xasprintf ("Mr. %020s %d", "Ronald Reagan", 33, 44, 55);
 202     static const uint8_t expected[] = "Mr.        Ronald Reagan 33";
 203     ASSERT (result != NULL);
 204     ASSERT (u8_strcmp (result, expected) == 0);
 205     free (result);
 206   }
 207 
 208   /* Test the support of the 'a' and 'A' conversion specifier for hexadecimal
 209      output of floating-point numbers.  */
 210 
 211   { /* A positive number.  */
 212     uint8_t *result =
 213       my_xasprintf ("%a %d", 3.1416015625, 33, 44, 55);
 214     static const uint8_t expected1[] = "0x1.922p+1 33";
 215     static const uint8_t expected2[] = "0x3.244p+0 33";
 216     static const uint8_t expected3[] = "0x6.488p-1 33";
 217     static const uint8_t expected4[] = "0xc.91p-2 33";
 218     ASSERT (result != NULL);
 219     ASSERT (u8_strcmp (result, expected1) == 0
 220             || u8_strcmp (result, expected2) == 0
 221             || u8_strcmp (result, expected3) == 0
 222             || u8_strcmp (result, expected4) == 0);
 223     free (result);
 224   }
 225 
 226   { /* Width.  */
 227     uint8_t *result =
 228       my_xasprintf ("%10a %d", 1.75, 33, 44, 55);
 229     static const uint8_t expected1[] = "  0x1.cp+0 33";
 230     static const uint8_t expected2[] = "  0x3.8p-1 33";
 231     static const uint8_t expected3[] = "    0x7p-2 33";
 232     static const uint8_t expected4[] = "    0xep-3 33";
 233     ASSERT (result != NULL);
 234     ASSERT (u8_strcmp (result, expected1) == 0
 235             || u8_strcmp (result, expected2) == 0
 236             || u8_strcmp (result, expected3) == 0
 237             || u8_strcmp (result, expected4) == 0);
 238     free (result);
 239   }
 240 
 241   { /* Small precision.  */
 242     uint8_t *result =
 243       my_xasprintf ("%.10a %d", 1.75, 33, 44, 55);
 244     static const uint8_t expected1[] = "0x1.c000000000p+0 33";
 245     static const uint8_t expected2[] = "0x3.8000000000p-1 33";
 246     static const uint8_t expected3[] = "0x7.0000000000p-2 33";
 247     static const uint8_t expected4[] = "0xe.0000000000p-3 33";
 248     ASSERT (result != NULL);
 249     ASSERT (u8_strcmp (result, expected1) == 0
 250             || u8_strcmp (result, expected2) == 0
 251             || u8_strcmp (result, expected3) == 0
 252             || u8_strcmp (result, expected4) == 0);
 253     free (result);
 254   }
 255 
 256   { /* Large precision.  */
 257     uint8_t *result =
 258       my_xasprintf ("%.50a %d", 1.75, 33, 44, 55);
 259     static const uint8_t expected1[] = "0x1.c0000000000000000000000000000000000000000000000000p+0 33";
 260     static const uint8_t expected2[] = "0x3.80000000000000000000000000000000000000000000000000p-1 33";
 261     static const uint8_t expected3[] = "0x7.00000000000000000000000000000000000000000000000000p-2 33";
 262     static const uint8_t expected4[] = "0xe.00000000000000000000000000000000000000000000000000p-3 33";
 263     ASSERT (result != NULL);
 264     ASSERT (u8_strcmp (result, expected1) == 0
 265             || u8_strcmp (result, expected2) == 0
 266             || u8_strcmp (result, expected3) == 0
 267             || u8_strcmp (result, expected4) == 0);
 268     free (result);
 269   }
 270 
 271   { /* A positive number.  */
 272     uint8_t *result =
 273       my_xasprintf ("%La %d", 3.1416015625L, 33, 44, 55);
 274     static const uint8_t expected1[] = "0x1.922p+1 33";
 275     static const uint8_t expected2[] = "0x3.244p+0 33";
 276     static const uint8_t expected3[] = "0x6.488p-1 33";
 277     static const uint8_t expected4[] = "0xc.91p-2 33";
 278     ASSERT (result != NULL);
 279     ASSERT (u8_strcmp (result, expected1) == 0
 280             || u8_strcmp (result, expected2) == 0
 281             || u8_strcmp (result, expected3) == 0
 282             || u8_strcmp (result, expected4) == 0);
 283     free (result);
 284   }
 285 
 286   { /* Width.  */
 287     uint8_t *result =
 288       my_xasprintf ("%10La %d", 1.75L, 33, 44, 55);
 289     static const uint8_t expected1[] = "  0x1.cp+0 33";
 290     static const uint8_t expected2[] = "  0x3.8p-1 33";
 291     static const uint8_t expected3[] = "    0x7p-2 33";
 292     static const uint8_t expected4[] = "    0xep-3 33";
 293     ASSERT (result != NULL);
 294     ASSERT (u8_strcmp (result, expected1) == 0
 295             || u8_strcmp (result, expected2) == 0
 296             || u8_strcmp (result, expected3) == 0
 297             || u8_strcmp (result, expected4) == 0);
 298     free (result);
 299   }
 300 
 301   { /* Small precision.  */
 302     uint8_t *result =
 303       my_xasprintf ("%.10La %d", 1.75L, 33, 44, 55);
 304     static const uint8_t expected1[] = "0x1.c000000000p+0 33";
 305     static const uint8_t expected2[] = "0x3.8000000000p-1 33";
 306     static const uint8_t expected3[] = "0x7.0000000000p-2 33";
 307     static const uint8_t expected4[] = "0xe.0000000000p-3 33";
 308     ASSERT (result != NULL);
 309     ASSERT (u8_strcmp (result, expected1) == 0
 310             || u8_strcmp (result, expected2) == 0
 311             || u8_strcmp (result, expected3) == 0
 312             || u8_strcmp (result, expected4) == 0);
 313     free (result);
 314   }
 315 
 316   { /* Large precision.  */
 317     uint8_t *result =
 318       my_xasprintf ("%.50La %d", 1.75L, 33, 44, 55);
 319     static const uint8_t expected1[] = "0x1.c0000000000000000000000000000000000000000000000000p+0 33";
 320     static const uint8_t expected2[] = "0x3.80000000000000000000000000000000000000000000000000p-1 33";
 321     static const uint8_t expected3[] = "0x7.00000000000000000000000000000000000000000000000000p-2 33";
 322     static const uint8_t expected4[] = "0xe.00000000000000000000000000000000000000000000000000p-3 33";
 323     ASSERT (result != NULL);
 324     ASSERT (u8_strcmp (result, expected1) == 0
 325             || u8_strcmp (result, expected2) == 0
 326             || u8_strcmp (result, expected3) == 0
 327             || u8_strcmp (result, expected4) == 0);
 328     free (result);
 329   }
 330 
 331   /* Test the support of the %f format directive.  */
 332 
 333   { /* A positive number.  */
 334     uint8_t *result =
 335       my_xasprintf ("%f %d", 12.75, 33, 44, 55);
 336     static const uint8_t expected[] = "12.750000 33";
 337     ASSERT (result != NULL);
 338     ASSERT (u8_strcmp (result, expected) == 0);
 339     free (result);
 340   }
 341 
 342   { /* Width.  */
 343     uint8_t *result =
 344       my_xasprintf ("%10f %d", 1.75, 33, 44, 55);
 345     static const uint8_t expected[] = "  1.750000 33";
 346     ASSERT (result != NULL);
 347     ASSERT (u8_strcmp (result, expected) == 0);
 348     free (result);
 349   }
 350 
 351   { /* Precision.  */
 352     uint8_t *result =
 353       my_xasprintf ("%.f %d", 1234.0, 33, 44, 55);
 354     static const uint8_t expected[] = "1234 33";
 355     ASSERT (result != NULL);
 356     ASSERT (u8_strcmp (result, expected) == 0);
 357     free (result);
 358   }
 359 
 360   { /* A positive number.  */
 361     uint8_t *result =
 362       my_xasprintf ("%Lf %d", 12.75L, 33, 44, 55);
 363     static const uint8_t expected[] = "12.750000 33";
 364     ASSERT (result != NULL);
 365     ASSERT (u8_strcmp (result, expected) == 0);
 366     free (result);
 367   }
 368 
 369   { /* Width.  */
 370     uint8_t *result =
 371       my_xasprintf ("%10Lf %d", 1.75L, 33, 44, 55);
 372     static const uint8_t expected[] = "  1.750000 33";
 373     ASSERT (result != NULL);
 374     ASSERT (u8_strcmp (result, expected) == 0);
 375     free (result);
 376   }
 377 
 378   { /* Precision.  */
 379     uint8_t *result =
 380       my_xasprintf ("%.Lf %d", 1234.0L, 33, 44, 55);
 381     static const uint8_t expected[] = "1234 33";
 382     ASSERT (result != NULL);
 383     ASSERT (u8_strcmp (result, expected) == 0);
 384     free (result);
 385   }
 386 
 387   /* Test the support of the %F format directive.  */
 388 
 389   { /* A positive number.  */
 390     uint8_t *result =
 391       my_xasprintf ("%F %d", 12.75, 33, 44, 55);
 392     static const uint8_t expected[] = "12.750000 33";
 393     ASSERT (result != NULL);
 394     ASSERT (u8_strcmp (result, expected) == 0);
 395     free (result);
 396   }
 397 
 398   { /* Precision.  */
 399     uint8_t *result =
 400       my_xasprintf ("%.F %d", 1234.0, 33, 44, 55);
 401     static const uint8_t expected[] = "1234 33";
 402     ASSERT (result != NULL);
 403     ASSERT (u8_strcmp (result, expected) == 0);
 404     free (result);
 405   }
 406 
 407   { /* A positive number.  */
 408     uint8_t *result =
 409       my_xasprintf ("%LF %d", 12.75L, 33, 44, 55);
 410     static const uint8_t expected[] = "12.750000 33";
 411     ASSERT (result != NULL);
 412     ASSERT (u8_strcmp (result, expected) == 0);
 413     free (result);
 414   }
 415 
 416   { /* Precision.  */
 417     uint8_t *result =
 418       my_xasprintf ("%.LF %d", 1234.0L, 33, 44, 55);
 419     static const uint8_t expected[] = "1234 33";
 420     ASSERT (result != NULL);
 421     ASSERT (u8_strcmp (result, expected) == 0);
 422     free (result);
 423   }
 424 
 425   /* Test the support of the %e format directive.  */
 426 
 427   { /* A positive number.  */
 428     uint8_t *result =
 429       my_xasprintf ("%e %d", 12.75, 33, 44, 55);
 430     static const uint8_t expected1[] = "1.275000e+01 33";
 431     static const uint8_t expected2[] = "1.275000e+001 33";
 432     ASSERT (result != NULL);
 433     ASSERT (u8_strcmp (result, expected1) == 0
 434             || u8_strcmp (result, expected2) == 0);
 435     free (result);
 436   }
 437 
 438   { /* Width.  */
 439     uint8_t *result =
 440       my_xasprintf ("%15e %d", 1.75, 33, 44, 55);
 441     static const uint8_t expected1[] = "   1.750000e+00 33";
 442     static const uint8_t expected2[] = "  1.750000e+000 33";
 443     ASSERT (result != NULL);
 444     ASSERT (u8_strcmp (result, expected1) == 0
 445             || u8_strcmp (result, expected2) == 0);
 446     free (result);
 447   }
 448 
 449   { /* Precision.  */
 450     uint8_t *result =
 451       my_xasprintf ("%.e %d", 1234.0, 33, 44, 55);
 452     static const uint8_t expected1[] = "1e+03 33";
 453     static const uint8_t expected2[] = "1e+003 33";
 454     ASSERT (result != NULL);
 455     ASSERT (u8_strcmp (result, expected1) == 0
 456             || u8_strcmp (result, expected2) == 0);
 457     free (result);
 458   }
 459 
 460   { /* A positive number.  */
 461     uint8_t *result =
 462       my_xasprintf ("%Le %d", 12.75L, 33, 44, 55);
 463     static const uint8_t expected[] = "1.275000e+01 33";
 464     ASSERT (result != NULL);
 465     ASSERT (u8_strcmp (result, expected) == 0);
 466     free (result);
 467   }
 468 
 469   { /* Width.  */
 470     uint8_t *result =
 471       my_xasprintf ("%15Le %d", 1.75L, 33, 44, 55);
 472     static const uint8_t expected[] = "   1.750000e+00 33";
 473     ASSERT (result != NULL);
 474     ASSERT (u8_strcmp (result, expected) == 0);
 475     free (result);
 476   }
 477 
 478   { /* Precision.  */
 479     uint8_t *result =
 480       my_xasprintf ("%.Le %d", 1234.0L, 33, 44, 55);
 481     static const uint8_t expected[] = "1e+03 33";
 482     ASSERT (result != NULL);
 483     ASSERT (u8_strcmp (result, expected) == 0);
 484     free (result);
 485   }
 486 
 487   /* Test the support of the %g format directive.  */
 488 
 489   { /* A positive number.  */
 490     uint8_t *result =
 491       my_xasprintf ("%g %d", 12.75, 33, 44, 55);
 492     static const uint8_t expected[] = "12.75 33";
 493     ASSERT (result != NULL);
 494     ASSERT (u8_strcmp (result, expected) == 0);
 495     free (result);
 496   }
 497 
 498   { /* Width.  */
 499     uint8_t *result =
 500       my_xasprintf ("%10g %d", 1.75, 33, 44, 55);
 501     static const uint8_t expected[] = "      1.75 33";
 502     ASSERT (result != NULL);
 503     ASSERT (u8_strcmp (result, expected) == 0);
 504     free (result);
 505   }
 506 
 507   { /* Precision.  */
 508     uint8_t *result =
 509       my_xasprintf ("%.g %d", 1234.0, 33, 44, 55);
 510     static const uint8_t expected1[] = "1e+03 33";
 511     static const uint8_t expected2[] = "1e+003 33";
 512     ASSERT (result != NULL);
 513     ASSERT (u8_strcmp (result, expected1) == 0
 514             || u8_strcmp (result, expected2) == 0);
 515     free (result);
 516   }
 517 
 518   { /* A positive number.  */
 519     uint8_t *result =
 520       my_xasprintf ("%Lg %d", 12.75L, 33, 44, 55);
 521     static const uint8_t expected[] = "12.75 33";
 522     ASSERT (result != NULL);
 523     ASSERT (u8_strcmp (result, expected) == 0);
 524     free (result);
 525   }
 526 
 527   { /* Width.  */
 528     uint8_t *result =
 529       my_xasprintf ("%10Lg %d", 1.75L, 33, 44, 55);
 530     static const uint8_t expected[] = "      1.75 33";
 531     ASSERT (result != NULL);
 532     ASSERT (u8_strcmp (result, expected) == 0);
 533     free (result);
 534   }
 535 
 536   { /* Precision.  */
 537     uint8_t *result =
 538       my_xasprintf ("%.Lg %d", 1234.0L, 33, 44, 55);
 539     static const uint8_t expected[] = "1e+03 33";
 540     ASSERT (result != NULL);
 541     ASSERT (u8_strcmp (result, expected) == 0);
 542     free (result);
 543   }
 544 
 545   /* Test the support of the %n format directive.  */
 546 
 547   {
 548     int count = -1;
 549     uint8_t *result =
 550       my_xasprintf ("%d %n", 123, &count, 33, 44, 55);
 551     static const uint8_t expected[] = "123 ";
 552     ASSERT (result != NULL);
 553     ASSERT (u8_strcmp (result, expected) == 0);
 554     ASSERT (count == 4);
 555     free (result);
 556   }
 557 
 558   /* Test the support of the POSIX/XSI format strings with positions.  */
 559 
 560   {
 561     uint8_t *result =
 562       my_xasprintf ("%2$d %1$d", 33, 55);
 563     static const uint8_t expected[] = "55 33";
 564     ASSERT (result != NULL);
 565     ASSERT (u8_strcmp (result, expected) == 0);
 566     free (result);
 567   }
 568 
 569   /* Test the support of the grouping flag.  */
 570 
 571   {
 572     uint8_t *result =
 573       my_xasprintf ("%'d %d", 1234567, 99);
 574     ASSERT (result != NULL);
 575     ASSERT (result[u8_strlen (result) - 1] == '9');
 576     free (result);
 577   }
 578 
 579   /* Test the support of the 'U' conversion specifier for Unicode strings.  */
 580 
 581   {
 582     static const uint8_t unicode_string[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */
 583     {
 584       uint8_t *result =
 585         my_xasprintf ("%U %d", unicode_string, 33, 44, 55);
 586       static const uint8_t expected[] = "Rafa\305\202 Maszkowski 33";
 587       ASSERT (result != NULL);
 588       ASSERT (u8_strcmp (result, expected) == 0);
 589       free (result);
 590     }
 591     { /* Width.  */
 592       uint8_t *result =
 593         my_xasprintf ("%20U %d", unicode_string, 33, 44, 55);
 594       static const uint8_t expected[] = "    Rafa\305\202 Maszkowski 33";
 595       ASSERT (result != NULL);
 596       ASSERT (u8_strcmp (result, expected) == 0);
 597       free (result);
 598     }
 599     { /* FLAG_LEFT.  */
 600       uint8_t *result =
 601         my_xasprintf ("%-20U %d", unicode_string, 33, 44, 55);
 602       static const uint8_t expected[] = "Rafa\305\202 Maszkowski     33";
 603       ASSERT (result != NULL);
 604       ASSERT (u8_strcmp (result, expected) == 0);
 605       free (result);
 606     }
 607     { /* FLAG_ZERO: no effect.  */
 608       uint8_t *result =
 609         my_xasprintf ("%020U %d", unicode_string, 33, 44, 55);
 610       static const uint8_t expected[] = "    Rafa\305\202 Maszkowski 33";
 611       ASSERT (result != NULL);
 612       ASSERT (u8_strcmp (result, expected) == 0);
 613       free (result);
 614     }
 615   }
 616 
 617   {
 618     static const uint16_t unicode_string[] = /* Rafał Maszkowski */
 619       {
 620         'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
 621         's', 'k', 'i', 0
 622       };
 623     {
 624       uint8_t *result =
 625         my_xasprintf ("%lU %d", unicode_string, 33, 44, 55);
 626       static const uint8_t expected[] = "Rafa\305\202 Maszkowski 33";
 627       ASSERT (result != NULL);
 628       ASSERT (u8_strcmp (result, expected) == 0);
 629       free (result);
 630     }
 631     { /* Width.  */
 632       uint8_t *result =
 633         my_xasprintf ("%20lU %d", unicode_string, 33, 44, 55);
 634       static const uint8_t expected[] = "    Rafa\305\202 Maszkowski 33";
 635       ASSERT (result != NULL);
 636       ASSERT (u8_strcmp (result, expected) == 0);
 637       free (result);
 638     }
 639     { /* FLAG_LEFT.  */
 640       uint8_t *result =
 641         my_xasprintf ("%-20lU %d", unicode_string, 33, 44, 55);
 642       static const uint8_t expected[] = "Rafa\305\202 Maszkowski     33";
 643       ASSERT (result != NULL);
 644       ASSERT (u8_strcmp (result, expected) == 0);
 645       free (result);
 646     }
 647     { /* FLAG_ZERO: no effect.  */
 648       uint8_t *result =
 649         my_xasprintf ("%020lU %d", unicode_string, 33, 44, 55);
 650       static const uint8_t expected[] = "    Rafa\305\202 Maszkowski 33";
 651       ASSERT (result != NULL);
 652       ASSERT (u8_strcmp (result, expected) == 0);
 653       free (result);
 654     }
 655   }
 656 
 657   {
 658     static const uint32_t unicode_string[] = /* Rafał Maszkowski */
 659       {
 660         'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
 661         's', 'k', 'i', 0
 662       };
 663     {
 664       uint8_t *result =
 665         my_xasprintf ("%llU %d", unicode_string, 33, 44, 55);
 666       static const uint8_t expected[] = "Rafa\305\202 Maszkowski 33";
 667       ASSERT (result != NULL);
 668       ASSERT (u8_strcmp (result, expected) == 0);
 669       free (result);
 670     }
 671     { /* Width.  */
 672       uint8_t *result =
 673         my_xasprintf ("%20llU %d", unicode_string, 33, 44, 55);
 674       static const uint8_t expected[] = "    Rafa\305\202 Maszkowski 33";
 675       ASSERT (result != NULL);
 676       ASSERT (u8_strcmp (result, expected) == 0);
 677       free (result);
 678     }
 679     { /* FLAG_LEFT.  */
 680       uint8_t *result =
 681         my_xasprintf ("%-20llU %d", unicode_string, 33, 44, 55);
 682       static const uint8_t expected[] = "Rafa\305\202 Maszkowski     33";
 683       ASSERT (result != NULL);
 684       ASSERT (u8_strcmp (result, expected) == 0);
 685       free (result);
 686     }
 687     { /* FLAG_ZERO: no effect.  */
 688       uint8_t *result =
 689         my_xasprintf ("%020llU %d", unicode_string, 33, 44, 55);
 690       static const uint8_t expected[] = "    Rafa\305\202 Maszkowski 33";
 691       ASSERT (result != NULL);
 692       ASSERT (u8_strcmp (result, expected) == 0);
 693       free (result);
 694     }
 695   }
 696 
 697   /* Test non-ASCII characters in the format string.  */
 698 
 699   {
 700     uint8_t *result =
 701       my_xasprintf ("\304rger", 33, 44, 55);
 702     ASSERT (result == NULL && errno == EINVAL);
 703   }
 704 }

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