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