2
0

test_array.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <irrArray.h>
  2. #include "test_helper.h"
  3. using namespace irr;
  4. using core::array;
  5. static void test_basics()
  6. {
  7. array<int> v;
  8. v.push_back(1); // 1
  9. v.push_front(2); // 2, 1
  10. v.insert(4, 0); // 4, 2, 1
  11. v.insert(3, 1); // 4, 3, 2, 1
  12. v.insert(0, 4); // 4, 3, 2, 1, 0
  13. UASSERTEQ(v.size(), 5);
  14. UASSERTEQ(v[0], 4);
  15. UASSERTEQ(v[1], 3);
  16. UASSERTEQ(v[2], 2);
  17. UASSERTEQ(v[3], 1);
  18. UASSERTEQ(v[4], 0);
  19. array<int> w = v;
  20. UASSERTEQ(w.size(), 5);
  21. UASSERT(w == v);
  22. w.clear();
  23. UASSERTEQ(w.size(), 0);
  24. UASSERTEQ(w.allocated_size(), 0);
  25. UASSERT(w.empty());
  26. w = v;
  27. UASSERTEQ(w.size(), 5);
  28. w.set_used(3);
  29. UASSERTEQ(w.size(), 3);
  30. UASSERTEQ(w[0], 4);
  31. UASSERTEQ(w[1], 3);
  32. UASSERTEQ(w[2], 2);
  33. UASSERTEQ(w.getLast(), 2);
  34. w.set_used(20);
  35. UASSERTEQ(w.size(), 20);
  36. w = v;
  37. w.sort();
  38. UASSERTEQ(w.size(), 5);
  39. UASSERTEQ(w[0], 0);
  40. UASSERTEQ(w[1], 1);
  41. UASSERTEQ(w[2], 2);
  42. UASSERTEQ(w[3], 3);
  43. UASSERTEQ(w[4], 4);
  44. w.erase(0);
  45. UASSERTEQ(w.size(), 4);
  46. UASSERTEQ(w[0], 1);
  47. UASSERTEQ(w[1], 2);
  48. UASSERTEQ(w[2], 3);
  49. UASSERTEQ(w[3], 4);
  50. w.erase(1, 2);
  51. UASSERTEQ(w.size(), 2);
  52. UASSERTEQ(w[0], 1);
  53. UASSERTEQ(w[1], 4);
  54. w.swap(v);
  55. UASSERTEQ(w.size(), 5);
  56. UASSERTEQ(v.size(), 2);
  57. }
  58. static void test_linear_searches()
  59. {
  60. // Populate the array with 0, 1, 2, ..., 100, 100, 99, 98, 97, ..., 0
  61. array<int> arr;
  62. for (int i = 0; i <= 100; i++)
  63. arr.push_back(i);
  64. for (int i = 100; i >= 0; i--)
  65. arr.push_back(i);
  66. s32 end = arr.size() - 1;
  67. for (int i = 0; i <= 100; i++) {
  68. s32 index = arr.linear_reverse_search(i);
  69. UASSERTEQ(index, end - i);
  70. }
  71. for (int i = 0; i <= 100; i++) {
  72. s32 index = arr.linear_search(i);
  73. UASSERTEQ(index, i);
  74. }
  75. }
  76. static void test_binary_searches()
  77. {
  78. const auto &values = {3, 5, 1, 2, 5, 10, 19, 9, 7, 1, 2, 5, 8, 15};
  79. array<int> arr;
  80. for (int value : values) {
  81. arr.push_back(value);
  82. }
  83. // Test the const form first, it uses a linear search without sorting
  84. const array<int> &carr = arr;
  85. UASSERTEQ(carr.binary_search(20), -1);
  86. UASSERTEQ(carr.binary_search(0), -1);
  87. UASSERTEQ(carr.binary_search(1), 2);
  88. // Sorted: 1, 1, 2, 2, 3, 5, 5, 5, 7, 8, 9, 10, 15, 19
  89. UASSERTEQ(arr.binary_search(20), -1);
  90. UASSERTEQ(arr.binary_search(0), -1);
  91. for (int value : values) {
  92. s32 i = arr.binary_search(value);
  93. UASSERTNE(i, -1);
  94. UASSERTEQ(arr[i], value);
  95. }
  96. s32 first, last;
  97. first = arr.binary_search_multi(1, last);
  98. UASSERTEQ(first, 0);
  99. UASSERTEQ(last, 1);
  100. first = arr.binary_search_multi(2, last);
  101. UASSERTEQ(first, 2);
  102. UASSERTEQ(last, 3);
  103. first = arr.binary_search_multi(3, last);
  104. UASSERTEQ(first, 4);
  105. UASSERTEQ(last, 4);
  106. first = arr.binary_search_multi(4, last);
  107. UASSERTEQ(first, -1);
  108. first = arr.binary_search_multi(5, last);
  109. UASSERTEQ(first, 5);
  110. UASSERTEQ(last, 7);
  111. first = arr.binary_search_multi(7, last);
  112. UASSERTEQ(first, 8);
  113. UASSERTEQ(last, 8);
  114. first = arr.binary_search_multi(19, last);
  115. UASSERTEQ(first, 13);
  116. UASSERTEQ(last, 13);
  117. }
  118. void test_irr_array()
  119. {
  120. test_basics();
  121. test_linear_searches();
  122. test_binary_searches();
  123. std::cout << " test_irr_array PASSED" << std::endl;
  124. }