Add UnaryDerivativeExpression tests

This CL adds tests for the UnaryDerivativeExpression AST element.

Bug: tint:11
Change-Id: Ieabfdcb7940aedc58a3455b74c0ddb508b4859ff
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16673
Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
This commit is contained in:
Dan Sinclair 2020-03-17 03:54:38 +00:00 committed by Sarah Mashayekhi
parent 5ced2178a6
commit f4434cd403
4 changed files with 98 additions and 2 deletions

View File

@ -245,6 +245,7 @@ set(TINT_TEST_SRCS
ast/type/vector_type_test.cc ast/type/vector_type_test.cc
ast/type_initializer_expression_test.cc ast/type_initializer_expression_test.cc
ast/uint_literal_test.cc ast/uint_literal_test.cc
ast/unary_derivative_expression_test.cc
ast/variable_test.cc ast/variable_test.cc
reader/wgsl/lexer_test.cc reader/wgsl/lexer_test.cc
reader/wgsl/parser_test.cc reader/wgsl/parser_test.cc

View File

@ -17,6 +17,8 @@
namespace tint { namespace tint {
namespace ast { namespace ast {
UnaryDerivativeExpression::UnaryDerivativeExpression() : Expression() {}
UnaryDerivativeExpression::UnaryDerivativeExpression( UnaryDerivativeExpression::UnaryDerivativeExpression(
UnaryDerivative op, UnaryDerivative op,
DerivativeModifier mod, DerivativeModifier mod,
@ -33,6 +35,9 @@ UnaryDerivativeExpression::UnaryDerivativeExpression(
UnaryDerivativeExpression::~UnaryDerivativeExpression() = default; UnaryDerivativeExpression::~UnaryDerivativeExpression() = default;
bool UnaryDerivativeExpression::IsValid() const { bool UnaryDerivativeExpression::IsValid() const {
if (param_ == nullptr || !param_->IsValid()) {
return false;
}
return true; return true;
} }
@ -43,9 +48,9 @@ void UnaryDerivativeExpression::to_str(std::ostream& out, size_t indent) const {
out << op_ << std::endl; out << op_ << std::endl;
make_indent(out, indent + 2); make_indent(out, indent + 2);
out << modifier_ << std::endl; out << modifier_ << std::endl;
param_->to_str(out, indent); param_->to_str(out, indent + 2);
make_indent(out, indent); make_indent(out, indent);
out << "}"; out << "}" << std::endl;
} }
} // namespace ast } // namespace ast

View File

@ -29,6 +29,8 @@ namespace ast {
/// A unary derivative expression /// A unary derivative expression
class UnaryDerivativeExpression : public Expression { class UnaryDerivativeExpression : public Expression {
public: public:
/// Constructor
UnaryDerivativeExpression();
/// Constructor /// Constructor
/// @param op the op /// @param op the op
/// @param mod the derivative modifier /// @param mod the derivative modifier

View File

@ -0,0 +1,88 @@
// 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/unary_derivative_expression.h"
#include <sstream>
#include "gtest/gtest.h"
#include "src/ast/identifier_expression.h"
namespace tint {
namespace ast {
using UnaryDerivativeExpressionTest = testing::Test;
TEST_F(UnaryDerivativeExpressionTest, Creation) {
auto ident = std::make_unique<IdentifierExpression>("ident");
auto ident_ptr = ident.get();
UnaryDerivativeExpression d(UnaryDerivative::kDpdy,
DerivativeModifier::kCoarse, std::move(ident));
EXPECT_EQ(d.param(), ident_ptr);
EXPECT_EQ(d.modifier(), DerivativeModifier::kCoarse);
EXPECT_EQ(d.op(), UnaryDerivative::kDpdy);
}
TEST_F(UnaryDerivativeExpressionTest, Creation_WithSource) {
auto ident = std::make_unique<IdentifierExpression>("ident");
UnaryDerivativeExpression d(Source{20, 2}, UnaryDerivative::kDpdy,
DerivativeModifier::kCoarse, std::move(ident));
auto src = d.source();
EXPECT_EQ(src.line, 20);
EXPECT_EQ(src.column, 2);
}
TEST_F(UnaryDerivativeExpressionTest, IsUnaryDerivative) {
UnaryDerivativeExpression d;
EXPECT_TRUE(d.IsUnaryDerivative());
}
TEST_F(UnaryDerivativeExpressionTest, IsValid) {
auto ident = std::make_unique<IdentifierExpression>("ident");
UnaryDerivativeExpression d(UnaryDerivative::kDpdy,
DerivativeModifier::kCoarse, std::move(ident));
EXPECT_TRUE(d.IsValid());
}
TEST_F(UnaryDerivativeExpressionTest, IsValid_NullParam) {
UnaryDerivativeExpression d(UnaryDerivative::kDpdy,
DerivativeModifier::kCoarse, nullptr);
EXPECT_FALSE(d.IsValid());
}
TEST_F(UnaryDerivativeExpressionTest, IsValid_InvalidParam) {
auto ident = std::make_unique<IdentifierExpression>("");
UnaryDerivativeExpression d(UnaryDerivative::kDpdy,
DerivativeModifier::kCoarse, std::move(ident));
EXPECT_FALSE(d.IsValid());
}
TEST_F(UnaryDerivativeExpressionTest, ToStr) {
auto ident = std::make_unique<IdentifierExpression>("ident");
UnaryDerivativeExpression d(UnaryDerivative::kDpdy,
DerivativeModifier::kCoarse, std::move(ident));
std::ostringstream out;
d.to_str(out, 2);
EXPECT_EQ(out.str(), R"( UnaryDerivative{
dpdy
coarse
Identifier{ident}
}
)");
}
} // namespace ast
} // namespace tint