Path: blob/main_old/src/tests/compiler_tests/PruneEmptyCases_test.cpp
1693 views
//1// Copyright 2018 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//5// PruneEmptyCases_test.cpp:6// Tests for pruning empty cases and switch statements. This ensures that the translator doesn't7// produce switch statements where the last case statement is not followed by anything.8//910#include "GLSLANG/ShaderLang.h"11#include "angle_gl.h"12#include "gtest/gtest.h"13#include "tests/test_utils/compiler_test.h"1415using namespace sh;1617namespace18{1920class PruneEmptyCasesTest : public MatchOutputCodeTest21{22public:23PruneEmptyCasesTest() : MatchOutputCodeTest(GL_FRAGMENT_SHADER, 0, SH_GLSL_COMPATIBILITY_OUTPUT)24{}25};2627// Test that a switch statement that only contains no-ops is pruned entirely.28TEST_F(PruneEmptyCasesTest, SwitchStatementWithOnlyNoOps)29{30const std::string shaderString =31R"(#version 300 es3233uniform int ui;3435void main(void)36{37int i = ui;38switch (i)39{40case 0:41case 1:42{ {} }43int j;441;45}46})";47compile(shaderString);48ASSERT_TRUE(notFoundInCode("switch"));49ASSERT_TRUE(notFoundInCode("case"));50}5152// Test that a init statement that has a side effect is preserved even if the switch is pruned.53TEST_F(PruneEmptyCasesTest, SwitchStatementWithOnlyNoOpsAndInitWithSideEffect)54{55const std::string shaderString =56R"(#version 300 es5758precision mediump float;59out vec4 my_FragColor;6061uniform int uni_i;6263void main(void)64{65int i = uni_i;66switch (++i)67{68case 0:69case 1:70{ {} }71int j;721;73}74my_FragColor = vec4(i);75})";76compile(shaderString);77ASSERT_TRUE(notFoundInCode("switch"));78ASSERT_TRUE(notFoundInCode("case"));79ASSERT_TRUE(foundInCode("++_ui"));80}8182// Test a switch statement where the last case only contains no-ops.83TEST_F(PruneEmptyCasesTest, SwitchStatementLastCaseOnlyNoOps)84{85const std::string shaderString =86R"(#version 300 es8788precision mediump float;89out vec4 my_FragColor;9091uniform int ui;9293void main(void)94{95int i = ui;96switch (i)97{98case 0:99my_FragColor = vec4(0);100break;101case 1:102case 2:103{ {} }104int j;1051;106}107})";108compile(shaderString);109ASSERT_TRUE(foundInCode("switch"));110ASSERT_TRUE(foundInCode("case", 1));111}112113} // namespace114115116