DECLARE_TEST(objectmap, thread) { objectmap_t* map; thread_t thread[32]; size_t ith; size_t num_threads = math_clamp(system_hardware_threads() * 4, 4, 32); map = objectmap_allocate((num_threads + 1) * 512); for (ith = 0; ith < num_threads; ++ith) thread_initialize(&thread[ith], objectmap_thread, map, STRING_CONST("objectmap_thread"), THREAD_PRIORITY_NORMAL, 0); for (ith = 0; ith < num_threads; ++ith) thread_start(&thread[ith]); test_wait_for_threads_startup(thread, num_threads); test_wait_for_threads_finish(thread, num_threads); for (ith = 0; ith < num_threads; ++ith) EXPECT_EQ(thread[ith].result, 0); for (ith = 0; ith < num_threads; ++ith) thread_finalize(&thread[ith]); objectmap_deallocate(map); return 0; }
DECLARE_TEST(objectmap, initialize) { objectmap_t* map; map = objectmap_allocate(129); EXPECT_EQ(objectmap_size(map), 129); EXPECT_EQ(objectmap_lookup(map, 0), 0); EXPECT_EQ(objectmap_lookup(map, 1), 0); EXPECT_EQ(objectmap_raw_lookup(map, 0), 0); EXPECT_EQ(objectmap_raw_lookup(map, 1), 0); objectmap_deallocate(map); return 0; }
DECLARE_TEST(objectmap, store) { objectmap_t* map; object_base_t first; object_base_t second; object_base_t third; map = objectmap_allocate(129); memset(&first, 0, sizeof(first)); memset(&second, 0, sizeof(first)); memset(&third, 0, sizeof(first)); atomic_store32(&first.ref, 1); atomic_store32(&second.ref, 1); atomic_store32(&third.ref, 1); first.id = 1; second.id = 2; third.id = 3; EXPECT_EQ(objectmap_lookup(map, 0), 0); EXPECT_EQ(objectmap_lookup(map, 1), 0); first.id = objectmap_reserve(map); EXPECT_EQ(objectmap_lookup(map, first.id), 0); EXPECT_EQ(objectmap_raw_lookup(map, 0), 0); second.id = objectmap_reserve(map); EXPECT_EQ(objectmap_lookup(map, first.id), 0); EXPECT_EQ(objectmap_raw_lookup(map, 0), 0); EXPECT_EQ(objectmap_lookup(map, second.id), 0); EXPECT_EQ(objectmap_raw_lookup(map, 1), 0); objectmap_set(map, first.id, &first); EXPECT_EQ(objectmap_lookup(map, first.id), &first); EXPECT_EQ(objectmap_raw_lookup(map, 0), &first); EXPECT_EQ(objectmap_lookup(map, second.id), 0); EXPECT_EQ(objectmap_raw_lookup(map, 1), 0); objectmap_set(map, second.id, &second); EXPECT_EQ(objectmap_lookup(map, first.id), &first); EXPECT_EQ(objectmap_raw_lookup(map, 0), &first); EXPECT_EQ(objectmap_lookup(map, second.id), &second); EXPECT_EQ(objectmap_raw_lookup(map, 1), &second); objectmap_free(map, first.id); EXPECT_EQ(objectmap_lookup(map, first.id), 0); EXPECT_EQ(objectmap_raw_lookup(map, 0), 0); EXPECT_EQ(objectmap_lookup(map, second.id), &second); EXPECT_EQ(objectmap_raw_lookup(map, 1), &second); objectmap_free(map, first.id); EXPECT_EQ(objectmap_lookup(map, first.id), 0); EXPECT_EQ(objectmap_raw_lookup(map, 0), 0); EXPECT_EQ(objectmap_lookup(map, second.id), &second); EXPECT_EQ(objectmap_raw_lookup(map, 1), &second); objectmap_free(map, second.id); EXPECT_EQ(objectmap_lookup(map, first.id), 0); EXPECT_EQ(objectmap_raw_lookup(map, 0), 0); EXPECT_EQ(objectmap_lookup(map, second.id), 0); EXPECT_EQ(objectmap_raw_lookup(map, 1), 0); objectmap_deallocate(map); //Size should be clamped to three map = objectmap_allocate(1); EXPECT_EQ(objectmap_lookup(map, 0), 0); EXPECT_EQ(objectmap_lookup(map, 1), 0); first.id = objectmap_reserve(map); EXPECT_TYPENE(first.id, 0, object_t, PRIx64); EXPECT_EQ(objectmap_lookup(map, first.id), 0); EXPECT_EQ(objectmap_raw_lookup(map, 0), 0); second.id = objectmap_reserve(map); EXPECT_TYPENE(second.id, 0, object_t, PRIx64); EXPECT_EQ(objectmap_lookup(map, first.id), 0); EXPECT_EQ(objectmap_raw_lookup(map, 0), 0); EXPECT_EQ(objectmap_lookup(map, second.id), 0); EXPECT_EQ(objectmap_raw_lookup(map, 1), 0); third.id = objectmap_reserve(map); EXPECT_TYPENE(third.id, 0, object_t, PRIx64); EXPECT_EQ(objectmap_lookup(map, first.id), 0); EXPECT_EQ(objectmap_raw_lookup(map, 0), 0); EXPECT_EQ(objectmap_lookup(map, second.id), 0); EXPECT_EQ(objectmap_raw_lookup(map, 1), 0); EXPECT_EQ(objectmap_lookup(map, third.id), 0); EXPECT_EQ(objectmap_raw_lookup(map, 1), 0); objectmap_set(map, first.id, &first); objectmap_set(map, second.id, &second); objectmap_set(map, third.id, &third); log_enable_stdout(false); EXPECT_TYPEEQ(objectmap_reserve(map), 0, object_t, PRIx64); EXPECT_TYPEEQ(objectmap_reserve(map), 0, object_t, PRIx64); log_enable_stdout(true); objectmap_free(map, first.id); objectmap_free(map, second.id); //Leak one object //objectmap_free(map, third.id); EXPECT_EQ(objectmap_lookup_ref(map, first.id), nullptr); EXPECT_EQ(objectmap_raw_lookup(map, 0), 0); EXPECT_EQ(objectmap_lookup_ref(map, second.id), nullptr); EXPECT_EQ(objectmap_raw_lookup(map, 1), 0); EXPECT_NE(objectmap_lookup_ref(map, third.id), nullptr); EXPECT_NE(objectmap_raw_lookup(map, 2), 0); log_enable_stdout(false); objectmap_deallocate(map); log_enable_stdout(true); return 0; }
void render_buffer_finalize(void) { objectmap_deallocate(_render_map_buffer); _render_map_buffer = 0; }
void _library_shutdown( void ) { objectmap_deallocate( _library_map ); _library_map = 0; }