Skip to content

Commit

Permalink
Remove assert, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmeow committed Jul 3, 2024
1 parent 41aa00d commit 89d52a0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
1 change: 0 additions & 1 deletion common/array_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class ArrayStack {

// Returns the array at a specific index.
auto PeekArrayAt(int index) const -> llvm::ArrayRef<ValueT> {
CARBON_CHECK(index < static_cast<int>(array_offsets_.size()));
auto ref = llvm::ArrayRef(values_).slice(array_offsets_[index]);
if (index + 1 < static_cast<int>(array_offsets_.size())) {
ref = ref.take_front(array_offsets_[index + 1] - array_offsets_[index]);
Expand Down
30 changes: 30 additions & 0 deletions common/array_stack_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,35 @@ TEST(ArrayStack, Basics) {
EXPECT_THAT(stack.PeekAllValues(), ElementsAre(5));
}

TEST(ArrayStack, AppendArray) {
ArrayStack<int> stack;

stack.PushArray();
stack.AppendToTop(llvm::ArrayRef<int>());
EXPECT_THAT(stack.PeekArray(), IsEmpty());
stack.AppendToTop({1, 2});
EXPECT_THAT(stack.PeekArray(), ElementsAre(1, 2));
}

TEST(ArrayStack, PeekArrayAt) {
ArrayStack<int> stack;

// Verify behavior with a single array.
stack.PushArray();
stack.AppendToTop(1);
stack.AppendToTop(2);

EXPECT_THAT(stack.PeekArrayAt(0), ElementsAre(1, 2));

// Verify behavior with a couple more arrays.
stack.PushArray();
stack.PushArray();
stack.AppendToTop(3);

EXPECT_THAT(stack.PeekArrayAt(0), ElementsAre(1, 2));
EXPECT_THAT(stack.PeekArrayAt(1), IsEmpty());
EXPECT_THAT(stack.PeekArrayAt(2), ElementsAre(3));
}

} // namespace
} // namespace Carbon::Testing

0 comments on commit 89d52a0

Please sign in to comment.