From f4434cd40344a20ddc20e8d2c629d134c869cb3c Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Tue, 17 Mar 2020 03:54:38 +0000 Subject: [PATCH] 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 --- src/CMakeLists.txt | 1 + src/ast/unary_derivative_expression.cc | 9 ++- src/ast/unary_derivative_expression.h | 2 + src/ast/unary_derivative_expression_test.cc | 88 +++++++++++++++++++++ 4 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 src/ast/unary_derivative_expression_test.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4be66dac44..8483f55540 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -245,6 +245,7 @@ set(TINT_TEST_SRCS ast/type/vector_type_test.cc ast/type_initializer_expression_test.cc ast/uint_literal_test.cc + ast/unary_derivative_expression_test.cc ast/variable_test.cc reader/wgsl/lexer_test.cc reader/wgsl/parser_test.cc diff --git a/src/ast/unary_derivative_expression.cc b/src/ast/unary_derivative_expression.cc index 1ae9a86dab..b2416f3930 100644 --- a/src/ast/unary_derivative_expression.cc +++ b/src/ast/unary_derivative_expression.cc @@ -17,6 +17,8 @@ namespace tint { namespace ast { +UnaryDerivativeExpression::UnaryDerivativeExpression() : Expression() {} + UnaryDerivativeExpression::UnaryDerivativeExpression( UnaryDerivative op, DerivativeModifier mod, @@ -33,6 +35,9 @@ UnaryDerivativeExpression::UnaryDerivativeExpression( UnaryDerivativeExpression::~UnaryDerivativeExpression() = default; bool UnaryDerivativeExpression::IsValid() const { + if (param_ == nullptr || !param_->IsValid()) { + return false; + } return true; } @@ -43,9 +48,9 @@ void UnaryDerivativeExpression::to_str(std::ostream& out, size_t indent) const { out << op_ << std::endl; make_indent(out, indent + 2); out << modifier_ << std::endl; - param_->to_str(out, indent); + param_->to_str(out, indent + 2); make_indent(out, indent); - out << "}"; + out << "}" << std::endl; } } // namespace ast diff --git a/src/ast/unary_derivative_expression.h b/src/ast/unary_derivative_expression.h index f601286ea1..d8568f2f03 100644 --- a/src/ast/unary_derivative_expression.h +++ b/src/ast/unary_derivative_expression.h @@ -29,6 +29,8 @@ namespace ast { /// A unary derivative expression class UnaryDerivativeExpression : public Expression { public: + /// Constructor + UnaryDerivativeExpression(); /// Constructor /// @param op the op /// @param mod the derivative modifier diff --git a/src/ast/unary_derivative_expression_test.cc b/src/ast/unary_derivative_expression_test.cc new file mode 100644 index 0000000000..17f43b381d --- /dev/null +++ b/src/ast/unary_derivative_expression_test.cc @@ -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 + +#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("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("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("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(""); + UnaryDerivativeExpression d(UnaryDerivative::kDpdy, + DerivativeModifier::kCoarse, std::move(ident)); + EXPECT_FALSE(d.IsValid()); +} + +TEST_F(UnaryDerivativeExpressionTest, ToStr) { + auto ident = std::make_unique("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