Blame localedata/collate-test.c

Packit 6c4009
/* Test collation function using real data.
Packit 6c4009
   Copyright (C) 1997-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <ctype.h>
Packit 6c4009
#include <error.h>
Packit 6c4009
#include <locale.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
struct lines
Packit 6c4009
{
Packit 6c4009
  char *key;
Packit 6c4009
  char *line;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static int xstrcoll (const void *, const void *);
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  int result = 0;
Packit 6c4009
  size_t nstrings, nstrings_max;
Packit 6c4009
  struct lines *strings;
Packit 6c4009
  char *line = NULL;
Packit 6c4009
  size_t len = 0;
Packit 6c4009
  size_t n;
Packit 6c4009
Packit 6c4009
  if (argc < 2)
Packit 6c4009
    error (1, 0, "usage: %s <random seed>", argv[0]);
Packit 6c4009
Packit 6c4009
  setlocale (LC_ALL, "");
Packit 6c4009
Packit 6c4009
  nstrings_max = 100;
Packit 6c4009
  nstrings = 0;
Packit 6c4009
  strings = (struct lines *) malloc (nstrings_max * sizeof (struct lines));
Packit 6c4009
  if (strings == NULL)
Packit 6c4009
    {
Packit 6c4009
      perror (argv[0]);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  while (1)
Packit 6c4009
    {
Packit 6c4009
      int l;
Packit 6c4009
      if (getline (&line, &len, stdin) < 0)
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      if (nstrings == nstrings_max)
Packit 6c4009
	{
Packit 6c4009
	  strings = (struct lines *) realloc (strings,
Packit 6c4009
					      (nstrings_max *= 2)
Packit 6c4009
					       * sizeof (*strings));
Packit 6c4009
	  if (strings == NULL)
Packit 6c4009
	    {
Packit 6c4009
	      perror (argv[0]);
Packit 6c4009
	      exit (1);
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      strings[nstrings].line = strdup (line);
Packit 6c4009
      l = strcspn (line, ":(;");
Packit 6c4009
      while (l > 0 && isspace (line[l - 1]))
Packit 6c4009
	--l;
Packit 6c4009
      strings[nstrings].key = strndup (line, l);
Packit 6c4009
      ++nstrings;
Packit 6c4009
    }
Packit 6c4009
  free (line);
Packit 6c4009
Packit 6c4009
  /* First shuffle.  */
Packit 6c4009
  srandom (atoi (argv[1]));
Packit 6c4009
  for (n = 0; n < 10 * nstrings; ++n)
Packit 6c4009
    {
Packit 6c4009
      int r1, r2, r;
Packit 6c4009
      size_t idx1 = random () % nstrings;
Packit 6c4009
      size_t idx2 = random () % nstrings;
Packit 6c4009
      struct lines tmp = strings[idx1];
Packit 6c4009
      strings[idx1] = strings[idx2];
Packit 6c4009
      strings[idx2] = tmp;
Packit 6c4009
Packit 6c4009
      /* While we are at it a first little test.  */
Packit 6c4009
      r1 = strcoll (strings[idx1].key, strings[idx2].key);
Packit 6c4009
      r2 = strcoll (strings[idx2].key, strings[idx1].key);
Packit 6c4009
      r = r1 * r2;
Packit 6c4009
Packit 6c4009
      if (r > 0 || (r == 0 && r1 != 0) || (r == 0 && r2 != 0))
Packit 6c4009
	printf ("`%s' and `%s' collate wrong: %d vs. %d\n",
Packit 6c4009
		strings[idx1].key, strings[idx2].key, r1, r2);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now sort.  */
Packit 6c4009
  qsort (strings, nstrings, sizeof (struct lines), xstrcoll);
Packit 6c4009
Packit 6c4009
  /* Print the result.  */
Packit 6c4009
  for (n = 0; n < nstrings; ++n)
Packit 6c4009
    {
Packit 6c4009
      fputs (strings[n].line, stdout);
Packit 6c4009
      free (strings[n].line);
Packit 6c4009
      free (strings[n].key);
Packit 6c4009
    }
Packit 6c4009
  free (strings);
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
xstrcoll (const void *ptr1, const void *ptr2)
Packit 6c4009
{
Packit 6c4009
  const struct lines *l1 = (const struct lines *) ptr1;
Packit 6c4009
  const struct lines *l2 = (const struct lines *) ptr2;
Packit 6c4009
Packit 6c4009
  return strcoll (l1->key, l2->key);
Packit 6c4009
}