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 1 commit
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(std::string &pgm);

#endif /* _P4_P4_PARSE_H_ */
12 changes: 12 additions & 0 deletions frontends/p4/p4-parse.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,18 @@ const IR::P4Program *parse_P4_16_file(const char *name, FILE *in) {
return new IR::P4Program(declarations->srcInfo, declarations);
}

const IR::P4Program *parse_string(std::string &pgm) {
Copy link
Contributor

Choose a reason for hiding this comment

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

const std::string& is probably preferable, since it doesn't look like you mutate pgm in here.

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;
}
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
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)
51 changes: 51 additions & 0 deletions test/gtest/helper.h
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) {
Copy link
Contributor

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?

Copy link
Contributor

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 a const std::string&.

I agree it'd be best to read core.p4 from the file once when the GTest executable starts (so in main()). We can then reuse it for every invocation of with_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.

Copy link
Member

Choose a reason for hiding this comment

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

+1

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;
}
62 changes: 62 additions & 0 deletions test/gtest/typecheck_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
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 "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");
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_TRUE(pgm != nullptr);
Copy link
Contributor

Choose a reason for hiding this comment

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

ASSERT_NE(nullptr, pgm); is better; GTest can give much nicer log messages in that case.


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);
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above.

}