OSDN Git Service

Replace FSF snail mail address with URLs
[uclinux-h8/uClibc.git] / test / locale / xfrm-test.c
1 /* Test collation function via transformation using real data.
2    Copyright (C) 1997, 1998, 2000, 2003 Free Software Foundation, Inc.
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
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.
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
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19
20 #include <ctype.h>
21 #include <error.h>
22 #include <locale.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27
28 struct lines
29 {
30   char *xfrm;
31   char *line;
32 };
33
34 static int xstrcmp (const void *, const void *);
35
36 int
37 main (int argc, char *argv[])
38 {
39   int result = 0;
40   size_t nstrings, nstrings_max;
41   struct lines *strings;
42   char *line = NULL;
43   size_t len = 0;
44   size_t n;
45
46   if (argc < 2)
47     error (1, 0, "usage: %s <random seed>", argv[0]);
48
49   setlocale (LC_ALL, "");
50
51   nstrings_max = 100;
52   nstrings = 0;
53   strings = (struct lines *) malloc (nstrings_max * sizeof (struct lines));
54   if (strings == NULL)
55     {
56       perror (argv[0]);
57       exit (1);
58     }
59
60   while (1)
61     {
62       char saved, *newp;
63       int needed;
64       int l;
65       if (getline (&line, &len, stdin) < 0)
66         break;
67
68       if (nstrings == nstrings_max)
69         {
70           strings = (struct lines *) realloc (strings,
71                                               (nstrings_max *= 2)
72                                                * sizeof (*strings));
73           if (strings == NULL)
74             {
75               perror (argv[0]);
76               exit (1);
77             }
78         }
79       strings[nstrings].line = strdup (line);
80       l = strcspn (line, ":(;");
81       while (l > 0 && isspace (line[l - 1]))
82         --l;
83
84       saved = line[l];
85       line[l] = '\0';
86       needed = strxfrm (NULL, line, 0);
87       newp = malloc (needed + 1);
88       strxfrm (newp, line, needed + 1);
89       strings[nstrings].xfrm = newp;
90       line[l] = saved;
91       ++nstrings;
92     }
93   free (line);
94
95   /* First shuffle.  */
96   srandom (atoi (argv[1]));
97   for (n = 0; n < 10 * nstrings; ++n)
98     {
99       int r1, r2, r;
100       size_t idx1 = random () % nstrings;
101       size_t idx2 = random () % nstrings;
102       struct lines tmp = strings[idx1];
103       strings[idx1] = strings[idx2];
104       strings[idx2] = tmp;
105
106       /* While we are at it a first little test.  */
107       r1 = strcmp (strings[idx1].xfrm, strings[idx2].xfrm);
108       r2 = strcmp (strings[idx2].xfrm, strings[idx1].xfrm);
109       r = -(r1 ^ r2);
110       if (r)
111         r /= abs (r1 ^ r2);
112
113       if (r < 0 || (r == 0 && (r1 != 0 || r2 != 0))
114           || (r > 0 && (r1 ^ r2) >= 0))
115         printf ("collate wrong: %d vs. %d\n", r1, r2);
116     }
117
118   /* Now sort.  */
119   qsort (strings, nstrings, sizeof (struct lines), xstrcmp);
120
121   /* Print the result.  */
122   for (n = 0; n < nstrings; ++n)
123     {
124       fputs (strings[n].line, stdout);
125       free (strings[n].line);
126       free (strings[n].xfrm);
127     }
128   free (strings);
129
130   return result;
131 }
132
133
134 static int
135 xstrcmp (ptr1, ptr2)
136      const void *ptr1;
137      const void *ptr2;
138 {
139   const struct lines *l1 = (const struct lines *) ptr1;
140   const struct lines *l2 = (const struct lines *) ptr2;
141
142   return strcmp (l1->xfrm, l2->xfrm);
143 }