sort_test.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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("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(-1 == 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(-1 == 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("lower/upperBound int32_t", "")
  61. {
  62. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 | 14
  63. const 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. const uint32_t resultLowerBound[] = { 0, 1, 4, 4, 5, 6, 9, 11, 12, 13 };
  66. const uint32_t resultUpperBound[] = { 1, 4, 4, 5, 6, 9, 11, 12, 13, 14 };
  67. STATIC_REQUIRE(10 == BX_COUNTOF(resultLowerBound) );
  68. STATIC_REQUIRE(10 == BX_COUNTOF(resultUpperBound) );
  69. for (int32_t key = test[0], keyMax = test[BX_COUNTOF(test)-1], ii = 0; key <= keyMax; ++key, ++ii)
  70. {
  71. REQUIRE(resultLowerBound[ii] == bx::lowerBound(key, test, BX_COUNTOF(test) ) );
  72. REQUIRE(resultUpperBound[ii] == bx::upperBound(key, test, BX_COUNTOF(test) ) );
  73. }
  74. }
  75. template<typename Ty>
  76. int32_t compareAscendingTest(const Ty& _lhs, const Ty& _rhs)
  77. {
  78. return bx::compareAscending<Ty>(&_lhs, &_rhs);
  79. }
  80. template<typename Ty>
  81. int32_t compareDescendingTest(const Ty& _lhs, const Ty& _rhs)
  82. {
  83. return bx::compareDescending<Ty>(&_lhs, &_rhs);
  84. }
  85. template<typename Ty>
  86. void compareTest(const Ty& _min, const Ty& _max)
  87. {
  88. REQUIRE(_min < _max);
  89. REQUIRE(-1 == compareAscendingTest<Ty>(bx::min<Ty>(), bx::max<Ty>() ) );
  90. REQUIRE(-1 == compareAscendingTest<Ty>(Ty(0), bx::max<Ty>() ) );
  91. REQUIRE( 0 == compareAscendingTest<Ty>(bx::min<Ty>(), bx::min<Ty>() ) );
  92. REQUIRE( 0 == compareAscendingTest<Ty>(bx::max<Ty>(), bx::max<Ty>() ) );
  93. REQUIRE( 1 == compareAscendingTest<Ty>(bx::max<Ty>(), Ty(0) ) );
  94. REQUIRE( 1 == compareAscendingTest<Ty>(bx::max<Ty>(), bx::min<Ty>() ) );
  95. REQUIRE(-1 == compareAscendingTest<Ty>(_min, _max) );
  96. REQUIRE( 0 == compareAscendingTest<Ty>(_min, _min) );
  97. REQUIRE( 0 == compareAscendingTest<Ty>(_max, _max) );
  98. REQUIRE( 1 == compareAscendingTest<Ty>(_max, _min) );
  99. REQUIRE( 1 == compareDescendingTest<Ty>(_min, _max) );
  100. REQUIRE( 0 == compareDescendingTest<Ty>(_min, _min) );
  101. REQUIRE( 0 == compareDescendingTest<Ty>(_max, _max) );
  102. REQUIRE(-1 == compareDescendingTest<Ty>(_max, _min) );
  103. }
  104. TEST_CASE("ComparisonFn", "")
  105. {
  106. compareTest< int8_t>( -13, 89);
  107. compareTest<int16_t>(-1389, 1389);
  108. compareTest<int32_t>(-1389, 1389);
  109. compareTest<int64_t>(-1389, 1389);
  110. compareTest< uint8_t>( 13, 89);
  111. compareTest<uint16_t>( 13, 1389);
  112. compareTest<uint32_t>( 13, 1389);
  113. compareTest<uint64_t>( 13, 1389);
  114. compareTest< float>(-13.89f, 1389.0f);
  115. compareTest<double>(-13.89f, 1389.0f);
  116. }