]>
Commit | Line | Data |
---|---|---|
f5f52655 | 1 | /* Test collation function via transformation using real data. |
04277e02 | 2 | Copyright (C) 1997-2019 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 | 17 | License along with the GNU C Library; if not, see |
5a82c748 | 18 | <https://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> | |
0f9e5854 | 26 | #include <stdbool.h> |
f5f52655 | 27 | |
0f9e5854 LH |
28 | /* Keep in sync with string/strxfrm_l.c. */ |
29 | #define SMALL_STR_SIZE 4095 | |
f5f52655 UD |
30 | |
31 | struct lines | |
32 | { | |
dade1ade UD |
33 | char *xfrm; |
34 | char *line; | |
f5f52655 UD |
35 | }; |
36 | ||
bcaad6ee | 37 | static int xstrcmp (const void *, const void *); |
f5f52655 UD |
38 | |
39 | int | |
40 | main (int argc, char *argv[]) | |
41 | { | |
42 | int result = 0; | |
0f9e5854 | 43 | bool nocache = false; |
f5f52655 UD |
44 | size_t nstrings, nstrings_max; |
45 | struct lines *strings; | |
46 | char *line = NULL; | |
47 | size_t len = 0; | |
48 | size_t n; | |
49 | ||
dade1ade | 50 | if (argc < 2) |
0f9e5854 LH |
51 | error (1, 0, "usage: %s <random seed> [-nocache]", argv[0]); |
52 | ||
53 | if (argc == 3) | |
54 | { | |
55 | if (strcmp (argv[2], "-nocache") == 0) | |
56 | nocache = true; | |
57 | else | |
58 | { | |
59 | printf ("Unknown option %s!\n", argv[2]); | |
60 | exit (1); | |
61 | } | |
62 | } | |
dade1ade | 63 | |
f5f52655 UD |
64 | setlocale (LC_ALL, ""); |
65 | ||
66 | nstrings_max = 100; | |
67 | nstrings = 0; | |
68 | strings = (struct lines *) malloc (nstrings_max * sizeof (struct lines)); | |
69 | if (strings == NULL) | |
70 | { | |
71 | perror (argv[0]); | |
72 | exit (1); | |
73 | } | |
74 | ||
75 | while (1) | |
76 | { | |
0f9e5854 LH |
77 | char saved, *word, *newp; |
78 | size_t l, line_len, needed; | |
79 | ||
f5f52655 UD |
80 | if (getline (&line, &len, stdin) < 0) |
81 | break; | |
82 | ||
83 | if (nstrings == nstrings_max) | |
84 | { | |
85 | strings = (struct lines *) realloc (strings, | |
dade1ade UD |
86 | (nstrings_max *= 2) |
87 | * sizeof (*strings)); | |
f5f52655 UD |
88 | if (strings == NULL) |
89 | { | |
90 | perror (argv[0]); | |
91 | exit (1); | |
92 | } | |
93 | } | |
94 | strings[nstrings].line = strdup (line); | |
95 | l = strcspn (line, ":(;"); | |
96 | while (l > 0 && isspace (line[l - 1])) | |
97 | --l; | |
98 | ||
99 | saved = line[l]; | |
100 | line[l] = '\0'; | |
0f9e5854 LH |
101 | |
102 | if (nocache) | |
103 | { | |
104 | line_len = strlen (line); | |
105 | word = malloc (line_len + SMALL_STR_SIZE + 1); | |
106 | if (word == NULL) | |
107 | { | |
108 | printf ("malloc failed: %m\n"); | |
109 | exit (1); | |
110 | } | |
111 | memset (word, ' ', SMALL_STR_SIZE); | |
112 | memcpy (word + SMALL_STR_SIZE, line, line_len); | |
113 | word[line_len + SMALL_STR_SIZE] = '\0'; | |
114 | } | |
115 | else | |
116 | word = line; | |
117 | ||
118 | needed = strxfrm (NULL, word, 0); | |
f5f52655 | 119 | newp = malloc (needed + 1); |
0f9e5854 LH |
120 | if (newp == NULL) |
121 | { | |
122 | printf ("malloc failed: %m\n"); | |
123 | exit (1); | |
124 | } | |
125 | strxfrm (newp, word, needed + 1); | |
f5f52655 | 126 | strings[nstrings].xfrm = newp; |
0f9e5854 LH |
127 | |
128 | if (nocache) | |
129 | free (word); | |
f5f52655 UD |
130 | line[l] = saved; |
131 | ++nstrings; | |
132 | } | |
dade1ade | 133 | free (line); |
f5f52655 UD |
134 | |
135 | /* First shuffle. */ | |
136 | srandom (atoi (argv[1])); | |
137 | for (n = 0; n < 10 * nstrings; ++n) | |
138 | { | |
139 | int r1, r2, r; | |
140 | size_t idx1 = random () % nstrings; | |
141 | size_t idx2 = random () % nstrings; | |
142 | struct lines tmp = strings[idx1]; | |
143 | strings[idx1] = strings[idx2]; | |
144 | strings[idx2] = tmp; | |
145 | ||
146 | /* While we are at it a first little test. */ | |
147 | r1 = strcmp (strings[idx1].xfrm, strings[idx2].xfrm); | |
148 | r2 = strcmp (strings[idx2].xfrm, strings[idx1].xfrm); | |
dc30f461 | 149 | r = -(r1 ^ r2); |
f5f52655 | 150 | if (r) |
dc30f461 | 151 | r /= abs (r1 ^ r2); |
f5f52655 UD |
152 | |
153 | if (r < 0 || (r == 0 && (r1 != 0 || r2 != 0)) | |
dc30f461 | 154 | || (r > 0 && (r1 ^ r2) >= 0)) |
f5f52655 UD |
155 | printf ("collate wrong: %d vs. %d\n", r1, r2); |
156 | } | |
157 | ||
158 | /* Now sort. */ | |
159 | qsort (strings, nstrings, sizeof (struct lines), xstrcmp); | |
160 | ||
161 | /* Print the result. */ | |
162 | for (n = 0; n < nstrings; ++n) | |
dade1ade UD |
163 | { |
164 | fputs (strings[n].line, stdout); | |
165 | free (strings[n].line); | |
166 | free (strings[n].xfrm); | |
167 | } | |
168 | free (strings); | |
f5f52655 UD |
169 | |
170 | return result; | |
171 | } | |
172 | ||
173 | ||
174 | static int | |
9d46370c | 175 | xstrcmp (const void *ptr1, const void *ptr2) |
f5f52655 | 176 | { |
d64a397a UD |
177 | const struct lines *l1 = (const struct lines *) ptr1; |
178 | const struct lines *l2 = (const struct lines *) ptr2; | |
f5f52655 UD |
179 | |
180 | return strcmp (l1->xfrm, l2->xfrm); | |
181 | } |