#include #include #include namespace NYT { namespace { //////////////////////////////////////////////////////////////////////////////// TEST(CompactHeapTest, Simple) { TCompactHeap heap; heap.push(3); heap.push(1); heap.push(2); EXPECT_EQ(3u, heap.size()); EXPECT_EQ(1, heap.extract_min()); EXPECT_EQ(2, heap.get_min()); EXPECT_EQ(2, heap.extract_min()); EXPECT_EQ(3, heap.extract_min()); EXPECT_TRUE(heap.empty()); } TEST(CompactHeapTest, SimpleComparator) { TCompactHeap> heap; heap.push(3); heap.push(1); heap.push(2); EXPECT_EQ(3u, heap.size()); EXPECT_EQ(3, heap.extract_min()); EXPECT_EQ(2, heap.get_min()); EXPECT_EQ(2, heap.extract_min()); EXPECT_EQ(1, heap.extract_min()); EXPECT_TRUE(heap.empty()); } TEST(CompactHeapTest, Capacity) { TCompactHeap heap; EXPECT_EQ(2u, heap.capacity()); EXPECT_EQ(0u, heap.size()); for (int i = 0; i < 100; ++i) { heap.push(i); } EXPECT_LE(100u, heap.capacity()); EXPECT_EQ(100u, heap.size()); for (int i = 0; i < 99; ++i) { heap.pop(); } EXPECT_LE(100u, heap.capacity()); EXPECT_EQ(1u, heap.size()); heap.shrink_to_small(); EXPECT_EQ(2u, heap.capacity()); EXPECT_EQ(1u, heap.size()); } TEST(CompactHeapTest, Stress) { TCompactHeap> heap; std::vector values; std::mt19937 rng(42); for (int iteration = 0; iteration < 1000; ++iteration) { EXPECT_EQ(values.size(), heap.size()); EXPECT_EQ(values.empty(), heap.empty()); { std::vector content(heap.begin(), heap.end()); std::sort(content.rbegin(), content.rend()); EXPECT_EQ(values, content); } if (!values.empty()) { EXPECT_EQ(values[0], heap.get_min()); } if (values.empty() || rng() % 2 == 0) { int x = rng() % 100; heap.push(x); values.push_back(x); std::sort(values.rbegin(), values.rend()); } else { if (rng() % 2 == 0) { EXPECT_EQ(values[0], heap.extract_min()); } else { heap.pop(); } values.erase(values.begin()); } } } //////////////////////////////////////////////////////////////////////////////// } // namespace } // namespace NYT