• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gtest/gtest-spi.h"
16 #include "src/ast/binary_expression.h"
17 #include "src/ast/test_helper.h"
18 
19 namespace tint {
20 namespace ast {
21 namespace {
22 
23 using ForLoopStatementTest = TestHelper;
24 
TEST_F(ForLoopStatementTest,Creation)25 TEST_F(ForLoopStatementTest, Creation) {
26   auto* init = Decl(Var("i", ty.u32()));
27   auto* cond =
28       create<BinaryExpression>(BinaryOp::kLessThan, Expr("i"), Expr(5u));
29   auto* cont = Assign("i", Add("i", 1));
30   auto* body = Block(Return());
31   auto* l = For(init, cond, cont, body);
32 
33   EXPECT_EQ(l->initializer, init);
34   EXPECT_EQ(l->condition, cond);
35   EXPECT_EQ(l->continuing, cont);
36   EXPECT_EQ(l->body, body);
37 }
38 
TEST_F(ForLoopStatementTest,Creation_WithSource)39 TEST_F(ForLoopStatementTest, Creation_WithSource) {
40   auto* body = Block(Return());
41   auto* l = For(Source{{20u, 2u}}, nullptr, nullptr, nullptr, body);
42   auto src = l->source;
43   EXPECT_EQ(src.range.begin.line, 20u);
44   EXPECT_EQ(src.range.begin.column, 2u);
45 }
46 
TEST_F(ForLoopStatementTest,Creation_Null_InitCondCont)47 TEST_F(ForLoopStatementTest, Creation_Null_InitCondCont) {
48   auto* body = Block(Return());
49   auto* l = For(nullptr, nullptr, nullptr, body);
50   EXPECT_EQ(l->body, body);
51 }
52 
TEST_F(ForLoopStatementTest,Assert_Null_Body)53 TEST_F(ForLoopStatementTest, Assert_Null_Body) {
54   EXPECT_FATAL_FAILURE(
55       {
56         ProgramBuilder b;
57         b.For(nullptr, nullptr, nullptr, nullptr);
58       },
59       "internal compiler error");
60 }
61 
TEST_F(ForLoopStatementTest,Assert_DifferentProgramID_Initializer)62 TEST_F(ForLoopStatementTest, Assert_DifferentProgramID_Initializer) {
63   EXPECT_FATAL_FAILURE(
64       {
65         ProgramBuilder b1;
66         ProgramBuilder b2;
67         b1.For(b2.Block(), nullptr, nullptr, b1.Block());
68       },
69       "internal compiler error");
70 }
71 
TEST_F(ForLoopStatementTest,Assert_DifferentProgramID_Condition)72 TEST_F(ForLoopStatementTest, Assert_DifferentProgramID_Condition) {
73   EXPECT_FATAL_FAILURE(
74       {
75         ProgramBuilder b1;
76         ProgramBuilder b2;
77         b1.For(nullptr, b2.Expr(true), nullptr, b1.Block());
78       },
79       "internal compiler error");
80 }
81 
TEST_F(ForLoopStatementTest,Assert_DifferentProgramID_Continuing)82 TEST_F(ForLoopStatementTest, Assert_DifferentProgramID_Continuing) {
83   EXPECT_FATAL_FAILURE(
84       {
85         ProgramBuilder b1;
86         ProgramBuilder b2;
87         b1.For(nullptr, nullptr, b2.Block(), b1.Block());
88       },
89       "internal compiler error");
90 }
91 
TEST_F(ForLoopStatementTest,Assert_DifferentProgramID_Body)92 TEST_F(ForLoopStatementTest, Assert_DifferentProgramID_Body) {
93   EXPECT_FATAL_FAILURE(
94       {
95         ProgramBuilder b1;
96         ProgramBuilder b2;
97         b1.For(nullptr, nullptr, nullptr, b2.Block());
98       },
99       "internal compiler error");
100 }
101 
102 }  // namespace
103 }  // namespace ast
104 }  // namespace tint
105