From 6b0f253a45c325e6100c1e74da503532c7321024 Mon Sep 17 00:00:00 2001 From: myaaaaaaaaa <103326468+myaaaaaaaaa@users.noreply.github.com> Date: Thu, 16 Feb 2023 14:54:04 -0500 Subject: Fix PagedArray.merge_unordered() dropping pages --- tests/core/templates/test_paged_array.h | 51 +++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'tests/core') diff --git a/tests/core/templates/test_paged_array.h b/tests/core/templates/test_paged_array.h index 37be3080e8..10dc4473ca 100644 --- a/tests/core/templates/test_paged_array.h +++ b/tests/core/templates/test_paged_array.h @@ -148,6 +148,57 @@ TEST_CASE("[PagedArray] Shared pool fill, including merging") { array2.reset(); //reset so pagepool can be reset pool.reset(); } + +TEST_CASE("[PagedArray] Extensive merge_unordered() test") { + for (int page_size = 1; page_size <= 128; page_size *= 2) { + PagedArrayPool pool(page_size); + PagedArray array1; + PagedArray array2; + array1.set_page_pool(&pool); + array2.set_page_pool(&pool); + + const int max_count = 123; + // Test merging arrays of lengths 0+123, 1+122, 2+121, ..., 123+0 + for (uint32_t j = 0; j < max_count; j++) { + CHECK(array1.size() == 0); + CHECK(array2.size() == 0); + + uint32_t sum = 12345; + for (uint32_t i = 0; i < j; i++) { + // Hashing the addend makes it extremely unlikely for any values + // other than the original inputs to produce a matching sum + uint32_t addend = hash_murmur3_one_32(i) + i; + array1.push_back(addend); + sum += addend; + } + for (uint32_t i = j; i < max_count; i++) { + // See above + uint32_t addend = hash_murmur3_one_32(i) + i; + array2.push_back(addend); + sum += addend; + } + + CHECK(array1.size() == j); + CHECK(array2.size() == max_count - j); + + array1.merge_unordered(array2); + CHECK_MESSAGE(array1.size() == max_count, "merge_unordered() added/dropped elements while merging"); + + // If any elements were altered during merging, the sum will not match up. + for (uint32_t i = 0; i < array1.size(); i++) { + sum -= array1[i]; + } + CHECK_MESSAGE(sum == 12345, "merge_unordered() altered elements while merging"); + + array1.clear(); + } + + array1.reset(); + array2.reset(); + pool.reset(); + } +} + } // namespace TestPagedArray #endif // TEST_PAGED_ARRAY_H -- cgit v1.2.3