Dan Sinclair | c99fdb8 | 2020-03-16 13:58:53 +0000 | [diff] [blame] | 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 <sstream> |
| 18 | |
| 19 | #include "gtest/gtest.h" |
Dan Sinclair | c99fdb8 | 2020-03-16 13:58:53 +0000 | [diff] [blame] | 20 | #include "src/ast/if_statement.h" |
Dan Sinclair | f300756 | 2020-03-16 14:17:28 +0000 | [diff] [blame] | 21 | #include "src/ast/kill_statement.h" |
Dan Sinclair | c99fdb8 | 2020-03-16 13:58:53 +0000 | [diff] [blame] | 22 | #include "src/ast/nop_statement.h" |
| 23 | |
| 24 | namespace tint { |
| 25 | namespace ast { |
dan sinclair | 989cee6 | 2020-03-26 15:31:43 +0000 | [diff] [blame] | 26 | namespace { |
Dan Sinclair | c99fdb8 | 2020-03-16 13:58:53 +0000 | [diff] [blame] | 27 | |
| 28 | using LoopStatementTest = testing::Test; |
| 29 | |
| 30 | TEST_F(LoopStatementTest, Creation) { |
| 31 | std::vector<std::unique_ptr<Statement>> body; |
| 32 | body.push_back(std::make_unique<KillStatement>()); |
| 33 | auto b_ptr = body[0].get(); |
| 34 | |
| 35 | std::vector<std::unique_ptr<Statement>> continuing; |
| 36 | continuing.push_back(std::make_unique<NopStatement>()); |
| 37 | auto c_ptr = continuing[0].get(); |
| 38 | |
| 39 | LoopStatement l(std::move(body), std::move(continuing)); |
| 40 | ASSERT_EQ(l.body().size(), 1); |
| 41 | EXPECT_EQ(l.body()[0].get(), b_ptr); |
| 42 | ASSERT_EQ(l.continuing().size(), 1); |
| 43 | EXPECT_EQ(l.continuing()[0].get(), c_ptr); |
| 44 | } |
| 45 | |
| 46 | TEST_F(LoopStatementTest, Creation_WithSource) { |
| 47 | std::vector<std::unique_ptr<Statement>> body; |
| 48 | body.push_back(std::make_unique<KillStatement>()); |
| 49 | |
| 50 | std::vector<std::unique_ptr<Statement>> continuing; |
| 51 | continuing.push_back(std::make_unique<NopStatement>()); |
| 52 | |
| 53 | LoopStatement l(Source{20, 2}, std::move(body), std::move(continuing)); |
| 54 | auto src = l.source(); |
| 55 | EXPECT_EQ(src.line, 20); |
| 56 | EXPECT_EQ(src.column, 2); |
| 57 | } |
| 58 | |
| 59 | TEST_F(LoopStatementTest, IsLoop) { |
| 60 | LoopStatement l; |
| 61 | EXPECT_TRUE(l.IsLoop()); |
| 62 | } |
| 63 | |
dan sinclair | 0d00402 | 2020-03-20 19:07:00 +0000 | [diff] [blame] | 64 | TEST_F(LoopStatementTest, HasContinuing_WithoutContinuing) { |
| 65 | std::vector<std::unique_ptr<Statement>> body; |
| 66 | body.push_back(std::make_unique<KillStatement>()); |
| 67 | |
| 68 | LoopStatement l(std::move(body), {}); |
| 69 | EXPECT_FALSE(l.has_continuing()); |
| 70 | } |
| 71 | |
| 72 | TEST_F(LoopStatementTest, HasContinuing_WithContinuing) { |
| 73 | std::vector<std::unique_ptr<Statement>> body; |
| 74 | body.push_back(std::make_unique<KillStatement>()); |
| 75 | |
| 76 | std::vector<std::unique_ptr<Statement>> continuing; |
| 77 | continuing.push_back(std::make_unique<NopStatement>()); |
| 78 | |
| 79 | LoopStatement l(std::move(body), std::move(continuing)); |
| 80 | EXPECT_TRUE(l.has_continuing()); |
| 81 | } |
| 82 | |
Dan Sinclair | c99fdb8 | 2020-03-16 13:58:53 +0000 | [diff] [blame] | 83 | TEST_F(LoopStatementTest, IsValid) { |
| 84 | std::vector<std::unique_ptr<Statement>> body; |
| 85 | body.push_back(std::make_unique<KillStatement>()); |
| 86 | |
| 87 | std::vector<std::unique_ptr<Statement>> continuing; |
| 88 | continuing.push_back(std::make_unique<NopStatement>()); |
| 89 | |
| 90 | LoopStatement l(std::move(body), std::move(continuing)); |
| 91 | EXPECT_TRUE(l.IsValid()); |
| 92 | } |
| 93 | |
| 94 | TEST_F(LoopStatementTest, IsValid_WithoutContinuing) { |
| 95 | std::vector<std::unique_ptr<Statement>> body; |
| 96 | body.push_back(std::make_unique<KillStatement>()); |
| 97 | |
| 98 | LoopStatement l(std::move(body), {}); |
| 99 | EXPECT_TRUE(l.IsValid()); |
| 100 | } |
| 101 | |
| 102 | TEST_F(LoopStatementTest, IsValid_WithoutBody) { |
| 103 | LoopStatement l({}, {}); |
| 104 | EXPECT_TRUE(l.IsValid()); |
| 105 | } |
| 106 | |
| 107 | TEST_F(LoopStatementTest, IsValid_NullBodyStatement) { |
| 108 | std::vector<std::unique_ptr<Statement>> body; |
| 109 | body.push_back(std::make_unique<KillStatement>()); |
| 110 | body.push_back(nullptr); |
| 111 | |
| 112 | std::vector<std::unique_ptr<Statement>> continuing; |
| 113 | continuing.push_back(std::make_unique<NopStatement>()); |
| 114 | |
| 115 | LoopStatement l(std::move(body), std::move(continuing)); |
| 116 | EXPECT_FALSE(l.IsValid()); |
| 117 | } |
| 118 | |
| 119 | TEST_F(LoopStatementTest, IsValid_InvalidBodyStatement) { |
| 120 | std::vector<std::unique_ptr<Statement>> body; |
| 121 | body.push_back(std::make_unique<KillStatement>()); |
| 122 | body.push_back(std::make_unique<IfStatement>()); |
| 123 | |
| 124 | std::vector<std::unique_ptr<Statement>> continuing; |
| 125 | continuing.push_back(std::make_unique<NopStatement>()); |
| 126 | |
| 127 | LoopStatement l(std::move(body), std::move(continuing)); |
| 128 | EXPECT_FALSE(l.IsValid()); |
| 129 | } |
| 130 | |
| 131 | TEST_F(LoopStatementTest, IsValid_NullContinuingStatement) { |
| 132 | std::vector<std::unique_ptr<Statement>> body; |
| 133 | body.push_back(std::make_unique<KillStatement>()); |
| 134 | |
| 135 | std::vector<std::unique_ptr<Statement>> continuing; |
| 136 | continuing.push_back(std::make_unique<NopStatement>()); |
| 137 | continuing.push_back(nullptr); |
| 138 | |
| 139 | LoopStatement l(std::move(body), std::move(continuing)); |
| 140 | EXPECT_FALSE(l.IsValid()); |
| 141 | } |
| 142 | |
| 143 | TEST_F(LoopStatementTest, IsValid_InvalidContinuingStatement) { |
| 144 | std::vector<std::unique_ptr<Statement>> body; |
| 145 | body.push_back(std::make_unique<KillStatement>()); |
| 146 | |
| 147 | std::vector<std::unique_ptr<Statement>> continuing; |
| 148 | continuing.push_back(std::make_unique<NopStatement>()); |
| 149 | continuing.push_back(std::make_unique<IfStatement>()); |
| 150 | |
| 151 | LoopStatement l(std::move(body), std::move(continuing)); |
| 152 | EXPECT_FALSE(l.IsValid()); |
| 153 | } |
| 154 | |
| 155 | TEST_F(LoopStatementTest, ToStr) { |
| 156 | std::vector<std::unique_ptr<Statement>> body; |
| 157 | body.push_back(std::make_unique<KillStatement>()); |
| 158 | |
| 159 | LoopStatement l(std::move(body), {}); |
| 160 | std::ostringstream out; |
| 161 | l.to_str(out, 2); |
| 162 | EXPECT_EQ(out.str(), R"( Loop{ |
| 163 | Kill{} |
| 164 | } |
| 165 | )"); |
| 166 | } |
| 167 | |
| 168 | TEST_F(LoopStatementTest, ToStr_WithContinuing) { |
| 169 | std::vector<std::unique_ptr<Statement>> body; |
| 170 | body.push_back(std::make_unique<KillStatement>()); |
| 171 | |
| 172 | std::vector<std::unique_ptr<Statement>> continuing; |
| 173 | continuing.push_back(std::make_unique<NopStatement>()); |
| 174 | |
| 175 | LoopStatement l(std::move(body), std::move(continuing)); |
| 176 | std::ostringstream out; |
| 177 | l.to_str(out, 2); |
| 178 | EXPECT_EQ(out.str(), R"( Loop{ |
| 179 | Kill{} |
| 180 | continuing { |
| 181 | Nop{} |
| 182 | } |
| 183 | } |
| 184 | )"); |
| 185 | } |
| 186 | |
dan sinclair | 989cee6 | 2020-03-26 15:31:43 +0000 | [diff] [blame] | 187 | } // namespace |
Dan Sinclair | c99fdb8 | 2020-03-16 13:58:53 +0000 | [diff] [blame] | 188 | } // namespace ast |
| 189 | } // namespace tint |