diff options
Diffstat (limited to 'tests/t_vector.c')
| -rw-r--r-- | tests/t_vector.c | 308 |
1 files changed, 308 insertions, 0 deletions
diff --git a/tests/t_vector.c b/tests/t_vector.c new file mode 100644 index 0000000..42ce31a --- /dev/null +++ b/tests/t_vector.c | |||
| @@ -0,0 +1,308 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. */ | ||
| 3 | |||
| 4 | /* System */ | ||
| 5 | #include <stdint.h> | ||
| 6 | #include <stdlib.h> | ||
| 7 | #include <string.h> | ||
| 8 | |||
| 9 | /* Opentracker */ | ||
| 10 | #include "ot_vector.h" | ||
| 11 | #include "trackerlogic.h" | ||
| 12 | #include "test.h" | ||
| 13 | |||
| 14 | /* vector_binary_search returns the matching member when there is one, and otherwise | ||
| 15 | the position at which the key would have to be inserted to keep the vector | ||
| 16 | sorted. exactmatch distinguishes the two. */ | ||
| 17 | |||
| 18 | static const unsigned char sorted[5][4] = {{0, 0, 0, 1}, {0, 0, 0, 3}, {0, 0, 0, 5}, {0, 0, 0, 7}, {0, 0, 0, 9}}; | ||
| 19 | |||
| 20 | static void case_search_finds_member(void) { | ||
| 21 | const unsigned char key[4] = {0, 0, 0, 5}; | ||
| 22 | int exactmatch; | ||
| 23 | void *found; | ||
| 24 | |||
| 25 | test_case("an existing key is found"); | ||
| 26 | found = vector_binary_search(key, sorted, 5, 4, 4, &exactmatch); | ||
| 27 | CHECK(exactmatch != 0); | ||
| 28 | CHECK_PTR(found, sorted[2]); | ||
| 29 | } | ||
| 30 | |||
| 31 | static void case_search_finds_first_and_last(void) { | ||
| 32 | const unsigned char first[4] = {0, 0, 0, 1}; | ||
| 33 | const unsigned char last[4] = {0, 0, 0, 9}; | ||
| 34 | int exactmatch; | ||
| 35 | |||
| 36 | test_case("the first and last members are found"); | ||
| 37 | CHECK_PTR(vector_binary_search(first, sorted, 5, 4, 4, &exactmatch), sorted[0]); | ||
| 38 | CHECK(exactmatch != 0); | ||
| 39 | CHECK_PTR(vector_binary_search(last, sorted, 5, 4, 4, &exactmatch), sorted[4]); | ||
| 40 | CHECK(exactmatch != 0); | ||
| 41 | } | ||
| 42 | |||
| 43 | static void case_search_insert_position(void) { | ||
| 44 | const unsigned char middle[4] = {0, 0, 0, 4}; | ||
| 45 | const unsigned char before[4] = {0, 0, 0, 0}; | ||
| 46 | int exactmatch; | ||
| 47 | void *slot; | ||
| 48 | |||
| 49 | test_case("a missing key yields its insert position"); | ||
| 50 | slot = vector_binary_search(middle, sorted, 5, 4, 4, &exactmatch); | ||
| 51 | CHECK_INT(exactmatch, 0); | ||
| 52 | CHECK_PTR(slot, sorted[2]); | ||
| 53 | |||
| 54 | slot = vector_binary_search(before, sorted, 5, 4, 4, &exactmatch); | ||
| 55 | CHECK_INT(exactmatch, 0); | ||
| 56 | CHECK_PTR(slot, sorted[0]); | ||
| 57 | } | ||
| 58 | |||
| 59 | static void case_search_past_the_end(void) { | ||
| 60 | const unsigned char beyond[4] = {0, 0, 0, 0xff}; | ||
| 61 | int exactmatch; | ||
| 62 | void *slot; | ||
| 63 | |||
| 64 | test_case("a key above every member yields the position past the end"); | ||
| 65 | slot = vector_binary_search(beyond, sorted, 5, 4, 4, &exactmatch); | ||
| 66 | CHECK_INT(exactmatch, 0); | ||
| 67 | CHECK_PTR(slot, sorted[5]); | ||
| 68 | } | ||
| 69 | |||
| 70 | static void case_search_empty(void) { | ||
| 71 | int exactmatch; | ||
| 72 | void *base = (void *)sorted; | ||
| 73 | |||
| 74 | test_case("searching an empty vector reports no match at the base"); | ||
| 75 | CHECK_PTR(vector_binary_search(sorted[0], base, 0, 4, 4, &exactmatch), base); | ||
| 76 | CHECK_INT(exactmatch, 0); | ||
| 77 | } | ||
| 78 | |||
| 79 | static void case_insert_keeps_order(void) { | ||
| 80 | ot_vector vector = {NULL, 0, 0}; | ||
| 81 | const unsigned char five[4] = {0, 0, 0, 5}; | ||
| 82 | const unsigned char one[4] = {0, 0, 0, 1}; | ||
| 83 | const unsigned char nine[4] = {0, 0, 0, 9}; | ||
| 84 | int exactmatch; | ||
| 85 | void *slot; | ||
| 86 | |||
| 87 | test_case("inserts land in sorted order and grow the vector"); | ||
| 88 | slot = vector_find_or_insert(&vector, (void *)five, 4, 4, &exactmatch); | ||
| 89 | CHECK(slot != NULL); | ||
| 90 | CHECK_INT(exactmatch, 0); | ||
| 91 | memcpy(slot, five, 4); | ||
| 92 | CHECK_INT(vector.size, 1); | ||
| 93 | CHECK_INT(vector.space, OT_VECTOR_MIN_MEMBERS); | ||
| 94 | |||
| 95 | slot = vector_find_or_insert(&vector, (void *)one, 4, 4, &exactmatch); | ||
| 96 | CHECK(slot != NULL); | ||
| 97 | CHECK_INT(exactmatch, 0); | ||
| 98 | memcpy(slot, one, 4); | ||
| 99 | CHECK_INT(vector.size, 2); | ||
| 100 | |||
| 101 | slot = vector_find_or_insert(&vector, (void *)nine, 4, 4, &exactmatch); | ||
| 102 | CHECK(slot != NULL); | ||
| 103 | CHECK_INT(exactmatch, 0); | ||
| 104 | memcpy(slot, nine, 4); | ||
| 105 | CHECK_INT(vector.size, 3); | ||
| 106 | CHECK_INT(vector.space, OT_VECTOR_MIN_MEMBERS * OT_VECTOR_GROW_RATIO); | ||
| 107 | |||
| 108 | CHECK_MEM(vector.data, "\0\0\0\1\0\0\0\5\0\0\0\11", 12); | ||
| 109 | |||
| 110 | free(vector.data); | ||
| 111 | } | ||
| 112 | |||
| 113 | /* The peer variant is reached with an empty vector on every first announce | ||
| 114 | for a torrent, and again whenever a swarm empties and refills, because | ||
| 115 | vector_fixup_peers drops the allocation and nulls the data pointer. */ | ||
| 116 | |||
| 117 | static void case_insert_peer_into_empty_vector(void) { | ||
| 118 | ot_vector vector = {NULL, 0, 0}; | ||
| 119 | ot_peer6 low, high; | ||
| 120 | int exactmatch; | ||
| 121 | ot_peer *slot; | ||
| 122 | |||
| 123 | memset(low, 0x11, sizeof(low)); | ||
| 124 | memset(high, 0x99, sizeof(high)); | ||
| 125 | |||
| 126 | test_case("the first peer goes into a vector that has no allocation yet"); | ||
| 127 | slot = vector_find_or_insert_peer(&vector, low, OT_PEER_SIZE6, &exactmatch); | ||
| 128 | CHECK(slot != NULL); | ||
| 129 | CHECK_INT(exactmatch, 0); | ||
| 130 | memcpy(slot, low, OT_PEER_SIZE6); | ||
| 131 | CHECK_INT(vector.size, 1); | ||
| 132 | CHECK_INT(vector.space, OT_VECTOR_MIN_MEMBERS); | ||
| 133 | CHECK_PTR(slot, vector.data); | ||
| 134 | |||
| 135 | test_case("a second peer keeps the order"); | ||
| 136 | slot = vector_find_or_insert_peer(&vector, high, OT_PEER_SIZE6, &exactmatch); | ||
| 137 | CHECK(slot != NULL); | ||
| 138 | CHECK_INT(exactmatch, 0); | ||
| 139 | memcpy(slot, high, OT_PEER_SIZE6); | ||
| 140 | CHECK_INT(vector.size, 2); | ||
| 141 | CHECK_MEM((char *)vector.data, low, OT_PEER_COMPARE_SIZE6); | ||
| 142 | CHECK_MEM((char *)vector.data + OT_PEER_SIZE6, high, OT_PEER_COMPARE_SIZE6); | ||
| 143 | |||
| 144 | test_case("and emptying the vector hands the allocation back"); | ||
| 145 | CHECK(vector_remove_peer(&vector, low, OT_PEER_SIZE6) != 0); | ||
| 146 | CHECK(vector_remove_peer(&vector, high, OT_PEER_SIZE6) != 0); | ||
| 147 | CHECK_INT(vector.size, 0); | ||
| 148 | CHECK_PTR(vector.data, NULL); | ||
| 149 | |||
| 150 | test_case("so the next peer starts from no allocation again"); | ||
| 151 | slot = vector_find_or_insert_peer(&vector, low, OT_PEER_SIZE6, &exactmatch); | ||
| 152 | CHECK(slot != NULL); | ||
| 153 | CHECK_INT(vector.size, 1); | ||
| 154 | |||
| 155 | free(vector.data); | ||
| 156 | } | ||
| 157 | |||
| 158 | static void case_insert_finds_existing(void) { | ||
| 159 | ot_vector vector = {NULL, 0, 0}; | ||
| 160 | const unsigned char five[4] = {0, 0, 0, 5}; | ||
| 161 | int exactmatch; | ||
| 162 | void *slot, *again; | ||
| 163 | |||
| 164 | test_case("a second insert of the same key returns the existing member"); | ||
| 165 | slot = vector_find_or_insert(&vector, (void *)five, 4, 4, &exactmatch); | ||
| 166 | CHECK(slot != NULL); | ||
| 167 | memcpy(slot, five, 4); | ||
| 168 | |||
| 169 | again = vector_find_or_insert(&vector, (void *)five, 4, 4, &exactmatch); | ||
| 170 | CHECK(exactmatch != 0); | ||
| 171 | CHECK_PTR(again, vector.data); | ||
| 172 | CHECK_INT(vector.size, 1); | ||
| 173 | |||
| 174 | free(vector.data); | ||
| 175 | } | ||
| 176 | |||
| 177 | static void case_insert_compare_prefix(void) { | ||
| 178 | ot_vector vector = {NULL, 0, 0}; | ||
| 179 | unsigned char first[6] = {1, 2, 3, 0xaa, 0xaa, 0xaa}; | ||
| 180 | unsigned char second[6] = {1, 2, 3, 0xbb, 0xbb, 0xbb}; | ||
| 181 | int exactmatch; | ||
| 182 | void *slot; | ||
| 183 | |||
| 184 | test_case("only the compare prefix decides identity"); | ||
| 185 | slot = vector_find_or_insert(&vector, first, 6, 3, &exactmatch); | ||
| 186 | CHECK(slot != NULL); | ||
| 187 | memcpy(slot, first, 6); | ||
| 188 | |||
| 189 | slot = vector_find_or_insert(&vector, second, 6, 3, &exactmatch); | ||
| 190 | CHECK(exactmatch != 0); | ||
| 191 | CHECK_INT(vector.size, 1); | ||
| 192 | CHECK_MEM(slot, first, 6); | ||
| 193 | |||
| 194 | free(vector.data); | ||
| 195 | } | ||
| 196 | |||
| 197 | /* ------------------------------------------------- bucket redistribution */ | ||
| 198 | |||
| 199 | /* A flat peer list of count distinct v4 peers. The low byte of the index goes | ||
| 200 | into the first address byte, so the peers are distinct but arrive in no | ||
| 201 | particular order: a list generated in ascending order would come back | ||
| 202 | sorted whether or not vector_redistribute_buckets sorted it. */ | ||
| 203 | static void build_flat_vector(ot_vector *peers, size_t count) { | ||
| 204 | size_t i; | ||
| 205 | |||
| 206 | memset(peers, 0, sizeof(*peers)); | ||
| 207 | peers->data = malloc(count * OT_PEER_SIZE4); | ||
| 208 | peers->size = count; | ||
| 209 | peers->space = count; | ||
| 210 | |||
| 211 | for (i = 0; i < count; ++i) { | ||
| 212 | uint8_t *peer = (uint8_t *)peers->data + i * OT_PEER_SIZE4; | ||
| 213 | |||
| 214 | memset(peer, 0, OT_PEER_SIZE4); | ||
| 215 | peer[0] = (uint8_t)i; | ||
| 216 | peer[1] = (uint8_t)(i >> 8); | ||
| 217 | peer[2] = (uint8_t)(i >> 16); | ||
| 218 | peer[3] = 1; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | static void case_redistribute_holds_every_peer(void) { | ||
| 223 | const size_t count = 513; | ||
| 224 | ot_vector peers; | ||
| 225 | size_t bucket, total_size = 0, empty = 0, largest = 0; | ||
| 226 | ot_vector *buckets; | ||
| 227 | |||
| 228 | test_case("a peer list past the threshold is split into buckets"); | ||
| 229 | build_flat_vector(&peers, count); | ||
| 230 | CHECK(peers.data != NULL); | ||
| 231 | vector_redistribute_buckets(&peers, OT_PEER_SIZE4, count); | ||
| 232 | CHECK_INT(OT_VECTOR_HASBUCKETS(&peers), 1); | ||
| 233 | CHECK_INT(peers.size, 16); | ||
| 234 | |||
| 235 | buckets = peers.data; | ||
| 236 | for (bucket = 0; bucket < peers.size; ++bucket) { | ||
| 237 | total_size += buckets[bucket].size; | ||
| 238 | if (!buckets[bucket].size) | ||
| 239 | ++empty; | ||
| 240 | if (buckets[bucket].size > largest) | ||
| 241 | largest = buckets[bucket].size; | ||
| 242 | } | ||
| 243 | |||
| 244 | test_case("no peer is lost on the way"); | ||
| 245 | CHECK_INT(total_size, count); | ||
| 246 | |||
| 247 | /* The point of the exercise is that a lookup only searches one bucket, so | ||
| 248 | a hash that piled everything into one would defeat it. The input and the | ||
| 249 | hash are both fixed, so this is not a matter of luck. */ | ||
| 250 | test_case("and they are spread over the buckets rather than piled up"); | ||
| 251 | CHECK_INT(empty, 0); | ||
| 252 | CHECK(largest < count / 2); | ||
| 253 | |||
| 254 | vector_clean_list(peers.data, peers.size); | ||
| 255 | } | ||
| 256 | |||
| 257 | static void case_redistribute_sorts_each_bucket(void) { | ||
| 258 | const size_t count = 1000; | ||
| 259 | ot_vector peers; | ||
| 260 | size_t bucket, i, out_of_order = 0; | ||
| 261 | ot_vector *buckets; | ||
| 262 | |||
| 263 | test_case("every bucket comes back sorted, so bsearch can be used on it"); | ||
| 264 | build_flat_vector(&peers, count); | ||
| 265 | CHECK(peers.data != NULL); | ||
| 266 | vector_redistribute_buckets(&peers, OT_PEER_SIZE4, count); | ||
| 267 | CHECK_INT(OT_VECTOR_HASBUCKETS(&peers), 1); | ||
| 268 | |||
| 269 | buckets = peers.data; | ||
| 270 | for (bucket = 0; bucket < peers.size; ++bucket) | ||
| 271 | for (i = 1; i < buckets[bucket].size; ++i) { | ||
| 272 | const uint8_t *prev = (uint8_t *)buckets[bucket].data + (i - 1) * OT_PEER_SIZE4; | ||
| 273 | const uint8_t *next = (uint8_t *)buckets[bucket].data + i * OT_PEER_SIZE4; | ||
| 274 | |||
| 275 | if (memcmp(prev, next, OT_PEER_COMPARE_SIZE4) >= 0) | ||
| 276 | ++out_of_order; | ||
| 277 | } | ||
| 278 | CHECK_INT(out_of_order, 0); | ||
| 279 | |||
| 280 | vector_clean_list(peers.data, peers.size); | ||
| 281 | } | ||
| 282 | |||
| 283 | static void case_redistribute_below_threshold(void) { | ||
| 284 | ot_vector peers; | ||
| 285 | |||
| 286 | test_case("a small peer list is left flat"); | ||
| 287 | build_flat_vector(&peers, 100); | ||
| 288 | CHECK(peers.data != NULL); | ||
| 289 | vector_redistribute_buckets(&peers, OT_PEER_SIZE4, 100); | ||
| 290 | CHECK_INT(OT_VECTOR_HASBUCKETS(&peers), 0); | ||
| 291 | CHECK_INT(peers.size, 100); | ||
| 292 | free(peers.data); | ||
| 293 | } | ||
| 294 | |||
| 295 | void test_vector(void) { | ||
| 296 | case_search_finds_member(); | ||
| 297 | case_search_finds_first_and_last(); | ||
| 298 | case_search_insert_position(); | ||
| 299 | case_search_past_the_end(); | ||
| 300 | case_search_empty(); | ||
| 301 | case_insert_keeps_order(); | ||
| 302 | case_insert_peer_into_empty_vector(); | ||
| 303 | case_insert_finds_existing(); | ||
| 304 | case_insert_compare_prefix(); | ||
| 305 | case_redistribute_holds_every_peer(); | ||
| 306 | case_redistribute_sorts_each_bucket(); | ||
| 307 | case_redistribute_below_threshold(); | ||
| 308 | } | ||
