Path: blob/master/tests/core/templates/test_hash_map.h
10278 views
/**************************************************************************/1/* test_hash_map.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/templates/hash_map.h"3334#include "tests/test_macros.h"3536namespace TestHashMap {3738TEST_CASE("[HashMap] List initialization") {39HashMap<int, String> map{ { 0, "A" }, { 1, "B" }, { 2, "C" }, { 3, "D" }, { 4, "E" } };4041CHECK(map.size() == 5);42CHECK(map[0] == "A");43CHECK(map[1] == "B");44CHECK(map[2] == "C");45CHECK(map[3] == "D");46CHECK(map[4] == "E");47}4849TEST_CASE("[HashMap] List initialization with existing elements") {50HashMap<int, String> map{ { 0, "A" }, { 0, "B" }, { 0, "C" }, { 0, "D" }, { 0, "E" } };5152CHECK(map.size() == 1);53CHECK(map[0] == "E");54}5556TEST_CASE("[HashMap] Insert element") {57HashMap<int, int> map;58HashMap<int, int>::Iterator e = map.insert(42, 84);5960CHECK(e);61CHECK(e->key == 42);62CHECK(e->value == 84);63CHECK(map[42] == 84);64CHECK(map.has(42));65CHECK(map.find(42));66}6768TEST_CASE("[HashMap] Overwrite element") {69HashMap<int, int> map;70map.insert(42, 84);71map.insert(42, 1234);7273CHECK(map[42] == 1234);74}7576TEST_CASE("[HashMap] Erase via element") {77HashMap<int, int> map;78HashMap<int, int>::Iterator e = map.insert(42, 84);79map.remove(e);80CHECK(!map.has(42));81CHECK(!map.find(42));82}8384TEST_CASE("[HashMap] Erase via key") {85HashMap<int, int> map;86map.insert(42, 84);87map.erase(42);88CHECK(!map.has(42));89CHECK(!map.find(42));90}9192TEST_CASE("[HashMap] Size") {93HashMap<int, int> map;94map.insert(42, 84);95map.insert(123, 84);96map.insert(123, 84);97map.insert(0, 84);98map.insert(123485, 84);99100CHECK(map.size() == 4);101}102103TEST_CASE("[HashMap] Iteration") {104HashMap<int, int> map;105map.insert(42, 84);106map.insert(123, 12385);107map.insert(0, 12934);108map.insert(123485, 1238888);109map.insert(123, 111111);110111Vector<Pair<int, int>> expected;112expected.push_back(Pair<int, int>(42, 84));113expected.push_back(Pair<int, int>(123, 111111));114expected.push_back(Pair<int, int>(0, 12934));115expected.push_back(Pair<int, int>(123485, 1238888));116117int idx = 0;118for (const KeyValue<int, int> &E : map) {119CHECK(expected[idx] == Pair<int, int>(E.key, E.value));120++idx;121}122}123124TEST_CASE("[HashMap] Const iteration") {125HashMap<int, int> map;126map.insert(42, 84);127map.insert(123, 12385);128map.insert(0, 12934);129map.insert(123485, 1238888);130map.insert(123, 111111);131132const HashMap<int, int> const_map = map;133134Vector<Pair<int, int>> expected;135expected.push_back(Pair<int, int>(42, 84));136expected.push_back(Pair<int, int>(123, 111111));137expected.push_back(Pair<int, int>(0, 12934));138expected.push_back(Pair<int, int>(123485, 1238888));139expected.push_back(Pair<int, int>(123, 111111));140141int idx = 0;142for (const KeyValue<int, int> &E : const_map) {143CHECK(expected[idx] == Pair<int, int>(E.key, E.value));144++idx;145}146}147148TEST_CASE("[HashMap] Sort") {149HashMap<int, int> hashmap;150int shuffled_ints[]{ 6, 1, 9, 8, 3, 0, 4, 5, 7, 2 };151152for (int i : shuffled_ints) {153hashmap[i] = i;154}155hashmap.sort();156157int i = 0;158for (const KeyValue<int, int> &kv : hashmap) {159CHECK_EQ(kv.key, i);160i++;161}162163struct ReverseSort {164bool operator()(const KeyValue<int, int> &p_a, const KeyValue<int, int> &p_b) {165return p_a.key > p_b.key;166}167};168hashmap.sort_custom<ReverseSort>();169170for (const KeyValue<int, int> &kv : hashmap) {171i--;172CHECK_EQ(kv.key, i);173}174}175} // namespace TestHashMap176177178