Skip to content

Commit

Permalink
test: Extend tests for OperandStack
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Sep 15, 2020
1 parent 7496f77 commit e6917fa
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions test/unittests/stack_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
using namespace fizzy;
using namespace testing;

namespace
{
intptr_t address_diff(const void* a, const void* b) noexcept
{
return std::abs(reinterpret_cast<intptr_t>(a) - reinterpret_cast<intptr_t>(b));
}
} // namespace

TEST(stack, push_and_pop)
{
Stack<char> stack;
Expand Down Expand Up @@ -146,6 +154,8 @@ TEST(operand_stack, top)
TEST(operand_stack, small)
{
OperandStack stack({}, 0, 3);
ASSERT_LT(address_diff(&stack, stack.rbegin()), 100) << "not allocated on the system stack";

EXPECT_EQ(stack.size(), 0);

stack.push(1);
Expand All @@ -171,10 +181,61 @@ TEST(operand_stack, small)
EXPECT_EQ(stack.top().i64, 12);
}

TEST(operand_stack, small_with_locals)
{
const fizzy::Value args[] = {0xa1, 0xa2};
OperandStack stack(args, 3, 1);
ASSERT_LT(address_diff(&stack, stack.rbegin()), 100) << "not allocated on the system stack";

EXPECT_EQ(stack.size(), 0);

stack.push(0xff);
EXPECT_EQ(stack.size(), 1);
EXPECT_EQ(stack.top().i64, 0xff);
EXPECT_EQ(stack[0].i64, 0xff);

EXPECT_EQ(stack.local(0).i64, 0xa1);
EXPECT_EQ(stack.local(1).i64, 0xa2);
EXPECT_EQ(stack.local(2).i64, 0);
EXPECT_EQ(stack.local(3).i64, 0);
EXPECT_EQ(stack.local(4).i64, 0);

stack.local(0) = 0xc0;
stack.local(1) = 0xc1;
stack.local(2) = 0xc2;
stack.local(3) = 0xc3;
stack.local(4) = 0xc4;

EXPECT_EQ(stack.local(0).i64, 0xc0);
EXPECT_EQ(stack.local(1).i64, 0xc1);
EXPECT_EQ(stack.local(2).i64, 0xc2);
EXPECT_EQ(stack.local(3).i64, 0xc3);
EXPECT_EQ(stack.local(4).i64, 0xc4);
}

TEST(operand_stack, large)
{
constexpr auto max_height = 33;
OperandStack stack({}, 0, max_height);
ASSERT_GT(address_diff(&stack, stack.rbegin()), 100) << "not allocated on the heap";

for (unsigned i = 0; i < max_height; ++i)
stack.push(i);

EXPECT_EQ(stack.size(), max_height);
for (int expected = max_height - 1; expected >= 0; --expected)
EXPECT_EQ(stack.pop().i64, expected);
EXPECT_EQ(stack.size(), 0);
}

TEST(operand_stack, large_with_locals)
{
const fizzy::Value args[] = {0xa1, 0xa2};
constexpr auto max_height = 33;
constexpr auto num_locals = 5;
constexpr auto num_args = std::size(args);
OperandStack stack(args, num_locals, max_height);
ASSERT_GT(address_diff(&stack, stack.rbegin()), 100) << "not allocated on the heap";

for (unsigned i = 0; i < max_height; ++i)
stack.push(i);
Expand All @@ -183,8 +244,20 @@ TEST(operand_stack, large)
for (int expected = max_height - 1; expected >= 0; --expected)
EXPECT_EQ(stack.pop().i64, expected);
EXPECT_EQ(stack.size(), 0);

EXPECT_EQ(stack.local(0).i64, 0xa1);
EXPECT_EQ(stack.local(1).i64, 0xa2);

for (unsigned i = 0; i < num_locals; ++i)
EXPECT_EQ(stack.local(num_args + i).i64, 0);

for (unsigned i = 0; i < num_args + num_locals; ++i)
stack.local(i) = fizzy::Value{i};
for (unsigned i = 0; i < num_args + num_locals; ++i)
EXPECT_EQ(stack.local(i).i64, i);
}


TEST(operand_stack, rbegin_rend)
{
OperandStack stack({}, 0, 3);
Expand Down

0 comments on commit e6917fa

Please sign in to comment.