2017-08-13 10:32:39 +02:00
|
|
|
/**************************************************************************/
|
2022-05-08 10:09:19 +02:00
|
|
|
/* test_hash_map.h */
|
2017-08-13 10:32:39 +02:00
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2022-05-08 10:09:19 +02:00
|
|
|
#ifndef TEST_HASH_MAP_H
|
|
|
|
#define TEST_HASH_MAP_H
|
2017-08-13 10:32:39 +02:00
|
|
|
|
2022-05-08 10:09:19 +02:00
|
|
|
#include "core/templates/hash_map.h"
|
2020-08-08 07:08:05 +02:00
|
|
|
|
|
|
|
#include "tests/test_macros.h"
|
2020-03-24 09:50:51 +01:00
|
|
|
|
2022-05-08 10:09:19 +02:00
|
|
|
namespace TestHashMap {
|
2017-08-13 10:32:39 +02:00
|
|
|
|
2022-05-08 10:09:19 +02:00
|
|
|
TEST_CASE("[HashMap] Insert element") {
|
|
|
|
HashMap<int, int> map;
|
|
|
|
HashMap<int, int>::Iterator e = map.insert(42, 84);
|
2020-08-08 07:08:05 +02:00
|
|
|
|
|
|
|
CHECK(e);
|
2022-05-08 10:09:19 +02:00
|
|
|
CHECK(e->key == 42);
|
|
|
|
CHECK(e->value == 84);
|
2020-08-08 07:08:05 +02:00
|
|
|
CHECK(map[42] == 84);
|
|
|
|
CHECK(map.has(42));
|
|
|
|
CHECK(map.find(42));
|
|
|
|
}
|
|
|
|
|
2022-05-08 10:09:19 +02:00
|
|
|
TEST_CASE("[HashMap] Overwrite element") {
|
|
|
|
HashMap<int, int> map;
|
2020-08-08 07:08:05 +02:00
|
|
|
map.insert(42, 84);
|
|
|
|
map.insert(42, 1234);
|
|
|
|
|
|
|
|
CHECK(map[42] == 1234);
|
|
|
|
}
|
|
|
|
|
2022-05-08 10:09:19 +02:00
|
|
|
TEST_CASE("[HashMap] Erase via element") {
|
|
|
|
HashMap<int, int> map;
|
|
|
|
HashMap<int, int>::Iterator e = map.insert(42, 84);
|
|
|
|
map.remove(e);
|
2020-08-08 07:08:05 +02:00
|
|
|
CHECK(!map.has(42));
|
|
|
|
CHECK(!map.find(42));
|
|
|
|
}
|
|
|
|
|
2022-05-08 10:09:19 +02:00
|
|
|
TEST_CASE("[HashMap] Erase via key") {
|
|
|
|
HashMap<int, int> map;
|
2020-08-08 07:08:05 +02:00
|
|
|
map.insert(42, 84);
|
|
|
|
map.erase(42);
|
|
|
|
CHECK(!map.has(42));
|
|
|
|
CHECK(!map.find(42));
|
2017-08-13 10:32:39 +02:00
|
|
|
}
|
|
|
|
|
2022-05-08 10:09:19 +02:00
|
|
|
TEST_CASE("[HashMap] Size") {
|
|
|
|
HashMap<int, int> map;
|
2020-08-08 07:08:05 +02:00
|
|
|
map.insert(42, 84);
|
|
|
|
map.insert(123, 84);
|
|
|
|
map.insert(123, 84);
|
|
|
|
map.insert(0, 84);
|
|
|
|
map.insert(123485, 84);
|
|
|
|
|
|
|
|
CHECK(map.size() == 4);
|
|
|
|
}
|
|
|
|
|
2022-05-08 10:09:19 +02:00
|
|
|
TEST_CASE("[HashMap] Iteration") {
|
|
|
|
HashMap<int, int> map;
|
2020-08-08 07:08:05 +02:00
|
|
|
map.insert(42, 84);
|
|
|
|
map.insert(123, 12385);
|
|
|
|
map.insert(0, 12934);
|
|
|
|
map.insert(123485, 1238888);
|
|
|
|
map.insert(123, 111111);
|
|
|
|
|
|
|
|
Vector<Pair<int, int>> expected;
|
|
|
|
expected.push_back(Pair<int, int>(42, 84));
|
|
|
|
expected.push_back(Pair<int, int>(123, 111111));
|
|
|
|
expected.push_back(Pair<int, int>(0, 12934));
|
|
|
|
expected.push_back(Pair<int, int>(123485, 1238888));
|
|
|
|
|
|
|
|
int idx = 0;
|
2022-05-08 10:09:19 +02:00
|
|
|
for (const KeyValue<int, int> &E : map) {
|
|
|
|
CHECK(expected[idx] == Pair<int, int>(E.key, E.value));
|
2020-08-08 07:08:05 +02:00
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-08 10:09:19 +02:00
|
|
|
TEST_CASE("[HashMap] Const iteration") {
|
|
|
|
HashMap<int, int> map;
|
2020-08-08 07:08:05 +02:00
|
|
|
map.insert(42, 84);
|
|
|
|
map.insert(123, 12385);
|
|
|
|
map.insert(0, 12934);
|
|
|
|
map.insert(123485, 1238888);
|
|
|
|
map.insert(123, 111111);
|
|
|
|
|
2022-05-08 10:09:19 +02:00
|
|
|
const HashMap<int, int> const_map = map;
|
2020-08-08 07:08:05 +02:00
|
|
|
|
|
|
|
Vector<Pair<int, int>> expected;
|
|
|
|
expected.push_back(Pair<int, int>(42, 84));
|
|
|
|
expected.push_back(Pair<int, int>(123, 111111));
|
|
|
|
expected.push_back(Pair<int, int>(0, 12934));
|
|
|
|
expected.push_back(Pair<int, int>(123485, 1238888));
|
2022-05-08 10:09:19 +02:00
|
|
|
expected.push_back(Pair<int, int>(123, 111111));
|
2020-08-08 07:08:05 +02:00
|
|
|
|
|
|
|
int idx = 0;
|
2022-05-08 10:09:19 +02:00
|
|
|
for (const KeyValue<int, int> &E : const_map) {
|
|
|
|
CHECK(expected[idx] == Pair<int, int>(E.key, E.value));
|
2020-08-08 07:08:05 +02:00
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
}
|
2022-05-08 10:09:19 +02:00
|
|
|
} // namespace TestHashMap
|
2020-08-08 07:08:05 +02:00
|
|
|
|
2022-05-08 10:09:19 +02:00
|
|
|
#endif // TEST_HASH_MAP_H
|