Back to index

glibc  2.9
xfrm-test.c
Go to the documentation of this file.
00001 /* Test collation function via transformation using real data.
00002    Copyright (C) 1997, 1998, 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 *xfrm;
00032   char *line;
00033 };
00034 
00035 static int xstrcmp (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       char saved, *newp;
00064       int needed;
00065       int l;
00066       if (getline (&line, &len, stdin) < 0)
00067        break;
00068 
00069       if (nstrings == nstrings_max)
00070        {
00071          strings = (struct lines *) realloc (strings,
00072                                          (nstrings_max *= 2)
00073                                           * sizeof (*strings));
00074          if (strings == NULL)
00075            {
00076              perror (argv[0]);
00077              exit (1);
00078            }
00079        }
00080       strings[nstrings].line = strdup (line);
00081       l = strcspn (line, ":(;");
00082       while (l > 0 && isspace (line[l - 1]))
00083        --l;
00084 
00085       saved = line[l];
00086       line[l] = '\0';
00087       needed = strxfrm (NULL, line, 0);
00088       newp = malloc (needed + 1);
00089       strxfrm (newp, line, needed + 1);
00090       strings[nstrings].xfrm = newp;
00091       line[l] = saved;
00092       ++nstrings;
00093     }
00094   free (line);
00095 
00096   /* First shuffle.  */
00097   srandom (atoi (argv[1]));
00098   for (n = 0; n < 10 * nstrings; ++n)
00099     {
00100       int r1, r2, r;
00101       size_t idx1 = random () % nstrings;
00102       size_t idx2 = random () % nstrings;
00103       struct lines tmp = strings[idx1];
00104       strings[idx1] = strings[idx2];
00105       strings[idx2] = tmp;
00106 
00107       /* While we are at it a first little test.  */
00108       r1 = strcmp (strings[idx1].xfrm, strings[idx2].xfrm);
00109       r2 = strcmp (strings[idx2].xfrm, strings[idx1].xfrm);
00110       r = -(r1 ^ r2);
00111       if (r)
00112        r /= abs (r1 ^ r2);
00113 
00114       if (r < 0 || (r == 0 && (r1 != 0 || r2 != 0))
00115          || (r > 0 && (r1 ^ r2) >= 0))
00116        printf ("collate wrong: %d vs. %d\n", r1, r2);
00117     }
00118 
00119   /* Now sort.  */
00120   qsort (strings, nstrings, sizeof (struct lines), xstrcmp);
00121 
00122   /* Print the result.  */
00123   for (n = 0; n < nstrings; ++n)
00124     {
00125       fputs (strings[n].line, stdout);
00126       free (strings[n].line);
00127       free (strings[n].xfrm);
00128     }
00129   free (strings);
00130 
00131   return result;
00132 }
00133 
00134 
00135 static int
00136 xstrcmp (ptr1, ptr2)
00137      const void *ptr1;
00138      const void *ptr2;
00139 {
00140   const struct lines *l1 = (const struct lines *) ptr1;
00141   const struct lines *l2 = (const struct lines *) ptr2;
00142 
00143   return strcmp (l1->xfrm, l2->xfrm);
00144 }