1 /* Test collation function using real data.
2 Copyright (C) 1997-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
33 static int xstrcoll (const void *, const void *);
38 return (0 < n
) - (n
< 0);
42 main (int argc
, char *argv
[])
45 size_t nstrings
, nstrings_max
;
46 struct lines
*strings
;
52 error (1, 0, "usage: %s <random seed>", argv
[0]);
54 setlocale (LC_ALL
, "");
58 strings
= (struct lines
*) malloc (nstrings_max
* sizeof (struct lines
));
68 if (getline (&line
, &len
, stdin
) < 0)
71 if (nstrings
== nstrings_max
)
73 strings
= (struct lines
*) realloc (strings
,
82 strings
[nstrings
].line
= strdup (line
);
83 l
= strcspn (line
, ":(;");
84 while (l
> 0 && isspace (line
[l
- 1]))
86 strings
[nstrings
].key
= strndup (line
, l
);
92 srandom (atoi (argv
[1]));
93 for (n
= 0; n
< 10 * nstrings
; ++n
)
96 size_t idx1
= random () % nstrings
;
97 size_t idx2
= random () % nstrings
;
98 struct lines tmp
= strings
[idx1
];
99 strings
[idx1
] = strings
[idx2
];
102 /* While we are at it a first little test. */
103 r1
= strcoll (strings
[idx1
].key
, strings
[idx2
].key
);
104 r2
= strcoll (strings
[idx2
].key
, strings
[idx1
].key
);
106 if (signum (r1
) != - signum (r2
))
107 printf ("`%s' and `%s' collate wrong: %d vs. %d\n",
108 strings
[idx1
].key
, strings
[idx2
].key
, r1
, r2
);
112 qsort (strings
, nstrings
, sizeof (struct lines
), xstrcoll
);
114 /* Print the result. */
115 for (n
= 0; n
< nstrings
; ++n
)
117 fputs (strings
[n
].line
, stdout
);
118 free (strings
[n
].line
);
119 free (strings
[n
].key
);
128 xstrcoll (const void *ptr1
, const void *ptr2
)
130 const struct lines
*l1
= (const struct lines
*) ptr1
;
131 const struct lines
*l2
= (const struct lines
*) ptr2
;
133 return strcoll (l1
->key
, l2
->key
);