1 // Copyright 2021 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/writer/glsl/test_helper.h"
16
17 namespace tint {
18 namespace writer {
19 namespace glsl {
20 namespace {
21
22 using GlslUnaryOpTest = TestHelper;
23
TEST_F(GlslUnaryOpTest,AddressOf)24 TEST_F(GlslUnaryOpTest, AddressOf) {
25 Global("expr", ty.f32(), ast::StorageClass::kPrivate);
26 auto* op =
27 create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf, Expr("expr"));
28 WrapInFunction(op);
29
30 GeneratorImpl& gen = Build();
31
32 std::stringstream out;
33 ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.error();
34 EXPECT_EQ(out.str(), "expr");
35 }
36
TEST_F(GlslUnaryOpTest,Complement)37 TEST_F(GlslUnaryOpTest, Complement) {
38 Global("expr", ty.u32(), ast::StorageClass::kPrivate);
39 auto* op =
40 create<ast::UnaryOpExpression>(ast::UnaryOp::kComplement, Expr("expr"));
41 WrapInFunction(op);
42
43 GeneratorImpl& gen = Build();
44
45 std::stringstream out;
46 ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.error();
47 EXPECT_EQ(out.str(), "~(expr)");
48 }
49
TEST_F(GlslUnaryOpTest,Indirection)50 TEST_F(GlslUnaryOpTest, Indirection) {
51 Global("G", ty.f32(), ast::StorageClass::kPrivate);
52 auto* p = Const(
53 "expr", nullptr,
54 create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf, Expr("G")));
55 auto* op =
56 create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection, Expr("expr"));
57 WrapInFunction(p, op);
58
59 GeneratorImpl& gen = Build();
60
61 std::stringstream out;
62 ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.error();
63 EXPECT_EQ(out.str(), "expr");
64 }
65
TEST_F(GlslUnaryOpTest,Not)66 TEST_F(GlslUnaryOpTest, Not) {
67 Global("expr", ty.bool_(), ast::StorageClass::kPrivate);
68 auto* op = create<ast::UnaryOpExpression>(ast::UnaryOp::kNot, Expr("expr"));
69 WrapInFunction(op);
70
71 GeneratorImpl& gen = Build();
72
73 std::stringstream out;
74 ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.error();
75 EXPECT_EQ(out.str(), "!(expr)");
76 }
77
TEST_F(GlslUnaryOpTest,Negation)78 TEST_F(GlslUnaryOpTest, Negation) {
79 Global("expr", ty.i32(), ast::StorageClass::kPrivate);
80 auto* op =
81 create<ast::UnaryOpExpression>(ast::UnaryOp::kNegation, Expr("expr"));
82 WrapInFunction(op);
83
84 GeneratorImpl& gen = Build();
85
86 std::stringstream out;
87 ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.error();
88 EXPECT_EQ(out.str(), "-(expr)");
89 }
90 } // namespace
91 } // namespace glsl
92 } // namespace writer
93 } // namespace tint
94