]>
Commit | Line | Data |
---|---|---|
f5f52655 | 1 | /* Test collation function using real data. |
688903eb | 2 | Copyright (C) 1997-2018 Free Software Foundation, Inc. |
f5f52655 UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
f5f52655 UD |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
f5f52655 | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
f5f52655 UD |
19 | |
20 | #include <ctype.h> | |
3f04b0c1 | 21 | #include <error.h> |
f5f52655 UD |
22 | #include <locale.h> |
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | ||
27 | ||
28 | struct lines | |
29 | { | |
dade1ade UD |
30 | char *key; |
31 | char *line; | |
f5f52655 UD |
32 | }; |
33 | ||
bcaad6ee | 34 | static int xstrcoll (const void *, const void *); |
f5f52655 UD |
35 | |
36 | int | |
37 | main (int argc, char *argv[]) | |
38 | { | |
39 | int result = 0; | |
40 | size_t nstrings, nstrings_max; | |
41 | struct lines *strings; | |
42 | char *line = NULL; | |
43 | size_t len = 0; | |
44 | size_t n; | |
45 | ||
dade1ade UD |
46 | if (argc < 2) |
47 | error (1, 0, "usage: %s <random seed>", argv[0]); | |
48 | ||
f5f52655 UD |
49 | setlocale (LC_ALL, ""); |
50 | ||
51 | nstrings_max = 100; | |
52 | nstrings = 0; | |
53 | strings = (struct lines *) malloc (nstrings_max * sizeof (struct lines)); | |
54 | if (strings == NULL) | |
55 | { | |
56 | perror (argv[0]); | |
57 | exit (1); | |
58 | } | |
59 | ||
60 | while (1) | |
61 | { | |
62 | int l; | |
63 | if (getline (&line, &len, stdin) < 0) | |
64 | break; | |
65 | ||
66 | if (nstrings == nstrings_max) | |
67 | { | |
68 | strings = (struct lines *) realloc (strings, | |
dade1ade UD |
69 | (nstrings_max *= 2) |
70 | * sizeof (*strings)); | |
f5f52655 UD |
71 | if (strings == NULL) |
72 | { | |
73 | perror (argv[0]); | |
74 | exit (1); | |
75 | } | |
76 | } | |
77 | strings[nstrings].line = strdup (line); | |
78 | l = strcspn (line, ":(;"); | |
79 | while (l > 0 && isspace (line[l - 1])) | |
80 | --l; | |
81 | strings[nstrings].key = strndup (line, l); | |
82 | ++nstrings; | |
83 | } | |
dade1ade | 84 | free (line); |
f5f52655 UD |
85 | |
86 | /* First shuffle. */ | |
87 | srandom (atoi (argv[1])); | |
88 | for (n = 0; n < 10 * nstrings; ++n) | |
89 | { | |
90 | int r1, r2, r; | |
91 | size_t idx1 = random () % nstrings; | |
92 | size_t idx2 = random () % nstrings; | |
93 | struct lines tmp = strings[idx1]; | |
94 | strings[idx1] = strings[idx2]; | |
95 | strings[idx2] = tmp; | |
96 | ||
97 | /* While we are at it a first little test. */ | |
98 | r1 = strcoll (strings[idx1].key, strings[idx2].key); | |
99 | r2 = strcoll (strings[idx2].key, strings[idx1].key); | |
a35766db | 100 | r = r1 * r2; |
f5f52655 | 101 | |
a35766db | 102 | if (r > 0 || (r == 0 && r1 != 0) || (r == 0 && r2 != 0)) |
f5f52655 UD |
103 | printf ("`%s' and `%s' collate wrong: %d vs. %d\n", |
104 | strings[idx1].key, strings[idx2].key, r1, r2); | |
105 | } | |
106 | ||
107 | /* Now sort. */ | |
108 | qsort (strings, nstrings, sizeof (struct lines), xstrcoll); | |
109 | ||
110 | /* Print the result. */ | |
111 | for (n = 0; n < nstrings; ++n) | |
dade1ade UD |
112 | { |
113 | fputs (strings[n].line, stdout); | |
114 | free (strings[n].line); | |
115 | free (strings[n].key); | |
116 | } | |
117 | free (strings); | |
f5f52655 UD |
118 | |
119 | return result; | |
120 | } | |
121 | ||
122 | ||
123 | static int | |
9d46370c | 124 | xstrcoll (const void *ptr1, const void *ptr2) |
f5f52655 | 125 | { |
d64a397a UD |
126 | const struct lines *l1 = (const struct lines *) ptr1; |
127 | const struct lines *l2 = (const struct lines *) ptr2; | |
f5f52655 UD |
128 | |
129 | return strcoll (l1->key, l2->key); | |
130 | } |