xfrm-test.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Test collation function via transformation using real data.
  2. Copyright (C) 1997, 1998, 2000, 2003 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library 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 GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <ctype.h>
  18. #include <error.h>
  19. #include <locale.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. struct lines
  24. {
  25. char *xfrm;
  26. char *line;
  27. };
  28. static int xstrcmp (const void *, const void *);
  29. int
  30. main (int argc, char *argv[])
  31. {
  32. int result = 0;
  33. size_t nstrings, nstrings_max;
  34. struct lines *strings;
  35. char *line = NULL;
  36. size_t len = 0;
  37. size_t n;
  38. if (argc < 2)
  39. error (1, 0, "usage: %s <random seed>", argv[0]);
  40. setlocale (LC_ALL, "");
  41. nstrings_max = 100;
  42. nstrings = 0;
  43. strings = (struct lines *) malloc (nstrings_max * sizeof (struct lines));
  44. if (strings == NULL)
  45. {
  46. perror (argv[0]);
  47. exit (1);
  48. }
  49. while (1)
  50. {
  51. char saved, *newp;
  52. int needed;
  53. int l;
  54. if (getline (&line, &len, stdin) < 0)
  55. break;
  56. if (nstrings == nstrings_max)
  57. {
  58. strings = (struct lines *) realloc (strings,
  59. (nstrings_max *= 2)
  60. * sizeof (*strings));
  61. if (strings == NULL)
  62. {
  63. perror (argv[0]);
  64. exit (1);
  65. }
  66. }
  67. strings[nstrings].line = strdup (line);
  68. l = strcspn (line, ":(;");
  69. while (l > 0 && isspace (line[l - 1]))
  70. --l;
  71. saved = line[l];
  72. line[l] = '\0';
  73. needed = strxfrm (NULL, line, 0);
  74. newp = malloc (needed + 1);
  75. strxfrm (newp, line, needed + 1);
  76. strings[nstrings].xfrm = newp;
  77. line[l] = saved;
  78. ++nstrings;
  79. }
  80. free (line);
  81. /* First shuffle. */
  82. srandom (atoi (argv[1]));
  83. for (n = 0; n < 10 * nstrings; ++n)
  84. {
  85. int r1, r2, r;
  86. size_t idx1 = random () % nstrings;
  87. size_t idx2 = random () % nstrings;
  88. struct lines tmp = strings[idx1];
  89. strings[idx1] = strings[idx2];
  90. strings[idx2] = tmp;
  91. /* While we are at it a first little test. */
  92. r1 = strcmp (strings[idx1].xfrm, strings[idx2].xfrm);
  93. r2 = strcmp (strings[idx2].xfrm, strings[idx1].xfrm);
  94. r = -(r1 ^ r2);
  95. if (r)
  96. r /= abs (r1 ^ r2);
  97. if (r < 0 || (r == 0 && (r1 != 0 || r2 != 0))
  98. || (r > 0 && (r1 ^ r2) >= 0))
  99. printf ("collate wrong: %d vs. %d\n", r1, r2);
  100. }
  101. /* Now sort. */
  102. qsort (strings, nstrings, sizeof (struct lines), xstrcmp);
  103. /* Print the result. */
  104. for (n = 0; n < nstrings; ++n)
  105. {
  106. fputs (strings[n].line, stdout);
  107. free (strings[n].line);
  108. free (strings[n].xfrm);
  109. }
  110. free (strings);
  111. return result;
  112. }
  113. static int
  114. xstrcmp (ptr1, ptr2)
  115. const void *ptr1;
  116. const void *ptr2;
  117. {
  118. const struct lines *l1 = (const struct lines *) ptr1;
  119. const struct lines *l2 = (const struct lines *) ptr2;
  120. return strcmp (l1->xfrm, l2->xfrm);
  121. }