test_heap.c 3.66 KB
Newer Older
Jan Včelák's avatar
Jan Včelák committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*  Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <tap/basic.h>

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "contrib/ucw/heap.h"

static void seed_random(void)
{
	unsigned short int seed[3] = { 0 };

	FILE *f = fopen("/dev/urandom", "r");
	if (f) {
33 34 35
		if (fread(seed, sizeof(seed), 1, f) != 1) {
			diag("failed to seed random source");
		}
Jan Včelák's avatar
Jan Včelák committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
		fclose(f);
	}

	diag("seed %hu %hu %hu", seed[0], seed[1], seed[2]);
	seed48(seed);
}

struct value {
	heap_val_t _heap;
	int data;
};

static int value_cmp(void *_a, void *_b)
{
	const struct value *a = _a;
	const struct value *b = _b;
	return (a->data - b->data);
}

int main(int argc, char *argv[])
{
	plan_lazy();

	seed_random();

	static const int VALUE_COUNT = 1000;
	static const int VALUE_RANGE = 950;
	static const int VALUE_REPLACE = 300;
	static const int VALUE_DELETE = 100;

	struct heap heap;
	heap_init(&heap, value_cmp, 0);

	ok(EMPTY_HEAP(&heap), "heap is empty");

	// fill the heap with random values (with duplicates)

	struct value *values = calloc(VALUE_COUNT, sizeof(struct value));
	assert(values);
	assert(VALUE_RANGE < VALUE_COUNT);

	bool valid = true;
	for (int i = 0; i < VALUE_COUNT; i++) {
		values[i].data = lrand48() % VALUE_RANGE;
		if (heap_insert(&heap, &values[i]._heap) == 0) {
			valid = false;
		}
	}
	ok(valid, "heap_insert");
	ok(!EMPTY_HEAP(&heap), "heap is non-empty");

	// excercise heap_insert

	valid = true;
	for (int i = 0; i < VALUE_COUNT; i++) {
		int pos = heap_find(&heap, &values[i]._heap);
		if (*HELEMENT(&heap, pos) != &values[i]._heap) {
			valid = false;
		}
	}
	ok(valid, "heap_find");

	// excercise heap_replace

	assert(VALUE_REPLACE <= VALUE_COUNT);
	struct value *replaced = calloc(VALUE_REPLACE, sizeof(struct value));
	assert(replaced);

	valid = true;
	for (int i = 0; i < VALUE_REPLACE; i++) {
		replaced[i].data = lrand48() % VALUE_RANGE;
		int pos = heap_find(&heap, &values[i]._heap);
		if (pos < 1) {
			valid = false;
			continue;
		}

		heap_replace(&heap, pos, &replaced[i]._heap);
		int newpos = heap_find(&heap, &replaced[i]._heap);
		if (newpos < 1) {
			valid = false;
		}
	}
	ok(valid, "heap_replace");

	// excercise heap_delete

	assert(VALUE_REPLACE + VALUE_DELETE < VALUE_COUNT);

	valid = true;
	for (int i = 0; i < VALUE_DELETE; i++) {
		heap_val_t *value = &values[i + VALUE_REPLACE]._heap;
		int pos = heap_find(&heap, value);
		if (pos < 1) {
			valid = false;
			continue;

		}
		heap_delete(&heap, pos);
		pos = heap_find(&heap, value);
		if (pos != 0) {
			valid = false;
		}
	}
	ok(valid, "heap_delete");

	// excercise item retrieval

	assert(VALUE_COUNT > VALUE_DELETE);

	valid = true;
	int current = -1;
	for (int i = 0; i < VALUE_COUNT - VALUE_DELETE; i++) {
		struct value *val = (struct value *)*HHEAD(&heap);
		heap_delmin(&heap);
		if (current <= val->data) {
			current = val->data;
		} else {
			valid = false;
		}
	}

	ok(valid, "heap ordering");
	ok(EMPTY_HEAP(&heap), "heap_delmin");

	free(replaced);
	free(values);
	heap_deinit(&heap);

	return 0;
}