1 // 2 // Copyright 2015 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 // compiler_test.h: 7 // utilities for compiler unit tests. 8 9 #ifndef TESTS_TEST_UTILS_COMPILER_TEST_H_ 10 #define TESTS_TEST_UTILS_COMPILER_TEST_H_ 11 12 #include <map> 13 #include <regex> 14 #include <vector> 15 16 #include "gtest/gtest.h" 17 18 #include "GLSLANG/ShaderLang.h" 19 #include "angle_gl.h" 20 #include "compiler/translator/glsl/TranslatorESSL.h" 21 #include "compiler/translator/tree_util/FindSymbolNode.h" 22 23 namespace sh 24 { 25 26 bool compileTestShader(GLenum type, 27 ShShaderSpec spec, 28 ShShaderOutput output, 29 const std::string &shaderString, 30 ShBuiltInResources *resources, 31 const ShCompileOptions &compileOptions, 32 std::string *translatedCode, 33 std::string *infoLog); 34 35 bool compileTestShader(GLenum type, 36 ShShaderSpec spec, 37 ShShaderOutput output, 38 const std::string &shaderString, 39 const ShCompileOptions &compileOptions, 40 std::string *translatedCode, 41 std::string *infoLog); 42 43 class MatchOutputCodeTest : public testing::Test 44 { 45 protected: 46 MatchOutputCodeTest(GLenum shaderType, ShShaderOutput outputType); 47 48 void setDefaultCompileOptions(const ShCompileOptions &defaultCompileOptions); 49 void addOutputType(const ShShaderOutput outputType); 50 51 ShBuiltInResources *getResources(); 52 53 // Compile functions clear any results from earlier calls to them. 54 void compile(const std::string &shaderString); 55 void compile(const std::string &shaderString, const ShCompileOptions &compileOptions); 56 foundInESSLCode(const char * stringToFind)57 bool foundInESSLCode(const char *stringToFind) const 58 { 59 return foundInCode(SH_ESSL_OUTPUT, stringToFind); 60 } 61 foundInGLSLCode(const char * stringToFind)62 bool foundInGLSLCode(const char *stringToFind) const 63 { 64 return foundInCode(SH_GLSL_COMPATIBILITY_OUTPUT, stringToFind); 65 } 66 67 bool foundInCode(ShShaderOutput output, const char *stringToFind) const; 68 bool foundInCodeRegex(ShShaderOutput output, 69 const std::regex ®exToFind, 70 std::smatch *match = nullptr) const; 71 72 // Test that the strings are found in the specified output in the specified order. 73 bool foundInCodeInOrder(ShShaderOutput output, std::vector<const char *> stringsToFind); 74 75 // Test that the string occurs for exactly expectedOccurrences times 76 bool foundInCode(ShShaderOutput output, 77 const char *stringToFind, 78 const int expectedOccurrences) const; 79 80 // Test that the string is found in all outputs 81 bool foundInCode(const char *stringToFind) const; 82 bool foundInCodeRegex(const std::regex ®exToFind, std::smatch *match = nullptr) const; 83 84 // Test that the string occurs for exactly expectedOccurrences times in all outputs 85 bool foundInCode(const char *stringToFind, const int expectedOccurrences) const; 86 87 // Test that the strings are found in all outputs in the specified order. 88 bool foundInCodeInOrder(std::vector<const char *> stringsToFind); 89 90 // Test that the string is found in none of the outputs 91 bool notFoundInCode(const char *stringToFind) const; 92 93 private: 94 bool compileWithSettings(ShShaderOutput output, 95 const std::string &shaderString, 96 const ShCompileOptions &compileOptions, 97 std::string *translatedCode, 98 std::string *infoLog); 99 100 GLenum mShaderType; 101 ShCompileOptions mDefaultCompileOptions; 102 ShBuiltInResources mResources; 103 104 std::map<ShShaderOutput, std::string> mOutputCode; 105 }; 106 107 // Returns a pointer to a function call node with a mangled name functionName. 108 const TIntermAggregate *FindFunctionCallNode(TIntermNode *root, const TString &functionName); 109 } // namespace sh 110 111 #endif // TESTS_TEST_UTILS_COMPILER_TEST_H_ 112