Skip to content

Commit

Permalink
qsort test
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinemine committed Jul 4, 2023
1 parent f1fb622 commit aeab4ce
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion newpolka/pk_qsort.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
} \
\
/* partition */ \
while (true) { \
while (1) { \
while (CMP(param, beg, &pivot) < 0) beg++; \
while (CMP(param, &pivot, end) < 0) end--; \
if (beg >= end) break; \
Expand Down
91 changes: 91 additions & 0 deletions test/sorttest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>

#include "../newpolka/pk_qsort.h"

int cmp_int(void* unused, int* a, int* b) {
return (*a > *b) ? 1 : (*a < *b) ? -1 : 0;
}

MAKE_SORT(sort_int, int, cmp_int)

int sum(int* a, size_t size) {
int x = 0;
for (size_t i = 0; i < size; i++) x += a[i];
return x;
}

void check_cst_array(size_t size) {
int x = lrand48();
int* a = malloc(sizeof(int)*size);
for (size_t i = 0; i < size; i++)
a[i] = x;
int s = sum(a,size);
checked_sort_int(a, size, NULL);
assert(s == sum(a,size));
free(a);
}

void check_inc_array(size_t size) {
int* a = malloc(sizeof(int)*size);
for (size_t i = 0; i < size; i++)
a[i] = i;
int s = sum(a,size);
checked_sort_int(a, size, NULL);
assert(s == sum(a,size));
free(a);
}

void check_dec_array(size_t size) {
int* a = malloc(sizeof(int)*size);
for (size_t i = 0; i < size; i++)
a[i] = size-i;
int s = sum(a,size);
checked_sort_int(a, size, NULL);
assert(s == sum(a,size));
free(a);
}

void check_rnd_array(size_t size, int max_rand) {
int* a = malloc(sizeof(int)*size);
for (size_t i = 0; i < size; i++)
a[i] = lrand48() & max_rand;
int s = sum(a,size);
checked_sort_int(a, size, NULL);
assert(s == sum(a,size));
free(a);
}


int main() {
for (int i = 0; i < 1000; i++) {
printf("check constant array of size %i\n",i);
check_cst_array(i);
}
for (int i = 1; i < 1000; i++) {
printf("check increasing array of size %i\n",i);
check_inc_array(i);
}
for (int i = 1; i < 1000; i++) {
printf("check decreasing array of size %i\n",i);
check_dec_array(i);
}
for (int i = 1; i < 1000; i++) {
printf("check random array of size %i\n",i);
for (int j = i/2; j < i*2; j += 2) {
check_rnd_array(i, j);
}
for (int j = 0; j < 1000; j++) {
check_rnd_array(i, INT_MAX);
}
}
for (int i = 1000; i < 1000000; i = i*4/3) {
printf("check random array of size %i\n",i);
for (int j = 0; j < 100; j++) {
check_rnd_array(i, INT_MAX);
}
}
return 0;
}

0 comments on commit aeab4ce

Please sign in to comment.