summaryrefslogtreecommitdiff
path: root/main/tests
diff options
context:
space:
mode:
authorPedro J. Estébanez <pedrojrulez@gmail.com>2020-05-15 00:40:28 +0200
committerPedro J. Estébanez <pedrojrulez@gmail.com>2020-05-18 14:02:52 +0200
commitb720a01849cd768890dd569325f89731b2cdd333 (patch)
treeda425cf0c98b0f76c9bc5bb442ac76541b680aae /main/tests
parent163687d17a8a11da3cf1a3595c511a5f8fc94571 (diff)
Fix leaks and crashes in OAHashMap
This changes the way the lifespan of items is managed to be consistent. Bonus: Simplify cases of destroy-then-emplace.
Diffstat (limited to 'main/tests')
-rw-r--r--main/tests/test_oa_hash_map.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/main/tests/test_oa_hash_map.cpp b/main/tests/test_oa_hash_map.cpp
index cae143bb5d..719817baf4 100644
--- a/main/tests/test_oa_hash_map.cpp
+++ b/main/tests/test_oa_hash_map.cpp
@@ -35,6 +35,37 @@
namespace TestOAHashMap {
+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");
@@ -152,6 +183,33 @@ 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;
}