tst-strxfrm.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Based on a test case by Paul Eggert. */
  2. #include <locale.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. char const string[] = "";
  7. static int
  8. test (const char *locale)
  9. {
  10. size_t bufsize;
  11. size_t r;
  12. size_t l;
  13. char *buf;
  14. locale_t loc;
  15. int result = 0;
  16. if (setlocale (LC_COLLATE, locale) == NULL)
  17. {
  18. printf ("cannot set locale \"%s\"\n", locale);
  19. return 1;
  20. }
  21. bufsize = strxfrm (NULL, string, 0) + 1;
  22. buf = malloc (bufsize);
  23. if (buf == NULL)
  24. {
  25. printf ("cannot allocate %zd bytes\n", bufsize);
  26. return 1;
  27. }
  28. r = strxfrm (buf, string, bufsize);
  29. l = strlen (buf);
  30. if (r != l)
  31. {
  32. printf ("locale \"%s\": strxfrm returned %zu, strlen returned %zu\n",
  33. locale, r, l);
  34. result = 1;
  35. }
  36. loc = newlocale (1 << LC_ALL, locale, NULL);
  37. r = strxfrm_l (buf, string, bufsize, loc);
  38. l = strlen (buf);
  39. if (r != l)
  40. {
  41. printf ("locale \"%s\": strxfrm_l returned %zu, strlen returned %zu\n",
  42. locale, r, l);
  43. result = 1;
  44. }
  45. freelocale (loc);
  46. free (buf);
  47. return result;
  48. }
  49. int
  50. do_test (void)
  51. {
  52. int result = 0;
  53. result |= test ("C");
  54. result |= test ("en_US.ISO-8859-1");
  55. result |= test ("de_DE.UTF-8");
  56. return result;
  57. }
  58. #include <support/test-driver.c>