Add case statement tests

This Cl adds tests for the case statement class.

Change-Id: I585fdff9631ed44e6fb297328ed164297f5fa8f3
Bug: tint:11
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16421
Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
This commit is contained in:
Dan Sinclair 2020-03-06 14:28:01 +00:00 committed by Dan Sinclair
parent 00cf5a4894
commit a659b2113d
4 changed files with 132 additions and 1 deletions

View File

@ -201,6 +201,7 @@ set(TINT_TEST_SRCS
ast/break_statement_test.cc ast/break_statement_test.cc
ast/builtin_decoration_test.cc ast/builtin_decoration_test.cc
ast/call_expression_test.cc ast/call_expression_test.cc
ast/case_statement_test.cc
ast/entry_point_test.cc ast/entry_point_test.cc
ast/import_test.cc ast/import_test.cc
ast/int_literal_test.cc ast/int_literal_test.cc

View File

@ -33,6 +33,10 @@ CaseStatement::CaseStatement(const Source& source,
CaseStatement::~CaseStatement() = default; CaseStatement::~CaseStatement() = default;
bool CaseStatement::IsValid() const { bool CaseStatement::IsValid() const {
for (const auto& stmt : body_) {
if (stmt == nullptr || !stmt->IsValid())
return false;
}
return true; return true;
} }
@ -40,7 +44,7 @@ void CaseStatement::to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
if (IsDefault()) { if (IsDefault()) {
out << "default{" << std::endl; out << "Default{" << std::endl;
} else { } else {
out << "Case " << condition_->to_str() << "{" << std::endl; out << "Case " << condition_->to_str() << "{" << std::endl;
} }

View File

@ -0,0 +1,125 @@
// Copyright 2020 The Tint Authors.
//
// 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 "src/ast/case_statement.h"
#include "gtest/gtest.h"
#include "src/ast/bool_literal.h"
#include "src/ast/if_statement.h"
#include "src/ast/nop_statement.h"
namespace tint {
namespace ast {
using CaseStatementTest = testing::Test;
TEST_F(CaseStatementTest, Creation) {
auto b = std::make_unique<BoolLiteral>(true);
std::vector<std::unique_ptr<Statement>> stmts;
stmts.push_back(std::make_unique<NopStatement>());
auto bool_ptr = b.get();
auto nop_ptr = stmts[0].get();
CaseStatement c(std::move(b), std::move(stmts));
EXPECT_EQ(c.condition(), bool_ptr);
ASSERT_EQ(c.body().size(), 1);
EXPECT_EQ(c.body()[0].get(), nop_ptr);
}
TEST_F(CaseStatementTest, Creation_WithSource) {
auto b = std::make_unique<BoolLiteral>(true);
std::vector<std::unique_ptr<Statement>> stmts;
stmts.push_back(std::make_unique<NopStatement>());
CaseStatement c(Source{20, 2}, std::move(b), std::move(stmts));
auto src = c.source();
EXPECT_EQ(src.line, 20);
EXPECT_EQ(src.column, 2);
}
TEST_F(CaseStatementTest, IsDefault_WithoutCondition) {
std::vector<std::unique_ptr<Statement>> stmts;
stmts.push_back(std::make_unique<NopStatement>());
CaseStatement c;
c.set_body(std::move(stmts));
EXPECT_TRUE(c.IsDefault());
}
TEST_F(CaseStatementTest, IsDefault_WithCondition) {
auto b = std::make_unique<BoolLiteral>(true);
CaseStatement c;
c.set_condition(std::move(b));
EXPECT_FALSE(c.IsDefault());
}
TEST_F(CaseStatementTest, IsCase) {
CaseStatement c;
EXPECT_TRUE(c.IsCase());
}
TEST_F(CaseStatementTest, IsValid) {
CaseStatement c;
EXPECT_TRUE(c.IsValid());
}
TEST_F(CaseStatementTest, IsValid_NullBodyStatement) {
auto b = std::make_unique<BoolLiteral>(true);
std::vector<std::unique_ptr<Statement>> stmts;
stmts.push_back(std::make_unique<NopStatement>());
stmts.push_back(nullptr);
CaseStatement c(std::move(b), std::move(stmts));
EXPECT_FALSE(c.IsValid());
}
TEST_F(CaseStatementTest, IsValid_InvalidBodyStatement) {
auto b = std::make_unique<BoolLiteral>(true);
std::vector<std::unique_ptr<Statement>> stmts;
stmts.push_back(std::make_unique<IfStatement>());
CaseStatement c(std::move(b), std::move(stmts));
EXPECT_FALSE(c.IsValid());
}
TEST_F(CaseStatementTest, ToStr_WithCondition) {
auto b = std::make_unique<BoolLiteral>(true);
std::vector<std::unique_ptr<Statement>> stmts;
stmts.push_back(std::make_unique<NopStatement>());
CaseStatement c(std::move(b), std::move(stmts));
std::ostringstream out;
c.to_str(out, 2);
EXPECT_EQ(out.str(), R"( Case true{
Nop{}
}
)");
}
TEST_F(CaseStatementTest, ToStr_WithoutCondition) {
std::vector<std::unique_ptr<Statement>> stmts;
stmts.push_back(std::make_unique<NopStatement>());
CaseStatement c(nullptr, std::move(stmts));
std::ostringstream out;
c.to_str(out, 2);
EXPECT_EQ(out.str(), R"( Default{
Nop{}
}
)");
}
} // namespace ast
} // namespace tint

View File

@ -19,6 +19,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "src/ast/else_statement.h"
#include "src/ast/expression.h" #include "src/ast/expression.h"
#include "src/ast/statement.h" #include "src/ast/statement.h"