Initial revision
[platform/upstream/glibc.git] / localedata / xfrm-test.c
1 /* Test collation function via transformation using real data.
2    Copyright (C) 1997 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 Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    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    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include <ctype.h>
22 #include <locale.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27
28 struct lines
29 {
30   const char *xfrm;
31   const char *line;
32 };
33
34 static int xstrcmp __P ((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   setlocale (LC_ALL, "");
47
48   nstrings_max = 100;
49   nstrings = 0;
50   strings = (struct lines *) malloc (nstrings_max * sizeof (struct lines));
51   if (strings == NULL)
52     {
53       perror (argv[0]);
54       exit (1);
55     }
56
57   while (1)
58     {
59       char saved, *newp;
60       int needed;
61       int l;
62       if (getline (&line, &len, stdin) < 0)
63         break;
64
65       if (nstrings == nstrings_max)
66         {
67           strings = (struct lines *) realloc (strings,
68                                               (nstrings_max *= 2
69                                                * sizeof (*strings)));
70           if (strings == NULL)
71             {
72               perror (argv[0]);
73               exit (1);
74             }
75         }
76       strings[nstrings].line = strdup (line);
77       l = strcspn (line, ":(;");
78       while (l > 0 && isspace (line[l - 1]))
79         --l;
80
81       saved = line[l];
82       line[l] = '\0';
83       needed = strxfrm (NULL, line, 0);
84       newp = malloc (needed + 1);
85       strxfrm (newp, line, needed + 1);
86       strings[nstrings].xfrm = newp;
87       line[l] = saved;
88       ++nstrings;
89     }
90
91   /* First shuffle.  */
92   srandom (atoi (argv[1]));
93   for (n = 0; n < 10 * nstrings; ++n)
94     {
95       int r1, r2, r;
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 = strcmp (strings[idx1].xfrm, strings[idx2].xfrm);
104       r2 = strcmp (strings[idx2].xfrm, strings[idx1].xfrm);
105       r = -(r1 * r2);
106       if (r)
107         r /= abs (r1 * r2);
108
109       if (r < 0 || (r == 0 && (r1 != 0 || r2 != 0))
110           || (r > 0 && (r1 * r2) >= 0))
111         printf ("collate wrong: %d vs. %d\n", r1, r2);
112     }
113
114   /* Now sort.  */
115   qsort (strings, nstrings, sizeof (struct lines), xstrcmp);
116
117   /* Print the result.  */
118   for (n = 0; n < nstrings; ++n)
119     fputs (strings[n].line, stdout);
120
121   return result;
122 }
123
124
125 static int
126 xstrcmp (ptr1, ptr2)
127      const void *ptr1;
128      const void *ptr2;
129 {
130   struct lines *l1 = (struct lines *) ptr1;
131   struct lines *l2 = (struct lines *) ptr2;
132
133   return strcmp (l1->xfrm, l2->xfrm);
134 }