sort_test.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright 2010-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include "test.h"
  6. #include <bx/sort.h>
  7. #include <bx/string.h>
  8. #include <bx/rng.h>
  9. TEST_CASE("quickSort", "")
  10. {
  11. const char* str[] =
  12. {
  13. "jabuka",
  14. "kruska",
  15. "malina",
  16. "jagoda",
  17. };
  18. bx::quickSort(str, BX_COUNTOF(str), sizeof(void*)
  19. , [](const void* _lhs, const void* _rhs)
  20. {
  21. const char* lhs = *(const char**)_lhs;
  22. const char* rhs = *(const char**)_rhs;
  23. return bx::strCmp(lhs, rhs);
  24. });
  25. REQUIRE(0 == bx::strCmp(str[0], "jabuka") );
  26. REQUIRE(0 == bx::strCmp(str[1], "jagoda") );
  27. REQUIRE(0 == bx::strCmp(str[2], "kruska") );
  28. REQUIRE(0 == bx::strCmp(str[3], "malina") );
  29. int8_t byte[128];
  30. bx::RngMwc rng;
  31. for (uint32_t ii = 0; ii < BX_COUNTOF(byte); ++ii)
  32. {
  33. byte[ii] = rng.gen()&0xff;
  34. }
  35. bx::quickSort(byte, BX_COUNTOF(byte), 1
  36. , [](const void* _lhs, const void* _rhs)
  37. {
  38. int8_t lhs = *(const int8_t*)_lhs;
  39. int8_t rhs = *(const int8_t*)_rhs;
  40. return lhs - rhs;
  41. });
  42. for (uint32_t ii = 1; ii < BX_COUNTOF(byte); ++ii)
  43. {
  44. REQUIRE(byte[ii-1] <= byte[ii]);
  45. }
  46. }