Path: blob/master/tests/core/templates/test_paged_array.h
10278 views
/**************************************************************************/1/* test_paged_array.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/paged_array.h"3334#include "thirdparty/doctest/doctest.h"3536namespace TestPagedArray {3738// PagedArray3940TEST_CASE("[PagedArray] Simple fill and refill") {41PagedArrayPool<uint32_t> pool;42PagedArray<uint32_t> array;43array.set_page_pool(&pool);4445for (uint32_t i = 0; i < 123456; i++) {46array.push_back(i);47}48CHECK_MESSAGE(49array.size() == 123456,50"PagedArray should have 123456 elements.");5152bool all_match = true;53for (uint32_t i = 0; i < 123456; i++) {54if (array[i] != i) {55all_match = false;56break;57}58}5960CHECK_MESSAGE(61all_match,62"PagedArray elements should match from 0 to 123455.");6364array.clear();6566CHECK_MESSAGE(67array.size() == 0,68"PagedArray elements should be 0 after clear.");6970for (uint32_t i = 0; i < 999; i++) {71array.push_back(i);72}73CHECK_MESSAGE(74array.size() == 999,75"PagedArray should have 999 elements.");7677all_match = true;78for (uint32_t i = 0; i < 999; i++) {79if (array[i] != i) {80all_match = false;81}82}8384CHECK_MESSAGE(85all_match,86"PagedArray elements should match from 0 to 998.");8788array.reset(); //reset so pagepool can be reset89pool.reset();90}9192TEST_CASE("[PagedArray] Shared pool fill, including merging") {93PagedArrayPool<uint32_t> pool;94PagedArray<uint32_t> array1;95PagedArray<uint32_t> array2;96array1.set_page_pool(&pool);97array2.set_page_pool(&pool);9899for (uint32_t i = 0; i < 123456; i++) {100array1.push_back(i);101}102CHECK_MESSAGE(103array1.size() == 123456,104"PagedArray #1 should have 123456 elements.");105106bool all_match = true;107for (uint32_t i = 0; i < 123456; i++) {108if (array1[i] != i) {109all_match = false;110}111}112113CHECK_MESSAGE(114all_match,115"PagedArray #1 elements should match from 0 to 123455.");116117for (uint32_t i = 0; i < 999; i++) {118array2.push_back(i);119}120CHECK_MESSAGE(121array2.size() == 999,122"PagedArray #2 should have 999 elements.");123124all_match = true;125for (uint32_t i = 0; i < 999; i++) {126if (array2[i] != i) {127all_match = false;128}129}130131CHECK_MESSAGE(132all_match,133"PagedArray #2 elements should match from 0 to 998.");134135array1.merge_unordered(array2);136137CHECK_MESSAGE(138array1.size() == 123456 + 999,139"PagedArray #1 should now be 123456 + 999 elements.");140141CHECK_MESSAGE(142array2.size() == 0,143"PagedArray #2 should now be 0 elements.");144145array1.reset(); //reset so pagepool can be reset146array2.reset(); //reset so pagepool can be reset147pool.reset();148}149150TEST_CASE("[PagedArray] Extensive merge_unordered() test") {151for (int page_size = 1; page_size <= 128; page_size *= 2) {152PagedArrayPool<uint32_t> pool(page_size);153PagedArray<uint32_t> array1;154PagedArray<uint32_t> array2;155array1.set_page_pool(&pool);156array2.set_page_pool(&pool);157158const int max_count = 123;159// Test merging arrays of lengths 0+123, 1+122, 2+121, ..., 123+0160for (uint32_t j = 0; j < max_count; j++) {161CHECK(array1.size() == 0);162CHECK(array2.size() == 0);163164uint32_t sum = 12345;165for (uint32_t i = 0; i < j; i++) {166// Hashing the addend makes it extremely unlikely for any values167// other than the original inputs to produce a matching sum168uint32_t addend = hash_murmur3_one_32(i) + i;169array1.push_back(addend);170sum += addend;171}172for (uint32_t i = j; i < max_count; i++) {173// See above174uint32_t addend = hash_murmur3_one_32(i) + i;175array2.push_back(addend);176sum += addend;177}178179CHECK(array1.size() == j);180CHECK(array2.size() == max_count - j);181182array1.merge_unordered(array2);183CHECK_MESSAGE(array1.size() == max_count, "merge_unordered() added/dropped elements while merging");184185// If any elements were altered during merging, the sum will not match up.186for (uint32_t i = 0; i < array1.size(); i++) {187sum -= array1[i];188}189CHECK_MESSAGE(sum == 12345, "merge_unordered() altered elements while merging");190191array1.clear();192}193194array1.reset();195array2.reset();196pool.reset();197}198}199200} // namespace TestPagedArray201202203