Back to index

glibc  2.9
tst-qsort.c
Go to the documentation of this file.
00001 /* Test case by Paul Eggert <eggert@twinsun.com> */
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <tst-stack-align.h>
00005 
00006 struct big { char c[4 * 1024]; };
00007 
00008 struct big *array;
00009 struct big *array_end;
00010 
00011 static int align_check;
00012 
00013 int
00014 compare (void const *a1, void const *b1)
00015 {
00016   struct big const *a = a1;
00017   struct big const *b = b1;
00018 
00019   if (!align_check)
00020     align_check = TEST_STACK_ALIGN () ? -1 : 1;
00021 
00022   if (! (array <= a && a < array_end
00023         && array <= b && b < array_end))
00024     {
00025       exit (EXIT_FAILURE);
00026     }
00027   return b->c[0] - a->c[0];
00028 }
00029 
00030 int
00031 main (int argc, char **argv)
00032 {
00033   size_t i;
00034   size_t array_members = argv[1] ? atoi (argv[1]) : 50;
00035   array = (struct big *) malloc (array_members * sizeof *array);
00036   if (array == NULL)
00037     {
00038       puts ("no memory");
00039       exit (EXIT_FAILURE);
00040     }
00041 
00042   array_end = array + array_members;
00043   for (i = 0; i < array_members; i++)
00044     array[i].c[0] = i % 128;
00045 
00046   qsort (array, array_members, sizeof *array, compare);
00047 
00048   if (align_check == -1)
00049     {
00050       puts ("stack not sufficiently aligned");
00051       exit (EXIT_FAILURE);
00052     }
00053 
00054   return 0;
00055 }