UtilityTests.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 testNegativeArguments() {
  20. char from[16];
  21. Core::memorySet(from, 1, sizeof(from));
  22. Core::memorySet(from, 0, -1);
  23. char to[16];
  24. Core::memorySet(to, 1, sizeof(to));
  25. Core::memoryCopy(to, from, -1);
  26. CORE_TEST_TRUE(Core::memoryCompare(from, to, sizeof(from)));
  27. }
  28. static void testNegativeRellocate() {
  29. char* buffer = nullptr;
  30. CORE_TEST_ERROR(Core::reallocate(buffer, 16));
  31. CORE_TEST_TRUE(buffer != nullptr);
  32. CORE_TEST_ERROR(Core::reallocate(buffer, -1));
  33. CORE_TEST_TRUE(buffer == nullptr);
  34. }
  35. static void testReallocateFail() {
  36. #ifdef ERROR_SIMULATOR
  37. char* buffer = nullptr;
  38. Core::Fail::realloc = true;
  39. CORE_TEST_EQUAL(Core::Error::OUT_OF_MEMORY, Core::reallocate(buffer, 16));
  40. CORE_TEST_TRUE(buffer == nullptr);
  41. Core::Fail::realloc = false;
  42. #endif
  43. }
  44. void Core::testUtility() {
  45. testPopCount();
  46. testIf();
  47. testNegativeArguments();
  48. testNegativeRellocate();
  49. testReallocateFail();
  50. }