Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an example gtest case, useful for unit-testing compiler pass #434

Merged
merged 7 commits into from
Apr 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion frontends/p4/p4-parse.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2013-present Barefoot Networks, Inc.
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.
Expand All @@ -23,5 +23,6 @@ limitations under the License.
namespace IR { class Global; }

const IR::P4Program *parse_P4_16_file(const char *name, FILE *in);
const IR::P4Program *parse_string(const std::string &pgm);

#endif /* _P4_P4_PARSE_H_ */
14 changes: 13 additions & 1 deletion frontends/p4/p4-parse.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ static Util::ProgramStructure structure;

namespace { // anonymous namespace
static int yylex();

%}

%union {
Expand Down Expand Up @@ -1034,6 +1033,19 @@ const IR::P4Program *parse_P4_16_file(const char *name, FILE *in) {
return new IR::P4Program(declarations->srcInfo, declarations);
}

const IR::P4Program *parse_string(const std::string &pgm) {
int errors = 0;
declarations = new IR::IndexedVector<IR::Node>();
YY_BUFFER_STATE state = yy_scan_string(pgm.c_str());
errors |= yyparse();
yy_delete_buffer(state);
if (errors)
return nullptr;
structure.clear();
allErrors = nullptr;
return new IR::P4Program(declarations->srcInfo, declarations);
}

// check that right shift >>
// has the two tokens at consecutive positions
// (not separated by anything else)
Expand Down
7 changes: 7 additions & 0 deletions frontends/p4/symbol_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ class Namespace : public NamedSymbol {
into << s;
into << "}" << std::endl;
}
void clear() {
contents.clear();
}
};

class Object : public NamedSymbol {
Expand Down Expand Up @@ -238,4 +241,8 @@ cstring ProgramStructure::toString() const {
rootNamespace->dump(res, 0);
return cstring(res.str());
}

void ProgramStructure::clear() {
rootNamespace->clear();
}
} // namespace Util
1 change: 1 addition & 0 deletions frontends/p4/symbol_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class ProgramStructure final {
void endParse();

cstring toString() const;
void clear();
};

} // namespace Util
Expand Down
3 changes: 2 additions & 1 deletion test/gtest/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
# 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
test/gtest/opeq_test.cpp \
test/gtest/typecheck_test.cpp

cpplint_FILES += $(gtest_unittest_UNIFIED)
gtest_SOURCES += $(gtest_unittest_SOURCES)
25 changes: 25 additions & 0 deletions test/gtest/helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
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 <string>

/* preprocessing by prepending the content of core.p4 to test program */
std::string with_core_p4 (const std::string& pgm) {
std::ifstream input("p4include/core.p4");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This relies on running the test in the build directory -- since that is what generally happens, perhaps ok?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I don't think make check works outside the build directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gtest will work in both p4c and p4c/build.

std::stringstream sstr;
while(input >> sstr.rdbuf());
return sstr.str() + pgm;
}
77 changes: 77 additions & 0 deletions test/gtest/typecheck_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more or less what the compiler is doing now.
It would be much easier to add a flag for the compiler to stop compiling after a certain pass.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For unit tests, we want as much control as possible. I think we're better off using an approach like this. It gives us maximum flexibility, and it has the advantage of not invoking cpp for every unit test.

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 "p4/p4-parse.h"
#include "frontends/p4/typeMap.h"
#include "frontends/common/constantFolding.h"
#include "frontends/p4/typeChecking/bindVariables.h"
#include "frontends/common/resolveReferences/resolveReferences.h"

#include "p4/validateParsedProgram.h"
#include "p4/createBuiltins.h"
#include "p4/unusedDeclarations.h"
#include "p4/typeChecking/typeChecker.h"

#include "helper.h"

using namespace P4;

TEST(UNITTEST, helloworld) {
std::string program = with_core_p4(
"parser Parser<H, M> (packet_in p){ state start{} };\n"
"control empty() { apply {} };\n"
"package top(empty e);\n"
"top(empty()) main;\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C++11's raw string literals might make this a little nicer. (And we can use them!)


const IR::P4Program* pgm = parse_string(program);
ASSERT_NE(nullptr, pgm);

ReferenceMap refMap;
TypeMap typeMap;

PassManager passes = {
new ValidateParsedProgram(false),
new CreateBuiltins(),
new ResolveReferences(&refMap, true),
new ConstantFolding(&refMap, nullptr),
new ResolveReferences(&refMap),
// new TypeInference(&refMap, &typeMap),
};

pgm = pgm->apply(passes);

ASSERT_NE(nullptr, pgm);
}

TEST(UNITTEST, package) {
std::string program = with_core_p4(
"parser Parser<H, M> (packet_in p){ state start{} };\n"
"control empty() { apply {} };\n"
"package top(empty e);\n");

const IR::P4Program* pgm = parse_string(program);

ASSERT_NE(nullptr, pgm);

PassManager passes = {
new CreateBuiltins(),
};
pgm = pgm->apply(passes);
dump(pgm);
}