-
Notifications
You must be signed in to change notification settings - Fork 445
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
Changes from all commits
16e8f68
4e8a045
4e9a60c
a37532c
4bb7f32
b062a9b
37d06d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"); | ||
std::stringstream sstr; | ||
while(input >> sstr.rdbuf()); | ||
return sstr.str() + pgm; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is more or less what the compiler is doing now. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
andp4c/build
.