Add cast expression tests
This CL adds unit tests for the cast expression AST element. Bug: tint:11 Change-Id: Ia5d82a00ef2a4c16d6591095b7674f1c14a33c5f Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16462 Commit-Queue: David Neto <dneto@google.com> Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
a659b2113d
commit
11be14076b
|
@ -202,6 +202,7 @@ set(TINT_TEST_SRCS
|
||||||
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/case_statement_test.cc
|
||||||
|
ast/cast_expression_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
|
||||||
|
|
|
@ -17,6 +17,8 @@
|
||||||
namespace tint {
|
namespace tint {
|
||||||
namespace ast {
|
namespace ast {
|
||||||
|
|
||||||
|
CastExpression::CastExpression() : Expression() {}
|
||||||
|
|
||||||
CastExpression::CastExpression(type::Type* type,
|
CastExpression::CastExpression(type::Type* type,
|
||||||
std::unique_ptr<Expression> expr)
|
std::unique_ptr<Expression> expr)
|
||||||
: Expression(), type_(type), expr_(std::move(expr)) {}
|
: Expression(), type_(type), expr_(std::move(expr)) {}
|
||||||
|
@ -29,13 +31,17 @@ CastExpression::CastExpression(const Source& source,
|
||||||
CastExpression::~CastExpression() = default;
|
CastExpression::~CastExpression() = default;
|
||||||
|
|
||||||
bool CastExpression::IsValid() const {
|
bool CastExpression::IsValid() const {
|
||||||
return type_ != nullptr && expr_ != nullptr;
|
if (expr_ == nullptr || !expr_->IsValid())
|
||||||
|
return false;
|
||||||
|
return type_ != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CastExpression::to_str(std::ostream& out, size_t indent) const {
|
void CastExpression::to_str(std::ostream& out, size_t indent) const {
|
||||||
out << "cast<" << type_->type_name() << ">(";
|
make_indent(out, indent);
|
||||||
expr_->to_str(out, indent);
|
out << "Cast<" << type_->type_name() << ">(" << std::endl;
|
||||||
out << ")";
|
expr_->to_str(out, indent + 2);
|
||||||
|
make_indent(out, indent);
|
||||||
|
out << ")" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ast
|
} // namespace ast
|
||||||
|
|
|
@ -29,6 +29,8 @@ namespace ast {
|
||||||
/// A cast expression
|
/// A cast expression
|
||||||
class CastExpression : public Expression {
|
class CastExpression : public Expression {
|
||||||
public:
|
public:
|
||||||
|
/// Constructor
|
||||||
|
CastExpression();
|
||||||
/// Constructor
|
/// Constructor
|
||||||
/// @param type the type
|
/// @param type the type
|
||||||
/// @param expr the expr
|
/// @param expr the expr
|
||||||
|
@ -70,7 +72,7 @@ class CastExpression : public Expression {
|
||||||
private:
|
private:
|
||||||
CastExpression(const CastExpression&) = delete;
|
CastExpression(const CastExpression&) = delete;
|
||||||
|
|
||||||
type::Type* type_;
|
type::Type* type_ = nullptr;
|
||||||
std::unique_ptr<Expression> expr_;
|
std::unique_ptr<Expression> expr_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,96 @@
|
||||||
|
// 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/cast_expression.h"
|
||||||
|
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "src/ast/identifier_expression.h"
|
||||||
|
#include "src/ast/type/f32_type.h"
|
||||||
|
|
||||||
|
namespace tint {
|
||||||
|
namespace ast {
|
||||||
|
|
||||||
|
using CastExpressionTest = testing::Test;
|
||||||
|
|
||||||
|
TEST_F(CastExpressionTest, Creation) {
|
||||||
|
type::F32Type f32;
|
||||||
|
auto expr = std::make_unique<IdentifierExpression>("expr");
|
||||||
|
auto expr_ptr = expr.get();
|
||||||
|
|
||||||
|
CastExpression c(&f32, std::move(expr));
|
||||||
|
EXPECT_EQ(c.type(), &f32);
|
||||||
|
EXPECT_EQ(c.expr(), expr_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(CastExpressionTest, Creation_withSource) {
|
||||||
|
type::F32Type f32;
|
||||||
|
auto expr = std::make_unique<IdentifierExpression>("expr");
|
||||||
|
|
||||||
|
CastExpression c(Source{20, 2}, &f32, std::move(expr));
|
||||||
|
auto src = c.source();
|
||||||
|
EXPECT_EQ(src.line, 20);
|
||||||
|
EXPECT_EQ(src.column, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(CastExpressionTest, IsCast) {
|
||||||
|
CastExpression c;
|
||||||
|
EXPECT_TRUE(c.IsCast());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(CastExpressionTest, IsValid) {
|
||||||
|
type::F32Type f32;
|
||||||
|
auto expr = std::make_unique<IdentifierExpression>("expr");
|
||||||
|
|
||||||
|
CastExpression c(&f32, std::move(expr));
|
||||||
|
EXPECT_TRUE(c.IsValid());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(CastExpressionTest, IsValid_MissingType) {
|
||||||
|
auto expr = std::make_unique<IdentifierExpression>("expr");
|
||||||
|
|
||||||
|
CastExpression c;
|
||||||
|
c.set_expr(std::move(expr));
|
||||||
|
EXPECT_FALSE(c.IsValid());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(CastExpressionTest, IsValid_MissingExpression) {
|
||||||
|
type::F32Type f32;
|
||||||
|
|
||||||
|
CastExpression c;
|
||||||
|
c.set_type(&f32);
|
||||||
|
EXPECT_FALSE(c.IsValid());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(CastExpressionTest, IsValid_InvalidExpression) {
|
||||||
|
type::F32Type f32;
|
||||||
|
auto expr = std::make_unique<IdentifierExpression>("");
|
||||||
|
CastExpression c(&f32, std::move(expr));
|
||||||
|
EXPECT_FALSE(c.IsValid());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(CastExpressionTest, ToStr) {
|
||||||
|
type::F32Type f32;
|
||||||
|
auto expr = std::make_unique<IdentifierExpression>("expr");
|
||||||
|
|
||||||
|
CastExpression c(Source{20, 2}, &f32, std::move(expr));
|
||||||
|
std::ostringstream out;
|
||||||
|
c.to_str(out, 2);
|
||||||
|
EXPECT_EQ(out.str(), R"( Cast<__f32>(
|
||||||
|
Identifier{expr}
|
||||||
|
)
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace ast
|
||||||
|
} // namespace tint
|
Loading…
Reference in New Issue