cdefs.h: fix "__clang_major" typo
[glibc.git] / localedata / collate-test.c
blob5d1a1d2f020acf0fd9d126507f942f5d80d72bc2
1 /* Test collation function using real data.
2 Copyright (C) 1997-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <ctype.h>
20 #include <error.h>
21 #include <locale.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
27 struct lines
29 char *key;
30 char *line;
33 static int xstrcoll (const void *, const void *);
35 static int
36 signum (int n)
38 return (0 < n) - (n < 0);
41 int
42 main (int argc, char *argv[])
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;
51 if (argc < 2)
52 error (1, 0, "usage: %s <random seed>", argv[0]);
54 setlocale (LC_ALL, "");
56 nstrings_max = 100;
57 nstrings = 0;
58 strings = (struct lines *) malloc (nstrings_max * sizeof (struct lines));
59 if (strings == NULL)
61 perror (argv[0]);
62 exit (1);
65 while (1)
67 int l;
68 if (getline (&line, &len, stdin) < 0)
69 break;
71 if (nstrings == nstrings_max)
73 strings = (struct lines *) realloc (strings,
74 (nstrings_max *= 2)
75 * sizeof (*strings));
76 if (strings == NULL)
78 perror (argv[0]);
79 exit (1);
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;
89 free (line);
91 /* First shuffle. */
92 srandom (atoi (argv[1]));
93 for (n = 0; n < 10 * nstrings; ++n)
95 int r1, r2;
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;
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);
106 if (signum (r1) != - signum (r2))
107 printf ("`%s' and `%s' collate wrong: %d vs. %d\n",
108 strings[idx1].key, strings[idx2].key, r1, r2);
111 /* Now sort. */
112 qsort (strings, nstrings, sizeof (struct lines), xstrcoll);
114 /* Print the result. */
115 for (n = 0; n < nstrings; ++n)
117 fputs (strings[n].line, stdout);
118 free (strings[n].line);
119 free (strings[n].key);
121 free (strings);
123 return result;
127 static int
128 xstrcoll (const void *ptr1, const void *ptr2)
130 const struct lines *l1 = (const struct lines *) ptr1;
131 const struct lines *l2 = (const struct lines *) ptr2;
133 return strcoll (l1->key, l2->key);