Regenerate configure.
[platform/upstream/glibc.git] / localedata / collate-test.c
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.
4
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.
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
13    Lesser General Public License for more details.
14
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/>.  */
18
19 #include <ctype.h>
20 #include <error.h>
21 #include <locale.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26
27 struct lines
28 {
29   char *key;
30   char *line;
31 };
32
33 static int xstrcoll (const void *, const void *);
34
35 static int
36 signum (int n)
37 {
38   return (0 < n) - (n < 0);
39 }
40
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
51   if (argc < 2)
52     error (1, 0, "usage: %s <random seed>", argv[0]);
53
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,
74                                               (nstrings_max *= 2)
75                                                * sizeof (*strings));
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     }
89   free (line);
90
91   /* First shuffle.  */
92   srandom (atoi (argv[1]));
93   for (n = 0; n < 10 * nstrings; ++n)
94     {
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;
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);
105
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);
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)
116     {
117       fputs (strings[n].line, stdout);
118       free (strings[n].line);
119       free (strings[n].key);
120     }
121   free (strings);
122
123   return result;
124 }
125
126
127 static int
128 xstrcoll (const void *ptr1, const void *ptr2)
129 {
130   const struct lines *l1 = (const struct lines *) ptr1;
131   const struct lines *l2 = (const struct lines *) ptr2;
132
133   return strcoll (l1->key, l2->key);
134 }