Commit 1720dc16 authored by Pekka Pessi's avatar Pekka Pessi

torture_heap.c: added tests for ##sort() and su_smoothsort()

darcs-hash:20070704225520-65a35-fbdca27efd42285f8b7faa7562df000ff141e9c1.gz
parent 6b3afaa4
......@@ -33,6 +33,7 @@
#include <sofia-sip/heap.h>
#include <unistd.h>
#include <stddef.h>
#include <string.h>
#include <assert.h>
......@@ -104,6 +105,82 @@ int tstflags;
char name[] = "torture_heap";
size_t _set, _cmp;
static int int_less(void *_array, size_t a, size_t b)
{
int *array = _array;
_cmp++;
return array[a] < array[b];
}
static void int_swap(void *_array, size_t a, size_t b)
{
int *array = _array;
int swap = array[a];
array[a] = array[b];
array[b] = swap;
_set++;
}
void test_sort(int *array, size_t r, size_t N)
{
su_smoothsort(array, r, N, int_less, int_swap);
}
int test_smooth_sort()
{
BEGIN();
size_t i, n, N = 300000;
int array[N];
size_t v1 = 3, v2 = 1;
for (n = v1; n <= N; n = v1 + v2 + 1, v2 = v1, v1 = n) {
for (i = 0; i < n; i++) {
array[i] = (int)(n - i - 1);
}
_set = 0;
write(1, ".", 1);
test_sort(array, 0, n);
TEST_1(_set > 0); _set = 0;
test_sort(array, 0, n);
TEST(_set, 0);
for (i = 0; i < n; i++) {
TEST(array[i], i);
}
}
for (n = 4; n <= N; n *= 2) {
for (i = 0; i < n; i++) {
array[i] = (int)(n - i - 1);
}
write(1, "/", 1);
test_sort(array, 0, n / 2);
test_sort(array, n / 2, n / 2);
for (i = 0; i < n / 2; i++) {
TEST(array[i], i + n / 2);
}
for (; i < n; i++) {
TEST(array[i], i - n / 2);
}
}
END();
}
int test_value()
{
BEGIN();
......@@ -112,7 +189,7 @@ int test_value()
unsigned i, previous, n, N;
unsigned char *tests;
N = 300000;
N = 3000;
TEST_1(tests = calloc(sizeof (unsigned char), N + 1));
......@@ -174,6 +251,12 @@ int test_value()
TEST(heap1_used(heap), N);
heap1_sort(heap);
for (i = 1; i <= N; i++) {
type1 e = heap1_get(heap, i);
TEST(e.index, i);
}
/* Remove 1000 entries from random places */
previous = 0;
......@@ -297,9 +380,41 @@ int test_ref()
TEST(heap2_used(heap), N);
/* Remove 1000 entries from random places */
previous = 0;
heap2_sort(heap);
for (i = 1; i <= N; i++) {
type2 const e = heap2_get(heap, i);
TEST_1(e); TEST(e->index, i);
}
heap2_sort(heap);
for (i = 1; i <= N; i++) {
type2 const e = heap2_get(heap, i);
TEST_1(e); TEST(e->index, i);
}
/* Remove all odd entries */
for (i = heap2_used(heap), n = 0; i > 0; i--) {
type2 e = heap2_get(heap, i);
TEST_1(e); TEST(e->index, i);
n++;
tests[e->value] |= 16;
if (e->value & 1) {
TEST(tests[e->value] & 8, 0); tests[e->value] |= 8;
heap2_remove(heap, i);
}
}
for (i = 1; i <= N; i++) {
if (i & 1)
TEST(tests[i] & 8, 8);
else
TEST(tests[i] & 8, 0);
}
/* Remove 1000 entries from random places */
for (i = 0; i < 1000 && heap2_used(heap) > 0; i++) {
type2 e;
n = i * 397651 % heap2_used(heap) + 1;
......@@ -338,7 +453,7 @@ int test_ref()
}
for (i = 1; i <= N; i++) {
TEST(tests[i], 8 | 4 | 2 | 1);
TEST(tests[i], 16 | 8 | 4 | 2 | 1);
}
TEST(heap2_resize(NULL, &heap, 63), 0);
......@@ -373,6 +488,7 @@ int main(int argc, char *argv[])
usage(1);
}
retval |= test_smooth_sort(); fflush(stdout);
retval |= test_value(); fflush(stdout);
retval |= test_ref(); fflush(stdout);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment