Skip to content

Commit

Permalink
Add support for GTest to the build system
Browse files Browse the repository at this point in the history
  • Loading branch information
sethfowler committed Mar 22, 2017
1 parent e925048 commit 4bba2ac
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 81 deletions.
9 changes: 8 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ noinst_HEADERS = # Headers that are not installed
noinst_LIBRARIES = # Static libraries built
noinst_PROGRAMS = # Binaries built that are not installed
BUILT_SOURCES = # Generated source files
check_PROGRAMS = # Test programs to build
TESTS = # Tests to execute

# Variables specific to our build system.
XFAIL_TESTS = $(IFAIL_TESTS) # Tests that are supposed to fail
IFAIL_TESTS = # intermittent failures
export XFAIL_TESTS
export IFAIL_TESTS
gtest_SOURCES = # GTests
cpplint_FILES = # Files that should be passed to cpplint (and etags)
ir_DEF_FILES = # Files that generate the IR
extension_frontend_SOURCES = # Files added to libfrontend by extensions
Expand Down Expand Up @@ -126,9 +128,14 @@ libfrontend_la_SOURCES = \
ir/ir-generated.cpp \
$(extension_frontend_SOURCES)

# unit tests
################ Testing

# C++ unit tests.
include $(srcdir)/test/Makefile.am

# GTest unit tests.
include $(srcdir)/test/gtest/Makefile.am

# Lint all header files.
cpplint_FILES += $(noinst_HEADERS)

Expand Down
39 changes: 34 additions & 5 deletions test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

################################################################################
# Unit tests
################################################################################

check_PROGRAMS = exception_test format_test source_file_test path_test \
enumerator_test default_test unittest_transform1 json_test \
opeq_test call_graph_test dumpjson
check_PROGRAMS += exception_test format_test source_file_test path_test \
enumerator_test default_test unittest_transform1 json_test \
call_graph_test dumpjson

default_test_SOURCES = test/unittests/default_test.cpp
default_test_LDADD = libp4ctoolkit.a
Expand All @@ -38,9 +40,36 @@ unittest_transform1_SOURCES = test/unittests/transform1.cpp
unittest_transform1_LDADD = libfrontend.la libp4ctoolkit.a
dumpjson_SOURCES = test/unittests/dumpjson.cpp
dumpjson_LDADD = libfrontend.la libp4ctoolkit.a
opeq_test_SOURCES = test/unittests/opeq_test.cpp
opeq_test_LDADD = libfrontend.la libp4ctoolkit.a


################################################################################
# GTests
################################################################################

# Flags needed for GTest tests.
GTEST_ROOT = $(top_srcdir)/test/frameworks/gtest/googletest
GTEST_CPPFLAGS = -I$(GTEST_ROOT) -I$(GTEST_ROOT)/include $(AM_CPPFLAGS)

# Build the GTest library itself.
check_LTLIBRARIES = libgtest.la
libgtest_la_CPPFLAGS = $(GTEST_CPPFLAGS)
libgtest_la_SOURCES = test/frameworks/gtest/googletest/src/gtest-all.cc

# Build `gtestp4c`, which will contain all of our tests. Makefiles can add tests
# to $(gtest_SOURCES) to include them in the test executable.
gtestp4c_SOURCES = \
test/gtest/gtestp4c.cpp \
$(gtest_SOURCES)

check_PROGRAMS += gtestp4c
gtestp4c_LDADD = libgtest.la libfrontend.la libp4ctoolkit.a
gtestp4c_CPPFLAGS = $(GTEST_CPPFLAGS)
cpplint_FILES += test/gtest/gtestp4c.cpp
TESTS += gtestp4c


################################################################################
# Compiler tests
################################################################################

TESTS += $(check_PROGRAMS)
21 changes: 21 additions & 0 deletions test/gtest/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2013-present Barefoot Networks, Inc.
#
# 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.

# General GTest unit tests. Add tests here if they don't have a logical home
# elsewhere in the codebase.
gtest_unittest_UNIFIED = \
test/gtest/opeq_test.cpp

cpplint_FILES += $(gtest_unittest_UNIFIED)
gtest_SOURCES += $(gtest_unittest_SOURCES)
22 changes: 22 additions & 0 deletions test/gtest/gtestp4c.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright 2013-present Barefoot Networks, Inc.
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 "gtest/gtest.h"

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
73 changes: 73 additions & 0 deletions test/gtest/opeq_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright 2013-present Barefoot Networks, Inc.
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 "gtest/gtest.h"

