From a9baddd7d07b9fe15e212985a808a79773ec72e4 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Sat, 19 Mar 2011 22:26:06 -0400 Subject: initial check-in, taken from old libc svn repo with significant additions --- qsort.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 qsort.c (limited to 'qsort.c') diff --git a/qsort.c b/qsort.c new file mode 100644 index 0000000..818426c --- /dev/null +++ b/qsort.c @@ -0,0 +1,59 @@ +#include +#include +#include + +static int scmp(const void *a, const void *b) +{ + return strcmp(*(char **)a, *(char **)b); +} + +static int icmp(const void *a, const void *b) +{ + return *(int*)a - *(int*)b; +} + +#define FAIL(m) (printf(__FILE__ ":%d: %s failed\n", __LINE__, m), err++, 0) + +int test_qsort(void) +{ + int i; + int err=0; + /* 26 items -- even */ + char *s[] = { + "Bob", "Alice", "John", "Ceres", + "Helga", "Drepper", "Emeralda", "Zoran", + "Momo", "Frank", "Pema", "Xavier", + "Yeva", "Gedun", "Irina", "Nono", + "Wiener", "Vincent", "Tsering", "Karnica", + "Lulu", "Quincy", "Osama", "Riley", + "Ursula", "Sam" + }; + /* 23 items -- odd, prime */ + int n[] = { + 879045, 394, 99405644, 33434, 232323, 4334, 5454, + 343, 45545, 454, 324, 22, 34344, 233, 45345, 343, + 848405, 3434, 3434344, 3535, 93994, 2230404, 4334 + }; + + qsort(s, sizeof(s)/sizeof(char *), sizeof(char *), scmp); + for (i=0; i 0) { + FAIL("string sort"); + for (i=0; i n[i+1]) { + FAIL("integer sort"); + for (i=0; i