Commit 5f5d147a authored by Alastair Robertson's avatar Alastair Robertson

Add parser tests

parent fb2f33a6
......@@ -70,7 +70,7 @@ int main(int argc, char *argv[])
if (debug)
{
ast::Printer p = bpftrace::ast::Printer(std::cout);
ast::Printer p(std::cout);
driver.root_->accept(p);
}
......
......@@ -67,9 +67,7 @@ void Printer::visit(Unop &unop)
void Printer::visit(ExprStatement &expr)
{
++depth_;
expr.expr->accept(*this);
--depth_;
}
void Printer::visit(AssignMapStatement &assignment)
......
......@@ -15,6 +15,7 @@ add_executable(bpftrace_test
${CMAKE_SOURCE_DIR}/src/driver.cpp
${CMAKE_SOURCE_DIR}/src/map.cpp
${CMAKE_SOURCE_DIR}/src/mapkey.cpp
${CMAKE_SOURCE_DIR}/src/printer.cpp
${CMAKE_SOURCE_DIR}/src/semantic_analyser.cpp
${CMAKE_SOURCE_DIR}/src/types.cpp
)
......
#include <sstream>
#include "gtest/gtest.h"
#include "driver.h"
#include "printer.h"
namespace bpftrace {
TEST(Parser, test0)
using Printer = ast::Printer;
void test(const std::string &input, const std::string &output)
{
Driver driver;
std::string s = "kprobe:sys_open { @x = 1; }";
EXPECT_EQ(driver.parse_str(s), 0);
ASSERT_EQ(driver.parse_str(input), 0);
std::ostringstream out;
Printer printer(out);
driver.root_->accept(printer);
EXPECT_EQ(output, out.str());
}
TEST(Parser, test1)
TEST(Parser, map_assign)
{
Driver driver;
std::string s = "kprobe:sys_open { @x = 1; }";
EXPECT_EQ(driver.parse_str(s), 0);
test("kprobe:sys_open { @x = 1; }",
"Program\n"
" kprobe:sys_open\n"
" =\n"
" map: @x\n"
" int: 1\n");
test("kprobe:sys_open { @x = @y; }",
"Program\n"
" kprobe:sys_open\n"
" =\n"
" map: @x\n"
" map: @y\n");
test("kprobe:sys_open { @x = mybuiltin; }",
"Program\n"
" kprobe:sys_open\n"
" =\n"
" map: @x\n"
" builtin: mybuiltin\n");
test("kprobe:sys_open { @x = myfunc(); }",
"Program\n"
" kprobe:sys_open\n"
" =\n"
" map: @x\n"
" call: myfunc\n");
}
TEST(Parser, map_key)
{
test("kprobe:sys_open { @x[0] = 1; @x[0,1,2] = 1; }",
"Program\n"
" kprobe:sys_open\n"
" =\n"
" map: @x\n"
" int: 0\n"
" int: 1\n"
" =\n"
" map: @x\n"
" int: 0\n"
" int: 1\n"
" int: 2\n"
" int: 1\n");
test("kprobe:sys_open { @x[@a] = 1; @x[@a,@b,@c] = 1; }",
"Program\n"
" kprobe:sys_open\n"
" =\n"
" map: @x\n"
" map: @a\n"
" int: 1\n"
" =\n"
" map: @x\n"
" map: @a\n"
" map: @b\n"
" map: @c\n"
" int: 1\n");
test("kprobe:sys_open { @x[b1] = 1; @x[b1,b2,b3] = 1; }",
"Program\n"
" kprobe:sys_open\n"
" =\n"
" map: @x\n"
" builtin: b1\n"
" int: 1\n"
" =\n"
" map: @x\n"
" builtin: b1\n"
" builtin: b2\n"
" builtin: b3\n"
" int: 1\n");
}
TEST(Parser, predicate)
{
test("kprobe:sys_open / @x / { 1; }",
"Program\n"
" kprobe:sys_open\n"
" pred\n"
" map: @x\n"
" int: 1\n");
}
TEST(Parser, predicate_containing_division)
{
test("kprobe:sys_open /100/25/ { 1; }",
"Program\n"
" kprobe:sys_open\n"
" pred\n"
" /\n"
" int: 100\n"
" int: 25\n"
" int: 1\n");
}
TEST(Parser, expressions)
{
test("kprobe:sys_open / 1 <= 2 && (9 - 4 == 5*10 || ~0) || poop /\n"
"{\n"
" 1;\n"
"}",
"Program\n"
" kprobe:sys_open\n"
" pred\n"
" ||\n"
" &&\n"
" <=\n"
" int: 1\n"
" int: 2\n"
" ||\n"
" ==\n"
" -\n"
" int: 9\n"
" int: 4\n"
" *\n"
" int: 5\n"
" int: 10\n"
" ~\n"
" int: 0\n"
" builtin: poop\n"
" int: 1\n");
}
TEST(Parser, call)
{
test("kprobe:sys_open { @x = foo(); @y = bar(1,2,3); }",
"Program\n"
" kprobe:sys_open\n"
" =\n"
" map: @x\n"
" call: foo\n"
" =\n"
" map: @y\n"
" call: bar\n"
" int: 1\n"
" int: 2\n"
" int: 3\n");
}
TEST(Parser, multiple_probes)
{
test("kprobe:sys_open { 1; } kretprobe:sys_open { 2; }",
"Program\n"
" kprobe:sys_open\n"
" int: 1\n"
" kretprobe:sys_open\n"
" int: 2\n");
}
} // namespace bpftrace
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment