sort_test.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 2010-2022 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx/blob/master/LICENSE
  4. */
  5. #include "test.h"
  6. #include <bx/bx.h>
  7. #include <bx/sort.h>
  8. #include <bx/string.h>
  9. #include <bx/rng.h>
  10. TEST_CASE("sort-quickSort", "")
  11. {
  12. const char* str[] =
  13. {
  14. "jabuka",
  15. "kruska",
  16. "malina",
  17. "jagoda",
  18. };
  19. REQUIRE(!bx::isSorted(str, BX_COUNTOF(str) ) );
  20. bx::quickSort(str, BX_COUNTOF(str) );
  21. REQUIRE(0 == bx::strCmp(str[0], "jabuka") );
  22. REQUIRE(0 == bx::strCmp(str[1], "jagoda") );
  23. REQUIRE(0 == bx::strCmp(str[2], "kruska") );
  24. REQUIRE(0 == bx::strCmp(str[3], "malina") );
  25. REQUIRE(bx::isSorted(str, BX_COUNTOF(str) ) );
  26. auto bsearchStrCmpFn = [](const void* _lhs, const void* _rhs)
  27. {
  28. const char* lhs = (const char*)_lhs;
  29. const char* rhs = *(const char**)_rhs;
  30. return bx::strCmp(lhs, rhs);
  31. };
  32. REQUIRE(~4 == bx::binarySearch("sljiva", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) );
  33. REQUIRE( 0 == bx::binarySearch("jabuka", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) );
  34. REQUIRE( 1 == bx::binarySearch("jagoda", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) );
  35. REQUIRE( 2 == bx::binarySearch("kruska", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) );
  36. REQUIRE( 3 == bx::binarySearch("malina", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) );
  37. REQUIRE(~3 == bx::binarySearch("kupina", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) );
  38. REQUIRE( 0 == bx::lowerBound("jabuka", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) );
  39. REQUIRE( 1 == bx::upperBound("jabuka", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) );
  40. REQUIRE( 1 == bx::lowerBound("jagoda", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) );
  41. REQUIRE( 2 == bx::upperBound("jagoda", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) );
  42. REQUIRE( 2 == bx::lowerBound("kruska", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) );
  43. REQUIRE( 3 == bx::upperBound("kruska", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) );
  44. REQUIRE( 3 == bx::lowerBound("malina", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) );
  45. REQUIRE( 4 == bx::upperBound("malina", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) );
  46. int8_t byte[128];
  47. bx::RngMwc rng;
  48. for (uint32_t ii = 0; ii < BX_COUNTOF(byte); ++ii)
  49. {
  50. byte[ii] = rng.gen()&0xff;
  51. }
  52. REQUIRE(!bx::isSorted(byte, BX_COUNTOF(byte) ) );
  53. bx::quickSort(byte, BX_COUNTOF(byte) );
  54. for (uint32_t ii = 1; ii < BX_COUNTOF(byte); ++ii)
  55. {
  56. REQUIRE(byte[ii-1] <= byte[ii]);
  57. }
  58. REQUIRE(bx::isSorted(byte, BX_COUNTOF(byte) ) );
  59. }
  60. TEST_CASE("sort-unique", "")
  61. {
  62. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 | 14
  63. int32_t test[] = { 100, 101, 101, 101, 103, 104, 105, 105, 105, 106, 106, 107, 108, 109 };
  64. REQUIRE(bx::isSorted(test, BX_COUNTOF(test) ) );
  65. REQUIRE(0 == bx::unique(test, 0) );
  66. REQUIRE(1 == bx::unique(test, 1) );
  67. REQUIRE(2 == bx::unique(test, 4) );
  68. bx::quickSort(test, BX_COUNTOF(test) );
  69. REQUIRE(3 == bx::unique(test, 5) );
  70. bx::quickSort(test, BX_COUNTOF(test) );
  71. uint32_t last = bx::unique(test, BX_COUNTOF(test) );
  72. REQUIRE(9 == last);
  73. REQUIRE(9 == bx::unique(test, last) );
  74. }
  75. TEST_CASE("sort-lower/upperBound int32_t", "")
  76. {
  77. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 | 14
  78. const int32_t test[] = { 100, 101, 101, 101, 103, 104, 105, 105, 105, 106, 106, 107, 108, 109 };
  79. REQUIRE(bx::isSorted(test, BX_COUNTOF(test) ) );
  80. const uint32_t resultLowerBound[] = { 0, 1, 4, 4, 5, 6, 9, 11, 12, 13 };
  81. const uint32_t resultUpperBound[] = { 1, 4, 4, 5, 6, 9, 11, 12, 13, 14 };
  82. STATIC_REQUIRE(10 == BX_COUNTOF(resultLowerBound) );
  83. STATIC_REQUIRE(10 == BX_COUNTOF(resultUpperBound) );
  84. for (int32_t key = test[0], keyMax = test[BX_COUNTOF(test)-1], ii = 0; key <= keyMax; ++key, ++ii)
  85. {
  86. REQUIRE(resultLowerBound[ii] == bx::lowerBound(key, test, BX_COUNTOF(test) ) );
  87. REQUIRE(resultUpperBound[ii] == bx::upperBound(key, test, BX_COUNTOF(test) ) );
  88. }
  89. }
  90. template<typename Ty>
  91. int32_t compareAscendingTest(const Ty& _lhs, const Ty& _rhs)
  92. {
  93. return bx::compareAscending<Ty>(&_lhs, &_rhs);
  94. }
  95. template<typename Ty>
  96. int32_t compareDescendingTest(const Ty& _lhs, const Ty& _rhs)
  97. {
  98. return bx::compareDescending<Ty>(&_lhs, &_rhs);
  99. }
  100. template<typename Ty>
  101. void compareTest(const Ty& _min, const Ty& _max)
  102. {
  103. REQUIRE(_min < _max);
  104. REQUIRE(-1 == compareAscendingTest<Ty>(bx::min<Ty>(), bx::max<Ty>() ) );
  105. REQUIRE(-1 == compareAscendingTest<Ty>(Ty(0), bx::max<Ty>() ) );
  106. REQUIRE( 0 == compareAscendingTest<Ty>(bx::min<Ty>(), bx::min<Ty>() ) );
  107. REQUIRE( 0 == compareAscendingTest<Ty>(bx::max<Ty>(), bx::max<Ty>() ) );
  108. REQUIRE( 1 == compareAscendingTest<Ty>(bx::max<Ty>(), Ty(0) ) );
  109. REQUIRE( 1 == compareAscendingTest<Ty>(bx::max<Ty>(), bx::min<Ty>() ) );
  110. REQUIRE(-1 == compareAscendingTest<Ty>(_min, _max) );
  111. REQUIRE( 0 == compareAscendingTest<Ty>(_min, _min) );
  112. REQUIRE( 0 == compareAscendingTest<Ty>(_max, _max) );
  113. REQUIRE( 1 == compareAscendingTest<Ty>(_max, _min) );
  114. REQUIRE( 1 == compareDescendingTest<Ty>(_min, _max) );
  115. REQUIRE( 0 == compareDescendingTest<Ty>(_min, _min) );
  116. REQUIRE( 0 == compareDescendingTest<Ty>(_max, _max) );
  117. REQUIRE(-1 == compareDescendingTest<Ty>(_max, _min) );
  118. }
  119. TEST_CASE("sort-ComparisonFn", "")
  120. {
  121. compareTest< int8_t>( -13, 89);
  122. compareTest<int16_t>(-1389, 1389);
  123. compareTest<int32_t>(-1389, 1389);
  124. compareTest<int64_t>(-1389, 1389);
  125. compareTest< uint8_t>( 13, 89);
  126. compareTest<uint16_t>( 13, 1389);
  127. compareTest<uint32_t>( 13, 1389);
  128. compareTest<uint64_t>( 13, 1389);
  129. compareTest< float>(-13.89f, 1389.0f);
  130. compareTest<double>(-13.89f, 1389.0f);
  131. }