test_paged_array.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**************************************************************************/
  2. /* test_paged_array.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_PAGED_ARRAY_H
  31. #define TEST_PAGED_ARRAY_H
  32. #include "core/templates/paged_array.h"
  33. #include "thirdparty/doctest/doctest.h"
  34. namespace TestPagedArray {
  35. // PagedArray
  36. TEST_CASE("[PagedArray] Simple fill and refill") {
  37. PagedArrayPool<uint32_t> pool;
  38. PagedArray<uint32_t> array;
  39. array.set_page_pool(&pool);
  40. for (uint32_t i = 0; i < 123456; i++) {
  41. array.push_back(i);
  42. }
  43. CHECK_MESSAGE(
  44. array.size() == 123456,
  45. "PagedArray should have 123456 elements.");
  46. bool all_match = true;
  47. for (uint32_t i = 0; i < 123456; i++) {
  48. if (array[i] != i) {
  49. all_match = false;
  50. break;
  51. }
  52. }
  53. CHECK_MESSAGE(
  54. all_match,
  55. "PagedArray elements should match from 0 to 123455.");
  56. array.clear();
  57. CHECK_MESSAGE(
  58. array.size() == 0,
  59. "PagedArray elements should be 0 after clear.");
  60. for (uint32_t i = 0; i < 999; i++) {
  61. array.push_back(i);
  62. }
  63. CHECK_MESSAGE(
  64. array.size() == 999,
  65. "PagedArray should have 999 elements.");
  66. all_match = true;
  67. for (uint32_t i = 0; i < 999; i++) {
  68. if (array[i] != i) {
  69. all_match = false;
  70. }
  71. }
  72. CHECK_MESSAGE(
  73. all_match,
  74. "PagedArray elements should match from 0 to 998.");
  75. array.reset(); //reset so pagepool can be reset
  76. pool.reset();
  77. }
  78. TEST_CASE("[PagedArray] Shared pool fill, including merging") {
  79. PagedArrayPool<uint32_t> pool;
  80. PagedArray<uint32_t> array1;
  81. PagedArray<uint32_t> array2;
  82. array1.set_page_pool(&pool);
  83. array2.set_page_pool(&pool);
  84. for (uint32_t i = 0; i < 123456; i++) {
  85. array1.push_back(i);
  86. }
  87. CHECK_MESSAGE(
  88. array1.size() == 123456,
  89. "PagedArray #1 should have 123456 elements.");
  90. bool all_match = true;
  91. for (uint32_t i = 0; i < 123456; i++) {
  92. if (array1[i] != i) {
  93. all_match = false;
  94. }
  95. }
  96. CHECK_MESSAGE(
  97. all_match,
  98. "PagedArray #1 elements should match from 0 to 123455.");
  99. for (uint32_t i = 0; i < 999; i++) {
  100. array2.push_back(i);
  101. }
  102. CHECK_MESSAGE(
  103. array2.size() == 999,
  104. "PagedArray #2 should have 999 elements.");
  105. all_match = true;
  106. for (uint32_t i = 0; i < 999; i++) {
  107. if (array2[i] != i) {
  108. all_match = false;
  109. }
  110. }
  111. CHECK_MESSAGE(
  112. all_match,
  113. "PagedArray #2 elements should match from 0 to 998.");
  114. array1.merge_unordered(array2);
  115. CHECK_MESSAGE(
  116. array1.size() == 123456 + 999,
  117. "PagedArray #1 should now be 123456 + 999 elements.");
  118. CHECK_MESSAGE(
  119. array2.size() == 0,
  120. "PagedArray #2 should now be 0 elements.");
  121. array1.reset(); //reset so pagepool can be reset
  122. array2.reset(); //reset so pagepool can be reset
  123. pool.reset();
  124. }
  125. TEST_CASE("[PagedArray] Extensive merge_unordered() test") {
  126. for (int page_size = 1; page_size <= 128; page_size *= 2) {
  127. PagedArrayPool<uint32_t> pool(page_size);
  128. PagedArray<uint32_t> array1;
  129. PagedArray<uint32_t> array2;
  130. array1.set_page_pool(&pool);
  131. array2.set_page_pool(&pool);
  132. const int max_count = 123;
  133. // Test merging arrays of lengths 0+123, 1+122, 2+121, ..., 123+0
  134. for (uint32_t j = 0; j < max_count; j++) {
  135. CHECK(array1.size() == 0);
  136. CHECK(array2.size() == 0);
  137. uint32_t sum = 12345;
  138. for (uint32_t i = 0; i < j; i++) {
  139. // Hashing the addend makes it extremely unlikely for any values
  140. // other than the original inputs to produce a matching sum
  141. uint32_t addend = hash_murmur3_one_32(i) + i;
  142. array1.push_back(addend);
  143. sum += addend;
  144. }
  145. for (uint32_t i = j; i < max_count; i++) {
  146. // See above
  147. uint32_t addend = hash_murmur3_one_32(i) + i;
  148. array2.push_back(addend);
  149. sum += addend;
  150. }
  151. CHECK(array1.size() == j);
  152. CHECK(array2.size() == max_count - j);
  153. array1.merge_unordered(array2);
  154. CHECK_MESSAGE(array1.size() == max_count, "merge_unordered() added/dropped elements while merging");
  155. // If any elements were altered during merging, the sum will not match up.
  156. for (uint32_t i = 0; i < array1.size(); i++) {
  157. sum -= array1[i];
  158. }
  159. CHECK_MESSAGE(sum == 12345, "merge_unordered() altered elements while merging");
  160. array1.clear();
  161. }
  162. array1.reset();
  163. array2.reset();
  164. pool.reset();
  165. }
  166. }
  167. } // namespace TestPagedArray
  168. #endif // TEST_PAGED_ARRAY_H