Skip to content

Commit

Permalink
Add unit-tests for AutoPopStack.
Browse files Browse the repository at this point in the history
issues #289

PiperOrigin-RevId: 310247369
  • Loading branch information
fangism authored and hzeller committed May 6, 2020
1 parent b55e90a commit 8bfe7a7
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
13 changes: 12 additions & 1 deletion common/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ cc_library(
name = "auto_pop_stack",
hdrs = ["auto_pop_stack.h"],
deps = [
"//common/util:iterator_adaptors",
":iterator_adaptors",
":logging",
],
)

Expand Down Expand Up @@ -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"],
Expand Down
1 change: 1 addition & 0 deletions common/util/auto_pop_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <vector>

#include "common/util/iterator_adaptors.h"
#include "common/util/logging.h"

namespace verible {

Expand Down
68 changes: 68 additions & 0 deletions common/util/auto_pop_stack_test.cc
Original file line number Diff line number Diff line change
@@ -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<int> 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

0 comments on commit 8bfe7a7

Please sign in to comment.