diff --git a/common/util/BUILD b/common/util/BUILD index ca2a0442a..9886ffe3a 100644 --- a/common/util/BUILD +++ b/common/util/BUILD @@ -17,7 +17,8 @@ cc_library( name = "auto_pop_stack", hdrs = ["auto_pop_stack.h"], deps = [ - "//common/util:iterator_adaptors", + ":iterator_adaptors", + ":logging", ], ) @@ -244,6 +245,16 @@ cc_test( ], ) +cc_test( + name = "auto_pop_stack_test", + srcs = ["auto_pop_stack_test.cc"], + deps = [ + ":auto_pop_stack", + ":iterator_range", + "@com_google_googletest//:gtest_main", + ], +) + cc_test( name = "file_util_test", srcs = ["file_util_test.cc"], diff --git a/common/util/auto_pop_stack.h b/common/util/auto_pop_stack.h index c7fb959eb..e9a635a2a 100644 --- a/common/util/auto_pop_stack.h +++ b/common/util/auto_pop_stack.h @@ -21,6 +21,7 @@ #include #include "common/util/iterator_adaptors.h" +#include "common/util/logging.h" namespace verible { diff --git a/common/util/auto_pop_stack_test.cc b/common/util/auto_pop_stack_test.cc new file mode 100644 index 000000000..b22f58125 --- /dev/null +++ b/common/util/auto_pop_stack_test.cc @@ -0,0 +1,68 @@ +// Copyright 2017-2020 The Verible Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "common/util/auto_pop_stack.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "common/util/iterator_range.h" + +namespace verible { +namespace { + +using ::testing::ElementsAre; + +typedef AutoPopStack IntStack; + +// Test that AutoPop properly pushes and pops nodes on and off the stack +TEST(AutoPopStackTest, PushPopTest) { + IntStack context; + const auto& const_context = context; + EXPECT_TRUE(context.empty()); + { + IntStack::AutoPop p1(&context, 1); + EXPECT_EQ(const_context.top(), 1); + } + EXPECT_TRUE(context.empty()); + IntStack::AutoPop p2(&context, 2); + { + IntStack::AutoPop p3(&context, 3); + EXPECT_EQ(const_context.top(), 3); + IntStack::AutoPop p4(&context, 4); + EXPECT_EQ(const_context.top(), 4); + } + EXPECT_EQ(const_context.top(), 2); +} + +// Test that forward/reverse iterators correctly look down/up the stack. +TEST(IntStackTest, IteratorsTest) { + IntStack context; + { + IntStack::AutoPop p1(&context, 1); + { + IntStack::AutoPop p2(&context, 2); + { + IntStack::AutoPop p3(&context, 3); + + EXPECT_THAT(verible::make_range(context.begin(), context.end()), + ElementsAre(1, 2, 3)); + EXPECT_THAT(verible::make_range(context.rbegin(), context.rend()), + ElementsAre(3, 2, 1)); + } + } + } +} + +} // namespace +} // namespace verible