tst-strxfrm.c 1.3 KB

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