test_memory_allocator.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2016 UAVCAN Team
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. *
  22. * Contributors: https://github.com/UAVCAN/libcanard/contributors
  23. */
  24. #include <catch.hpp>
  25. #include "canard_internals.h"
  26. #define AVAILABLE_BLOCKS 3
  27. TEST_CASE("MemoryAllocatorTestGroup, FreeListIsConstructedCorrectly")
  28. {
  29. CanardPoolAllocator allocator;
  30. CanardPoolAllocatorBlock buffer[AVAILABLE_BLOCKS];
  31. initPoolAllocator(&allocator, buffer, AVAILABLE_BLOCKS);
  32. // Check that the memory list is constructed correctly.
  33. REQUIRE(&buffer[0] == allocator.free_list);
  34. REQUIRE(&buffer[1] == allocator.free_list->next);
  35. REQUIRE(&buffer[2] == allocator.free_list->next->next);
  36. REQUIRE(NULL == allocator.free_list->next->next->next);
  37. // Check statistics
  38. REQUIRE(AVAILABLE_BLOCKS == allocator.statistics.capacity_blocks);
  39. REQUIRE(0 == allocator.statistics.current_usage_blocks);
  40. REQUIRE(0 == allocator.statistics.peak_usage_blocks);
  41. }
  42. TEST_CASE("MemoryAllocatorTestGroup, CanAllocateBlock")
  43. {
  44. CanardPoolAllocator allocator;
  45. CanardPoolAllocatorBlock buffer[AVAILABLE_BLOCKS];
  46. initPoolAllocator(&allocator, buffer, AVAILABLE_BLOCKS);
  47. void* block = allocateBlock(&allocator);
  48. // Check that the first free memory block was used and that the next block is ready.
  49. REQUIRE(&buffer[0] == block);
  50. REQUIRE(&buffer[1] == allocator.free_list);
  51. // Check statistics
  52. REQUIRE(AVAILABLE_BLOCKS == allocator.statistics.capacity_blocks);
  53. REQUIRE(1 == allocator.statistics.current_usage_blocks);
  54. REQUIRE(1 == allocator.statistics.peak_usage_blocks);
  55. }
  56. TEST_CASE("MemoryAllocatorTestGroup, ReturnsNullIfThereIsNoBlockLeft")
  57. {
  58. CanardPoolAllocator allocator;
  59. CanardPoolAllocatorBlock buffer[AVAILABLE_BLOCKS];
  60. initPoolAllocator(&allocator, buffer, AVAILABLE_BLOCKS);
  61. // First exhaust all availables block
  62. for (int i = 0; i < AVAILABLE_BLOCKS; ++i)
  63. {
  64. allocateBlock(&allocator);
  65. }
  66. // Try to allocate one extra block
  67. void* block = allocateBlock(&allocator);
  68. REQUIRE(NULL == block);
  69. // Check statistics
  70. REQUIRE(AVAILABLE_BLOCKS == allocator.statistics.capacity_blocks);
  71. REQUIRE(AVAILABLE_BLOCKS == allocator.statistics.current_usage_blocks);
  72. REQUIRE(AVAILABLE_BLOCKS == allocator.statistics.peak_usage_blocks);
  73. }
  74. TEST_CASE("MemoryAllocatorTestGroup, CanFreeBlock")
  75. {
  76. CanardPoolAllocator allocator;
  77. CanardPoolAllocatorBlock buffer[AVAILABLE_BLOCKS];
  78. initPoolAllocator(&allocator, buffer, AVAILABLE_BLOCKS);
  79. void* block = allocateBlock(&allocator);
  80. freeBlock(&allocator, block);
  81. // Check that the block was added back to the beginning
  82. REQUIRE(&buffer[0] == allocator.free_list);
  83. REQUIRE(&buffer[1] == allocator.free_list->next);
  84. // Check statistics
  85. REQUIRE(AVAILABLE_BLOCKS == allocator.statistics.capacity_blocks);
  86. REQUIRE(0 == allocator.statistics.current_usage_blocks);
  87. REQUIRE(1 == allocator.statistics.peak_usage_blocks);
  88. }