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 33 34 35 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <stdio.h>
#include <stdbool.h>
#include <memory.h>
#include "unit.h"
#define HEAP_FORWARD_DECLARATION
#include "salad/heap.h"
#undef HEAP_FORWARD_DECLARATION
struct test_type {
uint32_t val1;
uint32_t val2;
char c;
struct heap_node node;
};
int test_type_less(const heap_t *heap,
const struct heap_node *a,
const struct heap_node *b) {
const struct test_type *left = (struct test_type *)((char *)a -
offsetof(struct test_type, node));
const struct test_type *right = (struct test_type *)((char *)b -
offsetof(struct test_type, node));
return left->val1 < right->val1;
}
#define HEAP_NAME test_heap
#define HEAP_LESS(h, a, b) test_type_less(h, a, b)
#include "salad/heap.h"
void free_all_nodes(heap_t *p_heap)
{
struct test_type *root_value;
for (heap_off_t i = 0; i < p_heap->size; ++i) {
root_value = (struct test_type *) ((char *)p_heap->harr[i] -
offsetof(struct test_type, node));
free(root_value);
}
}
static void
test_iterator_create()
{
header();
struct test_type *value, *root_value;
heap_t heap;
test_heap_create(&heap);
value = (struct test_type *)malloc(sizeof(struct test_type));
value->val1 = 0;
test_heap_insert(&heap, &value->node);
struct heap_iterator it;
test_heap_iterator_init(&heap, &it);
if (it.curr_pos != 0)
fail("incorrect position after create", "it.curr_pos != 0");
free_all_nodes(&heap);
footer();
}
static void
test_iterator_empty()
{
header();
struct heap_node *nd;
heap_t heap;
test_heap_create(&heap);
struct heap_iterator it;
test_heap_iterator_init(&heap, &it);
nd = test_heap_iterator_next(&it);
if (nd != NULL)
fail("incorrect node", "nd != NULL");
free_all_nodes(&heap);
footer();
}
static void
test_iterator_small()
{
header();
struct test_type *value, *root_value;
struct heap_node *test_node;
heap_t heap;
test_heap_create(&heap);
for (uint32_t i = 4; i > 0; --i) {
value = (struct test_type *)malloc(sizeof(struct test_type));
value->val1 = i;
test_heap_insert(&heap, &value->node);
}
struct heap_iterator it;
bool used_key[5];
memset((void *)used_key, 0, sizeof(used_key));
test_heap_iterator_init(&heap, &it);
test_node = NULL;
for (uint32_t i = 0; i < 4; ++i) {
test_node = test_heap_iterator_next(&it);
if (test_node == NULL)
fail("NULL returned from iterator",
"test_node == NULL");
value = (struct test_type *)((char *)test_node -
offsetof(struct test_type, node));
uint32_t val = value->val1;
if (val < 1 || val > 5)
fail("from iterator returned incorrect value",
"val < 1 || val > 5");
if (used_key[val])
fail("from iterator some value returned twice",
"used[val]");
used_key[val] = 1;
}
bool f = true;
for (uint32_t i = 1; i < 5; ++i)
f = used_key[i] && f;
if (!f)
fail("some node was skipped", "!f");
test_node = test_heap_iterator_next(&it);
if (test_node != NULL)
fail("after all iterator returns not NULL",
"test_node != NULL");
free_all_nodes(&heap);
footer();
}
static void
test_iterator_large()
{
header();
uint32_t const TEST_CASE_SIZE = 1000;
struct test_type *value, *root_value;
struct heap_node *test_node;
heap_t heap;
test_heap_create(&heap);
for (uint32_t i = TEST_CASE_SIZE; i > 0; --i) {
value = (struct test_type *)malloc(sizeof(struct test_type));
value->val1 = i;
test_heap_insert(&heap, &value->node);
}
struct heap_iterator it;
bool used_key[TEST_CASE_SIZE + 1];
memset((void *)used_key, 0, sizeof(used_key));
test_heap_iterator_init(&heap, &it);
test_node = NULL;
for (uint32_t i = 0; i < TEST_CASE_SIZE; ++i) {
test_node = test_heap_iterator_next(&it);
if (test_node == NULL)
fail("NULL returned from iterator",
"test_node == NULL");
value = (struct test_type *)((char *)test_node -
offsetof(struct test_type, node));
uint32_t val = value->val1;
if (val == 0 || val > TEST_CASE_SIZE)
fail("from iterator returned incorrect value",
"val < 0 || val > TEST_CASE_SIZE");
if (used_key[val])
fail("from iterator some value returned twice",
"used[val]");
used_key[val] = 1;
}
bool f = true;
for (uint32_t i = 1; i < TEST_CASE_SIZE; ++i) {
f = used_key[i] && f;
}
if (!f)
fail("some node was skipped", "!f");
test_node = test_heap_iterator_next(&it);
if (test_node != NULL)
fail("after all iterator returns not nil",
"test_node != NULL");
free_all_nodes(&heap);
footer();
}
int
main(int argc, const char** argv)
{
srand(179);
test_iterator_create();
test_iterator_empty();
test_iterator_small();
test_iterator_large();
}
|