Path: blob/master/tests/core/templates/test_paged_array.cpp
45991 views
/**************************************************************************/1/* test_paged_array.cpp */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#include "tests/test_macros.h"3132TEST_FORCE_LINK(test_paged_array)3334#include "core/templates/paged_array.h"3536namespace TestPagedArray {3738TEST_CASE("[PagedArray] Simple fill and refill") {39PagedArrayPool<uint32_t> pool;40PagedArray<uint32_t> array;41array.set_page_pool(&pool);4243for (uint32_t i = 0; i < 123456; i++) {44array.push_back(i);45}46CHECK_MESSAGE(47array.size() == 123456,48"PagedArray should have 123456 elements.");4950bool all_match = true;51for (uint32_t i = 0; i < 123456; i++) {52if (array[i] != i) {53all_match = false;54break;55}56}5758CHECK_MESSAGE(59all_match,60"PagedArray elements should match from 0 to 123455.");6162array.clear();6364CHECK_MESSAGE(65array.size() == 0,66"PagedArray elements should be 0 after clear.");6768for (uint32_t i = 0; i < 999; i++) {69array.push_back(i);70}71CHECK_MESSAGE(72array.size() == 999,73"PagedArray should have 999 elements.");7475all_match = true;76for (uint32_t i = 0; i < 999; i++) {77if (array[i] != i) {78all_match = false;79}80}8182CHECK_MESSAGE(83all_match,84"PagedArray elements should match from 0 to 998.");8586array.reset(); //reset so pagepool can be reset87pool.reset();88}8990TEST_CASE("[PagedArray] Shared pool fill, including merging") {91PagedArrayPool<uint32_t> pool;92PagedArray<uint32_t> array1;93PagedArray<uint32_t> array2;94array1.set_page_pool(&pool);95array2.set_page_pool(&pool);9697for (uint32_t i = 0; i < 123456; i++) {98array1.push_back(i);99}100CHECK_MESSAGE(101array1.size() == 123456,102"PagedArray #1 should have 123456 elements.");103104bool all_match = true;105for (uint32_t i = 0; i < 123456; i++) {106if (array1[i] != i) {107all_match = false;108}109}110111CHECK_MESSAGE(112all_match,113"PagedArray #1 elements should match from 0 to 123455.");114115for (uint32_t i = 0; i < 999; i++) {116array2.push_back(i);117}118CHECK_MESSAGE(119array2.size() == 999,120"PagedArray #2 should have 999 elements.");121122all_match = true;123for (uint32_t i = 0; i < 999; i++) {124if (array2[i] != i) {125all_match = false;126}127}128129CHECK_MESSAGE(130all_match,131"PagedArray #2 elements should match from 0 to 998.");132133array1.merge_unordered(array2);134135CHECK_MESSAGE(136array1.size() == 123456 + 999,137"PagedArray #1 should now be 123456 + 999 elements.");138139CHECK_MESSAGE(140array2.size() == 0,141"PagedArray #2 should now be 0 elements.");142143array1.reset(); //reset so pagepool can be reset144array2.reset(); //reset so pagepool can be reset145pool.reset();146}147148TEST_CASE("[PagedArray] Extensive merge_unordered() test") {149for (int page_size = 1; page_size <= 128; page_size *= 2) {150PagedArrayPool<uint32_t> pool(page_size);151PagedArray<uint32_t> array1;152PagedArray<uint32_t> array2;153array1.set_page_pool(&pool);154array2.set_page_pool(&pool);155156const int max_count = 123;157// Test merging arrays of lengths 0+123, 1+122, 2+121, ..., 123+0158for (uint32_t j = 0; j < max_count; j++) {159CHECK(array1.size() == 0);160CHECK(array2.size() == 0);161162uint32_t sum = 12345;163for (uint32_t i = 0; i < j; i++) {164// Hashing the addend makes it extremely unlikely for any values165// other than the original inputs to produce a matching sum166uint32_t addend = hash_murmur3_one_32(i) + i;167array1.push_back(addend);168sum += addend;169}170for (uint32_t i = j; i < max_count; i++) {171// See above172uint32_t addend = hash_murmur3_one_32(i) + i;173array2.push_back(addend);174sum += addend;175}176177CHECK(array1.size() == j);178CHECK(array2.size() == max_count - j);179180array1.merge_unordered(array2);181CHECK_MESSAGE(array1.size() == max_count, "merge_unordered() added/dropped elements while merging");182183// If any elements were altered during merging, the sum will not match up.184for (uint32_t i = 0; i < array1.size(); i++) {185sum -= array1[i];186}187CHECK_MESSAGE(sum == 12345, "merge_unordered() altered elements while merging");188189array1.clear();190}191192array1.reset();193array2.reset();194pool.reset();195}196}197198} // namespace TestPagedArray199200201