UtilityTests.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "../../src/ErrorSimulator.h"
  2. #include "../Tests.h"
  3. #include "core/Utility.h"
  4. static void testPopCount() {
  5. CORE_TEST_U64(4, corePopCount(0xF));
  6. CORE_TEST_U64(0, corePopCount(0x0));
  7. CORE_TEST_U64(2, corePopCount(0x6));
  8. CORE_TEST_U64(7, corePopCount(0x7F));
  9. CORE_TEST_U64(3, corePopCount(0x2A));
  10. CORE_TEST_U64(32, corePopCount(0xFFFFFFFF));
  11. CORE_TEST_U64(64, corePopCount(0xFFFFFFFFFFFFFFFF));
  12. CORE_TEST_U64(44, corePopCount(0xFFFF0FFFFFFF));
  13. }
  14. static void testZeroRellocate() {
  15. void* buffer = coreReallocate(nullptr, 16);
  16. CORE_TEST_NOT_NULL(buffer);
  17. buffer = coreReallocate(buffer, 0);
  18. CORE_TEST_NULL(buffer);
  19. }
  20. static void testMemoryInfoList() {
  21. void* a = coreAllocate(8);
  22. void* b = coreAllocate(8);
  23. void* c = coreAllocate(8);
  24. void* d = coreAllocate(8);
  25. coreFree(b); // remove middle element
  26. coreFree(a); // remove first
  27. coreFree(d); // remove last
  28. coreFree(c); // remove single
  29. coreFree(nullptr);
  30. }
  31. void coreTestUtility() {
  32. testPopCount();
  33. testZeroRellocate();
  34. testMemoryInfoList();
  35. }
  36. static void outOfMemory(void*) {
  37. coreSetOutOfMemoryHandler(nullptr, nullptr);
  38. }
  39. void coreTestInvalidAllocate(void) {
  40. coreSetOutOfMemoryHandler(outOfMemory, nullptr);
  41. coreAllocate(0xFFFFFFFFFFF);
  42. CORE_TEST_TRUE(false);
  43. coreFinalizeTests();
  44. CORE_EXIT(0);
  45. }
  46. void coreTestInvalidReallocate(void) {
  47. coreSetOutOfMemoryHandler(outOfMemory, nullptr);
  48. void* p = coreAllocate(0xFF);
  49. corePrintMemoryReport();
  50. coreReallocate(p, 0xFFFFFFFFFFF);
  51. CORE_TEST_TRUE(false);
  52. coreFinalizeTests();
  53. CORE_EXIT(0);
  54. }
  55. void coreTestPreCanary(void) {
  56. #ifdef CORE_CHECK_MEMORY
  57. char* p = coreAllocate(16);
  58. p[-1] = 0;
  59. coreFree(p);
  60. CORE_TEST_TRUE(false);
  61. #endif
  62. coreFinalizeTests();
  63. CORE_EXIT(0);
  64. }
  65. void coreTestPostCanary(void) {
  66. #ifdef CORE_CHECK_MEMORY
  67. char* p = coreAllocate(16);
  68. p[17] = 0;
  69. coreFree(p);
  70. CORE_TEST_TRUE(false);
  71. #endif
  72. coreFinalizeTests();
  73. CORE_EXIT(0);
  74. }