Path: blob/main_old/src/tests/compiler_tests/PrunePureLiteralStatements_test.cpp
1693 views
//1// Copyright 2017 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// PrunePureLiteralStatements_test.cpp:6// Tests for pruning literal statements.7//89#include "angle_gl.h"10#include "gtest/gtest.h"11#include "tests/test_utils/compiler_test.h"1213using namespace sh;1415namespace16{1718class PrunePureLiteralStatementsTest : public MatchOutputCodeTest19{20public:21// The PrunePureLiteralStatements pass is used when outputting ESSL22PrunePureLiteralStatementsTest() : MatchOutputCodeTest(GL_FRAGMENT_SHADER, 0, SH_ESSL_OUTPUT) {}23};2425// Most basic test for the pruning26TEST_F(PrunePureLiteralStatementsTest, FloatLiteralStatement)27{28const std::string shaderString =29R"(precision mediump float;30void main()31{32float f = 41.0;3342.0;34gl_FragColor = vec4(f);35})";36compile(shaderString);37ASSERT_TRUE(foundInCode("41"));38ASSERT_TRUE(notFoundInCode("42"));39}4041// Test the pruning works for constructed types too42TEST_F(PrunePureLiteralStatementsTest, ConstructorLiteralStatement)43{44const std::string shaderString =45R"(precision mediump float;46void main()47{48vec2 f = vec2(41.0, 41.0);49vec2(42.0, 42.0);50gl_FragColor = vec4(f, 0.0, 0.0);51})";52compile(shaderString);53ASSERT_TRUE(foundInCode("41"));54ASSERT_TRUE(notFoundInCode("42"));55}5657// Test the pruning works when the literal is a (non-trivial) expression58TEST_F(PrunePureLiteralStatementsTest, ExpressionLiteralStatement)59{60const std::string shaderString =61"precision mediump float;\n"62"void main()\n"63"{\n"64" vec2(21.0, 21.0) + vec2(21.0, 21.0);\n"65"}\n";66compile(shaderString);67ASSERT_TRUE(notFoundInCode("21"));68ASSERT_TRUE(notFoundInCode("42"));69}7071// Test that the pruning happens in the for-loop expression too72TEST_F(PrunePureLiteralStatementsTest, ForLoopLiteralExpression)73{74const std::string shaderString =75"precision mediump float;\n"76"void main()\n"77"{\n"78" for (;; vec2(42.0, 42.0)) {}\n"79"}\n";80compile(shaderString);81ASSERT_TRUE(notFoundInCode("42"));82}8384// Test that the pruning correctly handles the pruning inside switch statements - for a switch with85// one empty case.86TEST_F(PrunePureLiteralStatementsTest, SwitchLiteralExpressionEmptyCase)87{88const std::string shaderString =89"#version 300 es\n"90"precision mediump float;\n"91"void main()\n"92"{\n"93" switch(1)\n"94" {\n"95" default:\n"96" 42;\n"97" }\n"98"}\n";99compile(shaderString);100ASSERT_TRUE(notFoundInCode("default"));101ASSERT_TRUE(notFoundInCode("42"));102}103104// Test that the pruning correctly handles the pruning inside switch statements - for a switch with105// multiple cases.106TEST_F(PrunePureLiteralStatementsTest, SwitchLiteralExpressionEmptyCases)107{108const std::string shaderString =109"#version 300 es\n"110"precision mediump float;\n"111"void main()\n"112"{\n"113" switch(1)\n"114" {\n"115" case 1:\n"116" case 2:\n"117" default:\n"118" 42;\n"119" }\n"120"}\n";121compile(shaderString);122ASSERT_TRUE(notFoundInCode("default"));123ASSERT_TRUE(notFoundInCode("case"));124ASSERT_TRUE(notFoundInCode("42"));125}126127// Test that the pruning correctly handles the pruning inside switch statements - only cases at the128// end are deleted129TEST_F(PrunePureLiteralStatementsTest, SwitchLiteralExpressionOnlyLastCase)130{131const std::string shaderString =132"#version 300 es\n"133"precision mediump float;\n"134"void main()\n"135"{\n"136" switch(1)\n"137" {\n"138" case 1:\n"139" default:\n"140" 42;\n"141" break;\n"142" }\n"143"}\n";144compile(shaderString);145ASSERT_TRUE(foundInCode("default"));146ASSERT_TRUE(foundInCode("case"));147ASSERT_TRUE(notFoundInCode("42"));148}149150// Test that the pruning correctly handles the pruning inside switch statements - pruning isn't151// stopped by literal statements152TEST_F(PrunePureLiteralStatementsTest, SwitchLiteralExpressionLiteralDoesntStop)153{154const std::string shaderString =155"#version 300 es\n"156"precision mediump float;\n"157"void main()\n"158"{\n"159" switch(1)\n"160" {\n"161" case 1:\n"162" 42;\n"163" case 2:\n"164" 43;\n"165" }\n"166"}\n";167compile(shaderString);168ASSERT_TRUE(notFoundInCode("case"));169ASSERT_TRUE(notFoundInCode("42"));170ASSERT_TRUE(notFoundInCode("43"));171}172173} // namespace174175176