mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Create an array stack type for a shared use-case (#4100)
Based on discussion around the region handling in generic_region_stack, create a generic structure for the stack-of-vectors support. I also want to add this to InstBlockStack, but that's a little more complex due to GlobalInit, so cutting a PR here to check with review. My work here is how I noticed #4099; I want to be sure that I'm correct about the issue, but it's the difference between being able to use PeekArray or not. Note in scope_stack.h, I believe we could remove next_compile_time_index and make it just based on elements_size(). However, I want to verify with you before I make further changes there. --------- Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
This commit is contained in:
co-authored by
Chandler Carruth
parent
e71e6ca07f
commit
d437e4bffe
@@ -0,0 +1,77 @@
|
||||
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
||||
// Exceptions. See /LICENSE for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
#include "common/array_stack.h"
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace Carbon::Testing {
|
||||
namespace {
|
||||
|
||||
using ::testing::ElementsAre;
|
||||
using ::testing::IsEmpty;
|
||||
|
||||
TEST(ArrayStack, Basics) {
|
||||
ArrayStack<int> stack;
|
||||
|
||||
// PeekAllValues is valid when there are no arrays.
|
||||
EXPECT_THAT(stack.PeekAllValues(), IsEmpty());
|
||||
|
||||
// An array starts empty.
|
||||
stack.PushArray();
|
||||
EXPECT_THAT(stack.PeekArray(), IsEmpty());
|
||||
EXPECT_THAT(stack.PeekAllValues(), IsEmpty());
|
||||
|
||||
// Pushing a couple values works.
|
||||
stack.AppendToTop(1);
|
||||
stack.AppendToTop(2);
|
||||
EXPECT_THAT(stack.PeekArray(), ElementsAre(1, 2));
|
||||
EXPECT_THAT(stack.PeekAllValues(), ElementsAre(1, 2));
|
||||
|
||||
// Pushing a new array starts empty, old values are still there.
|
||||
stack.PushArray();
|
||||
EXPECT_THAT(stack.PeekArray(), IsEmpty());
|
||||
EXPECT_THAT(stack.PeekAllValues(), ElementsAre(1, 2));
|
||||
|
||||
// The added value goes to the 2nd array.
|
||||
stack.AppendToTop(3);
|
||||
EXPECT_THAT(stack.PeekArray(), ElementsAre(3));
|
||||
EXPECT_THAT(stack.PeekAllValues(), ElementsAre(1, 2, 3));
|
||||
|
||||
// Popping goes back to the 1st array.
|
||||
stack.PopArray();
|
||||
EXPECT_THAT(stack.PeekArray(), ElementsAre(1, 2));
|
||||
EXPECT_THAT(stack.PeekAllValues(), ElementsAre(1, 2));
|
||||
|
||||
// Push a couple arrays, then a value on the now-3rd array.
|
||||
stack.PushArray();
|
||||
stack.PushArray();
|
||||
stack.AppendToTop(4);
|
||||
EXPECT_THAT(stack.PeekArray(), ElementsAre(4));
|
||||
EXPECT_THAT(stack.PeekAllValues(), ElementsAre(1, 2, 4));
|
||||
|
||||
// Popping the 3rd array goes to the 2nd array, which is empty.
|
||||
stack.PopArray();
|
||||
EXPECT_THAT(stack.PeekArray(), IsEmpty());
|
||||
EXPECT_THAT(stack.PeekAllValues(), ElementsAre(1, 2));
|
||||
|
||||
// Again back to the 1st array.
|
||||
stack.PopArray();
|
||||
EXPECT_THAT(stack.PeekArray(), ElementsAre(1, 2));
|
||||
EXPECT_THAT(stack.PeekAllValues(), ElementsAre(1, 2));
|
||||
|
||||
// Go down to no arrays.
|
||||
stack.PopArray();
|
||||
EXPECT_THAT(stack.PeekAllValues(), IsEmpty());
|
||||
|
||||
// Add a new 1st array.
|
||||
stack.PushArray();
|
||||
stack.AppendToTop(5);
|
||||
EXPECT_THAT(stack.PeekArray(), ElementsAre(5));
|
||||
EXPECT_THAT(stack.PeekAllValues(), ElementsAre(5));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace Carbon::Testing
|
||||
Reference in New Issue
Block a user