blob: 1f607810860400a31e2a5582eac444445ac9d746 [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001// 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 Clayton0ce9ab02022-05-05 20:23:40 +000018using namespace tint::number_suffixes; // NOLINT
19
dan sinclair5d590592022-04-07 14:40:24 +000020namespace tint::writer::glsl {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000021namespace {
22
23using GlslGeneratorImplTest_Case = TestHelper;
24
25TEST_F(GlslGeneratorImplTest_Case, Emit_Case) {
Ben Clayton0ce9ab02022-05-05 20:23:40 +000026 auto* s = Switch(1_i, Case(Expr(5_i), Block(create<ast::BreakStatement>())), DefaultCase());
dan sinclair41e4d9a2022-05-01 14:40:55 +000027 WrapInFunction(s);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000028
dan sinclair41e4d9a2022-05-01 14:40:55 +000029 GeneratorImpl& gen = Build();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000030
dan sinclair41e4d9a2022-05-01 14:40:55 +000031 gen.increment_indent();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000032
dan sinclair41e4d9a2022-05-01 14:40:55 +000033 ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error();
34 EXPECT_EQ(gen.result(), R"( case 5: {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000035 break;
36 }
37)");
38}
39
40TEST_F(GlslGeneratorImplTest_Case, Emit_Case_BreaksByDefault) {
Ben Clayton0ce9ab02022-05-05 20:23:40 +000041 auto* s = Switch(1_i, Case(Expr(5_i), Block()), DefaultCase());
dan sinclair41e4d9a2022-05-01 14:40:55 +000042 WrapInFunction(s);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000043
dan sinclair41e4d9a2022-05-01 14:40:55 +000044 GeneratorImpl& gen = Build();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000045
dan sinclair41e4d9a2022-05-01 14:40:55 +000046 gen.increment_indent();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000047
dan sinclair41e4d9a2022-05-01 14:40:55 +000048 ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error();
49 EXPECT_EQ(gen.result(), R"( case 5: {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000050 break;
51 }
52)");
53}
54
55TEST_F(GlslGeneratorImplTest_Case, Emit_Case_WithFallthrough) {
Ben Clayton0ce9ab02022-05-05 20:23:40 +000056 auto* s =
57 Switch(1_i, Case(Expr(5_i), Block(create<ast::FallthroughStatement>())), DefaultCase());
dan sinclair41e4d9a2022-05-01 14:40:55 +000058 WrapInFunction(s);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000059
dan sinclair41e4d9a2022-05-01 14:40:55 +000060 GeneratorImpl& gen = Build();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000061
dan sinclair41e4d9a2022-05-01 14:40:55 +000062 gen.increment_indent();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000063
dan sinclair41e4d9a2022-05-01 14:40:55 +000064 ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error();
65 EXPECT_EQ(gen.result(), R"( case 5: {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000066 /* fallthrough */
67 }
68)");
69}
70
71TEST_F(GlslGeneratorImplTest_Case, Emit_Case_MultipleSelectors) {
Ben Clayton783b1692022-08-02 17:03:35 +000072 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 Clayton0ce9ab02022-05-05 20:23:40 +000079 DefaultCase());
dan sinclair41e4d9a2022-05-01 14:40:55 +000080 WrapInFunction(s);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000081
dan sinclair41e4d9a2022-05-01 14:40:55 +000082 GeneratorImpl& gen = Build();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000083
dan sinclair41e4d9a2022-05-01 14:40:55 +000084 gen.increment_indent();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000085
dan sinclair41e4d9a2022-05-01 14:40:55 +000086 ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error();
87 EXPECT_EQ(gen.result(), R"( case 5:
Ryan Harrisondbc13af2022-02-21 15:19:07 +000088 case 6: {
89 break;
90 }
91)");
92}
93
94TEST_F(GlslGeneratorImplTest_Case, Emit_Case_Default) {
Ben Clayton0ce9ab02022-05-05 20:23:40 +000095 auto* s = Switch(1_i, DefaultCase(Block(create<ast::BreakStatement>())));
dan sinclair41e4d9a2022-05-01 14:40:55 +000096 WrapInFunction(s);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000097
dan sinclair41e4d9a2022-05-01 14:40:55 +000098 GeneratorImpl& gen = Build();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000099
dan sinclair41e4d9a2022-05-01 14:40:55 +0000100 gen.increment_indent();
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000101
dan sinclair41e4d9a2022-05-01 14:40:55 +0000102 ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error();
103 EXPECT_EQ(gen.result(), R"( default: {
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000104 break;
105 }
106)");
107}
108
109} // namespace
dan sinclair5d590592022-04-07 14:40:24 +0000110} // namespace tint::writer::glsl