collate-test.c 3.3 KB

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