UtilityTests.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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::ErrorCode::OUT_OF_MEMORY,
  40. Core::reallocate(buffer, 16));
  41. CORE_TEST_TRUE(buffer == nullptr);
  42. Core::Fail::realloc = false;
  43. #endif
  44. }
  45. void Core::testUtility() {
  46. testPopCount();
  47. testIf();
  48. testNegativeArguments();
  49. testNegativeRellocate();
  50. testReallocateFail();
  51. }