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 | |
dan sinclair | 5d59059 | 2022-04-07 14:40:24 +0000 | [diff] [blame] | 18 | namespace tint::writer::glsl { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 19 | namespace { |
| 20 | |
| 21 | using GlslGeneratorImplTest_Case = TestHelper; |
| 22 | |
| 23 | TEST_F(GlslGeneratorImplTest_Case, Emit_Case) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 24 | auto* s = Switch(1, Case(Expr(5), Block(create<ast::BreakStatement>())), DefaultCase()); |
| 25 | WrapInFunction(s); |
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 | GeneratorImpl& gen = Build(); |
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 | gen.increment_indent(); |
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 | ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error(); |
| 32 | EXPECT_EQ(gen.result(), R"( case 5: { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 33 | break; |
| 34 | } |
| 35 | )"); |
| 36 | } |
| 37 | |
| 38 | TEST_F(GlslGeneratorImplTest_Case, Emit_Case_BreaksByDefault) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 39 | auto* s = Switch(1, Case(Expr(5), Block()), DefaultCase()); |
| 40 | WrapInFunction(s); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 41 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 42 | GeneratorImpl& gen = Build(); |
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 | gen.increment_indent(); |
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 | ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error(); |
| 47 | EXPECT_EQ(gen.result(), R"( case 5: { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 48 | break; |
| 49 | } |
| 50 | )"); |
| 51 | } |
| 52 | |
| 53 | TEST_F(GlslGeneratorImplTest_Case, Emit_Case_WithFallthrough) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 54 | auto* s = Switch(1, Case(Expr(5), Block(create<ast::FallthroughStatement>())), DefaultCase()); |
| 55 | WrapInFunction(s); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 56 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 57 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 58 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 59 | gen.increment_indent(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 60 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 61 | ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error(); |
| 62 | EXPECT_EQ(gen.result(), R"( case 5: { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 63 | /* fallthrough */ |
| 64 | } |
| 65 | )"); |
| 66 | } |
| 67 | |
| 68 | TEST_F(GlslGeneratorImplTest_Case, Emit_Case_MultipleSelectors) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 69 | auto* s = |
| 70 | Switch(1, Case({Expr(5), Expr(6)}, Block(create<ast::BreakStatement>())), DefaultCase()); |
| 71 | WrapInFunction(s); |
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 | GeneratorImpl& gen = Build(); |
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 | gen.increment_indent(); |
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 | ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error(); |
| 78 | EXPECT_EQ(gen.result(), R"( case 5: |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 79 | case 6: { |
| 80 | break; |
| 81 | } |
| 82 | )"); |
| 83 | } |
| 84 | |
| 85 | TEST_F(GlslGeneratorImplTest_Case, Emit_Case_Default) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 86 | auto* s = Switch(1, DefaultCase(Block(create<ast::BreakStatement>()))); |
| 87 | WrapInFunction(s); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 88 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 89 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 90 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 91 | gen.increment_indent(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 92 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 93 | ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error(); |
| 94 | EXPECT_EQ(gen.result(), R"( default: { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 95 | break; |
| 96 | } |
| 97 | )"); |
| 98 | } |
| 99 | |
| 100 | } // namespace |
dan sinclair | 5d59059 | 2022-04-07 14:40:24 +0000 | [diff] [blame] | 101 | } // namespace tint::writer::glsl |