Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +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/tint/writer/wgsl/test_helper.h" |
| 16 | |
dan sinclair | 27c7722 | 2023-04-13 02:57:27 +0000 | [diff] [blame] | 17 | #include "gmock/gmock.h" |
| 18 | |
dan sinclair | 9f54a51 | 2022-04-07 11:08:24 +0000 | [diff] [blame] | 19 | namespace tint::writer::wgsl { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | using WgslGeneratorImplTest = TestHelper; |
| 23 | |
| 24 | TEST_F(WgslGeneratorImplTest, Emit_If) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 25 | GlobalVar("cond", ty.bool_(), builtin::AddressSpace::kPrivate); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 26 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 27 | auto* cond = Expr("cond"); |
| 28 | auto* body = Block(Return()); |
| 29 | auto* i = If(cond, body); |
| 30 | WrapInFunction(i); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 31 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 32 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 33 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 34 | gen.increment_indent(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 35 | |
dan sinclair | 27c7722 | 2023-04-13 02:57:27 +0000 | [diff] [blame] | 36 | gen.EmitStatement(i); |
| 37 | EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 38 | EXPECT_EQ(gen.result(), R"( if (cond) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 39 | return; |
| 40 | } |
| 41 | )"); |
| 42 | } |
| 43 | |
| 44 | TEST_F(WgslGeneratorImplTest, Emit_IfWithElseIf) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 45 | GlobalVar("cond", ty.bool_(), builtin::AddressSpace::kPrivate); |
| 46 | GlobalVar("else_cond", ty.bool_(), builtin::AddressSpace::kPrivate); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 47 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 48 | auto* else_cond = Expr("else_cond"); |
| 49 | auto* else_body = Block(Return()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 50 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 51 | auto* cond = Expr("cond"); |
| 52 | auto* body = Block(Return()); |
James Price | 8aff0ed | 2022-05-02 14:53:36 +0000 | [diff] [blame] | 53 | auto* i = If(cond, body, Else(If(else_cond, else_body))); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 54 | WrapInFunction(i); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 55 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 56 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 57 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 58 | gen.increment_indent(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 59 | |
dan sinclair | 27c7722 | 2023-04-13 02:57:27 +0000 | [diff] [blame] | 60 | gen.EmitStatement(i); |
| 61 | EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 62 | EXPECT_EQ(gen.result(), R"( if (cond) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 63 | return; |
| 64 | } else if (else_cond) { |
| 65 | return; |
| 66 | } |
| 67 | )"); |
| 68 | } |
| 69 | |
| 70 | TEST_F(WgslGeneratorImplTest, Emit_IfWithElse) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 71 | GlobalVar("cond", ty.bool_(), builtin::AddressSpace::kPrivate); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 72 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 73 | auto* else_body = Block(Return()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 74 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 75 | auto* cond = Expr("cond"); |
| 76 | auto* body = Block(Return()); |
James Price | 8aff0ed | 2022-05-02 14:53:36 +0000 | [diff] [blame] | 77 | auto* i = If(cond, body, Else(else_body)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 78 | WrapInFunction(i); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 79 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 80 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 81 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 82 | gen.increment_indent(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 83 | |
dan sinclair | 27c7722 | 2023-04-13 02:57:27 +0000 | [diff] [blame] | 84 | gen.EmitStatement(i); |
| 85 | EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 86 | EXPECT_EQ(gen.result(), R"( if (cond) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 87 | return; |
| 88 | } else { |
| 89 | return; |
| 90 | } |
| 91 | )"); |
| 92 | } |
| 93 | |
| 94 | TEST_F(WgslGeneratorImplTest, Emit_IfWithMultiple) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 95 | GlobalVar("cond", ty.bool_(), builtin::AddressSpace::kPrivate); |
| 96 | GlobalVar("else_cond", ty.bool_(), builtin::AddressSpace::kPrivate); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 97 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 98 | auto* else_cond = Expr("else_cond"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 99 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 100 | auto* else_body = Block(Return()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 101 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 102 | auto* else_body_2 = Block(Return()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 103 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 104 | auto* cond = Expr("cond"); |
| 105 | auto* body = Block(Return()); |
James Price | 8aff0ed | 2022-05-02 14:53:36 +0000 | [diff] [blame] | 106 | auto* i = If(cond, body, Else(If(else_cond, else_body, Else(else_body_2)))); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 107 | WrapInFunction(i); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 108 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 109 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 110 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 111 | gen.increment_indent(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 112 | |
dan sinclair | 27c7722 | 2023-04-13 02:57:27 +0000 | [diff] [blame] | 113 | gen.EmitStatement(i); |
| 114 | EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 115 | EXPECT_EQ(gen.result(), R"( if (cond) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 116 | return; |
| 117 | } else if (else_cond) { |
| 118 | return; |
| 119 | } else { |
| 120 | return; |
| 121 | } |
| 122 | )"); |
| 123 | } |
| 124 | |
| 125 | } // namespace |
dan sinclair | 9f54a51 | 2022-04-07 11:08:24 +0000 | [diff] [blame] | 126 | } // namespace tint::writer::wgsl |