|
Szabolcs Nagy |
cfa23c |
#include <inttypes.h>
|
|
nsz |
7da660 |
#include <stdlib.h>
|
|
nsz |
7da660 |
#include <stdio.h>
|
|
nsz |
7da660 |
#include <string.h>
|
|
nsz |
7da660 |
#include "test.h"
|
|
nsz |
7da660 |
|
|
nsz |
7da660 |
static int scmp(const void *a, const void *b)
|
|
nsz |
7da660 |
{
|
|
nsz |
7da660 |
return strcmp(*(char **)a, *(char **)b);
|
|
nsz |
7da660 |
}
|
|
nsz |
7da660 |
|
|
nsz |
7da660 |
static int icmp(const void *a, const void *b)
|
|
nsz |
7da660 |
{
|
|
nsz |
7da660 |
return *(int*)a - *(int*)b;
|
|
nsz |
7da660 |
}
|
|
nsz |
7da660 |
|
|
Szabolcs Nagy |
1d2a59 |
static int ccmp(const void *a, const void *b)
|
|
Szabolcs Nagy |
1d2a59 |
{
|
|
Szabolcs Nagy |
1d2a59 |
return *(char*)a - *(char*)b;
|
|
Szabolcs Nagy |
1d2a59 |
}
|
|
Szabolcs Nagy |
1d2a59 |
|
|
Szabolcs Nagy |
cfa23c |
static int cmp64(const void *a, const void *b)
|
|
Szabolcs Nagy |
cfa23c |
{
|
|
Szabolcs Nagy |
cfa23c |
const uint64_t *ua = a, *ub = b;
|
|
Szabolcs Nagy |
cfa23c |
return *ua < *ub ? -1 : *ua != *ub;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
|
|
nsz |
7da660 |
/* 26 items -- even */
|
|
Szabolcs Nagy |
1d2a59 |
static const char *s[] = {
|
|
nsz |
7da660 |
"Bob", "Alice", "John", "Ceres",
|
|
nsz |
7da660 |
"Helga", "Drepper", "Emeralda", "Zoran",
|
|
nsz |
7da660 |
"Momo", "Frank", "Pema", "Xavier",
|
|
nsz |
7da660 |
"Yeva", "Gedun", "Irina", "Nono",
|
|
nsz |
7da660 |
"Wiener", "Vincent", "Tsering", "Karnica",
|
|
nsz |
7da660 |
"Lulu", "Quincy", "Osama", "Riley",
|
|
nsz |
7da660 |
"Ursula", "Sam"
|
|
nsz |
7da660 |
};
|
|
Szabolcs Nagy |
1d2a59 |
static const char *s_sorted[] = {
|
|
Szabolcs Nagy |
1d2a59 |
"Alice", "Bob", "Ceres", "Drepper",
|
|
Szabolcs Nagy |
1d2a59 |
"Emeralda", "Frank", "Gedun", "Helga",
|
|
Szabolcs Nagy |
1d2a59 |
"Irina", "John", "Karnica", "Lulu",
|
|
Szabolcs Nagy |
1d2a59 |
"Momo", "Nono", "Osama", "Pema",
|
|
Szabolcs Nagy |
1d2a59 |
"Quincy", "Riley", "Sam", "Tsering",
|
|
Szabolcs Nagy |
1d2a59 |
"Ursula", "Vincent", "Wiener", "Xavier",
|
|
Szabolcs Nagy |
1d2a59 |
"Yeva", "Zoran"
|
|
Szabolcs Nagy |
1d2a59 |
};
|
|
Szabolcs Nagy |
1d2a59 |
|
|
nsz |
7da660 |
/* 23 items -- odd, prime */
|
|
nsz |
7da660 |
static int n[] = {
|
|
nsz |
7da660 |
879045, 394, 99405644, 33434, 232323, 4334, 5454,
|
|
nsz |
7da660 |
343, 45545, 454, 324, 22, 34344, 233, 45345, 343,
|
|
nsz |
7da660 |
848405, 3434, 3434344, 3535, 93994, 2230404, 4334
|
|
nsz |
7da660 |
};
|
|
Szabolcs Nagy |
1d2a59 |
static int n_sorted[] = {
|
|
Szabolcs Nagy |
1d2a59 |
22, 233, 324, 343, 343, 394, 454, 3434,
|
|
Szabolcs Nagy |
1d2a59 |
3535, 4334, 4334, 5454, 33434, 34344, 45345, 45545,
|
|
Szabolcs Nagy |
1d2a59 |
93994, 232323, 848405, 879045, 2230404, 3434344, 99405644
|
|
Szabolcs Nagy |
1d2a59 |
};
|
|
nsz |
7da660 |
|
|
Szabolcs Nagy |
cfa23c |
static void str_test(const char **a, const char **a_sorted, int len)
|
|
nsz |
462b4f |
{
|
|
nsz |
7da660 |
int i;
|
|
Szabolcs Nagy |
1d2a59 |
qsort(a, len, sizeof *a, scmp);
|
|
Szabolcs Nagy |
1d2a59 |
for (i=0; i
|
|
Szabolcs Nagy |
1d2a59 |
if (strcmp(a[i], a_sorted[i]) != 0) {
|
|
Szabolcs Nagy |
cfa23c |
t_error("string sort failed at index %d\n", i);
|
|
Szabolcs Nagy |
cfa23c |
t_printf("\ti\tgot\twant\n");
|
|
Szabolcs Nagy |
1d2a59 |
for (i=0; i
|
|
Szabolcs Nagy |
cfa23c |
t_printf("\t%d\t%s\t%s\n", i, a[i], a_sorted[i]);
|
|
nsz |
7da660 |
break;
|
|
nsz |
7da660 |
}
|
|
nsz |
7da660 |
}
|
|
Szabolcs Nagy |
1d2a59 |
}
|
|
nsz |
7da660 |
|
|
Szabolcs Nagy |
cfa23c |
static void int_test(int *a, int *a_sorted, int len)
|
|
Szabolcs Nagy |
1d2a59 |
{
|
|
Szabolcs Nagy |
1d2a59 |
int i;
|
|
Szabolcs Nagy |
1d2a59 |
qsort(a, len, sizeof *a, icmp);
|
|
Szabolcs Nagy |
1d2a59 |
for (i=0; i
|
|
Szabolcs Nagy |
1d2a59 |
if (a[i] != a_sorted[i]) {
|
|
Szabolcs Nagy |
cfa23c |
t_error("integer sort failed at index %d\n", i);
|
|
Szabolcs Nagy |
cfa23c |
t_printf("\ti\tgot\twant\n");
|
|
Szabolcs Nagy |
1d2a59 |
for (i=0; i
|
|
Szabolcs Nagy |
cfa23c |
t_printf("\t%d\t%d\t%d\n", i, a[i], a_sorted[i]);
|
|
Szabolcs Nagy |
cfa23c |
break;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
static void uint64_gen(uint64_t *p, uint64_t *p_sorted, int n)
|
|
Szabolcs Nagy |
cfa23c |
{
|
|
Szabolcs Nagy |
cfa23c |
int i;
|
|
Szabolcs Nagy |
cfa23c |
uint64_t r = 0;
|
|
Szabolcs Nagy |
cfa23c |
t_randseed(n);
|
|
Szabolcs Nagy |
cfa23c |
for (i = 0; i < n; i++) {
|
|
Szabolcs Nagy |
cfa23c |
r += t_randn(20);
|
|
Szabolcs Nagy |
cfa23c |
p[i] = r;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
memcpy(p_sorted, p, n * sizeof *p);
|
|
Szabolcs Nagy |
cfa23c |
t_shuffle(p, n);
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
static void uint64_test(uint64_t *a, uint64_t *a_sorted, int len)
|
|
Szabolcs Nagy |
cfa23c |
{
|
|
Szabolcs Nagy |
cfa23c |
int i;
|
|
Szabolcs Nagy |
cfa23c |
qsort(a, len, sizeof *a, cmp64);
|
|
Szabolcs Nagy |
cfa23c |
for (i=0; i
|
|
Szabolcs Nagy |
cfa23c |
if (a[i] != a_sorted[i]) {
|
|
Szabolcs Nagy |
cfa23c |
t_error("uint64 sort failed at index %d\n", i);
|
|
Szabolcs Nagy |
cfa23c |
t_printf("\ti\tgot\twant\n");
|
|
Szabolcs Nagy |
cfa23c |
for (i=0; i
|
|
Szabolcs Nagy |
cfa23c |
t_printf("\t%d\t%" PRIu64 "\t%" PRIu64 "\n", i, a[i], a_sorted[i]);
|
|
nsz |
7da660 |
break;
|
|
nsz |
7da660 |
}
|
|
nsz |
7da660 |
}
|
|
Szabolcs Nagy |
1d2a59 |
}
|
|
Szabolcs Nagy |
1d2a59 |
|
|
Szabolcs Nagy |
1d2a59 |
#define T(a, a_sorted) do { \
|
|
Szabolcs Nagy |
1d2a59 |
char p[] = a; \
|
|
Szabolcs Nagy |
1d2a59 |
qsort(p, sizeof p - 1, 1, ccmp); \
|
|
Szabolcs Nagy |
1d2a59 |
if (memcmp(p, a_sorted, sizeof p) != 0) { \
|
|
Szabolcs Nagy |
cfa23c |
t_error("character sort failed\n"); \
|
|
Szabolcs Nagy |
cfa23c |
t_printf("\tgot: \"%s\"\n", p); \
|
|
Szabolcs Nagy |
cfa23c |
t_printf("\twant: \"%s\"\n", a_sorted); \
|
|
Szabolcs Nagy |
1d2a59 |
} \
|
|
Szabolcs Nagy |
1d2a59 |
} while(0)
|
|
Szabolcs Nagy |
1d2a59 |
|
|
Szabolcs Nagy |
cfa23c |
static void char_test(void)
|
|
Szabolcs Nagy |
1d2a59 |
{
|
|
Szabolcs Nagy |
1d2a59 |
T("", "");
|
|
Szabolcs Nagy |
1d2a59 |
T("1", "1");
|
|
Szabolcs Nagy |
1d2a59 |
T("11", "11");
|
|
Szabolcs Nagy |
1d2a59 |
T("12", "12");
|
|
Szabolcs Nagy |
1d2a59 |
T("21", "12");
|
|
Szabolcs Nagy |
1d2a59 |
T("111", "111");
|
|
Szabolcs Nagy |
1d2a59 |
T("211", "112");
|
|
Szabolcs Nagy |
1d2a59 |
T("121", "112");
|
|
Szabolcs Nagy |
1d2a59 |
T("112", "112");
|
|
Szabolcs Nagy |
1d2a59 |
T("221", "122");
|
|
Szabolcs Nagy |
1d2a59 |
T("212", "122");
|
|
Szabolcs Nagy |
1d2a59 |
T("122", "122");
|
|
Szabolcs Nagy |
1d2a59 |
T("123", "123");
|
|
Szabolcs Nagy |
1d2a59 |
T("132", "123");
|
|
Szabolcs Nagy |
1d2a59 |
T("213", "123");
|
|
Szabolcs Nagy |
1d2a59 |
T("231", "123");
|
|
Szabolcs Nagy |
1d2a59 |
T("321", "123");
|
|
Szabolcs Nagy |
1d2a59 |
T("312", "123");
|
|
Szabolcs Nagy |
1d2a59 |
T("1423", "1234");
|
|
Szabolcs Nagy |
1d2a59 |
T("51342", "12345");
|
|
Szabolcs Nagy |
1d2a59 |
T("261435", "123456");
|
|
Szabolcs Nagy |
1d2a59 |
T("4517263", "1234567");
|
|
Szabolcs Nagy |
1d2a59 |
T("37245618", "12345678");
|
|
Szabolcs Nagy |
1d2a59 |
T("812436597", "123456789");
|
|
Szabolcs Nagy |
1d2a59 |
T("987654321", "123456789");
|
|
Szabolcs Nagy |
1d2a59 |
T("321321321", "111222333");
|
|
Szabolcs Nagy |
1d2a59 |
T("49735862185236174", "11223344556677889");
|
|
Szabolcs Nagy |
1d2a59 |
}
|
|
Szabolcs Nagy |
1d2a59 |
|
|
Szabolcs Nagy |
1d2a59 |
int main(void)
|
|
Szabolcs Nagy |
1d2a59 |
{
|
|
Szabolcs Nagy |
cfa23c |
int i;
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
str_test(s, s_sorted, sizeof s/sizeof*s);
|
|
Szabolcs Nagy |
cfa23c |
int_test(n, n_sorted, sizeof n/sizeof*n);
|
|
Szabolcs Nagy |
cfa23c |
char_test();
|
|
Szabolcs Nagy |
cfa23c |
for (i = 1023; i<=1026; i++) {
|
|
Szabolcs Nagy |
cfa23c |
uint64_t p[1026], p_sorted[1026];
|
|
Szabolcs Nagy |
cfa23c |
uint64_gen(p, p_sorted, i);
|
|
Szabolcs Nagy |
cfa23c |
uint64_test(p, p_sorted, i);
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
return t_status;
|
|
nsz |
7da660 |
}
|