Skip to content

Commit dc13f1d

Browse files
committed
test: Extend tests for OperandStack
1 parent 8564199 commit dc13f1d

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

test/unittests/stack_test.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
using namespace fizzy;
99
using namespace testing;
1010

11+
namespace
12+
{
13+
intptr_t address_diff(const void* a, const void* b) noexcept
14+
{
15+
return std::abs(reinterpret_cast<intptr_t>(a) - reinterpret_cast<intptr_t>(b));
16+
}
17+
} // namespace
18+
1119
TEST(stack, push_and_pop)
1220
{
1321
Stack<char> stack;
@@ -146,6 +154,8 @@ TEST(operand_stack, top)
146154
TEST(operand_stack, small)
147155
{
148156
OperandStack stack({}, 0, 3);
157+
ASSERT_LT(address_diff(&stack, stack.rbegin()), 100) << "not allocated on the system stack";
158+
149159
EXPECT_EQ(stack.size(), 0);
150160

151161
stack.push(1);
@@ -171,10 +181,61 @@ TEST(operand_stack, small)
171181
EXPECT_EQ(stack.top().i64, 12);
172182
}
173183

184+
TEST(operand_stack, small_with_locals)
185+
{
186+
const fizzy::Value args[] = {0xa1, 0xa2};
187+
OperandStack stack(args, 3, 1);
188+
ASSERT_LT(address_diff(&stack, stack.rbegin()), 100) << "not allocated on the system stack";
189+
190+
EXPECT_EQ(stack.size(), 0);
191+
192+
stack.push(0xff);
193+
EXPECT_EQ(stack.size(), 1);
194+
EXPECT_EQ(stack.top().i64, 0xff);
195+
EXPECT_EQ(stack[0].i64, 0xff);
196+
197+
EXPECT_EQ(stack.local(0).i64, 0xa1);
198+
EXPECT_EQ(stack.local(1).i64, 0xa2);
199+
EXPECT_EQ(stack.local(2).i64, 0);
200+
EXPECT_EQ(stack.local(3).i64, 0);
201+
EXPECT_EQ(stack.local(4).i64, 0);
202+
203+
stack.local(0) = 0xc0;
204+
stack.local(1) = 0xc1;
205+
stack.local(2) = 0xc2;
206+
stack.local(3) = 0xc3;
207+
stack.local(4) = 0xc4;
208+
209+
EXPECT_EQ(stack.local(0).i64, 0xc0);
210+
EXPECT_EQ(stack.local(1).i64, 0xc1);
211+
EXPECT_EQ(stack.local(2).i64, 0xc2);
212+
EXPECT_EQ(stack.local(3).i64, 0xc3);
213+
EXPECT_EQ(stack.local(4).i64, 0xc4);
214+
}
215+
174216
TEST(operand_stack, large)
175217
{
176218
constexpr auto max_height = 33;
177219
OperandStack stack({}, 0, max_height);
220+
ASSERT_GT(address_diff(&stack, stack.rbegin()), 100) << "not allocated on the heap";
221+
222+
for (unsigned i = 0; i < max_height; ++i)
223+
stack.push(i);
224+
225+
EXPECT_EQ(stack.size(), max_height);
226+
for (int expected = max_height - 1; expected >= 0; --expected)
227+
EXPECT_EQ(stack.pop().i64, expected);
228+
EXPECT_EQ(stack.size(), 0);
229+
}
230+
231+
TEST(operand_stack, large_with_locals)
232+
{
233+
const fizzy::Value args[] = {0xa1, 0xa2};
234+
constexpr auto max_height = 33;
235+
constexpr auto num_locals = 5;
236+
constexpr auto num_args = std::size(args);
237+
OperandStack stack(args, num_locals, max_height);
238+
ASSERT_GT(address_diff(&stack, stack.rbegin()), 100) << "not allocated on the heap";
178239

179240
for (unsigned i = 0; i < max_height; ++i)
180241
stack.push(i);
@@ -183,8 +244,20 @@ TEST(operand_stack, large)
183244
for (int expected = max_height - 1; expected >= 0; --expected)
184245
EXPECT_EQ(stack.pop().i64, expected);
185246
EXPECT_EQ(stack.size(), 0);
247+
248+
EXPECT_EQ(stack.local(0).i64, 0xa1);
249+
EXPECT_EQ(stack.local(1).i64, 0xa2);
250+
251+
for (unsigned i = 0; i < num_locals; ++i)
252+
EXPECT_EQ(stack.local(num_args + i).i64, 0);
253+
254+
for (unsigned i = 0; i < num_args + num_locals; ++i)
255+
stack.local(i) = fizzy::Value{i};
256+
for (unsigned i = 0; i < num_args + num_locals; ++i)
257+
EXPECT_EQ(stack.local(i).i64, i);
186258
}
187259

260+
188261
TEST(operand_stack, rbegin_rend)
189262
{
190263
OperandStack stack({}, 0, 3);

0 commit comments

Comments
 (0)