xref: /aosp_15_r20/external/angle/src/tests/compiler_tests/PruneNoOps_test.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2024 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 // PruneNoOps_test.cpp:
7 //   Tests for pruning no-op statements.
8 //
9 
10 #include "GLSLANG/ShaderLang.h"
11 #include "angle_gl.h"
12 #include "gtest/gtest.h"
13 #include "tests/test_utils/compiler_test.h"
14 
15 using namespace sh;
16 
17 namespace
18 {
19 
20 class PruneNoOpsTest : public MatchOutputCodeTest
21 {
22   public:
PruneNoOpsTest()23     PruneNoOpsTest() : MatchOutputCodeTest(GL_FRAGMENT_SHADER, SH_GLSL_COMPATIBILITY_OUTPUT) {}
24 };
25 
26 // Test that a switch statement with a constant expression without a matching case is pruned.
TEST_F(PruneNoOpsTest,SwitchStatementWithConstantExpressionNoMatchingCase)27 TEST_F(PruneNoOpsTest, SwitchStatementWithConstantExpressionNoMatchingCase)
28 {
29     const std::string shaderString = R"(#version 300 es
30 precision mediump float;
31 out vec4 color;
32 
33 void main(void)
34 {
35     switch (10)
36     {
37         case 0:
38             color = vec4(0);
39             break;
40         case 1:
41             color = vec4(1);
42             break;
43     }
44 })";
45     compile(shaderString);
46     ASSERT_TRUE(notFoundInCode("switch"));
47     ASSERT_TRUE(notFoundInCode("case"));
48 }
49 
50 // Test that a switch statement with a constant expression with a default is not pruned.
TEST_F(PruneNoOpsTest,SwitchStatementWithConstantExpressionWithDefault)51 TEST_F(PruneNoOpsTest, SwitchStatementWithConstantExpressionWithDefault)
52 {
53     const std::string shaderString = R"(#version 300 es
54 precision mediump float;
55 out vec4 color;
56 
57 void main(void)
58 {
59     switch (10)
60     {
61         case 0:
62             color = vec4(0);
63             break;
64         case 1:
65             color = vec4(1);
66             break;
67         default:
68             color = vec4(0.5);
69             break;
70     }
71 })";
72     compile(shaderString);
73     ASSERT_TRUE(foundInCode("switch"));
74     ASSERT_TRUE(foundInCode("case"));
75 }
76 
77 // Test that a switch statement with a constant expression with a matching case is not pruned.
TEST_F(PruneNoOpsTest,SwitchStatementWithConstantExpressionWithMatchingCase)78 TEST_F(PruneNoOpsTest, SwitchStatementWithConstantExpressionWithMatchingCase)
79 {
80     const std::string shaderString = R"(#version 300 es
81 precision mediump float;
82 out vec4 color;
83 
84 void main(void)
85 {
86     switch (10)
87     {
88         case 0:
89             color = vec4(0);
90             break;
91         case 10:
92             color = vec4(1);
93             break;
94     }
95 })";
96     compile(shaderString);
97     ASSERT_TRUE(foundInCode("switch"));
98     ASSERT_TRUE(foundInCode("case"));
99 }
100 
101 }  // namespace
102