-
Notifications
You must be signed in to change notification settings - Fork 444
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 2 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,51 @@ | ||
/* | ||
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 (std::string pgm) { | ||
const std::string core_p4 = " \ | ||
error { \ | ||
NoError, \ | ||
PacketTooShort, \ | ||
NoMatch, \ | ||
StackOutOfBounds, \ | ||
OverwritingHeader, \ | ||
HeaderTooShort, \ | ||
ParserTimeout \ | ||
} \ | ||
extern packet_in { \ | ||
void extract<T>(out T hdr); \ | ||
void extract<T>(out T variableSizeHeader, \ | ||
in bit<32> variableFieldSizeInBits); \ | ||
T lookahead<T>(); \ | ||
void advance(in bit<32> sizeInBits); \ | ||
bit<32> length(); \ | ||
} \ | ||
extern packet_out { \ | ||
void emit<T>(in T hdr); \ | ||
void emit<T>(in bool condition, in T data); \ | ||
} \ | ||
@name(\"NoAction\") \ | ||
action NoAction() {} \ | ||
match_kind { \ | ||
exact, \ | ||
ternary, \ | ||
lpm \ | ||
} \n"; | ||
return core_p4 + pgm; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
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 "p4/createBuiltins.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_TRUE(pgm != nullptr); | ||
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.
|
||
|
||
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_TRUE(pgm != nullptr); | ||
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. Same as above. |
||
} |
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.
Maybe this should be read from the file?
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.
Again, since you don't mutate
pgm
, it's probably better to make it aconst std::string&
.I agree it'd be best to read
core.p4
from the file once when the GTest executable starts (so inmain()
). We can then reuse it for every invocation ofwith_core_p4
. If that's not quick to do, though, I think it'd be fine to do this now and file a followup issue to fix that later.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.
+1