Path: blob/master/tests/core/templates/test_hash_set.h
10278 views
/**************************************************************************/1/* test_hash_set.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_set.h"3334#include "tests/test_macros.h"3536namespace TestHashSet {3738TEST_CASE("[HashSet] List initialization") {39HashSet<int> set{ 0, 1, 2, 3, 4 };4041CHECK(set.size() == 5);42CHECK(set.has(0));43CHECK(set.has(1));44CHECK(set.has(2));45CHECK(set.has(3));46CHECK(set.has(4));47}4849TEST_CASE("[HashSet] List initialization with existing elements") {50HashSet<int> set{ 0, 0, 0, 0, 0 };5152CHECK(set.size() == 1);53CHECK(set.has(0));54}5556TEST_CASE("[HashSet] Insert element") {57HashSet<int> set;58HashSet<int>::Iterator e = set.insert(42);5960CHECK(e);61CHECK(*e == 42);62CHECK(set.has(42));63CHECK(set.find(42));64set.reset();65}6667TEST_CASE("[HashSet] Insert existing element") {68HashSet<int> set;69set.insert(42);70set.insert(42);7172CHECK(set.has(42));73CHECK(set.size() == 1);74}7576TEST_CASE("[HashSet] Insert, iterate and remove many elements") {77const int elem_max = 12343;78HashSet<int> set;79for (int i = 0; i < elem_max; i++) {80set.insert(i);81}8283//insert order should have been kept84int idx = 0;85for (const int &K : set) {86CHECK(idx == K);87CHECK(set.has(idx));88idx++;89}9091Vector<int> elems_still_valid;9293for (int i = 0; i < elem_max; i++) {94if ((i % 5) == 0) {95set.erase(i);96} else {97elems_still_valid.push_back(i);98}99}100101CHECK(elems_still_valid.size() == set.size());102103for (int i = 0; i < elems_still_valid.size(); i++) {104CHECK(set.has(elems_still_valid[i]));105}106}107108TEST_CASE("[HashSet] Insert, iterate and remove many strings") {109// This tests a key that uses allocation, to see if any leaks occur110111uint64_t pre_mem = Memory::get_mem_usage();112const int elem_max = 4018;113HashSet<String> set;114for (int i = 0; i < elem_max; i++) {115set.insert(itos(i));116}117118//insert order should have been kept119int idx = 0;120for (const String &K : set) {121CHECK(itos(idx) == K);122CHECK(set.has(itos(idx)));123idx++;124}125126Vector<String> elems_still_valid;127128for (int i = 0; i < elem_max; i++) {129if ((i % 5) == 0) {130set.erase(itos(i));131} else {132elems_still_valid.push_back(itos(i));133}134}135136CHECK(elems_still_valid.size() == set.size());137138for (int i = 0; i < elems_still_valid.size(); i++) {139CHECK(set.has(elems_still_valid[i]));140}141142elems_still_valid.clear();143set.reset();144145CHECK(Memory::get_mem_usage() == pre_mem);146}147148TEST_CASE("[HashSet] Erase via element") {149HashSet<int> set;150HashSet<int>::Iterator e = set.insert(42);151set.remove(e);152CHECK(!set.has(42));153CHECK(!set.find(42));154}155156TEST_CASE("[HashSet] Erase via key") {157HashSet<int> set;158set.insert(42);159set.insert(49);160set.erase(42);161CHECK(!set.has(42));162CHECK(!set.find(42));163}164165TEST_CASE("[HashSet] Insert and erase half elements") {166HashSet<int> set;167set.insert(1);168set.insert(2);169set.insert(3);170set.insert(4);171set.erase(1);172set.erase(3);173174CHECK(set.size() == 2);175CHECK(set.has(2));176CHECK(set.has(4));177}178179TEST_CASE("[HashSet] Size") {180HashSet<int> set;181set.insert(42);182set.insert(123);183set.insert(123);184set.insert(0);185set.insert(123485);186187CHECK(set.size() == 4);188}189190TEST_CASE("[HashSet] Iteration") {191HashSet<int> set;192set.insert(42);193set.insert(123);194set.insert(0);195set.insert(123485);196197Vector<int> expected;198expected.push_back(42);199expected.push_back(123);200expected.push_back(0);201expected.push_back(123485);202203int idx = 0;204for (const int &E : set) {205CHECK(expected[idx] == E);206++idx;207}208}209210TEST_CASE("[HashSet] Copy") {211HashSet<int> set;212set.insert(42);213set.insert(123);214set.insert(0);215set.insert(123485);216217Vector<int> expected;218expected.push_back(42);219expected.push_back(123);220expected.push_back(0);221expected.push_back(123485);222223HashSet<int> copy_assign = set;224225int idx = 0;226for (const int &E : copy_assign) {227CHECK(expected[idx] == E);228++idx;229}230231HashSet<int> copy_construct(set);232233idx = 0;234for (const int &E : copy_construct) {235CHECK(expected[idx] == E);236++idx;237}238}239240TEST_CASE("[HashSet] Equality") {241// Empty sets.242CHECK(HashSet<int>{} == HashSet<int>{});243CHECK(HashSet<int>{} != HashSet<int>{ 1, 2, 3 });244CHECK(HashSet<int>{ 1, 2, 3 } != HashSet<int>{});245246// Different length.247CHECK(HashSet<int>{ 1, 2, 3 } != HashSet<int>{ 1, 2, 3, 4 });248CHECK(HashSet<int>{ 1, 2, 3, 4 } != HashSet<int>{ 4, 3, 2 });249250// Same length.251CHECK(HashSet<int>{ 1, 2, 3 } == HashSet<int>{ 1, 2, 3 });252CHECK(HashSet<int>{ 1, 2, 3 } == HashSet<int>{ 3, 2, 1 });253CHECK(HashSet<int>{ 1, 2, 3 } != HashSet<int>{ 1, 2, 8 });254}255256} // namespace TestHashSet257258259