UtilityTests.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <stdlib.h>
  2. #include "../../src/ErrorSimulator.hpp"
  3. #include "../Tests.hpp"
  4. #include "core/utils/Utility.hpp"
  5. static void testPopCount() {
  6. CORE_TEST_EQUAL(4, Core::popCount(0xF));
  7. CORE_TEST_EQUAL(0, Core::popCount(0x0));
  8. CORE_TEST_EQUAL(2, Core::popCount(0x6));
  9. CORE_TEST_EQUAL(7, Core::popCount(0x7F));
  10. CORE_TEST_EQUAL(3, Core::popCount(0x2A));
  11. CORE_TEST_EQUAL(32, Core::popCount(0xFFFFFFFF));
  12. CORE_TEST_EQUAL(64, Core::popCount(0xFFFFFFFFFFFFFFFFL));
  13. CORE_TEST_EQUAL(44, Core::popCount(0xFFFF0FFFFFFF));
  14. CORE_TEST_EQUAL(32, Core::popCount(-1));
  15. }
  16. static void testIf() {
  17. CORE_TEST_TRUE((Core::IsSame<Core::If<true, int, double>, int>));
  18. CORE_TEST_TRUE((Core::IsSame<Core::If<false, int, double>, double>));
  19. }
  20. #ifdef ERROR_SIMULATOR
  21. static void onOutOfMemory(void* p) {
  22. *static_cast<bool*>(p) = true;
  23. }
  24. #endif
  25. static void testAllocateFail() {
  26. #ifdef ERROR_SIMULATOR
  27. Core::Fail::alloc = true;
  28. bool check = false;
  29. Core::setOutOfMemoryHandler(onOutOfMemory, &check);
  30. void* p = Core::allocate(16);
  31. CORE_TEST_NOT_NULL(p);
  32. CORE_TEST_TRUE(check);
  33. Core::Fail::alloc = false;
  34. Core::setOutOfMemoryHandler(nullptr, nullptr);
  35. free(p);
  36. #endif
  37. }
  38. static void testZeroRellocate() {
  39. void* buffer = Core::reallocate(nullptr, 16);
  40. CORE_TEST_NOT_NULL(buffer);
  41. buffer = Core::reallocate(buffer, 0);
  42. CORE_TEST_NULL(buffer);
  43. }
  44. static void testReallocateFail() {
  45. #ifdef ERROR_SIMULATOR
  46. Core::Fail::alloc = true;
  47. bool check = false;
  48. Core::setOutOfMemoryHandler(onOutOfMemory, &check);
  49. void* p = Core::reallocate(nullptr, 16);
  50. CORE_TEST_NOT_NULL(p);
  51. CORE_TEST_TRUE(check);
  52. Core::Fail::alloc = false;
  53. Core::setOutOfMemoryHandler(nullptr, nullptr);
  54. free(p);
  55. #endif
  56. }
  57. void Core::testUtility() {
  58. testPopCount();
  59. testIf();
  60. testAllocateFail();
  61. testZeroRellocate();
  62. testReallocateFail();
  63. }