Merge pull request #190 from antiagainst/gtest
[platform/upstream/glslang.git] / gtests / TestFixture.cpp
1 //
2 // Copyright (C) 2016 Google, Inc.
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions
8 // are met:
9 //
10 //    Redistributions of source code must retain the above copyright
11 //    notice, this list of conditions and the following disclaimer.
12 //
13 //    Redistributions in binary form must reproduce the above
14 //    copyright notice, this list of conditions and the following
15 //    disclaimer in the documentation and/or other materials provided
16 //    with the distribution.
17 //
18 //    Neither the name of Google Inc. nor the names of its
19 //    contributors may be used to endorse or promote products derived
20 //    from this software without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 // POSSIBILITY OF SUCH DAMAGE.
34
35 #include "TestFixture.h"
36
37 namespace glslangtest {
38
39 std::string FileNameAsCustomTestName(
40     const ::testing::TestParamInfo<std::string>& info)
41 {
42     std::string name = info.param;
43     // A valid test case suffix cannot have '.' and '-' inside.
44     std::replace(name.begin(), name.end(), '.', '_');
45     std::replace(name.begin(), name.end(), '-', '_');
46     return name;
47 }
48
49 EShLanguage GetGlslLanguageForStage(const std::string& stage)
50 {
51     if (stage == "vert") {
52         return EShLangVertex;
53     } else if (stage == "tesc") {
54         return EShLangTessControl;
55     } else if (stage == "tese") {
56         return EShLangTessEvaluation;
57     } else if (stage == "geom") {
58         return EShLangGeometry;
59     } else if (stage == "frag") {
60         return EShLangFragment;
61     } else if (stage == "comp") {
62         return EShLangCompute;
63     } else {
64         assert(0 && "Unknown shader stage");
65         return EShLangCount;
66     }
67 }
68
69 EShMessages GetSpirvMessageOptionsForSemanticsAndTarget(Semantics semantics,
70                                                         Target target)
71 {
72     EShMessages result = EShMsgDefault;
73
74     switch (target) {
75         case Target::AST:
76             result = EShMsgAST;
77             break;
78         case Target::Spirv:
79             result = EShMsgSpvRules;
80             break;
81     };
82
83     switch (semantics) {
84         case Semantics::OpenGL:
85             break;
86         case Semantics::Vulkan:
87             result = static_cast<EShMessages>(result | EShMsgVulkanRules);
88             break;
89     }
90
91     return result;
92 }
93
94 std::pair<bool, std::string> ReadFile(const std::string& path)
95 {
96     std::ifstream fstream(path, std::ios::in);
97     if (fstream) {
98         std::string contents;
99         fstream.seekg(0, std::ios::end);
100         contents.reserve(fstream.tellg());
101         fstream.seekg(0, std::ios::beg);
102         contents.assign((std::istreambuf_iterator<char>(fstream)),
103                         std::istreambuf_iterator<char>());
104         return std::make_pair(true, contents);
105     }
106     return std::make_pair(false, "");
107 }
108
109 bool WriteFile(const std::string& path, const std::string& contents)
110 {
111     std::ofstream fstream(path, std::ios::out);
112     if (!fstream) return false;
113     fstream << contents;
114     fstream.flush();
115     return true;
116 }
117
118 std::string GetSuffix(const std::string& name)
119 {
120     const size_t pos = name.rfind('.');
121     return (pos == std::string::npos) ? "" : name.substr(name.rfind('.') + 1);
122 }
123
124 }  // namespace glslangtest