File: tst-qsort.c

package info (click to toggle)
glibc 2.24-11+deb9u4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 225,852 kB
  • sloc: ansic: 996,505; asm: 261,827; sh: 10,484; makefile: 9,856; cpp: 4,169; python: 3,971; perl: 2,254; awk: 1,753; pascal: 1,521; yacc: 291; sed: 80
file content (55 lines) | stat: -rw-r--r-- 1,082 bytes parent folder | download | duplicates (28)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* Test case by Paul Eggert <eggert@twinsun.com> */
#include <stdio.h>
#include <stdlib.h>
#include <tst-stack-align.h>

struct big { char c[4 * 1024]; };

struct big *array;
struct big *array_end;

static int align_check;

int
compare (void const *a1, void const *b1)
{
  struct big const *a = a1;
  struct big const *b = b1;

  if (!align_check)
    align_check = TEST_STACK_ALIGN () ? -1 : 1;

  if (! (array <= a && a < array_end
	 && array <= b && b < array_end))
    {
      exit (EXIT_FAILURE);
    }
  return b->c[0] - a->c[0];
}

int
main (int argc, char **argv)
{
  size_t i;
  size_t array_members = argv[1] ? atoi (argv[1]) : 50;
  array = (struct big *) malloc (array_members * sizeof *array);
  if (array == NULL)
    {
      puts ("no memory");
      exit (EXIT_FAILURE);
    }

  array_end = array + array_members;
  for (i = 0; i < array_members; i++)
    array[i].c[0] = i % 128;

  qsort (array, array_members, sizeof *array, compare);

  if (align_check == -1)
    {
      puts ("stack not sufficiently aligned");
      exit (EXIT_FAILURE);
    }

  return 0;
}