UtilityTests.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. char* buffer = nullptr;
  37. Core::Fail::realloc = true;
  38. CORE_TEST_EQUAL(Core::Error::OUT_OF_MEMORY, Core::reallocate(buffer, 16));
  39. CORE_TEST_TRUE(buffer == nullptr);
  40. Core::Fail::realloc = false;
  41. }
  42. void Core::testUtility() {
  43. testPopCount();
  44. testIf();
  45. testNegativeArguments();
  46. testNegativeRellocate();
  47. testReallocateFail();
  48. }