| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/templates/paged_array.h" |
| 34 |
|
| 35 |
#include "thirdparty/doctest/doctest.h" |
| 36 |
|
| 37 |
namespace TestPagedArray { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
TEST_CASE("[PagedArray] Simple fill and refill") { |
| 42 |
PagedArrayPool<uint32_t> pool; |
| 43 |
PagedArray<uint32_t> array; |
| 44 |
array.set_page_pool(&pool); |
| 45 |
|
| 46 |
for (uint32_t i = 0; i < 123456; i++) { |
| 47 |
array.push_back(i); |
| 48 |
} |
| 49 |
CHECK_MESSAGE( |
| 50 |
array.size() == 123456, |
| 51 |
"PagedArray should have 123456 elements."); |
| 52 |
|
| 53 |
bool all_match = true; |
| 54 |
for (uint32_t i = 0; i < 123456; i++) { |
| 55 |
if (array[i] != i) { |
| 56 |
all_match = false; |
| 57 |
break; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
CHECK_MESSAGE( |
| 62 |
all_match, |
| 63 |
"PagedArray elements should match from 0 to 123455."); |
| 64 |
|
| 65 |
array.clear(); |
| 66 |
|
| 67 |
CHECK_MESSAGE( |
| 68 |
array.size() == 0, |
| 69 |
"PagedArray elements should be 0 after clear."); |
| 70 |
|
| 71 |
for (uint32_t i = 0; i < 999; i++) { |
| 72 |
array.push_back(i); |
| 73 |
} |
| 74 |
CHECK_MESSAGE( |
| 75 |
array.size() == 999, |
| 76 |
"PagedArray should have 999 elements."); |
| 77 |
|
| 78 |
all_match = true; |
| 79 |
for (uint32_t i = 0; i < 999; i++) { |
| 80 |
if (array[i] != i) { |
| 81 |
all_match = false; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
CHECK_MESSAGE( |
| 86 |
all_match, |
| 87 |
"PagedArray elements should match from 0 to 998."); |
| 88 |
|
| 89 |
array.reset(); |
| 90 |
pool.reset(); |
| 91 |
} |
| 92 |
|
| 93 |
TEST_CASE("[PagedArray] Shared pool fill, including merging") { |
| 94 |
PagedArrayPool<uint32_t> pool; |
| 95 |
PagedArray<uint32_t> array1; |
| 96 |
PagedArray<uint32_t> array2; |
| 97 |
array1.set_page_pool(&pool); |
| 98 |
array2.set_page_pool(&pool); |
| 99 |
|
| 100 |
for (uint32_t i = 0; i < 123456; i++) { |
| 101 |
array1.push_back(i); |
| 102 |
} |
| 103 |
CHECK_MESSAGE( |
| 104 |
array1.size() == 123456, |
| 105 |
"PagedArray #1 should have 123456 elements."); |
| 106 |
|
| 107 |
bool all_match = true; |
| 108 |
for (uint32_t i = 0; i < 123456; i++) { |
| 109 |
if (array1[i] != i) { |
| 110 |
all_match = false; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
CHECK_MESSAGE( |
| 115 |
all_match, |
| 116 |
"PagedArray #1 elements should match from 0 to 123455."); |
| 117 |
|
| 118 |
for (uint32_t i = 0; i < 999; i++) { |
| 119 |
array2.push_back(i); |
| 120 |
} |
| 121 |
CHECK_MESSAGE( |
| 122 |
array2.size() == 999, |
| 123 |
"PagedArray #2 should have 999 elements."); |
| 124 |
|
| 125 |
all_match = true; |
| 126 |
for (uint32_t i = 0; i < 999; i++) { |
| 127 |
if (array2[i] != i) { |
| 128 |
all_match = false; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
CHECK_MESSAGE( |
| 133 |
all_match, |
| 134 |
"PagedArray #2 elements should match from 0 to 998."); |
| 135 |
|
| 136 |
array1.merge_unordered(array2); |
| 137 |
|
| 138 |
CHECK_MESSAGE( |
| 139 |
array1.size() == 123456 + 999, |
| 140 |
"PagedArray #1 should now be 123456 + 999 elements."); |
| 141 |
|
| 142 |
CHECK_MESSAGE( |
| 143 |
array2.size() == 0, |
| 144 |
"PagedArray #2 should now be 0 elements."); |
| 145 |
|
| 146 |
array1.reset(); |
| 147 |
array2.reset(); |
| 148 |
pool.reset(); |
| 149 |
} |
| 150 |
|
| 151 |
TEST_CASE("[PagedArray] Extensive merge_unordered() test") { |
| 152 |
for (int page_size = 1; page_size <= 128; page_size *= 2) { |
| 153 |
PagedArrayPool<uint32_t> pool(page_size); |
| 154 |
PagedArray<uint32_t> array1; |
| 155 |
PagedArray<uint32_t> array2; |
| 156 |
array1.set_page_pool(&pool); |
| 157 |
array2.set_page_pool(&pool); |
| 158 |
|
| 159 |
const int max_count = 123; |
| 160 |
|
| 161 |
for (uint32_t j = 0; j < max_count; j++) { |
| 162 |
CHECK(array1.size() == 0); |
| 163 |
CHECK(array2.size() == 0); |
| 164 |
|
| 165 |
uint32_t sum = 12345; |
| 166 |
for (uint32_t i = 0; i < j; i++) { |
| 167 |
|
| 168 |
|
| 169 |
uint32_t addend = hash_murmur3_one_32(i) + i; |
| 170 |
array1.push_back(addend); |
| 171 |
sum += addend; |
| 172 |
} |
| 173 |
for (uint32_t i = j; i < max_count; i++) { |
| 174 |
|
| 175 |
uint32_t addend = hash_murmur3_one_32(i) + i; |
| 176 |
array2.push_back(addend); |
| 177 |
sum += addend; |
| 178 |
} |
| 179 |
|
| 180 |
CHECK(array1.size() == j); |
| 181 |
CHECK(array2.size() == max_count - j); |
| 182 |
|
| 183 |
array1.merge_unordered(array2); |
| 184 |
CHECK_MESSAGE(array1.size() == max_count, "merge_unordered() added/dropped elements while merging"); |
| 185 |
|
| 186 |
|
| 187 |
for (uint32_t i = 0; i < array1.size(); i++) { |
| 188 |
sum -= array1[i]; |
| 189 |
} |
| 190 |
CHECK_MESSAGE(sum == 12345, "merge_unordered() altered elements while merging"); |
| 191 |
|
| 192 |
array1.clear(); |
| 193 |
} |
| 194 |
|
| 195 |
array1.reset(); |
| 196 |
array2.reset(); |
| 197 |
pool.reset(); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
} |