xfrm-test.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <ctype.h>
  17. #include <error.h>
  18. #include <locale.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. struct lines
  23. {
  24. char *xfrm;
  25. char *line;
  26. };
  27. static int xstrcmp (const void *, const void *);
  28. int
  29. main (int argc, char *argv[])
  30. {
  31. int result = 0;
  32. size_t nstrings, nstrings_max;
  33. struct lines *strings;
  34. char *line = NULL;
  35. size_t len = 0;
  36. size_t n;
  37. if (argc < 2)
  38. error (1, 0, "usage: %s <random seed>", argv[0]);
  39. setlocale (LC_ALL, "");
  40. nstrings_max = 100;
  41. nstrings = 0;
  42. strings = (struct lines *) malloc (nstrings_max * sizeof (struct lines));
  43. if (strings == NULL)
  44. {
  45. perror (argv[0]);
  46. exit (1);
  47. }
  48. while (1)
  49. {
  50. char saved, *newp;
  51. int needed;
  52. int l;
  53. if (getline (&line, &len, stdin) < 0)
  54. break;
  55. if (nstrings == nstrings_max)
  56. {
  57. strings = (struct lines *) realloc (strings,
  58. (nstrings_max *= 2)
  59. * sizeof (*strings));
  60. if (strings == NULL)
  61. {
  62. perror (argv[0]);
  63. exit (1);
  64. }
  65. }
  66. strings[nstrings].line = strdup (line);
  67. l = strcspn (line, ":(;");
  68. while (l > 0 && isspace (line[l - 1]))
  69. --l;
  70. saved = line[l];
  71. line[l] = '\0';
  72. needed = strxfrm (NULL, line, 0);
  73. newp = malloc (needed + 1);
  74. strxfrm (newp, line, needed + 1);
  75. strings[nstrings].xfrm = newp;
  76. line[l] = saved;
  77. ++nstrings;
  78. }
  79. free (line);
  80. /* First shuffle. */
  81. srandom (atoi (argv[1]));
  82. for (n = 0; n < 10 * nstrings; ++n)
  83. {
  84. int r1, r2, r;
  85. size_t idx1 = random () % nstrings;
  86. size_t idx2 = random () % nstrings;
  87. struct lines tmp = strings[idx1];
  88. strings[idx1] = strings[idx2];
  89. strings[idx2] = tmp;
  90. /* While we are at it a first little test. */
  91. r1 = strcmp (strings[idx1].xfrm, strings[idx2].xfrm);
  92. r2 = strcmp (strings[idx2].xfrm, strings[idx1].xfrm);
  93. r = -(r1 ^ r2);
  94. if (r)
  95. r /= abs (r1 ^ r2);
  96. if (r < 0 || (r == 0 && (r1 != 0 || r2 != 0))
  97. || (r > 0 && (r1 ^ r2) >= 0))
  98. printf ("collate wrong: %d vs. %d\n", r1, r2);
  99. }
  100. /* Now sort. */
  101. qsort (strings, nstrings, sizeof (struct lines), xstrcmp);
  102. /* Print the result. */
  103. for (n = 0; n < nstrings; ++n)
  104. {
  105. fputs (strings[n].line, stdout);
  106. free (strings[n].line);
  107. free (strings[n].xfrm);
  108. }
  109. free (strings);
  110. return result;
  111. }
  112. static int
  113. xstrcmp (ptr1, ptr2)
  114. const void *ptr1;
  115. const void *ptr2;
  116. {
  117. const struct lines *l1 = (const struct lines *) ptr1;
  118. const struct lines *l2 = (const struct lines *) ptr2;
  119. return strcmp (l1->xfrm, l2->xfrm);
  120. }