xref: /aosp_15_r20/external/angle/src/tests/compiler_tests/PruneEmptyCases_test.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2018 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // PruneEmptyCases_test.cpp:
7 //   Tests for pruning empty cases and switch statements. This ensures that the translator doesn't
8 //   produce switch statements where the last case statement is not followed by anything.
9 //
10 
11 #include "GLSLANG/ShaderLang.h"
12 #include "angle_gl.h"
13 #include "gtest/gtest.h"
14 #include "tests/test_utils/compiler_test.h"
15 
16 using namespace sh;
17 
18 namespace
19 {
20 
21 class PruneEmptyCasesTest : public MatchOutputCodeTest
22 {
23   public:
PruneEmptyCasesTest()24     PruneEmptyCasesTest() : MatchOutputCodeTest(GL_FRAGMENT_SHADER, SH_GLSL_COMPATIBILITY_OUTPUT) {}
25 };
26 
27 // Test that a switch statement that only contains no-ops is pruned entirely.
TEST_F(PruneEmptyCasesTest,SwitchStatementWithOnlyNoOps)28 TEST_F(PruneEmptyCasesTest, SwitchStatementWithOnlyNoOps)
29 {
30     const std::string shaderString =
31         R"(#version 300 es
32 
33         uniform int ui;
34 
35         void main(void)
36         {
37             int i = ui;
38             switch (i)
39             {
40                 case 0:
41                 case 1:
42                     { {} }
43                     int j;
44                     1;
45             }
46         })";
47     compile(shaderString);
48     ASSERT_TRUE(notFoundInCode("switch"));
49     ASSERT_TRUE(notFoundInCode("case"));
50 }
51 
52 // Test that a init statement that has a side effect is preserved even if the switch is pruned.
TEST_F(PruneEmptyCasesTest,SwitchStatementWithOnlyNoOpsAndInitWithSideEffect)53 TEST_F(PruneEmptyCasesTest, SwitchStatementWithOnlyNoOpsAndInitWithSideEffect)
54 {
55     const std::string shaderString =
56         R"(#version 300 es
57 
58         precision mediump float;
59         out vec4 my_FragColor;
60 
61         uniform int uni_i;
62 
63         void main(void)
64         {
65             int i = uni_i;
66             switch (++i)
67             {
68                 case 0:
69                 case 1:
70                     { {} }
71                     int j;
72                     1;
73             }
74             my_FragColor = vec4(i);
75         })";
76     compile(shaderString);
77     ASSERT_TRUE(notFoundInCode("switch"));
78     ASSERT_TRUE(notFoundInCode("case"));
79     ASSERT_TRUE(foundInCode("++_ui"));
80 }
81 
82 // Test a switch statement where the last case only contains no-ops.
TEST_F(PruneEmptyCasesTest,SwitchStatementLastCaseOnlyNoOps)83 TEST_F(PruneEmptyCasesTest, SwitchStatementLastCaseOnlyNoOps)
84 {
85     const std::string shaderString =
86         R"(#version 300 es
87 
88         precision mediump float;
89         out vec4 my_FragColor;
90 
91         uniform int ui;
92 
93         void main(void)
94         {
95             int i = ui;
96             switch (i)
97             {
98                 case 0:
99                     my_FragColor = vec4(0);
100                     break;
101                 case 1:
102                 case 2:
103                     { {} }
104                     int j;
105                     1;
106             }
107         })";
108     compile(shaderString);
109     ASSERT_TRUE(foundInCode("switch"));
110     ASSERT_TRUE(foundInCode("case", 1));
111 }
112 
113 }  // namespace
114