• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2017 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 // PrunePureLiteralStatements_test.cpp:
7 //   Tests for pruning literal statements.
8 //
9 
10 #include "angle_gl.h"
11 #include "gtest/gtest.h"
12 #include "tests/test_utils/compiler_test.h"
13 
14 using namespace sh;
15 
16 namespace
17 {
18 
19 class PrunePureLiteralStatementsTest : public MatchOutputCodeTest
20 {
21   public:
22     // The PrunePureLiteralStatements pass is used when outputting ESSL
PrunePureLiteralStatementsTest()23     PrunePureLiteralStatementsTest() : MatchOutputCodeTest(GL_FRAGMENT_SHADER, 0, SH_ESSL_OUTPUT) {}
24 };
25 
26 // Most basic test for the pruning
TEST_F(PrunePureLiteralStatementsTest,FloatLiteralStatement)27 TEST_F(PrunePureLiteralStatementsTest, FloatLiteralStatement)
28 {
29     const std::string shaderString =
30         R"(precision mediump float;
31         void main()
32         {
33            float f = 41.0;
34            42.0;
35            gl_FragColor = vec4(f);
36         })";
37     compile(shaderString);
38     ASSERT_TRUE(foundInCode("41"));
39     ASSERT_TRUE(notFoundInCode("42"));
40 }
41 
42 // Test the pruning works for constructed types too
TEST_F(PrunePureLiteralStatementsTest,ConstructorLiteralStatement)43 TEST_F(PrunePureLiteralStatementsTest, ConstructorLiteralStatement)
44 {
45     const std::string shaderString =
46         R"(precision mediump float;
47         void main()
48         {
49             vec2 f = vec2(41.0, 41.0);
50             vec2(42.0, 42.0);
51             gl_FragColor = vec4(f, 0.0, 0.0);
52         })";
53     compile(shaderString);
54     ASSERT_TRUE(foundInCode("41"));
55     ASSERT_TRUE(notFoundInCode("42"));
56 }
57 
58 // Test the pruning works when the literal is a (non-trivial) expression
TEST_F(PrunePureLiteralStatementsTest,ExpressionLiteralStatement)59 TEST_F(PrunePureLiteralStatementsTest, ExpressionLiteralStatement)
60 {
61     const std::string shaderString =
62         "precision mediump float;\n"
63         "void main()\n"
64         "{\n"
65         "   vec2(21.0, 21.0) + vec2(21.0, 21.0);\n"
66         "}\n";
67     compile(shaderString);
68     ASSERT_TRUE(notFoundInCode("21"));
69     ASSERT_TRUE(notFoundInCode("42"));
70 }
71 
72 // Test that the pruning happens in the for-loop expression too
TEST_F(PrunePureLiteralStatementsTest,ForLoopLiteralExpression)73 TEST_F(PrunePureLiteralStatementsTest, ForLoopLiteralExpression)
74 {
75     const std::string shaderString =
76         "precision mediump float;\n"
77         "void main()\n"
78         "{\n"
79         "    for (;; vec2(42.0, 42.0)) {}\n"
80         "}\n";
81     compile(shaderString);
82     ASSERT_TRUE(notFoundInCode("42"));
83 }
84 
85 // Test that the pruning correctly handles the pruning inside switch statements - for a switch with
86 // one empty case.
TEST_F(PrunePureLiteralStatementsTest,SwitchLiteralExpressionEmptyCase)87 TEST_F(PrunePureLiteralStatementsTest, SwitchLiteralExpressionEmptyCase)
88 {
89     const std::string shaderString =
90         "#version 300 es\n"
91         "precision mediump float;\n"
92         "void main()\n"
93         "{\n"
94         "    switch(1)\n"
95         "    {\n"
96         "      default:\n"
97         "        42;\n"
98         "    }\n"
99         "}\n";
100     compile(shaderString);
101     ASSERT_TRUE(notFoundInCode("default"));
102     ASSERT_TRUE(notFoundInCode("42"));
103 }
104 
105 // Test that the pruning correctly handles the pruning inside switch statements - for a switch with
106 // multiple cases.
TEST_F(PrunePureLiteralStatementsTest,SwitchLiteralExpressionEmptyCases)107 TEST_F(PrunePureLiteralStatementsTest, SwitchLiteralExpressionEmptyCases)
108 {
109     const std::string shaderString =
110         "#version 300 es\n"
111         "precision mediump float;\n"
112         "void main()\n"
113         "{\n"
114         "    switch(1)\n"
115         "    {\n"
116         "      case 1:\n"
117         "      case 2:\n"
118         "      default:\n"
119         "        42;\n"
120         "    }\n"
121         "}\n";
122     compile(shaderString);
123     ASSERT_TRUE(notFoundInCode("default"));
124     ASSERT_TRUE(notFoundInCode("case"));
125     ASSERT_TRUE(notFoundInCode("42"));
126 }
127 
128 // Test that the pruning correctly handles the pruning inside switch statements - only cases at the
129 // end are deleted
TEST_F(PrunePureLiteralStatementsTest,SwitchLiteralExpressionOnlyLastCase)130 TEST_F(PrunePureLiteralStatementsTest, SwitchLiteralExpressionOnlyLastCase)
131 {
132     const std::string shaderString =
133         "#version 300 es\n"
134         "precision mediump float;\n"
135         "void main()\n"
136         "{\n"
137         "    switch(1)\n"
138         "    {\n"
139         "      case 1:\n"
140         "      default:\n"
141         "        42;\n"
142         "        break;\n"
143         "    }\n"
144         "}\n";
145     compile(shaderString);
146     ASSERT_TRUE(foundInCode("default"));
147     ASSERT_TRUE(foundInCode("case"));
148     ASSERT_TRUE(notFoundInCode("42"));
149 }
150 
151 // Test that the pruning correctly handles the pruning inside switch statements - pruning isn't
152 // stopped by literal statements
TEST_F(PrunePureLiteralStatementsTest,SwitchLiteralExpressionLiteralDoesntStop)153 TEST_F(PrunePureLiteralStatementsTest, SwitchLiteralExpressionLiteralDoesntStop)
154 {
155     const std::string shaderString =
156         "#version 300 es\n"
157         "precision mediump float;\n"
158         "void main()\n"
159         "{\n"
160         "    switch(1)\n"
161         "    {\n"
162         "      case 1:\n"
163         "        42;\n"
164         "      case 2:\n"
165         "        43;\n"
166         "    }\n"
167         "}\n";
168     compile(shaderString);
169     ASSERT_TRUE(notFoundInCode("case"));
170     ASSERT_TRUE(notFoundInCode("42"));
171     ASSERT_TRUE(notFoundInCode("43"));
172 }
173 
174 }  // namespace
175