Blame src/general/qsort.c

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
nsz 7da660
/* 26 items -- even */
nsz 7da660
static 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
};
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
};
nsz 7da660
nsz 462b4f
int main(void)
nsz 462b4f
{
nsz 7da660
	int i;
nsz 7da660
nsz 7da660
	qsort(s, sizeof(s)/sizeof(char *), sizeof(char *), scmp);
nsz 7da660
	for (i=0; i
nsz 7da660
		if (strcmp(s[i], s[i+1]) > 0) {
nsz 7da660
			error("string sort failed at index %d\n", i);
nsz 7da660
			for (i=0; i
nsz 462b4f
				error("\t%d\t%s\n", i, s[i]);
nsz 7da660
			break;
nsz 7da660
		}
nsz 7da660
	}
nsz 7da660
nsz 7da660
	qsort(n, sizeof(n)/sizeof(int), sizeof(int), icmp);
nsz 7da660
	for (i=0; i
nsz 7da660
		if (n[i] > n[i+1]) {
nsz 7da660
			error("integer sort failed at index %d\n", i);
nsz 7da660
			for (i=0; i
nsz 462b4f
				error("\t%d\t%d\n", i, n[i]);
nsz 7da660
			break;
nsz 7da660
		}
nsz 7da660
	}
nsz 462b4f
	return test_status;
nsz 7da660
}