Size: 5787 bytes.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// cs/q/heap/heap_test.cc
extern "C" {
#include "heap.h"

#include "limits.h"
}

#include <stdio.h>

#include <iostream>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "heap.hh"

namespace {  // use_usings
using ::testing::AtLeast;
using ::testing::Eq;
using ::testing::FieldsAre;
using ::testing::FloatEq;
using ::testing::IsFalse;
using ::testing::IsTrue;
using ::testing::Matcher;
using ::testing::Not;
using ::testing::StrEq;
}  // namespace

namespace {
TEST(NewHeap, Success) {
  int capacity = 3;
  Heap* heap = NewHeap(capacity);
  EXPECT_THAT(heap, Not(Eq((Heap*)NULL)));
  EXPECT_THAT(heap->capacity, Eq(3));
  EXPECT_THAT(heap->size, Eq(0));
  EXPECT_THAT(heap->values, Not(Eq((int*)NULL)));
}

TEST(HeapifyUp, NoChange) {
  int capacity = 3;
  Heap* heap = NewHeap(capacity);
  heap->values[1] = 10;
  heap->values[2] = 9;
  heap->values[3] = 8;
  HeapifyUp(heap, 1);
  EXPECT_THAT(heap->values[1], Eq(10));
  EXPECT_THAT(heap->values[2], Eq(9));
  EXPECT_THAT(heap->values[3], Eq(8));
}

TEST(Heap, SortsValues) {
  int capacity = 3;
  Heap* heap = NewHeap(capacity);
  // 2 -> 3 -> 1 should be saved as [0, 3, 2, 1]
  EXPECT_THAT(Insert(heap, 2), IsTrue());
  EXPECT_THAT(Insert(heap, 3), IsTrue());
  EXPECT_THAT(Insert(heap, 1), IsTrue());
  EXPECT_THAT(Insert(heap, 1), IsFalse());
  EXPECT_THAT(heap->values[0], Eq(0));
  EXPECT_THAT(heap->values[1], Eq(3));
  EXPECT_THAT(heap->values[2], Eq(2));
  EXPECT_THAT(heap->values[3], Eq(1));
  // Pop elements
  EXPECT_THAT(PeekMax(heap), Eq(3));
  EXPECT_THAT(PopMax(heap), Eq(3));
  EXPECT_THAT(PopMax(heap), Eq(2));
  EXPECT_THAT(PopMax(heap), Eq(1));
  EXPECT_THAT(PopMax(heap), Eq(INT_MIN));
}

// 1. Small heap, single insert
TEST(Heap, SingleInsertAndPop) {
  int capacity = 1;
  Heap* heap = NewHeap(capacity);
  EXPECT_THAT(Insert(heap, 42), IsTrue());
  EXPECT_THAT(PeekMax(heap), Eq(42));
  EXPECT_THAT(PopMax(heap), Eq(42));
  EXPECT_THAT(PopMax(heap), Eq(INT_MIN));
}

// 2. Insertion beyond capacity
TEST(Heap, CapacityExceeded) {
  int capacity = 2;
  Heap* heap = NewHeap(capacity);
  EXPECT_THAT(Insert(heap, 5), IsTrue());
  EXPECT_THAT(Insert(heap, 10), IsTrue());
  EXPECT_THAT(Insert(heap, 3),
              IsFalse());  // exceeds capacity
}

// 3. Descending order after mixed inserts
TEST(Heap, SortsDescending) {
  int capacity = 4;
  Heap* heap = NewHeap(capacity);
  EXPECT_THAT(Insert(heap, 7), IsTrue());
  EXPECT_THAT(Insert(heap, 1), IsTrue());
  EXPECT_THAT(Insert(heap, 9), IsTrue());
  EXPECT_THAT(Insert(heap, 3), IsTrue());
  EXPECT_THAT(PopMax(heap), Eq(9));
  EXPECT_THAT(PopMax(heap), Eq(7));
  EXPECT_THAT(PopMax(heap), Eq(3));
  EXPECT_THAT(PopMax(heap), Eq(1));
}

// 4. Duplicate values allowed
TEST(Heap, HandlesDuplicates) {
  int capacity = 5;
  Heap* heap = NewHeap(capacity);
  EXPECT_THAT(Insert(heap, 4), IsTrue());
  EXPECT_THAT(Insert(heap, 4), IsTrue());
  EXPECT_THAT(Insert(heap, 4), IsTrue());
  EXPECT_THAT(PopMax(heap), Eq(4));
  EXPECT_THAT(PopMax(heap), Eq(4));
  EXPECT_THAT(PopMax(heap), Eq(4));
}

// 5. Pop from empty
TEST(Heap, EmptyPop) {
  int capacity = 2;
  Heap* heap = NewHeap(capacity);
  EXPECT_THAT(PopMax(heap), Eq(INT_MIN));
}

// 6. Peek from empty
TEST(Heap, EmptyPeek) {
  int capacity = 2;
  Heap* heap = NewHeap(capacity);
  EXPECT_THAT(PeekMax(heap), Eq(INT_MIN));
}

// 7. Min and Max tracked correctly
TEST(Heap, PeekMax) {
  int capacity = 3;
  Heap* heap = NewHeap(capacity);
  Insert(heap, 10);
  Insert(heap, 20);
  Insert(heap, 5);
  EXPECT_THAT(PeekMax(heap), Eq(20));
}

// 8. Insert decreasing sequence
TEST(Heap, InsertDescending) {
  int capacity = 5;
  Heap* heap = NewHeap(capacity);
  EXPECT_TRUE(Insert(heap, 5));
  EXPECT_TRUE(Insert(heap, 4));
  EXPECT_TRUE(Insert(heap, 3));
  EXPECT_TRUE(Insert(heap, 2));
  EXPECT_TRUE(Insert(heap, 1));
  EXPECT_THAT(PopMax(heap), Eq(5));
  EXPECT_THAT(PopMax(heap), Eq(4));
  EXPECT_THAT(PopMax(heap), Eq(3));
  EXPECT_THAT(PopMax(heap), Eq(2));
  EXPECT_THAT(PopMax(heap), Eq(1));
}

// 9. Insert increasing sequence
TEST(Heap, InsertAscending) {
  int capacity = 5;
  Heap* heap = NewHeap(capacity);
  Insert(heap, 1);
  Insert(heap, 2);
  Insert(heap, 3);
  Insert(heap, 4);
  Insert(heap, 5);
  EXPECT_THAT(PopMax(heap), Eq(5));
  EXPECT_THAT(PopMax(heap), Eq(4));
  EXPECT_THAT(PopMax(heap), Eq(3));
  EXPECT_THAT(PopMax(heap), Eq(2));
  EXPECT_THAT(PopMax(heap), Eq(1));
}

// 10. Stress small capacity
TEST(Heap, StressTinyHeap) {
  int capacity = 1;
  Heap* heap = NewHeap(capacity);
  EXPECT_THAT(Insert(heap, 100), IsTrue());
  EXPECT_THAT(Insert(heap, 200), IsFalse());
  EXPECT_THAT(PopMax(heap), Eq(100));
  EXPECT_THAT(PopMax(heap), Eq(INT_MIN));
}

// 11. Negative numbers
TEST(Heap, HandlesNegativeValues) {
  int capacity = 4;
  Heap* heap = NewHeap(capacity);
  Insert(heap, -5);
  Insert(heap, -1);
  Insert(heap, -10);
  Insert(heap, -3);
  EXPECT_THAT(PopMax(heap), Eq(-1));
  EXPECT_THAT(PopMax(heap), Eq(-3));
  EXPECT_THAT(PopMax(heap), Eq(-5));
  EXPECT_THAT(PopMax(heap), Eq(-10));
}

// 12. Interleaved inserts and pops
TEST(Heap, InterleavedOps) {
  int capacity = 5;
  Heap* heap = NewHeap(capacity);
  Insert(heap, 8);
  Insert(heap, 2);
  EXPECT_THAT(PopMax(heap), Eq(8));
  Insert(heap, 10);
  Insert(heap, 1);
  EXPECT_THAT(PopMax(heap), Eq(10));
  EXPECT_THAT(PopMax(heap), Eq(2));
  EXPECT_THAT(PopMax(heap), Eq(1));
}

// Test C++ implementation
TEST(MaxHeap, Basic) {
  cs::q::MaxHeap<int> heap{3};
  EXPECT_TRUE(heap.Insert(3, 3));
  EXPECT_TRUE(heap.Insert(2, 2));
  EXPECT_TRUE(heap.Insert(1, 1));
  EXPECT_FALSE(heap.Insert(1, 1));
  EXPECT_THAT(heap.Pop(), FieldsAre(3, 3));
  EXPECT_THAT(heap.Pop(), FieldsAre(2, 2));
  EXPECT_THAT(heap.Pop(), FieldsAre(1, 1));
  EXPECT_THAT(heap.Pop(), FieldsAre(0, INT_MIN));
}

}  // namespace
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-2:8080 in 2ms.