#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <assert.h>
#include <stdbool.h>
#include <yaxt.h>
#include "tests.h"
static void
test1(
void (*anysort_index)(
Xt_int * a,
int n,
int * idx,
int reset_index)) {
Xt_int ivec[] = {7,5,3,1,2,2,3,3};
static const Xt_int sorted_ivec[] = {1,2,2,3,3,3,5,7};
int pvec[] = {1,2,3,4,5,6,7,8};
static const int sorted_pvec[] = {3,4,5,2,6,7,1,0};
size_t tsize = sizeof(ivec) / sizeof(ivec[0]);
assert(tsize == sizeof(pvec) / sizeof(pvec[0]));
anysort_index(ivec, (int)tsize, pvec, 1);
bool mismatch_values = false, mismatch_positions = false;
for(size_t i=0; i < tsize; i++) {
mismatch_values |= ( ivec[i] != sorted_ivec[i] );
mismatch_positions |= ( pvec[i] != sorted_pvec[i] );
}
if ( mismatch_values )
PUT_ERR("wrong sorting values\n");
if ( mismatch_positions )
PUT_ERR("wrong sorting positions\n");
}
static void
test2(void (*sort_int)(int *a, size_t n))
{
int ivec[] = {7,5,3,1,2,2,3,3};
static const int sorted_ivec[] = {1,2,2,3,3,3,5,7};
size_t ivec_size = sizeof (ivec) / sizeof (ivec[0]);
sort_int(ivec, ivec_size);
bool mismatch = false;
for(size_t i=0; i < ivec_size; i++)
mismatch |= ( ivec[i] != sorted_ivec[i] );
if (mismatch)
PUT_ERR("wrong sorting values\n");
size_t n = 40000;
int *large_ivec = malloc(n * sizeof (*large_ivec));
for (size_t i = 0; i < n; ++i)
large_ivec[i] = (int)(n - i);
sort_int(large_ivec, n);
mismatch = false;
for (size_t i = 0; i < n; ++i)
mismatch |= (large_ivec[i] != (int)(i + 1));
if (mismatch)
PUT_ERR("wrong sorting values\n");
size_t ofs = 50;
for (size_t i = 0; i < n; ++i)
large_ivec[i] = (int)(((i + ofs)%n) / 2);
sort_int(large_ivec, n);
mismatch = false;
for (size_t i = 0; i < n; ++i)
mismatch |= (large_ivec[i] != (int)(i/2));
if (mismatch)
PUT_ERR("wrong sorting values\n");
free(large_ivec);
}
int main(void) {
return TEST_EXIT_CODE;
}
void xt_mergesort_index(Xt_int *restrict v_idx, int n, int *restrict v_pos, int reset_pos)
void xt_mergesort_int(int a[], size_t n)
void xt_quicksort_index(Xt_int *restrict v_idx, int n, int *restrict v_pos, int reset_pos)
void xt_quicksort_int(int a[], size_t n)