UtilityTests.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "../../src/ErrorSimulator.hpp"
  2. #include "../Tests.hpp"
  3. #include "core/utils/Utility.hpp"
  4. static void testPopCount() {
  5. CORE_TEST_EQUAL(4, Core::popCount(0xF));
  6. CORE_TEST_EQUAL(0, Core::popCount(0x0));
  7. CORE_TEST_EQUAL(2, Core::popCount(0x6));
  8. CORE_TEST_EQUAL(7, Core::popCount(0x7F));
  9. CORE_TEST_EQUAL(3, Core::popCount(0x2A));
  10. CORE_TEST_EQUAL(32, Core::popCount(0xFFFFFFFF));
  11. CORE_TEST_EQUAL(64, Core::popCount(0xFFFFFFFFFFFFFFFFL));
  12. CORE_TEST_EQUAL(44, Core::popCount(0xFFFF0FFFFFFF));
  13. CORE_TEST_EQUAL(32, Core::popCount(-1));
  14. }
  15. static void testIf() {
  16. CORE_TEST_TRUE((Core::IsSame<Core::If<true, int, double>, int>));
  17. CORE_TEST_TRUE((Core::IsSame<Core::If<false, int, double>, double>));
  18. }
  19. static void testZeroRellocate() {
  20. char* buffer = nullptr;
  21. CORE_TEST_ERROR(Core::reallocate(buffer, 16));
  22. CORE_TEST_TRUE(buffer != nullptr);
  23. CORE_TEST_ERROR(Core::reallocate(buffer, 0));
  24. CORE_TEST_TRUE(buffer == nullptr);
  25. }
  26. static void testReallocateFail() {
  27. #ifdef ERROR_SIMULATOR
  28. char* buffer = nullptr;
  29. Core::Fail::realloc = true;
  30. CORE_TEST_EQUAL(Core::ErrorCode::OUT_OF_MEMORY,
  31. Core::reallocate(buffer, 16));
  32. CORE_TEST_TRUE(buffer == nullptr);
  33. Core::Fail::realloc = false;
  34. #endif
  35. }
  36. void Core::testUtility() {
  37. testPopCount();
  38. testIf();
  39. testZeroRellocate();
  40. testReallocateFail();
  41. }