mkql_alloc_ut.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "mkql_alloc.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. namespace NKikimr {
  4. namespace NMiniKQL {
  5. Y_UNIT_TEST_SUITE(TMiniKQLAllocTest) {
  6. Y_UNIT_TEST(TestPagedArena) {
  7. TAlignedPagePool pagePool(__LOCATION__);
  8. {
  9. TPagedArena arena(&pagePool);
  10. auto p1 = arena.Alloc(10);
  11. auto p2 = arena.Alloc(20);
  12. auto p3 = arena.Alloc(100000);
  13. auto p4 = arena.Alloc(30);
  14. arena.Clear();
  15. auto p5 = arena.Alloc(40);
  16. Y_UNUSED(p1);
  17. Y_UNUSED(p2);
  18. Y_UNUSED(p3);
  19. Y_UNUSED(p4);
  20. Y_UNUSED(p5);
  21. TPagedArena arena2 = std::move(arena);
  22. auto p6 = arena2.Alloc(50);
  23. Y_UNUSED(p6);
  24. }
  25. }
  26. Y_UNIT_TEST(TestDeallocated) {
  27. TScopedAlloc alloc(__LOCATION__);
  28. void* p1 = TWithDefaultMiniKQLAlloc::AllocWithSize(10);
  29. void* p2 = TWithDefaultMiniKQLAlloc::AllocWithSize(20);
  30. UNIT_ASSERT_VALUES_EQUAL(alloc.Ref().GetUsed(), TAlignedPagePool::POOL_PAGE_SIZE);
  31. UNIT_ASSERT_VALUES_EQUAL(alloc.Ref().GetDeallocatedInPages(), 0);
  32. UNIT_ASSERT_VALUES_EQUAL(alloc.Ref().GetFreePageCount(), 0);
  33. TWithDefaultMiniKQLAlloc::FreeWithSize(p1, 10);
  34. UNIT_ASSERT_VALUES_EQUAL(alloc.Ref().GetUsed(), TAlignedPagePool::POOL_PAGE_SIZE);
  35. UNIT_ASSERT_VALUES_EQUAL(alloc.Ref().GetDeallocatedInPages(), 10);
  36. UNIT_ASSERT_VALUES_EQUAL(alloc.Ref().GetFreePageCount(), 0);
  37. TWithDefaultMiniKQLAlloc::FreeWithSize(p2, 20);
  38. UNIT_ASSERT_VALUES_EQUAL(alloc.Ref().GetUsed(), 0);
  39. UNIT_ASSERT_VALUES_EQUAL(alloc.Ref().GetDeallocatedInPages(), 0);
  40. UNIT_ASSERT_VALUES_EQUAL(alloc.Ref().GetFreePageCount(), 1);
  41. p1 = TWithDefaultMiniKQLAlloc::AllocWithSize(10);
  42. UNIT_ASSERT_VALUES_EQUAL(alloc.Ref().GetUsed(), TAlignedPagePool::POOL_PAGE_SIZE);
  43. UNIT_ASSERT_VALUES_EQUAL(alloc.Ref().GetDeallocatedInPages(), 0);
  44. UNIT_ASSERT_VALUES_EQUAL(alloc.Ref().GetFreePageCount(), 0);
  45. TWithDefaultMiniKQLAlloc::FreeWithSize(p1, 10);
  46. UNIT_ASSERT_VALUES_EQUAL(alloc.Ref().GetUsed(), 0);
  47. UNIT_ASSERT_VALUES_EQUAL(alloc.Ref().GetDeallocatedInPages(), 0);
  48. UNIT_ASSERT_VALUES_EQUAL(alloc.Ref().GetFreePageCount(), 1);
  49. }
  50. Y_UNIT_TEST(FreeInWrongAllocator) {
  51. if (true) {
  52. return;
  53. }
  54. TScopedAlloc alloc1(__LOCATION__);
  55. void* p1 = TWithDefaultMiniKQLAlloc::AllocWithSize(10);
  56. void* p2 = TWithDefaultMiniKQLAlloc::AllocWithSize(10);
  57. {
  58. TScopedAlloc alloc2(__LOCATION__);
  59. TWithDefaultMiniKQLAlloc::FreeWithSize(p1, 10);
  60. }
  61. TWithDefaultMiniKQLAlloc::FreeWithSize(p2, 10);
  62. }
  63. Y_UNIT_TEST(InitiallyAcqured) {
  64. {
  65. TScopedAlloc alloc(__LOCATION__);
  66. UNIT_ASSERT_VALUES_EQUAL(true, alloc.IsAttached());
  67. {
  68. auto guard = Guard(alloc);
  69. UNIT_ASSERT_VALUES_EQUAL(true, alloc.IsAttached());
  70. }
  71. UNIT_ASSERT_VALUES_EQUAL(true, alloc.IsAttached());
  72. }
  73. {
  74. TScopedAlloc alloc(__LOCATION__, TAlignedPagePoolCounters(), false, false);
  75. UNIT_ASSERT_VALUES_EQUAL(false, alloc.IsAttached());
  76. {
  77. auto guard = Guard(alloc);
  78. UNIT_ASSERT_VALUES_EQUAL(true, alloc.IsAttached());
  79. }
  80. UNIT_ASSERT_VALUES_EQUAL(false, alloc.IsAttached());
  81. }
  82. }
  83. }
  84. }
  85. }