static void test(void) { const Key list[] = { {12, {0x20, 0x10, 0x00, 0x00}}, {12, {0x20, 0x20, 0x00, 0x00}}, {13, {0x20, 0x18, 0x00, 0x00}} }; const int n = sizeof(list) / sizeof(list[0]); CritbitPos pos; Critbit tree; Tuple *t; int ret; int i, j; critbit_init(&tree); for (i = 0; i < n; i++) { t = critbit_lookup(&tree, list[i].oct, list[i].nbits, &pos); assert(t == NULL); t = malloc(sizeof(Tuple)); assert(t != NULL); printf("cbit: %d\n", pos.bit); t->key = list[i]; t->val = i; ret = critbit_insert(&tree, t, &pos); assert(ret == 0); printf("===> Insert %d\n", i); printf("nums: %d\n", tree.nums); tree_dump(tree.root, 0); } for (i = 0; i < n; i++) { t = critbit_lookup(&tree, list[i].oct, list[i].nbits, NULL); assert(t != NULL); printf("get key:"); for (j = 0; j < 4; j++) { printf(" %02x", t->key.oct[j]); } printf("/%d\n", t->key.nbits); printf("get val: %d\n", t->val); assert(memcmp(&t->key, &list[i], sizeof(t->key)) == 0); } }
static void test(void) { const char *list[] = { "10.0.0.1", "10.0.0.2", "11.0.0.1" }; const int n = sizeof(list) / sizeof(list[0]); struct in_addr addr; CritbitPos pos; Critbit tree; Tuple *t; int ret; int i; critbit_init(&tree); for (i = 0; i < n; i++) { inet_pton(AF_INET, list[i], &addr); t = critbit_lookup(&tree, &addr, 32, &pos); assert(t == NULL); t = malloc(sizeof(Tuple)); assert(t != NULL); t->key.mask = 32; t->key.addr = addr; t->val = i; ret = critbit_insert(&tree, t, &pos); assert(ret == 0); printf("===> Insert %d\n", i); printf("nums: %d\n", tree.nums); tree_dump(tree.root, 0); } }
int register_fblock_type(struct fblock_factory *fops) { return critbit_insert(&fbmap, fops->type); }