]>
Commit | Line | Data |
---|---|---|
f5f52655 | 1 | /* Test collation function using real data. |
6d7e8eda | 2 | Copyright (C) 1997-2023 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> | |
25 | ||
26 | ||
27 | struct lines | |
28 | { | |
dade1ade UD |
29 | char *key; |
30 | char *line; | |
f5f52655 UD |
31 | }; |
32 | ||
bcaad6ee | 33 | static int xstrcoll (const void *, const void *); |
f5f52655 | 34 | |
3a523ccd AZ |
35 | static int |
36 | signum (int n) | |
37 | { | |
38 | return (0 < n) - (n < 0); | |
39 | } | |
40 | ||
f5f52655 UD |
41 | int |
42 | main (int argc, char *argv[]) | |
43 | { | |
44 | int result = 0; | |
45 | size_t nstrings, nstrings_max; | |
46 | struct lines *strings; | |
47 | char *line = NULL; | |
48 | size_t len = 0; | |
49 | size_t n; | |
50 | ||
dade1ade UD |
51 | if (argc < 2) |
52 | error (1, 0, "usage: %s <random seed>", argv[0]); | |
53 | ||
f5f52655 UD |
54 | setlocale (LC_ALL, ""); |
55 | ||
56 | nstrings_max = 100; | |
57 | nstrings = 0; | |
58 | strings = (struct lines *) malloc (nstrings_max * sizeof (struct lines)); | |
59 | if (strings == NULL) | |
60 | { | |
61 | perror (argv[0]); | |
62 | exit (1); | |
63 | } | |
64 | ||
65 | while (1) | |
66 | { | |
67 | int l; | |
68 | if (getline (&line, &len, stdin) < 0) | |
69 | break; | |
70 | ||
71 | if (nstrings == nstrings_max) | |
72 | { | |
73 | strings = (struct lines *) realloc (strings, | |
dade1ade UD |
74 | (nstrings_max *= 2) |
75 | * sizeof (*strings)); | |
f5f52655 UD |
76 | if (strings == NULL) |
77 | { | |
78 | perror (argv[0]); | |
79 | exit (1); | |
80 | } | |
81 | } | |
82 | strings[nstrings].line = strdup (line); | |
83 | l = strcspn (line, ":(;"); | |
84 | while (l > 0 && isspace (line[l - 1])) | |
85 | --l; | |
86 | strings[nstrings].key = strndup (line, l); | |
87 | ++nstrings; | |
88 | } | |
dade1ade | 89 | free (line); |
f5f52655 UD |
90 | |
91 | /* First shuffle. */ | |
92 | srandom (atoi (argv[1])); | |
93 | for (n = 0; n < 10 * nstrings; ++n) | |
94 | { | |
3a523ccd | 95 | int r1, r2; |
f5f52655 UD |
96 | size_t idx1 = random () % nstrings; |
97 | size_t idx2 = random () % nstrings; | |
98 | struct lines tmp = strings[idx1]; | |
99 | strings[idx1] = strings[idx2]; | |
100 | strings[idx2] = tmp; | |
101 | ||
102 | /* While we are at it a first little test. */ | |
103 | r1 = strcoll (strings[idx1].key, strings[idx2].key); | |
104 | r2 = strcoll (strings[idx2].key, strings[idx1].key); | |
f5f52655 | 105 | |
3a523ccd | 106 | if (signum (r1) != - signum (r2)) |
f5f52655 UD |
107 | printf ("`%s' and `%s' collate wrong: %d vs. %d\n", |
108 | strings[idx1].key, strings[idx2].key, r1, r2); | |
109 | } | |
110 | ||
111 | /* Now sort. */ | |
112 | qsort (strings, nstrings, sizeof (struct lines), xstrcoll); | |
113 | ||
114 | /* Print the result. */ | |
115 | for (n = 0; n < nstrings; ++n) | |
dade1ade UD |
116 | { |
117 | fputs (strings[n].line, stdout); | |
118 | free (strings[n].line); | |
119 | free (strings[n].key); | |
120 | } | |
121 | free (strings); | |
f5f52655 UD |
122 | |
123 | return result; | |
124 | } | |
125 | ||
126 | ||
127 | static int | |
9d46370c | 128 | xstrcoll (const void *ptr1, const void *ptr2) |
f5f52655 | 129 | { |
d64a397a UD |
130 | const struct lines *l1 = (const struct lines *) ptr1; |
131 | const struct lines *l2 = (const struct lines *) ptr2; | |
f5f52655 UD |
132 | |
133 | return strcoll (l1->key, l2->key); | |
134 | } |