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/ast/fallthrough_statement.h" |
| 16 | #include "src/tint/writer/glsl/test_helper.h" |
| 17 | |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 18 | using namespace tint::number_suffixes; // NOLINT |
| 19 | |
dan sinclair | 5d59059 | 2022-04-07 14:40:24 +0000 | [diff] [blame] | 20 | namespace tint::writer::glsl { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | using GlslGeneratorImplTest_Case = TestHelper; |
| 24 | |
| 25 | TEST_F(GlslGeneratorImplTest_Case, Emit_Case) { |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 26 | auto* s = Switch(1_i, Case(Expr(5_i), Block(create<ast::BreakStatement>())), DefaultCase()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 27 | WrapInFunction(s); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 28 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 29 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 30 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 31 | gen.increment_indent(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 32 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 33 | ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error(); |
| 34 | EXPECT_EQ(gen.result(), R"( case 5: { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 35 | break; |
| 36 | } |
| 37 | )"); |
| 38 | } |
| 39 | |
| 40 | TEST_F(GlslGeneratorImplTest_Case, Emit_Case_BreaksByDefault) { |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 41 | auto* s = Switch(1_i, Case(Expr(5_i), Block()), DefaultCase()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 42 | WrapInFunction(s); |
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 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 45 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 46 | gen.increment_indent(); |
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 | ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error(); |
| 49 | EXPECT_EQ(gen.result(), R"( case 5: { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 50 | break; |
| 51 | } |
| 52 | )"); |
| 53 | } |
| 54 | |
| 55 | TEST_F(GlslGeneratorImplTest_Case, Emit_Case_WithFallthrough) { |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 56 | auto* s = |
| 57 | Switch(1_i, Case(Expr(5_i), Block(create<ast::FallthroughStatement>())), DefaultCase()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 58 | WrapInFunction(s); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 59 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 60 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 61 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 62 | gen.increment_indent(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 63 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 64 | ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error(); |
| 65 | EXPECT_EQ(gen.result(), R"( case 5: { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 66 | /* fallthrough */ |
| 67 | } |
| 68 | )"); |
| 69 | } |
| 70 | |
| 71 | TEST_F(GlslGeneratorImplTest_Case, Emit_Case_MultipleSelectors) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 72 | auto* s = Switch(1_i, |
| 73 | Case( |
| 74 | utils::Vector{ |
| 75 | Expr(5_i), |
| 76 | Expr(6_i), |
| 77 | }, |
| 78 | Block(create<ast::BreakStatement>())), |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 79 | DefaultCase()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 80 | WrapInFunction(s); |
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 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 83 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 84 | gen.increment_indent(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 85 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 86 | ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error(); |
| 87 | EXPECT_EQ(gen.result(), R"( case 5: |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 88 | case 6: { |
| 89 | break; |
| 90 | } |
| 91 | )"); |
| 92 | } |
| 93 | |
| 94 | TEST_F(GlslGeneratorImplTest_Case, Emit_Case_Default) { |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 95 | auto* s = Switch(1_i, DefaultCase(Block(create<ast::BreakStatement>()))); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 96 | WrapInFunction(s); |
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 | GeneratorImpl& gen = Build(); |
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 | gen.increment_indent(); |
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 | ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error(); |
| 103 | EXPECT_EQ(gen.result(), R"( default: { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 104 | break; |
| 105 | } |
| 106 | )"); |
| 107 | } |
| 108 | |
| 109 | } // namespace |
dan sinclair | 5d59059 | 2022-04-07 14:40:24 +0000 | [diff] [blame] | 110 | } // namespace tint::writer::glsl |