Blame src/stdlib/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 7da660
void test_qsort(void) {
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 7da660
				fprintf(stderr, "\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 7da660
				fprintf(stderr, "\t%d\t%d\n", i, n[i]);
nsz 7da660
			break;
nsz 7da660
		}
nsz 7da660
	}
nsz 7da660
}
nsz 7da660
nsz 7da660
void bench_qsort_small(int N) {
nsz 7da660
	int i;
nsz 7da660
	int *a = malloc(sizeof n);
nsz 7da660
nsz 7da660
	for (i = 0; i < N; i++) {
nsz 7da660
		memcpy(a, n, sizeof n);
nsz 7da660
		qsort(a, sizeof n/sizeof *n, sizeof *a, icmp);
nsz 7da660
	}
nsz 7da660
	free(a);
nsz 7da660
}
nsz 7da660
nsz 7da660
void bench_qsort_large(int N) {
nsz 7da660
	int i,j;
nsz 8f27a3
	int len = 10000;
nsz 7da660
	int *a = malloc(len * sizeof *a);
nsz 7da660
nsz 7da660
	for (i = 0; i < N; i++) {
nsz 7da660
		srand(13);
nsz 7da660
		for (j = 0; j < len; j++)
nsz 7da660
			a[j] = rand();
nsz 7da660
		qsort(a, len, sizeof *a, icmp);
nsz 7da660
	}
nsz 7da660
	free(a);
nsz 7da660
}