#include "ir/ir.h"
#include "ir/visitor.h"
#include "lib/exceptions.h"

TEST(IR, OperatorEq)
{
auto *t = IR::Type::Bits::get(16);
IR::Constant *a = new IR::Constant(t, 10);
IR::Constant *b = new IR::Constant(t, 10);
IR::Constant *c = new IR::Constant(t, 20);

EXPECT_EQ(*a, *b);
EXPECT_EQ(*a, *static_cast<IR::Expression *>(b));
EXPECT_EQ(*a, *static_cast<IR::Node *>(b));
EXPECT_EQ(*static_cast<IR::Expression *>(a), *b);
EXPECT_EQ(*static_cast<IR::Expression *>(a), *static_cast<IR::Expression *>(b));
EXPECT_EQ(*static_cast<IR::Expression *>(a), *static_cast<IR::Node *>(b));
EXPECT_EQ(*static_cast<IR::Node *>(a), *b);
EXPECT_EQ(*static_cast<IR::Node *>(a), *static_cast<IR::Expression *>(b));
EXPECT_EQ(*static_cast<IR::Node *>(a), *static_cast<IR::Node *>(b));

EXPECT_NE(*a, *c);
EXPECT_NE(*a, *static_cast<IR::Expression *>(c));
EXPECT_NE(*a, *static_cast<IR::Node *>(c));
EXPECT_NE(*static_cast<IR::Expression *>(a), *c);
EXPECT_NE(*static_cast<IR::Expression *>(a), *static_cast<IR::Expression *>(c));
EXPECT_NE(*static_cast<IR::Expression *>(a), *static_cast<IR::Node *>(c));
EXPECT_NE(*static_cast<IR::Node *>(a), *c);
EXPECT_NE(*static_cast<IR::Node *>(a), *static_cast<IR::Expression *>(c));
EXPECT_NE(*static_cast<IR::Node *>(a), *static_cast<IR::Node *>(c));

auto *p1 = new IR::IndexedVector<IR::Node>(a);
auto *p2 = p1->clone();
auto *p3 = new IR::IndexedVector<IR::Node>(c);

EXPECT_EQ(*p1, *p2);
EXPECT_EQ(*p1, *static_cast<IR::Vector<IR::Node> *>(p2));
EXPECT_EQ(*p1, *static_cast<IR::Node *>(p2));
EXPECT_EQ(*static_cast<IR::Vector<IR::Node> *>(p1), *p2);
EXPECT_EQ(*static_cast<IR::Vector<IR::Node> *>(p1), *static_cast<IR::Vector<IR::Node> *>(p2));
EXPECT_EQ(*static_cast<IR::Vector<IR::Node> *>(p1), *static_cast<IR::Node *>(p2));
EXPECT_EQ(*static_cast<IR::Node *>(p1), *p2);
EXPECT_EQ(*static_cast<IR::Node *>(p1), *static_cast<IR::Vector<IR::Node> *>(p2));
EXPECT_EQ(*static_cast<IR::Node *>(p1), *static_cast<IR::Node *>(p2));

EXPECT_NE(*p1, *p3);
EXPECT_NE(*p1, *static_cast<IR::Vector<IR::Node> *>(p3));
EXPECT_NE(*p1, *static_cast<IR::Node *>(p3));
EXPECT_NE(*static_cast<IR::Vector<IR::Node> *>(p1), *p3);
EXPECT_NE(*static_cast<IR::Vector<IR::Node> *>(p1), *static_cast<IR::Vector<IR::Node> *>(p3));
EXPECT_NE(*static_cast<IR::Vector<IR::Node> *>(p1), *static_cast<IR::Node *>(p3));
EXPECT_NE(*static_cast<IR::Node *>(p1), *p3);
EXPECT_NE(*static_cast<IR::Node *>(p1), *static_cast<IR::Vector<IR::Node> *>(p3));
EXPECT_NE(*static_cast<IR::Node *>(p1), *static_cast<IR::Node *>(p3));
}
75 changes: 0 additions & 75 deletions test/unittests/opeq_test.cpp

This file was deleted.

0 comments on commit 4bba2ac

Please sign in to comment.