diff options
Diffstat (limited to 'main/tests/test_oa_hash_map.cpp')
-rw-r--r-- | main/tests/test_oa_hash_map.cpp | 67 |
1 files changed, 63 insertions, 4 deletions
diff --git a/main/tests/test_oa_hash_map.cpp b/main/tests/test_oa_hash_map.cpp index cffec7fa77..719817baf4 100644 --- a/main/tests/test_oa_hash_map.cpp +++ b/main/tests/test_oa_hash_map.cpp @@ -35,8 +35,38 @@ namespace TestOAHashMap { -MainLoop *test() { +struct CountedItem { + static int count; + + int id = -1; + bool destroyed = false; + + CountedItem() { + count++; + } + CountedItem(int p_id) : + id(p_id) { + count++; + } + + CountedItem(const CountedItem &p_other) : + id(p_other.id) { + count++; + } + + CountedItem &operator=(const CountedItem &p_other) = default; + + ~CountedItem() { + CRASH_COND(destroyed); + count--; + destroyed = true; + } +}; + +int CountedItem::count; + +MainLoop *test() { OS::get_singleton()->print("\n\n\nHello from test\n"); // test element tracking. @@ -71,8 +101,9 @@ MainLoop *test() { uint32_t num_elems = 0; for (int i = 0; i < 500; i++) { int tmp; - if (map.lookup(i, tmp) && tmp == i * 2) + if (map.lookup(i, tmp) && tmp == i * 2) { num_elems++; + } } OS::get_singleton()->print("elements %d == %d.\n", map.get_num_elements(), num_elems); @@ -105,8 +136,9 @@ MainLoop *test() { keys[i] = Math::rand(); map.set(keys[i], dummy); - if (!map.lookup(keys[i], dummy)) + if (!map.lookup(keys[i], dummy)) { OS::get_singleton()->print("could not find 0x%X despite it was just inserted!\n", unsigned(keys[i])); + } } // check whether the keys are still present @@ -122,7 +154,6 @@ MainLoop *test() { // regression test / test for issue related to #31402 { - OS::get_singleton()->print("test for issue #31402 started...\n"); const int num_test_values = 12; @@ -152,6 +183,34 @@ MainLoop *test() { map.set(5, 1); } + // test memory management of items, should not crash or leak items + { + // Exercise different patterns of removal + for (int i = 0; i < 4; ++i) { + { + OAHashMap<String, CountedItem> map; + int id = 0; + for (int j = 0; j < 100; ++j) { + map.insert(itos(j), CountedItem(id)); + } + if (i <= 1) { + for (int j = 0; j < 100; ++j) { + map.remove(itos(j)); + } + } + if (i % 2 == 0) { + map.clear(); + } + } + + if (CountedItem::count != 0) { + OS::get_singleton()->print("%d != 0 (not performing the other test sub-cases, breaking...)\n", CountedItem::count); + break; + } + } + } + return nullptr; } + } // namespace TestOAHashMap |