]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdlib/tst-qsort.c
Sync time/mktime.c with gnulib
[thirdparty/glibc.git] / stdlib / tst-qsort.c
CommitLineData
207b66ce
UD
1/* Test case by Paul Eggert <eggert@twinsun.com> */
2#include <stdio.h>
3#include <stdlib.h>
06f6ca90 4#include <tst-stack-align.h>
207b66ce
UD
5
6struct big { char c[4 * 1024]; };
7
8struct big *array;
9struct big *array_end;
10
06f6ca90
UD
11static int align_check;
12
207b66ce
UD
13int
14compare (void const *a1, void const *b1)
15{
16 struct big const *a = a1;
17 struct big const *b = b1;
06f6ca90
UD
18
19 if (!align_check)
20 align_check = TEST_STACK_ALIGN () ? -1 : 1;
21
207b66ce
UD
22 if (! (array <= a && a < array_end
23 && array <= b && b < array_end))
24 {
25 exit (EXIT_FAILURE);
26 }
27 return b->c[0] - a->c[0];
28}
29
30int
31main (int argc, char **argv)
32{
33 size_t i;
34 size_t array_members = argv[1] ? atoi (argv[1]) : 50;
35 array = (struct big *) malloc (array_members * sizeof *array);
36 if (array == NULL)
37 {
38 puts ("no memory");
39 exit (EXIT_FAILURE);
40 }
41
42 array_end = array + array_members;
43 for (i = 0; i < array_members; i++)
44 array[i].c[0] = i % 128;
45
46 qsort (array, array_members, sizeof *array, compare);
47
06f6ca90
UD
48 if (align_check == -1)
49 {
50 puts ("stack not sufficiently aligned");
51 exit (EXIT_FAILURE);
52 }
53
207b66ce
UD
54 return 0;
55}