Esempio n. 1
0
static void
test_vasnprintf ()
{
  char buf[8];
  int size;

  for (size = 0; size <= 8; size++)
    {
      size_t length = size;
      char *result = my_asnprintf (NULL, &length, "%d", 12345);
      ASSERT (result != NULL);
      ASSERT (strcmp (result, "12345") == 0);
      ASSERT (length == 5);
      free (result);
    }

  for (size = 0; size <= 8; size++)
    {
      size_t length;
      char *result;

      memcpy (buf, "DEADBEEF", 8);
      length = size;
      result = my_asnprintf (buf, &length, "%d", 12345);
      ASSERT (result != NULL);
      ASSERT (strcmp (result, "12345") == 0);
      ASSERT (length == 5);
      if (size < 6)
	ASSERT (result != buf);
      ASSERT (memcmp (buf + size, "DEADBEEF" + size, 8 - size) == 0);
      if (result != buf)
	free (result);
    }
}
static void
test_function (uint32_t * (*my_asnprintf) (uint32_t *, size_t *, const char *, ...))
{
  /* Test the support of the 's' conversion specifier for strings.  */

  {
    const char *locale_string = "\304rger"; /* Ärger */
    {
      size_t length;
      uint32_t *result =
        my_asnprintf (NULL, &length, "%s %d", locale_string, 33, 44, 55);
      static const uint32_t expected[] =
        { 0x00c4, 'r', 'g', 'e', 'r', ' ', '3', '3', 0 };
      ASSERT (result != NULL);
      ASSERT (u32_strcmp (result, expected) == 0);
      ASSERT (length == u32_strlen (result));
      free (result);
    }
    { /* Width.  */
      size_t length;
      uint32_t *result =
        my_asnprintf (NULL, &length, "%10s %d", locale_string, 33, 44, 55);
      static const uint32_t expected[] =
        { ' ', ' ', ' ', ' ', ' ', 0x00c4, 'r', 'g', 'e', 'r',
          ' ', '3', '3', 0
        };
      ASSERT (result != NULL);
      ASSERT (u32_strcmp (result, expected) == 0);
      ASSERT (length == u32_strlen (result));
      free (result);
    }
    { /* FLAG_LEFT.  */
      size_t length;
      uint32_t *result =
        my_asnprintf (NULL, &length, "%-10s %d", locale_string, 33, 44, 55);
      static const uint32_t expected[] =
        { 0x00c4, 'r', 'g', 'e', 'r', ' ', ' ', ' ', ' ', ' ',
          ' ', '3', '3', 0
        };
      ASSERT (result != NULL);
      ASSERT (u32_strcmp (result, expected) == 0);
      ASSERT (length == u32_strlen (result));
      free (result);
    }
    { /* FLAG_ZERO: no effect.  */
      size_t length;
      uint32_t *result =
        my_asnprintf (NULL, &length, "%010s %d", locale_string, 33, 44, 55);
      static const uint32_t expected[] =
        { ' ', ' ', ' ', ' ', ' ', 0x00c4, 'r', 'g', 'e', 'r',
          ' ', '3', '3', 0
        };
      ASSERT (result != NULL);
      ASSERT (u32_strcmp (result, expected) == 0);
      ASSERT (length == u32_strlen (result));
      free (result);
    }
  }
}
Esempio n. 3
0
static void
test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
{
  /* glibc >= 2.2 supports the 'I' flag, and in glibc >= 2.2.3 the fa_IR
     locale defines the 'outdigits' to be U+06F0..U+06F9.
     So we test for glibc >= 2.3.  */
#if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) && !defined __UCLIBC__
  /* Test that the 'I' flag is supported.  */
  {
    size_t length;
    char *result =
      my_asnprintf (NULL, &length, "%Id %d", 1234567, 99);
    static const char expected[] = /* "۱۲۳۴۵۶۷ 99" */
      "\xDB\xB1\xDB\xB2\xDB\xB3\xDB\xB4\xDB\xB5\xDB\xB6\xDB\xB7 99";
    ASSERT (result != NULL);
    ASSERT (strcmp (result, expected) == 0);
    ASSERT (length == strlen (result));
    free (result);
  }
#endif
}
Esempio n. 4
0
static void
test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
{
  /* Test the support of the 'U' conversion specifier for Unicode strings.  */

  {
    static const uint8_t unicode_string[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */
    {
      size_t length;
      char *result =
        my_asnprintf (NULL, &length, "%U %d", unicode_string, 33, 44, 55);
      ASSERT (result != NULL);
      ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0);
      ASSERT (length == strlen (result));
      free (result);
    }
    { /* Width.  */
      size_t length;
      char *result =
        my_asnprintf (NULL, &length, "%20U %d", unicode_string, 33, 44, 55);
      ASSERT (result != NULL);
      ASSERT (strcmp (result, "    Rafa\305\202 Maszkowski 33") == 0);
      ASSERT (length == strlen (result));
      free (result);
    }
    { /* FLAG_LEFT.  */
      size_t length;
      char *result =
        my_asnprintf (NULL, &length, "%-20U %d", unicode_string, 33, 44, 55);
      ASSERT (result != NULL);
      ASSERT (strcmp (result, "Rafa\305\202 Maszkowski     33") == 0);
      ASSERT (length == strlen (result));
      free (result);
    }
    { /* FLAG_ZERO: no effect.  */
      size_t length;
      char *result =
        my_asnprintf (NULL, &length, "%020U %d", unicode_string, 33, 44, 55);
      ASSERT (result != NULL);
      ASSERT (strcmp (result, "    Rafa\305\202 Maszkowski 33") == 0);
      ASSERT (length == strlen (result));
      free (result);
    }
  }

  {
    static const uint16_t unicode_string[] = /* Rafał Maszkowski */
      {
        'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
        's', 'k', 'i', 0
      };
    {
      size_t length;
      char *result =
        my_asnprintf (NULL, &length, "%lU %d", unicode_string, 33, 44, 55);
      ASSERT (result != NULL);
      ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0);
      ASSERT (length == strlen (result));
      free (result);
    }
    { /* Width.  */
      size_t length;
      char *result =
        my_asnprintf (NULL, &length, "%20lU %d", unicode_string, 33, 44, 55);
      ASSERT (result != NULL);
      ASSERT (strcmp (result, "    Rafa\305\202 Maszkowski 33") == 0);
      ASSERT (length == strlen (result));
      free (result);
    }
    { /* FLAG_LEFT.  */
      size_t length;
      char *result =
        my_asnprintf (NULL, &length, "%-20lU %d", unicode_string, 33, 44, 55);
      ASSERT (result != NULL);
      ASSERT (strcmp (result, "Rafa\305\202 Maszkowski     33") == 0);
      ASSERT (length == strlen (result));
      free (result);
    }
    { /* FLAG_ZERO: no effect.  */
      size_t length;
      char *result =
        my_asnprintf (NULL, &length, "%020lU %d", unicode_string, 33, 44, 55);
      ASSERT (result != NULL);
      ASSERT (strcmp (result, "    Rafa\305\202 Maszkowski 33") == 0);
      ASSERT (length == strlen (result));
      free (result);
    }
  }

  {
    static const uint32_t unicode_string[] = /* Rafał Maszkowski */
      {
        'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
        's', 'k', 'i', 0
      };
    {
      size_t length;
      char *result =
        my_asnprintf (NULL, &length, "%llU %d", unicode_string, 33, 44, 55);
      ASSERT (result != NULL);
      ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0);
      ASSERT (length == strlen (result));
      free (result);
    }
    { /* Width.  */
      size_t length;
      char *result =
        my_asnprintf (NULL, &length, "%20llU %d", unicode_string, 33, 44, 55);
      ASSERT (result != NULL);
      ASSERT (strcmp (result, "    Rafa\305\202 Maszkowski 33") == 0);
      ASSERT (length == strlen (result));
      free (result);
    }
    { /* FLAG_LEFT.  */
      size_t length;
      char *result =
        my_asnprintf (NULL, &length, "%-20llU %d", unicode_string, 33, 44, 55);
      ASSERT (result != NULL);
      ASSERT (strcmp (result, "Rafa\305\202 Maszkowski     33") == 0);
      ASSERT (length == strlen (result));
      free (result);
    }
    { /* FLAG_ZERO: no effect.  */
      size_t length;
      char *result =
        my_asnprintf (NULL, &length, "%020llU %d", unicode_string, 33, 44, 55);
      ASSERT (result != NULL);
      ASSERT (strcmp (result, "    Rafa\305\202 Maszkowski 33") == 0);
      ASSERT (length == strlen (result));
      free (result);
    }
  }

  /* Test the support of the 's' conversion specifier for strings.  */

  {
    const char *locale_string = "\303\204rger"; /* Ärger */
    {
      size_t length;
      char *result =
        my_asnprintf (NULL, &length, "%s %d", locale_string, 33, 44, 55);
      ASSERT (result != NULL);
      ASSERT (strcmp (result, "\303\204rger 33") == 0);
      ASSERT (length == strlen (result));
      free (result);
    }
    { /* Width.  */
      size_t length;
      char *result =
        my_asnprintf (NULL, &length, "%10s %d", locale_string, 33, 44, 55);
      ASSERT (result != NULL);
      ASSERT (strcmp (result, "     \303\204rger 33") == 0);
      ASSERT (length == strlen (result));
      free (result);
    }
    { /* FLAG_LEFT.  */
      size_t length;
      char *result =
        my_asnprintf (NULL, &length, "%-10s %d", locale_string, 33, 44, 55);
      ASSERT (result != NULL);
      ASSERT (strcmp (result, "\303\204rger      33") == 0);
      ASSERT (length == strlen (result));
      free (result);
    }
    { /* FLAG_ZERO: no effect.  */
      size_t length;
      char *result =
        my_asnprintf (NULL, &length, "%010s %d", locale_string, 33, 44, 55);
      ASSERT (result != NULL);
      ASSERT (strcmp (result, "     \303\204rger 33") == 0);
      ASSERT (length == strlen (result));
      free (result);
    }
  }
}