Loading...
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | #import <Foundation/Foundation.h> #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <stdbool.h> #include <string.h> #include <radix_tree.h> #include <radix_tree_internal.h> #include <darwintest.h> #include "../src/radix_tree_debug.c" bool failed = false; #if 0 #define ASSERT(x) \ if (!(x)) { \ abort(); \ } #define ASSERT_EQ(x, y) \ if ((x) != (y)) { \ abort(); \ } #else #define ASSERT(x) \ if (!(x)) { \ failed = true; \ } #define ASSERT_EQ(x, y) \ if ((x) != (y)) { \ failed = true; \ } #endif static int log2i(uint64_t x) { if (x == 1) { return 0; } if (x & 1) { abort(); } return 1 + log2(x >> 1); } T_DECL(radix_tree_test, "radix_tree_test") { bool ok; // size_t size = 100 * 4096; // void *buf = malloc(size); // memset(buf, 0xde, size); // struct radix_tree *tree = radix_tree_init(buf, size); struct radix_tree *tree = radix_tree_create(); ASSERT(tree); const int n = 999; uint64_t minsize = 4096; for (uint64_t i = 0; i < n * minsize; i += minsize) { ok = radix_tree_insert(&tree, i, minsize, 3 * i); ASSERT(ok); /* printf("@@@@@@@@@@@--------\n"); */ /* radix_tree_print(tree); */ /* printf("@@@@@@@@@@---------\n"); */ for (uint64_t j = 0; j < n * minsize; j += minsize) { // printf ("testing %lld %lld\n", i, j); if (j <= i) { ASSERT(radix_tree_lookup(tree, j) == 3 * j); } else { ASSERT(radix_tree_lookup(tree, j) == radix_tree_invalid_value); } } } T_EXPECT_FALSE(failed, "insert 1 to n"); for (uint64_t i = 0; i < n * minsize; i += minsize) { ok = radix_tree_delete(&tree, i, minsize); ASSERT(ok); for (uint64_t j = 0; j < n * minsize; j += minsize) { if (j > i) { ASSERT(radix_tree_lookup(tree, j) == 3 * j); } else { ASSERT(radix_tree_lookup(tree, j) == radix_tree_invalid_value); } } } T_EXPECT_FALSE(failed, "delete 1 to n"); for (uint64_t i = 0; i < n * minsize; i += minsize) { ok = radix_tree_insert(&tree, i, minsize, 3 * i); ASSERT(ok); } for (uint64_t i = n * minsize; i > 0;) { i -= minsize; // printf("@@@@@@@@@@@--------\n"); // radix_tree_print(tree); // printf("@@@@@@@@@@---------\n"); ok = radix_tree_delete(&tree, i, minsize); ASSERT(ok); for (uint64_t j = 0; j < n * minsize; j += minsize) { if (j < i) { ASSERT_EQ(radix_tree_lookup(tree, j), 3 * j); } else { ASSERT_EQ(radix_tree_lookup(tree, j), radix_tree_invalid_value); } } } T_EXPECT_FALSE(failed, "delete n to 1"); srand(12345); NSMutableDictionary *d = [[NSMutableDictionary alloc] init]; for (uint64_t i = 0; i < n; i++) { @autoreleasepool { for (int j = 0; j < 3; j++) { uint64_t key = minsize * (rand() + ((uint64_t)rand() << 32)); uint64_t value = rand(); // printf("@@@@@@@@@@@--------\n"); // radix_tree_print(tree); // printf("@@@@@@@@@@---------inserting %llx\n", key); ok = radix_tree_insert(&tree, key, minsize, value); // printf("@@@@@@@@@@---------ok\n"); ASSERT(ok); d[@(key)] = @(value); } NSArray *array = [d allKeys]; id k = [array objectAtIndex:rand() % [array count]]; ok = radix_tree_delete(&tree, [k unsignedLongLongValue], minsize); ASSERT(ok); [d removeObjectForKey:k]; for (id k in d) { ASSERT_EQ(radix_tree_lookup(tree, [k unsignedLongLongValue]), [d[k] unsignedLongLongValue]); } uint64_t count = radix_tree_count(tree); // for (id k in d) { // printf("key %llx -> %llx\n", [k unsignedLongLongValue], [d[k] unsignedLongLongValue]); // } // radix_tree_print(tree); ASSERT_EQ((long)(count % minsize), 0l); ASSERT_EQ([[d allKeys] count], (long)(count / minsize)); } } T_EXPECT_FALSE(failed, "random"); for (id k in d) { radix_tree_delete(&tree, [k unsignedLongLongValue], minsize); } T_EXPECT_EQ_ULLONG(0ull, radix_tree_count(tree), "delete randoms"); ASSERT_EQ(radix_tree_lookup(tree, 0), -1); ASSERT_EQ(radix_tree_lookup(tree, minsize - 1), -1); ASSERT_EQ(radix_tree_lookup(tree, minsize), -1); ok = radix_tree_insert(&tree, 0, minsize, 0xf00); ASSERT(ok); ASSERT_EQ(radix_tree_lookup(tree, 0), 0xf00); ASSERT_EQ(radix_tree_lookup(tree, minsize - 1), 0xf00); ASSERT_EQ(radix_tree_lookup(tree, minsize), -1); // this would abort: // ok = radix_tree_insert(&tree, -minsize, minsize, 0xb00); // ASSERT(!ok); ok = radix_tree_insert(&tree, -2 * minsize, minsize, 0xb00); ASSERT(ok); ASSERT_EQ(radix_tree_lookup(tree, -2 * minsize), 0xb00); ASSERT_EQ(radix_tree_lookup(tree, -2 * minsize - 1), -1); ASSERT_EQ(radix_tree_lookup(tree, -2 * minsize + 1), 0xb00); ASSERT_EQ(radix_tree_lookup(tree, -2 * minsize + minsize - 1), 0xb00); ASSERT_EQ(radix_tree_lookup(tree, -1 * minsize), -1); ASSERT_EQ(radix_tree_lookup(tree, minsize), -1); radix_tree_delete(&tree, -2 * minsize, minsize); radix_tree_delete(&tree, 0, minsize); ASSERT_EQ(radix_tree_count(tree), 0); T_EXPECT_FALSE(failed, "off by 1"); int modelsize = 1024; uint32_t model[modelsize]; while (log2i(modelsize) + log2i(minsize) <= 64) { memset(model, 0xff, sizeof(model)); for (int i = 0; i < n; i++) { uint64_t start = rand() % modelsize; if (start == modelsize - 1 && log2i(modelsize) + log2i(minsize) == 64) { start = modelsize - 2; } uint64_t maxsize = modelsize - start; uint64_t size; if (maxsize / 4 > 1) { size = (rand() % (maxsize / 4)) + (rand() % (maxsize / 4)) + (rand() % (maxsize / 4)) + (rand() % (maxsize / 4)); } else if (maxsize > 1) { size = rand() % maxsize; } else { size = 1; } if (size == 0) { size = 1; } if (minsize * start + minsize * size < minsize * start) { size--; } if (minsize * start + minsize * size < minsize * start) { abort(); } if (size == 0) { abort(); } uint32_t value = rand(); // printf("inserting %llx, %llx\n", start*minsize, size*minsize); // radix_tree_print(tree); ok = radix_tree_insert(&tree, start * minsize, size * minsize, value); ASSERT(ok); // radix_tree_print(tree); ASSERT(radix_tree_fsck(tree)); for (uint64_t i = start; i < start + size; i++) { model[i] = value; } for (uint64_t j = 0; j < modelsize; j++) { uint64_t expected; if (model[j] == (uint32_t)-1) { expected = (uint64_t)-1; } else { expected = model[j]; } // radix_tree_print(tree); uint64_t ans = radix_tree_lookup(tree, j * minsize); // printf("j*minsize=%llx expected=%llx ans=%llx\n", j*minsize, expected, ans); ASSERT_EQ(expected, ans); } } T_EXPECT_FALSE(failed, "model %d", log2i(minsize) + log2i(modelsize)); radix_tree_delete(&tree, 0, -1); T_EXPECT_EQ_ULLONG(0ull, radix_tree_count(tree), "delete model"); minsize *= 2; tree->leaf_size_shift++; } } T_DECL(radix_tree_holes, "radix_tree_holes_test") { bool ok; struct radix_tree *tree = radix_tree_create(); T_ASSERT_NOTNULL(tree, "radix_tree_create()"); uint64_t size = 0xff00000; uint64_t minsize = 0x1000; uint64_t start = 0x10303c000; ok = radix_tree_insert(&tree, start, size, 0xf00ba); T_ASSERT_TRUE(ok, "created region"); ok = radix_tree_fsck(tree); T_QUIET; T_ASSERT_TRUE(ok, "fsck"); for (uint64_t addr = start; addr < start + size; addr += minsize) { T_QUIET; T_ASSERT_EQ_ULLONG(radix_tree_lookup(tree, addr), 0xf00ball, "stackid"); uint64_t index = (addr - start) / minsize; if (index % 2) { ok = radix_tree_delete(&tree, addr, minsize); T_QUIET; T_ASSERT_TRUE(ok, "deleted odd %lld", index); } } for (uint64_t addr = start; addr < start + size; addr += minsize) { uint64_t index = (addr - start) / minsize; T_QUIET; T_ASSERT_EQ_ULLONG(radix_tree_lookup(tree, addr), index % 2 ? -1 : 0xf00ball, "stackid"); if (!(index % 2)) { ok = radix_tree_delete(&tree, addr, minsize); T_QUIET; T_ASSERT_TRUE(ok, "deleted even, %lld", index); } } ok = radix_tree_fsck(tree); T_ASSERT_TRUE(ok, "fsck"); } |