Back to index

glibc  2.9
collate-test.c
Go to the documentation of this file.
00001 /* Test collation function using real data.
00002    Copyright (C) 1997, 1999, 2000, 2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <ctype.h>
00022 #include <error.h>
00023 #include <locale.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 
00028 
00029 struct lines
00030 {
00031   char *key;
00032   char *line;
00033 };
00034 
00035 static int xstrcoll (const void *, const void *);
00036 
00037 int
00038 main (int argc, char *argv[])
00039 {
00040   int result = 0;
00041   size_t nstrings, nstrings_max;
00042   struct lines *strings;
00043   char *line = NULL;
00044   size_t len = 0;
00045   size_t n;
00046 
00047   if (argc < 2)
00048     error (1, 0, "usage: %s <random seed>", argv[0]);
00049 
00050   setlocale (LC_ALL, "");
00051 
00052   nstrings_max = 100;
00053   nstrings = 0;
00054   strings = (struct lines *) malloc (nstrings_max * sizeof (struct lines));
00055   if (strings == NULL)
00056     {
00057       perror (argv[0]);
00058       exit (1);
00059     }
00060 
00061   while (1)
00062     {
00063       int l;
00064       if (getline (&line, &len, stdin) < 0)
00065        break;
00066 
00067       if (nstrings == nstrings_max)
00068        {
00069          strings = (struct lines *) realloc (strings,
00070                                          (nstrings_max *= 2)
00071                                           * sizeof (*strings));
00072          if (strings == NULL)
00073            {
00074              perror (argv[0]);
00075              exit (1);
00076            }
00077        }
00078       strings[nstrings].line = strdup (line);
00079       l = strcspn (line, ":(;");
00080       while (l > 0 && isspace (line[l - 1]))
00081        --l;
00082       strings[nstrings].key = strndup (line, l);
00083       ++nstrings;
00084     }
00085   free (line);
00086 
00087   /* First shuffle.  */
00088   srandom (atoi (argv[1]));
00089   for (n = 0; n < 10 * nstrings; ++n)
00090     {
00091       int r1, r2, r;
00092       size_t idx1 = random () % nstrings;
00093       size_t idx2 = random () % nstrings;
00094       struct lines tmp = strings[idx1];
00095       strings[idx1] = strings[idx2];
00096       strings[idx2] = tmp;
00097 
00098       /* While we are at it a first little test.  */
00099       r1 = strcoll (strings[idx1].key, strings[idx2].key);
00100       r2 = strcoll (strings[idx2].key, strings[idx1].key);
00101       r = r1 * r2;
00102 
00103       if (r > 0 || (r == 0 && r1 != 0) || (r == 0 && r2 != 0))
00104        printf ("`%s' and `%s' collate wrong: %d vs. %d\n",
00105               strings[idx1].key, strings[idx2].key, r1, r2);
00106     }
00107 
00108   /* Now sort.  */
00109   qsort (strings, nstrings, sizeof (struct lines), xstrcoll);
00110 
00111   /* Print the result.  */
00112   for (n = 0; n < nstrings; ++n)
00113     {
00114       fputs (strings[n].line, stdout);
00115       free (strings[n].line);
00116       free (strings[n].key);
00117     }
00118   free (strings);
00119 
00120   return result;
00121 }
00122 
00123 
00124 static int
00125 xstrcoll (ptr1, ptr2)
00126      const void *ptr1;
00127      const void *ptr2;
00128 {
00129   const struct lines *l1 = (const struct lines *) ptr1;
00130   const struct lines *l2 = (const struct lines *) ptr2;
00131 
00132   return strcoll (l1->key, l2->key);
00133 }