1 #include <msp/core/algorithm.h>
2 #include <msp/fs/dir.h>
3 #include <msp/fs/utils.h>
4 #include <msp/gl/glsl/compiler.h>
5 #include <msp/gl/glsl/glsl_error.h>
6 #include <msp/gl/glsl/tokenizer.h>
7 #include <msp/strings/utils.h>
8 #include <msp/test/test.h>
10 class GlslCompilerHelper
17 Msp::GL::SL::Compiler::Mode compile_mode;
18 std::map<std::string, int> spec_values;
19 std::map<Msp::GL::SL::Stage::Type, std::string> expected_output;
20 std::string expected_error;
23 std::list<TestCase> test_cases;
25 void load_all_test_cases(const Msp::FS::Path &);
26 const TestCase &load_test_case(const std::string &);
28 void verify_output(const std::string &, const std::string &);
29 void verify_error(const std::string &, const std::string &);
30 std::string extract_line(const std::string &, const std::string::const_iterator &);
31 virtual void fail(const std::string &) = 0;
34 class GlslCompilerTest: public Msp::Test::RegisteredTest<GlslCompilerTest>, private GlslCompilerHelper
39 static const char *get_name() { return "GLSL compiler"; }
42 void run_test_case(const TestCase *);
43 virtual void fail(const std::string &m) { Test::fail(m); }
49 void GlslCompilerHelper::load_all_test_cases(const FS::Path &tests_dir)
51 list<string> test_files = FS::list_filtered(tests_dir, "\\.glsl$");
53 for(const auto &fn: test_files)
54 load_test_case((tests_dir/fn).str());
57 const GlslCompilerHelper::TestCase &GlslCompilerHelper::load_test_case(const string &fn)
59 IO::BufferedFile file(fn);
61 test_case.name = FS::basename(fn);
62 test_case.compile_mode = GL::SL::Compiler::PROGRAM;
63 string *target = &test_case.source;
67 if(!file.getline(line))
73 string::size_type pos = line.find("Expected output:");
76 string stage = strip(line.substr(pos+16));
78 target = &test_case.expected_output[GL::SL::Stage::VERTEX];
79 else if(stage=="geometry")
80 target = &test_case.expected_output[GL::SL::Stage::GEOMETRY];
81 else if(stage=="fragment")
82 target = &test_case.expected_output[GL::SL::Stage::FRAGMENT];
84 throw runtime_error("Unknown stage "+stage);
88 pos = line.find("Expected error:");
91 target = &test_case.expected_error;
95 pos = line.find("Compile mode:");
98 string mode = strip(line.substr(pos+13));
100 test_case.compile_mode = GL::SL::Compiler::MODULE;
101 else if(mode=="program")
102 test_case.compile_mode = GL::SL::Compiler::PROGRAM;
104 throw runtime_error("Unknown compile mode "+mode);
108 pos = line.find("Specialize:");
109 if(pos!=string::npos)
111 vector<string> parts = split(line.substr(pos+11));
115 else if(parts[1]=="false")
118 value = lexical_cast<int>(parts[1]);
119 test_case.spec_values[parts[0]] = value;
126 test_cases.push_back(test_case);
128 return test_cases.back();
131 void GlslCompilerHelper::verify_output(const string &output, const string &expected)
133 GL::SL::Tokenizer tokenizer;
134 tokenizer.begin(output, "<output>");
136 GL::SL::Tokenizer expected_tkn;
137 expected_tkn.begin(expected, "<expected>");
141 string token = expected_tkn.parse_token();
145 tokenizer.expect(token);
147 catch(const GL::SL::invalid_shader_source &exc)
157 void GlslCompilerHelper::verify_error(const string &output, const string &expected)
159 auto i = output.begin();
160 auto j = expected.begin();
162 while(i!=output.end() && j!=expected.end())
169 else if(isspace(*i) && isspace(*j))
175 else if(space && isspace(*i))
177 else if(space && isspace(*j))
181 string out_line = extract_line(output, i);
182 string expect_line = extract_line(expected, j);
183 fail(format("Incorrect error line:\n%s\nExpected:\n%s", out_line, expect_line));
187 while(i!=output.end() && isspace(*i))
189 while(j!=expected.end() && isspace(*j))
193 fail(format("Extra error line: %s", extract_line(output, i)));
194 if(j!=expected.end())
195 fail(format("Missing error line: %s", extract_line(expected, j)));
198 string GlslCompilerHelper::extract_line(const string &text, const string::const_iterator &iter)
200 string::const_iterator begin = iter;
201 for(; (begin!=text.begin() && *begin!='\n'); --begin) ;
204 string::const_iterator end = iter;
205 for(; (end!=text.end() && *end!='\n'); ++end) ;
206 return string(begin, end);
210 GlslCompilerTest::GlslCompilerTest()
212 load_all_test_cases("glsl");
213 for(const auto &tc: test_cases)
214 add(&GlslCompilerTest::run_test_case, &tc, tc.name);
217 void GlslCompilerTest::run_test_case(const TestCase *test_case)
219 GL::SL::Compiler compiler(GL::SL::Features::all());
222 compiler.set_source(test_case->source, "<test>");
223 if(test_case->compile_mode==GL::SL::Compiler::PROGRAM)
224 compiler.specialize(test_case->spec_values);
225 compiler.compile(test_case->compile_mode);
227 catch(const GL::SL::invalid_shader_source &exc)
229 if(!test_case->expected_error.empty())
231 debug("Errors from compile:");
233 verify_error(exc.what(), test_case->expected_error);
239 if(!test_case->expected_error.empty())
240 fail("Error expected but none thrown");
242 auto stages = compiler.get_stages();
245 auto i = test_case->expected_output.find(s);
246 if(i==test_case->expected_output.end())
247 fail(format("Compiler produced extra stage %s", GL::SL::Stage::get_stage_name(s)));
249 string output = compiler.get_stage_glsl(s);
250 debug(format("Output for stage %s:", GL::SL::Stage::get_stage_name(s)));
251 auto lines = split_fields(output, '\n');
252 for(unsigned j=0; j<lines.size(); ++j)
253 debug(format("%3d: %s", j+1, lines[j]));
255 verify_output(output, i->second);
258 for(const auto &s: test_case->expected_output)
259 if(find(stages, s.first)==stages.end())
260 fail(format("Compiler didn't produce stage %s", GL::SL::Stage::get_stage_name(s.first)));