2
0

test_paged_array.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*************************************************************************/
  2. /* test_paged_array.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. } // namespace TestPagedArray
  126. #endif // TEST_PAGED_ARRAY_H