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 "src/ast/loop_statement.h"
16 
17 #include "gtest/gtest-spi.h"
18 #include "src/ast/discard_statement.h"
19 #include "src/ast/if_statement.h"
20 #include "src/ast/test_helper.h"
21 
22 namespace tint {
23 namespace ast {
24 namespace {
25 
26 using LoopStatementTest = TestHelper;
27 
TEST_F(LoopStatementTest, Creation)28 TEST_F(LoopStatementTest, Creation) {
29   auto* body = Block(create<DiscardStatement>());
30   auto* b = body->Last();
31 
32   auto* continuing = Block(create<DiscardStatement>());
33 
34   auto* l = create<LoopStatement>(body, continuing);
35   ASSERT_EQ(l->body->statements.size(), 1u);
36   EXPECT_EQ(l->body->statements[0], b);
37   ASSERT_EQ(l->continuing->statements.size(), 1u);
38   EXPECT_EQ(l->continuing->statements[0], continuing->Last());
39 }
40 
TEST_F(LoopStatementTest, Creation_WithSource)41 TEST_F(LoopStatementTest, Creation_WithSource) {
42   auto* body = Block(create<DiscardStatement>());
43 
44   auto* continuing = Block(create<DiscardStatement>());
45 
46   auto* l =
47       create<LoopStatement>(Source{Source::Location{20, 2}}, body, continuing);
48   auto src = l->source;
49   EXPECT_EQ(src.range.begin.line, 20u);
50   EXPECT_EQ(src.range.begin.column, 2u);
51 }
52 
TEST_F(LoopStatementTest, IsLoop)53 TEST_F(LoopStatementTest, IsLoop) {
54   auto* l = create<LoopStatement>(Block(), Block());
55   EXPECT_TRUE(l->Is<LoopStatement>());
56 }
57 
TEST_F(LoopStatementTest, HasContinuing_WithoutContinuing)58 TEST_F(LoopStatementTest, HasContinuing_WithoutContinuing) {
59   auto* body = Block(create<DiscardStatement>());
60 
61   auto* l = create<LoopStatement>(body, nullptr);
62   EXPECT_FALSE(l->continuing);
63 }
64 
TEST_F(LoopStatementTest, HasContinuing_WithContinuing)65 TEST_F(LoopStatementTest, HasContinuing_WithContinuing) {
66   auto* body = Block(create<DiscardStatement>());
67 
68   auto* continuing = Block(create<DiscardStatement>());
69 
70   auto* l = create<LoopStatement>(body, continuing);
71   EXPECT_TRUE(l->continuing);
72 }
73 
TEST_F(LoopStatementTest, Assert_Null_Body)74 TEST_F(LoopStatementTest, Assert_Null_Body) {
75   EXPECT_FATAL_FAILURE(
76       {
77         ProgramBuilder b;
78         b.create<LoopStatement>(nullptr, nullptr);
79       },
80       "internal compiler error");
81 }
82 
TEST_F(LoopStatementTest, Assert_DifferentProgramID_Body)83 TEST_F(LoopStatementTest, Assert_DifferentProgramID_Body) {
84   EXPECT_FATAL_FAILURE(
85       {
86         ProgramBuilder b1;
87         ProgramBuilder b2;
88         b1.create<LoopStatement>(b2.Block(), b1.Block());
89       },
90       "internal compiler error");
91 }
92 
TEST_F(LoopStatementTest, Assert_DifferentProgramID_Continuing)93 TEST_F(LoopStatementTest, Assert_DifferentProgramID_Continuing) {
94   EXPECT_FATAL_FAILURE(
95       {
96         ProgramBuilder b1;
97         ProgramBuilder b2;
98         b1.create<LoopStatement>(b1.Block(), b2.Block());
99       },
100       "internal compiler error");
101 }
102 
103 }  // namespace
104 }  // namespace ast
105 }  // namespace tint
106