Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 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 "src/tint/writer/glsl/test_helper.h" |
| 16 | |
dan sinclair | 5d59059 | 2022-04-07 14:40:24 +0000 | [diff] [blame] | 17 | namespace tint::writer::glsl { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | using GlslGeneratorImplTest_If = TestHelper; |
| 21 | |
| 22 | TEST_F(GlslGeneratorImplTest_If, Emit_If) { |
Ben Clayton | 01208e7 | 2022-06-25 08:12:59 +0000 | [diff] [blame] | 23 | GlobalVar("cond", ty.bool_(), ast::StorageClass::kPrivate); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 24 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 25 | auto* cond = Expr("cond"); |
| 26 | auto* body = Block(Return()); |
| 27 | auto* i = If(cond, body); |
| 28 | WrapInFunction(i); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 29 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 30 | GeneratorImpl& gen = Build(); |
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 | gen.increment_indent(); |
| 33 | ASSERT_TRUE(gen.EmitStatement(i)) << gen.error(); |
| 34 | EXPECT_EQ(gen.result(), R"( if (cond) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 35 | return; |
| 36 | } |
| 37 | )"); |
| 38 | } |
| 39 | |
| 40 | TEST_F(GlslGeneratorImplTest_If, Emit_IfWithElseIf) { |
Ben Clayton | 01208e7 | 2022-06-25 08:12:59 +0000 | [diff] [blame] | 41 | GlobalVar("cond", ty.bool_(), ast::StorageClass::kPrivate); |
| 42 | GlobalVar("else_cond", ty.bool_(), ast::StorageClass::kPrivate); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 43 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 44 | auto* else_cond = Expr("else_cond"); |
| 45 | auto* else_body = Block(Return()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 46 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 47 | auto* cond = Expr("cond"); |
| 48 | auto* body = Block(Return()); |
James Price | 8aff0ed | 2022-05-02 14:53:36 +0000 | [diff] [blame] | 49 | auto* i = If(cond, body, Else(If(else_cond, else_body))); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 50 | WrapInFunction(i); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 51 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 52 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 53 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 54 | gen.increment_indent(); |
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 | ASSERT_TRUE(gen.EmitStatement(i)) << gen.error(); |
| 57 | EXPECT_EQ(gen.result(), R"( if (cond) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 58 | return; |
| 59 | } else { |
| 60 | if (else_cond) { |
| 61 | return; |
| 62 | } |
| 63 | } |
| 64 | )"); |
| 65 | } |
| 66 | |
| 67 | TEST_F(GlslGeneratorImplTest_If, Emit_IfWithElse) { |
Ben Clayton | 01208e7 | 2022-06-25 08:12:59 +0000 | [diff] [blame] | 68 | GlobalVar("cond", ty.bool_(), ast::StorageClass::kPrivate); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 69 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 70 | auto* else_body = Block(Return()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 71 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 72 | auto* cond = Expr("cond"); |
| 73 | auto* body = Block(Return()); |
James Price | 8aff0ed | 2022-05-02 14:53:36 +0000 | [diff] [blame] | 74 | auto* i = If(cond, body, Else(else_body)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 75 | WrapInFunction(i); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 76 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 77 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 78 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 79 | gen.increment_indent(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 80 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 81 | ASSERT_TRUE(gen.EmitStatement(i)) << gen.error(); |
| 82 | EXPECT_EQ(gen.result(), R"( if (cond) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 83 | return; |
| 84 | } else { |
| 85 | return; |
| 86 | } |
| 87 | )"); |
| 88 | } |
| 89 | |
| 90 | TEST_F(GlslGeneratorImplTest_If, Emit_IfWithMultiple) { |
Ben Clayton | 01208e7 | 2022-06-25 08:12:59 +0000 | [diff] [blame] | 91 | GlobalVar("cond", ty.bool_(), ast::StorageClass::kPrivate); |
| 92 | GlobalVar("else_cond", ty.bool_(), ast::StorageClass::kPrivate); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 93 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 94 | auto* else_cond = Expr("else_cond"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 95 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 96 | auto* else_body = Block(Return()); |
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_body_2 = Block(Return()); |
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* cond = Expr("cond"); |
| 101 | auto* body = Block(Return()); |
James Price | 8aff0ed | 2022-05-02 14:53:36 +0000 | [diff] [blame] | 102 | 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] | 103 | WrapInFunction(i); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 104 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 105 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 106 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 107 | gen.increment_indent(); |
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 | ASSERT_TRUE(gen.EmitStatement(i)) << gen.error(); |
| 110 | EXPECT_EQ(gen.result(), R"( if (cond) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 111 | return; |
| 112 | } else { |
| 113 | if (else_cond) { |
| 114 | return; |
| 115 | } else { |
| 116 | return; |
| 117 | } |
| 118 | } |
| 119 | )"); |
| 120 | } |
| 121 | |
| 122 | } // namespace |
dan sinclair | 5d59059 | 2022-04-07 14:40:24 +0000 | [diff] [blame] | 123 | } // namespace tint::writer::glsl |