/* Test of stable-sorting of an array using mergesort.
- Copyright (C) 2009 Free Software Foundation, Inc.
+ Copyright (C) 2009-2013 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
#define STATIC static
#include "array-mergesort.h"
-#include <stdio.h>
#include <stdlib.h>
-#define ASSERT(expr) \
- do \
- { \
- if (!(expr)) \
- { \
- fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
- fflush (stderr); \
- abort (); \
- } \
- } \
- while (0)
+#include "macros.h"
#define NMAX 257
static const struct foo data[NMAX] =
cmp_double (const void *a, const void *b)
{
return (*(const double *)a < *(const double *)b ? -1 :
- *(const double *)a > *(const double *)b ? 1 :
- 0);
+ *(const double *)a > *(const double *)b ? 1 :
+ 0);
}
int
/* Verify the result. */
qsort_result = (double *) malloc (n * sizeof (double));
for (i = 0; i < n; i++)
- qsort_result[i] = data[i].x;
+ qsort_result[i] = data[i].x;
qsort (qsort_result, n, sizeof (double), cmp_double);
for (i = 0; i < n; i++)
- ASSERT (dst[i].x == qsort_result[i]);
+ ASSERT (dst[i].x == qsort_result[i]);
/* Verify the stability. */
for (i = 0; i < n; i++)
- if (i > 0 && dst[i - 1].x == dst[i].x)
- ASSERT (dst[i - 1].index < dst[i].index);
+ if (i > 0 && dst[i - 1].x == dst[i].x)
+ ASSERT (dst[i - 1].index < dst[i].index);
free (qsort_result);
free (tmp);
tmp[n].x = 0x587EF149; /* canary */
for (i = 0; i < n; i++)
- src[i] = data[i];
+ src[i] = data[i];
merge_sort_inplace (src, n, tmp);
/* Verify the result. */
qsort_result = (double *) malloc (n * sizeof (double));
for (i = 0; i < n; i++)
- qsort_result[i] = data[i].x;
+ qsort_result[i] = data[i].x;
qsort (qsort_result, n, sizeof (double), cmp_double);
for (i = 0; i < n; i++)
- ASSERT (src[i].x == qsort_result[i]);
+ ASSERT (src[i].x == qsort_result[i]);
/* Verify the stability. */
for (i = 0; i < n; i++)
- if (i > 0 && src[i - 1].x == src[i].x)
- ASSERT (src[i - 1].index < src[i].index);
+ if (i > 0 && src[i - 1].x == src[i].x)
+ ASSERT (src[i - 1].index < src[i].index);
free (qsort_result);
free (tmp);