itzam_bool test_btree_thread(itzam_btree * btree, int maxkey, int test_size, int * completed) { itzam_state state = ITZAM_FAILED; int32_t key; int i; if (btree != NULL) { for (i = 0; i < test_size; ++i) { key = (int32_t)random_int32((int32_t)maxkey); if (ITZAM_OKAY != itzam_btree_insert(btree,(const void *)&key)) { // ignore error here, as the record may have been removed by another thread itzam_btree_remove(btree,(const void *)&key); } } state = ITZAM_OKAY; pthread_mutex_lock(&mutex); if (completed != NULL) (*completed)++; pthread_mutex_unlock(&mutex); } if (state == ITZAM_OKAY) return itzam_true; else return itzam_false; }
bool cave_generate_map(struct dm_map *map, struct random *r, enum dm_dungeon_type type, coord_t *ul, coord_t *dr) { FIX_UNUSED(type); /* initialise cellular automata */ coord_t size = { .x = dr->x - ul->x, .y = dr->y - ul->y, }; struct ca_map *cmap = ca_init(&size); /* fill the map randomly with floors */ coord_t p; for (p.y = 0; p.y < size.y; p.y++) { for (p.x = 0; p.x < size.x; p.x++) { ca_set_coord(cmap, &p, randpick(r, 45) ); } } /* Fill the map with large caves */ for (int i = 0; i < 4; i++) { ca_generation(cmap, 16, 8, 2); } /* Do a few passes to make them less smooth */ for (int i = 0; i < 2; i++) { int ri = (random_int32(r) % 4) +2; ca_generation(cmap, 5, ri, 1); } /* translate the ca_map to the real map */ for(p.y = 0; p.y < size.y; p.y++) { for(p.x = 0; p.x < size.x; p.x++) { coord_t c = cd_add(ul, &p); /* check if the cell is alive or dead */ if (ca_get_coord(cmap, &p) == CA_ALIVE) { /* fill the tile with the specified type */ if(random_int32(r)%100 < 1) { dm_get_map_me(&c,map)->tile = ts_get_tile_specific(TILE_ID_MAD_CAP_FUNGUS); } else dm_get_map_me(&c,map)->tile = ts_get_tile_specific(TILE_ID_CONCRETE_FLOOR); } } } /* cleanup and return */ ca_free(cmap); return true; }
itzam_bool test_btree_befaft() { itzam_bool result = false, bsuccess = false, asuccess = false; int n; const uint16_t order = 5; // small, so we get lots of tree changes time_t start; int32_t rec, before, after; char * filename = "relative.itz"; itzam_btree btree; itzam_state state; // banner for this test printf("\nTesting Itzam/C b-tree indexes with new \"nearby\" search functions\n"); // create an empty database file state = itzam_btree_create(&btree, filename, order, sizeof(int32_t), itzam_comparator_int32, error_handler); if (state != ITZAM_OKAY) { printf("uable to create index file\n"); return false; } // fill the btree with a known sequence int32_t largest = -1; int32_t x = -1; for (int n = 0; n < 100; ++n) { x += random_int32(3) + 1; largest = x; state = itzam_btree_insert(&btree, &x); if (state != ITZAM_OKAY) { printf("could not add %d to index file\n",n); return false; } } for (int n = -1; n < largest + 1; ++n) { before = -1; after = -1; // look before bsuccess = itzam_btree_find_before(&btree, &n, &before); // look after asuccess = itzam_btree_find_after(&btree, &n, &after); // print before (target) after printf("%8d (%s) <= %8d <= %8d (%s)\n", before, bsuccess ? "Y" : "N", n, after, asuccess ? "Y" : "N"); } // done return itzam_true; }
bool test_btree_cursor(int testn) { bool result = false, bsuccess = false, asuccess = false; int n; const uint16_t order = 5; // small, so we get lots of tree changes time_t start; int32_t rec, before, after; char * filename = "relative.itz"; itzam_btree btree; itzam_state state; if (testn < 0) testn = 50; // create an empty database file state = itzam_btree_create(&btree, filename, order, sizeof(int32_t), itzam_comparator_int32, error_handler); if (state != ITZAM_OKAY) { printf("uable to create index file\n"); return false; } // fill the btree with a known sequence int32_t largest = -1; int32_t x = -1; for (int n = 0; n < testn; ++n) { x += random_int32(3) + 1; largest = x; state = itzam_btree_insert(&btree, &x); if (state != ITZAM_OKAY) { printf("could not add %d to index file\n",n); return false; } } itzam_btree_analyze(&btree, stdout); itzam_btree_dump_btree(&btree); itzam_btree_cursor cursor; // forward tests printf("\nforward tests\n"); for (int target = 0; target < largest + 1; ++ target) { printf ("t = %3d: ", target); itzam_btree_cursor_create_at(&cursor, &btree, &target, false); do { // get the key pointed to by the cursor state = itzam_btree_cursor_read(&cursor,(void *)&rec); if (state == ITZAM_OKAY) { printf("%d, ",rec); fflush(stdout); } else not_okay(state); } while (itzam_btree_cursor_next(&cursor)); printf("\b\b\n"); itzam_btree_cursor_free(&cursor); } // backward tests printf("\nbackward tests\n"); for (int target = 0; target < largest + 1; ++target) { printf ("t = %3d: ", target); itzam_btree_cursor_create_at(&cursor, &btree, &target, false); do { // get the key pointed to by the cursor state = itzam_btree_cursor_read(&cursor,(void *)&rec); if (state == ITZAM_OKAY) { printf("%d, ",rec); fflush(stdout); } else not_okay(state); } while (itzam_btree_cursor_prev(&cursor)); printf("\b\b\n"); itzam_btree_cursor_free(&cursor); } // done return true; }