Blame localedata/xfrm-test.c

Packit 6c4009
/* Test collation function via transformation 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
#include <stdbool.h>
Packit 6c4009
Packit 6c4009
/* Keep in sync with string/strxfrm_l.c.  */
Packit 6c4009
#define SMALL_STR_SIZE 4095
Packit 6c4009
Packit 6c4009
struct lines
Packit 6c4009
{
Packit 6c4009
  char *xfrm;
Packit 6c4009
  char *line;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static int xstrcmp (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
  bool nocache = false;
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> [-nocache]", argv[0]);
Packit 6c4009
Packit 6c4009
  if (argc == 3)
Packit 6c4009
    {
Packit 6c4009
      if (strcmp (argv[2], "-nocache") == 0)
Packit 6c4009
	nocache = true;
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  printf ("Unknown option %s!\n", argv[2]);
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
    }
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
      char saved, *word, *newp;
Packit 6c4009
      size_t l, line_len, needed;
Packit 6c4009
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
Packit 6c4009
      saved = line[l];
Packit 6c4009
      line[l] = '\0';
Packit 6c4009
Packit 6c4009
      if (nocache)
Packit 6c4009
	{
Packit 6c4009
	  line_len = strlen (line);
Packit 6c4009
	  word = malloc (line_len + SMALL_STR_SIZE + 1);
Packit 6c4009
	  if (word == NULL)
Packit 6c4009
	    {
Packit 6c4009
	      printf ("malloc failed: %m\n");
Packit 6c4009
	      exit (1);
Packit 6c4009
	    }
Packit 6c4009
	  memset (word, ' ', SMALL_STR_SIZE);
Packit 6c4009
	  memcpy (word + SMALL_STR_SIZE, line, line_len);
Packit 6c4009
	  word[line_len + SMALL_STR_SIZE] = '\0';
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
        word = line;
Packit 6c4009
Packit 6c4009
      needed = strxfrm (NULL, word, 0);
Packit 6c4009
      newp = malloc (needed + 1);
Packit 6c4009
      if (newp == NULL)
Packit 6c4009
	{
Packit 6c4009
	  printf ("malloc failed: %m\n");
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
      strxfrm (newp, word, needed + 1);
Packit 6c4009
      strings[nstrings].xfrm = newp;
Packit 6c4009
Packit 6c4009
      if (nocache)
Packit 6c4009
	free (word);
Packit 6c4009
      line[l] = saved;
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 = strcmp (strings[idx1].xfrm, strings[idx2].xfrm);
Packit 6c4009
      r2 = strcmp (strings[idx2].xfrm, strings[idx1].xfrm);
Packit 6c4009
      r = -(r1 ^ r2);
Packit 6c4009
      if (r)
Packit 6c4009
	r /= abs (r1 ^ r2);
Packit 6c4009
Packit 6c4009
      if (r < 0 || (r == 0 && (r1 != 0 || r2 != 0))
Packit 6c4009
	  || (r > 0 && (r1 ^ r2) >= 0))
Packit 6c4009
	printf ("collate wrong: %d vs. %d\n", r1, r2);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now sort.  */
Packit 6c4009
  qsort (strings, nstrings, sizeof (struct lines), xstrcmp);
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].xfrm);
Packit 6c4009
    }
Packit 6c4009
  free (strings);
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
xstrcmp (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 strcmp (l1->xfrm, l2->xfrm);
Packit 6c4009
}