Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allocate space for locals together with stack #382

Merged
merged 3 commits into from
Sep 23, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
test: Extend tests for OperandStack
chfast committed Sep 23, 2020
commit 642359f1aad33d9194844bfb6acaf54e402dd722
113 changes: 113 additions & 0 deletions test/unittests/stack_test.cpp
Original file line number Diff line number Diff line change
@@ -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;
@@ -134,6 +142,11 @@ TEST(operand_stack, top)
EXPECT_EQ(stack.top().i64, 101);
EXPECT_EQ(stack[0].i64, 101);

stack.drop(0);
EXPECT_EQ(stack.size(), 1);
EXPECT_EQ(stack.top().i64, 101);
EXPECT_EQ(stack[0].i64, 101);

stack.drop(1);
EXPECT_EQ(stack.size(), 0);

@@ -146,6 +159,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);
@@ -171,20 +186,97 @@ 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);

EXPECT_EQ(stack.pop().i64, 0xff);
EXPECT_EQ(stack.size(), 0);
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";
Copy link
Member

@axic axic Sep 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this GT while the others LT? Ah I see heap in the message.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 100 is arbitrary.


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);

EXPECT_EQ(stack.size(), max_height);
for (unsigned i = 0; i < max_height; ++i)
EXPECT_EQ(stack[i].i64, max_height - i - 1);

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

for (unsigned i = num_args; i < num_args + num_locals; ++i)
EXPECT_EQ(stack.local(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);

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

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);
@@ -198,6 +290,27 @@ TEST(operand_stack, rbegin_rend)
EXPECT_EQ((stack.rend() - 1)->i64, 3);
}

TEST(operand_stack, rbegin_rend_locals)
{
const fizzy::Value args[] = {0xa1};
OperandStack stack(args, 4, 2);
EXPECT_EQ(stack.rbegin(), stack.rend());

stack.push(1);
EXPECT_LT(stack.rbegin(), stack.rend());
EXPECT_EQ(stack.rend() - stack.rbegin(), 1);
EXPECT_EQ(stack.rbegin()->i64, 1);
EXPECT_EQ((stack.rend() - 1)->i64, 1);

stack.push(2);
EXPECT_LT(stack.rbegin(), stack.rend());
EXPECT_EQ(stack.rend() - stack.rbegin(), 2);
EXPECT_EQ(stack.rbegin()->i64, 1);
EXPECT_EQ((stack.rbegin() + 1)->i64, 2);
EXPECT_EQ((stack.rend() - 1)->i64, 2);
EXPECT_EQ((stack.rend() - 2)->i64, 1);
}

TEST(operand_stack, to_vector)
{
OperandStack stack({}, 0, 3);