Update.
[platform/upstream/glibc.git] / localedata / collate-test.c
1 /* Test collation function using real data.
2    Copyright (C) 1997, 1999, 2000 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 *key;
31   const char *line;
32 };
33
34 static int xstrcoll (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       int l;
60       if (getline (&line, &len, stdin) < 0)
61         break;
62
63       if (nstrings == nstrings_max)
64         {
65           strings = (struct lines *) realloc (strings,
66                                               (nstrings_max *= 2
67                                                * sizeof (*strings)));
68           if (strings == NULL)
69             {
70               perror (argv[0]);
71               exit (1);
72             }
73         }
74       strings[nstrings].line = strdup (line);
75       l = strcspn (line, ":(;");
76       while (l > 0 && isspace (line[l - 1]))
77         --l;
78       strings[nstrings].key = strndup (line, l);
79       ++nstrings;
80     }
81
82   /* First shuffle.  */
83   srandom (atoi (argv[1]));
84   for (n = 0; n < 10 * nstrings; ++n)
85     {
86       int r1, r2, r;
87       size_t idx1 = random () % nstrings;
88       size_t idx2 = random () % nstrings;
89       struct lines tmp = strings[idx1];
90       strings[idx1] = strings[idx2];
91       strings[idx2] = tmp;
92
93       /* While we are at it a first little test.  */
94       r1 = strcoll (strings[idx1].key, strings[idx2].key);
95       r2 = strcoll (strings[idx2].key, strings[idx1].key);
96       r = r1 * r2;
97
98       if (r > 0 || (r == 0 && r1 != 0) || (r == 0 && r2 != 0))
99         printf ("`%s' and `%s' collate wrong: %d vs. %d\n",
100                 strings[idx1].key, strings[idx2].key, r1, r2);
101     }
102
103   /* Now sort.  */
104   qsort (strings, nstrings, sizeof (struct lines), xstrcoll);
105
106   /* Print the result.  */
107   for (n = 0; n < nstrings; ++n)
108     fputs (strings[n].line, stdout);
109
110   return result;
111 }
112
113
114 static int
115 xstrcoll (ptr1, ptr2)
116      const void *ptr1;
117      const void *ptr2;
118 {
119   const struct lines *l1 = (const struct lines *) ptr1;
120   const struct lines *l2 = (const struct lines *) ptr2;
121
122   return strcoll (l1->key, l2->key);
123 }