collate-test.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Test collation function using real data.
  2. Copyright (C) 1997, 1999, 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 *key;
  25. char *line;
  26. };
  27. static int xstrcoll (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. int l;
  51. if (getline (&line, &len, stdin) < 0)
  52. break;
  53. if (nstrings == nstrings_max)
  54. {
  55. strings = (struct lines *) realloc (strings,
  56. (nstrings_max *= 2)
  57. * sizeof (*strings));
  58. if (strings == NULL)
  59. {
  60. perror (argv[0]);
  61. exit (1);
  62. }
  63. }
  64. strings[nstrings].line = strdup (line);
  65. l = strcspn (line, ":(;");
  66. while (l > 0 && isspace (line[l - 1]))
  67. --l;
  68. strings[nstrings].key = strndup (line, l);
  69. ++nstrings;
  70. }
  71. free (line);
  72. /* First shuffle. */
  73. srandom (atoi (argv[1]));
  74. for (n = 0; n < 10 * nstrings; ++n)
  75. {
  76. int r1, r2, r;
  77. size_t idx1 = random () % nstrings;
  78. size_t idx2 = random () % nstrings;
  79. struct lines tmp = strings[idx1];
  80. strings[idx1] = strings[idx2];
  81. strings[idx2] = tmp;
  82. /* While we are at it a first little test. */
  83. r1 = strcoll (strings[idx1].key, strings[idx2].key);
  84. r2 = strcoll (strings[idx2].key, strings[idx1].key);
  85. r = r1 * r2;
  86. if (r > 0 || (r == 0 && r1 != 0) || (r == 0 && r2 != 0))
  87. printf ("`%s' and `%s' collate wrong: %d vs. %d\n",
  88. strings[idx1].key, strings[idx2].key, r1, r2);
  89. }
  90. /* Now sort. */
  91. qsort (strings, nstrings, sizeof (struct lines), xstrcoll);
  92. /* Print the result. */
  93. for (n = 0; n < nstrings; ++n)
  94. {
  95. fputs (strings[n].line, stdout);
  96. free (strings[n].line);
  97. free (strings[n].key);
  98. }
  99. free (strings);
  100. return result;
  101. }
  102. static int
  103. xstrcoll (ptr1, ptr2)
  104. const void *ptr1;
  105. const void *ptr2;
  106. {
  107. const struct lines *l1 = (const struct lines *) ptr1;
  108. const struct lines *l2 = (const struct lines *) ptr2;
  109. return strcoll (l1->key, l2->key);
  110. }