|
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 |
|
|
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 |
1d2a59 |
static void string_sort(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) {
|
|
nsz |
7da660 |
error("string sort failed at index %d\n", i);
|
|
Szabolcs Nagy |
1d2a59 |
test_printf("\ti\tgot\twant\n");
|
|
Szabolcs Nagy |
1d2a59 |
for (i=0; i
|
|
Szabolcs Nagy |
1d2a59 |
test_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 |
1d2a59 |
static void integer_sort(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]) {
|
|
nsz |
7da660 |
error("integer sort failed at index %d\n", i);
|
|
Szabolcs Nagy |
1d2a59 |
test_printf("\ti\tgot\twant\n");
|
|
Szabolcs Nagy |
1d2a59 |
for (i=0; i
|
|
Szabolcs Nagy |
1d2a59 |
test_printf("\t%d\t%d\t%d\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 |
1d2a59 |
error("character sort failed\n"); \
|
|
Szabolcs Nagy |
1d2a59 |
test_printf("\tgot: \"%s\"\n", p); \
|
|
Szabolcs Nagy |
1d2a59 |
test_printf("\twant: \"%s\"\n", a_sorted); \
|
|
Szabolcs Nagy |
1d2a59 |
} \
|
|
Szabolcs Nagy |
1d2a59 |
} while(0)
|
|
Szabolcs Nagy |
1d2a59 |
|
|
Szabolcs Nagy |
1d2a59 |
static void character_sort(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 |
1d2a59 |
string_sort(s, s_sorted, sizeof s/sizeof*s);
|
|
Szabolcs Nagy |
1d2a59 |
integer_sort(n, n_sorted, sizeof n/sizeof*n);
|
|
Szabolcs Nagy |
1d2a59 |
character_sort();
|
|
nsz |
462b4f |
return test_status;
|
|
nsz |
7da660 |
}
|