dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +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 | |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 15 | #include "src/ast/else_statement.h" |
| 16 | #include "src/ast/identifier_expression.h" |
| 17 | #include "src/ast/if_statement.h" |
| 18 | #include "src/ast/module.h" |
| 19 | #include "src/ast/return_statement.h" |
dan sinclair | df503f0 | 2020-08-26 19:05:46 +0000 | [diff] [blame] | 20 | #include "src/writer/hlsl/test_helper.h" |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 21 | |
| 22 | namespace tint { |
| 23 | namespace writer { |
| 24 | namespace hlsl { |
| 25 | namespace { |
| 26 | |
dan sinclair | 63b007d | 2020-08-26 20:02:26 +0000 | [diff] [blame] | 27 | using HlslGeneratorImplTest_If = TestHelper; |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 28 | |
dan sinclair | df503f0 | 2020-08-26 19:05:46 +0000 | [diff] [blame] | 29 | TEST_F(HlslGeneratorImplTest_If, Emit_If) { |
Ben Clayton | 9df857d | 2020-12-15 14:11:48 +0000 | [diff] [blame] | 30 | auto* cond = Expr("cond"); |
| 31 | auto* body = create<ast::BlockStatement>(ast::StatementList{ |
| 32 | create<ast::ReturnStatement>(), |
| 33 | }); |
| 34 | auto* i = create<ast::IfStatement>(cond, body, ast::ElseStatementList{}); |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 35 | gen.increment_indent(); |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 36 | |
Ben Clayton | 9df857d | 2020-12-15 14:11:48 +0000 | [diff] [blame] | 37 | ASSERT_TRUE(gen.EmitStatement(out, i)) << gen.error(); |
dan sinclair | f4bc0e7 | 2021-01-11 16:24:32 +0000 | [diff] [blame] | 38 | EXPECT_EQ(result(), R"( if (test_cond) { |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 39 | return; |
| 40 | } |
| 41 | )"); |
| 42 | } |
| 43 | |
dan sinclair | df503f0 | 2020-08-26 19:05:46 +0000 | [diff] [blame] | 44 | TEST_F(HlslGeneratorImplTest_If, Emit_IfWithElseIf) { |
Ben Clayton | 9df857d | 2020-12-15 14:11:48 +0000 | [diff] [blame] | 45 | auto* else_cond = Expr("else_cond"); |
| 46 | auto* else_body = create<ast::BlockStatement>(ast::StatementList{ |
| 47 | create<ast::ReturnStatement>(), |
| 48 | }); |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 49 | |
Ben Clayton | 9df857d | 2020-12-15 14:11:48 +0000 | [diff] [blame] | 50 | auto* cond = Expr("cond"); |
| 51 | auto* body = create<ast::BlockStatement>(ast::StatementList{ |
| 52 | create<ast::ReturnStatement>(), |
| 53 | }); |
| 54 | auto* i = create<ast::IfStatement>( |
| 55 | cond, body, |
| 56 | ast::ElseStatementList{create<ast::ElseStatement>(else_cond, else_body)}); |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 57 | |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 58 | gen.increment_indent(); |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 59 | |
Ben Clayton | 9df857d | 2020-12-15 14:11:48 +0000 | [diff] [blame] | 60 | ASSERT_TRUE(gen.EmitStatement(out, i)) << gen.error(); |
dan sinclair | f4bc0e7 | 2021-01-11 16:24:32 +0000 | [diff] [blame] | 61 | EXPECT_EQ(result(), R"( if (test_cond) { |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 62 | return; |
dan sinclair | a0842b5 | 2020-09-02 14:29:05 +0000 | [diff] [blame] | 63 | } else { |
dan sinclair | f4bc0e7 | 2021-01-11 16:24:32 +0000 | [diff] [blame] | 64 | if (test_else_cond) { |
dan sinclair | a0842b5 | 2020-09-02 14:29:05 +0000 | [diff] [blame] | 65 | return; |
| 66 | } |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 67 | } |
| 68 | )"); |
| 69 | } |
| 70 | |
dan sinclair | df503f0 | 2020-08-26 19:05:46 +0000 | [diff] [blame] | 71 | TEST_F(HlslGeneratorImplTest_If, Emit_IfWithElse) { |
Ben Clayton | 9df857d | 2020-12-15 14:11:48 +0000 | [diff] [blame] | 72 | auto* else_body = create<ast::BlockStatement>(ast::StatementList{ |
| 73 | create<ast::ReturnStatement>(), |
| 74 | }); |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 75 | |
Ben Clayton | 9df857d | 2020-12-15 14:11:48 +0000 | [diff] [blame] | 76 | auto* cond = Expr("cond"); |
| 77 | auto* body = create<ast::BlockStatement>(ast::StatementList{ |
| 78 | create<ast::ReturnStatement>(), |
| 79 | }); |
| 80 | auto* i = create<ast::IfStatement>( |
| 81 | cond, body, |
| 82 | ast::ElseStatementList{create<ast::ElseStatement>(nullptr, else_body)}); |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 83 | |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 84 | gen.increment_indent(); |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 85 | |
Ben Clayton | 9df857d | 2020-12-15 14:11:48 +0000 | [diff] [blame] | 86 | ASSERT_TRUE(gen.EmitStatement(out, i)) << gen.error(); |
dan sinclair | f4bc0e7 | 2021-01-11 16:24:32 +0000 | [diff] [blame] | 87 | EXPECT_EQ(result(), R"( if (test_cond) { |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 88 | return; |
| 89 | } else { |
| 90 | return; |
| 91 | } |
| 92 | )"); |
| 93 | } |
| 94 | |
dan sinclair | df503f0 | 2020-08-26 19:05:46 +0000 | [diff] [blame] | 95 | TEST_F(HlslGeneratorImplTest_If, Emit_IfWithMultiple) { |
Ben Clayton | 9df857d | 2020-12-15 14:11:48 +0000 | [diff] [blame] | 96 | auto* else_cond = Expr("else_cond"); |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 97 | |
Ben Clayton | 9df857d | 2020-12-15 14:11:48 +0000 | [diff] [blame] | 98 | auto* else_body = create<ast::BlockStatement>(ast::StatementList{ |
| 99 | create<ast::ReturnStatement>(), |
| 100 | }); |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 101 | |
Ben Clayton | 9df857d | 2020-12-15 14:11:48 +0000 | [diff] [blame] | 102 | auto* else_body_2 = create<ast::BlockStatement>(ast::StatementList{ |
| 103 | create<ast::ReturnStatement>(), |
| 104 | }); |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 105 | |
Ben Clayton | 9df857d | 2020-12-15 14:11:48 +0000 | [diff] [blame] | 106 | auto* cond = Expr("cond"); |
| 107 | auto* body = create<ast::BlockStatement>(ast::StatementList{ |
| 108 | create<ast::ReturnStatement>(), |
| 109 | }); |
| 110 | auto* i = create<ast::IfStatement>( |
| 111 | cond, body, |
| 112 | ast::ElseStatementList{ |
| 113 | create<ast::ElseStatement>(else_cond, else_body), |
| 114 | create<ast::ElseStatement>(nullptr, else_body_2), |
Ben Clayton | bbefff6 | 2020-12-12 11:58:44 +0000 | [diff] [blame] | 115 | }); |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 116 | |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 117 | gen.increment_indent(); |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 118 | |
Ben Clayton | 9df857d | 2020-12-15 14:11:48 +0000 | [diff] [blame] | 119 | ASSERT_TRUE(gen.EmitStatement(out, i)) << gen.error(); |
dan sinclair | f4bc0e7 | 2021-01-11 16:24:32 +0000 | [diff] [blame] | 120 | EXPECT_EQ(result(), R"( if (test_cond) { |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 121 | return; |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 122 | } else { |
dan sinclair | f4bc0e7 | 2021-01-11 16:24:32 +0000 | [diff] [blame] | 123 | if (test_else_cond) { |
dan sinclair | a0842b5 | 2020-09-02 14:29:05 +0000 | [diff] [blame] | 124 | return; |
| 125 | } else { |
| 126 | return; |
| 127 | } |
dan sinclair | 97bc4ad | 2020-07-30 22:26:55 +0000 | [diff] [blame] | 128 | } |
| 129 | )"); |
| 130 | } |
| 131 | |
| 132 | } // namespace |
| 133 | } // namespace hlsl |
| 134 | } // namespace writer |
| 135 | } // namespace tint |