1 // Copyright 2020 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 "gtest/gtest-spi.h"
16 #include "src/ast/test_helper.h"
17
18 namespace tint {
19 namespace ast {
20 namespace {
21
22 using CallExpressionTest = TestHelper;
23
TEST_F(CallExpressionTest,CreationIdentifier)24 TEST_F(CallExpressionTest, CreationIdentifier) {
25 auto* func = Expr("func");
26 ExpressionList params;
27 params.push_back(Expr("param1"));
28 params.push_back(Expr("param2"));
29
30 auto* stmt = create<CallExpression>(func, params);
31 EXPECT_EQ(stmt->target.name, func);
32 EXPECT_EQ(stmt->target.type, nullptr);
33
34 const auto& vec = stmt->args;
35 ASSERT_EQ(vec.size(), 2u);
36 EXPECT_EQ(vec[0], params[0]);
37 EXPECT_EQ(vec[1], params[1]);
38 }
39
TEST_F(CallExpressionTest,CreationIdentifier_WithSource)40 TEST_F(CallExpressionTest, CreationIdentifier_WithSource) {
41 auto* func = Expr("func");
42 auto* stmt = create<CallExpression>(Source{{20, 2}}, func, ExpressionList{});
43 EXPECT_EQ(stmt->target.name, func);
44 EXPECT_EQ(stmt->target.type, nullptr);
45
46 auto src = stmt->source;
47 EXPECT_EQ(src.range.begin.line, 20u);
48 EXPECT_EQ(src.range.begin.column, 2u);
49 }
50
TEST_F(CallExpressionTest,CreationType)51 TEST_F(CallExpressionTest, CreationType) {
52 auto* type = ty.f32();
53 ExpressionList params;
54 params.push_back(Expr("param1"));
55 params.push_back(Expr("param2"));
56
57 auto* stmt = create<CallExpression>(type, params);
58 EXPECT_EQ(stmt->target.name, nullptr);
59 EXPECT_EQ(stmt->target.type, type);
60
61 const auto& vec = stmt->args;
62 ASSERT_EQ(vec.size(), 2u);
63 EXPECT_EQ(vec[0], params[0]);
64 EXPECT_EQ(vec[1], params[1]);
65 }
66
TEST_F(CallExpressionTest,CreationType_WithSource)67 TEST_F(CallExpressionTest, CreationType_WithSource) {
68 auto* type = ty.f32();
69 auto* stmt = create<CallExpression>(Source{{20, 2}}, type, ExpressionList{});
70 EXPECT_EQ(stmt->target.name, nullptr);
71 EXPECT_EQ(stmt->target.type, type);
72
73 auto src = stmt->source;
74 EXPECT_EQ(src.range.begin.line, 20u);
75 EXPECT_EQ(src.range.begin.column, 2u);
76 }
77
TEST_F(CallExpressionTest,IsCall)78 TEST_F(CallExpressionTest, IsCall) {
79 auto* func = Expr("func");
80 auto* stmt = create<CallExpression>(func, ExpressionList{});
81 EXPECT_TRUE(stmt->Is<CallExpression>());
82 }
83
TEST_F(CallExpressionTest,Assert_Null_Identifier)84 TEST_F(CallExpressionTest, Assert_Null_Identifier) {
85 EXPECT_FATAL_FAILURE(
86 {
87 ProgramBuilder b;
88 b.create<CallExpression>(static_cast<IdentifierExpression*>(nullptr),
89 ExpressionList{});
90 },
91 "internal compiler error");
92 }
93
TEST_F(CallExpressionTest,Assert_Null_Type)94 TEST_F(CallExpressionTest, Assert_Null_Type) {
95 EXPECT_FATAL_FAILURE(
96 {
97 ProgramBuilder b;
98 b.create<CallExpression>(static_cast<Type*>(nullptr), ExpressionList{});
99 },
100 "internal compiler error");
101 }
102
TEST_F(CallExpressionTest,Assert_Null_Param)103 TEST_F(CallExpressionTest, Assert_Null_Param) {
104 EXPECT_FATAL_FAILURE(
105 {
106 ProgramBuilder b;
107 ExpressionList params;
108 params.push_back(b.Expr("param1"));
109 params.push_back(nullptr);
110 params.push_back(b.Expr("param2"));
111 b.create<CallExpression>(b.Expr("func"), params);
112 },
113 "internal compiler error");
114 }
115
TEST_F(CallExpressionTest,Assert_DifferentProgramID_Identifier)116 TEST_F(CallExpressionTest, Assert_DifferentProgramID_Identifier) {
117 EXPECT_FATAL_FAILURE(
118 {
119 ProgramBuilder b1;
120 ProgramBuilder b2;
121 b1.create<CallExpression>(b2.Expr("func"), ExpressionList{});
122 },
123 "internal compiler error");
124 }
125
TEST_F(CallExpressionTest,Assert_DifferentProgramID_Type)126 TEST_F(CallExpressionTest, Assert_DifferentProgramID_Type) {
127 EXPECT_FATAL_FAILURE(
128 {
129 ProgramBuilder b1;
130 ProgramBuilder b2;
131 b1.create<CallExpression>(b2.ty.f32(), ExpressionList{});
132 },
133 "internal compiler error");
134 }
135
TEST_F(CallExpressionTest,Assert_DifferentProgramID_Param)136 TEST_F(CallExpressionTest, Assert_DifferentProgramID_Param) {
137 EXPECT_FATAL_FAILURE(
138 {
139 ProgramBuilder b1;
140 ProgramBuilder b2;
141 b1.create<CallExpression>(b1.Expr("func"),
142 ExpressionList{b2.Expr("param1")});
143 },
144 "internal compiler error");
145 }
146
147 } // namespace
148 } // namespace ast
149 } // namespace tint
150