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); }
46 class GlslCompilerIdempotence: public Msp::Test::RegisteredTest<GlslCompilerIdempotence>, private GlslCompilerHelper
49 GlslCompilerIdempotence();
51 static const char *get_name() { return "GLSL compiler idempotence"; }
54 void run_test_case(const TestCase *);
55 virtual void fail(const std::string &m) { Test::fail(m); }
61 void GlslCompilerHelper::load_all_test_cases(const FS::Path &tests_dir)
63 list<string> test_files = FS::list_filtered(tests_dir, "\\.glsl$");
65 for(const auto &fn: test_files)
66 load_test_case((tests_dir/fn).str());
69 const GlslCompilerHelper::TestCase &GlslCompilerHelper::load_test_case(const string &fn)
71 IO::BufferedFile file(fn);
73 test_case.name = FS::basename(fn);
74 test_case.compile_mode = GL::SL::Compiler::PROGRAM;
75 string *target = &test_case.source;
79 if(!file.getline(line))
85 string::size_type pos = line.find("Expected output:");
88 string stage = strip(line.substr(pos+16));
90 target = &test_case.expected_output[GL::SL::Stage::VERTEX];
91 else if(stage=="geometry")
92 target = &test_case.expected_output[GL::SL::Stage::GEOMETRY];
93 else if(stage=="fragment")
94 target = &test_case.expected_output[GL::SL::Stage::FRAGMENT];
96 throw runtime_error("Unknown stage "+stage);
100 pos = line.find("Expected error:");
101 if(pos!=string::npos)
103 target = &test_case.expected_error;
107 pos = line.find("Compile mode:");
108 if(pos!=string::npos)
110 string mode = strip(line.substr(pos+13));
112 test_case.compile_mode = GL::SL::Compiler::MODULE;
113 else if(mode=="program")
114 test_case.compile_mode = GL::SL::Compiler::PROGRAM;
116 throw runtime_error("Unknown compile mode "+mode);
120 pos = line.find("Specialize:");
121 if(pos!=string::npos)
123 vector<string> parts = split(line.substr(pos+11));
127 else if(parts[1]=="false")
130 value = lexical_cast<int>(parts[1]);
131 test_case.spec_values[parts[0]] = value;
138 test_cases.push_back(test_case);
140 return test_cases.back();
143 void GlslCompilerHelper::verify_output(const string &output, const string &expected)
145 GL::SL::Tokenizer tokenizer;
146 tokenizer.begin(output, "<output>");
148 GL::SL::Tokenizer expected_tkn;
149 expected_tkn.begin(expected, "<expected>");
153 string token = expected_tkn.parse_token();
157 tokenizer.expect(token);
159 catch(const GL::SL::invalid_shader_source &exc)
169 void GlslCompilerHelper::verify_error(const string &output, const string &expected)
171 auto i = output.begin();
172 auto j = expected.begin();
174 while(i!=output.end() && j!=expected.end())
181 else if(isspace(*i) && isspace(*j))
187 else if(space && isspace(*i))
189 else if(space && isspace(*j))
193 string out_line = extract_line(output, i);
194 string expect_line = extract_line(expected, j);
195 fail(format("Incorrect error line:\n%s\nExpected:\n%s", out_line, expect_line));
199 while(i!=output.end() && isspace(*i))
201 while(j!=expected.end() && isspace(*j))
205 fail(format("Extra error line: %s", extract_line(output, i)));
206 if(j!=expected.end())
207 fail(format("Missing error line: %s", extract_line(expected, j)));
210 string GlslCompilerHelper::extract_line(const string &text, const string::const_iterator &iter)
212 string::const_iterator begin = iter;
213 for(; (begin!=text.begin() && *begin!='\n'); --begin) ;
216 string::const_iterator end = iter;
217 for(; (end!=text.end() && *end!='\n'); ++end) ;
218 return string(begin, end);
222 GlslCompilerTest::GlslCompilerTest()
224 load_all_test_cases("glsl");
225 for(const auto &tc: test_cases)
226 add(&GlslCompilerTest::run_test_case, &tc, tc.name);
229 void GlslCompilerTest::run_test_case(const TestCase *test_case)
231 GL::SL::Compiler compiler(GL::SL::Features::all());
234 compiler.set_source(test_case->source, "<test>");
235 if(test_case->compile_mode==GL::SL::Compiler::PROGRAM)
236 compiler.specialize(test_case->spec_values);
237 compiler.compile(test_case->compile_mode);
239 catch(const GL::SL::invalid_shader_source &exc)
241 if(!test_case->expected_error.empty())
243 debug("Errors from compile:");
245 verify_error(exc.what(), test_case->expected_error);
251 if(!test_case->expected_error.empty())
252 fail("Error expected but none thrown");
254 auto stages = compiler.get_stages();
257 auto i = test_case->expected_output.find(s);
258 if(i==test_case->expected_output.end())
259 fail(format("Compiler produced extra stage %s", GL::SL::Stage::get_stage_name(s)));
261 string output = compiler.get_stage_glsl(s);
262 debug(format("Output for stage %s:", GL::SL::Stage::get_stage_name(s)));
263 auto lines = split_fields(output, '\n');
264 for(unsigned j=0; j<lines.size(); ++j)
265 debug(format("%3d: %s", j+1, lines[j]));
267 verify_output(output, i->second);
270 for(const auto &s: test_case->expected_output)
271 if(find(stages, s.first)==stages.end())
272 fail(format("Compiler didn't produce stage %s", GL::SL::Stage::get_stage_name(s.first)));
276 GlslCompilerIdempotence::GlslCompilerIdempotence()
278 load_all_test_cases("glsl");
279 for(const auto &tc: test_cases)
280 if(tc.expected_error.empty())
281 add(&GlslCompilerIdempotence::run_test_case, &tc, tc.name);
284 void GlslCompilerIdempotence::run_test_case(const TestCase *test_case)
286 GL::SL::Compiler compiler(GL::SL::Features::all());
287 compiler.set_source(test_case->source, "<test>");
288 if(test_case->compile_mode==GL::SL::Compiler::PROGRAM)
289 compiler.specialize(test_case->spec_values);
290 compiler.compile(test_case->compile_mode);
292 GL::SL::Compiler compiler2(GL::SL::Features::all());
293 compiler2.set_source(compiler.get_combined_glsl(), "<loopback>");
294 compiler2.compile(test_case->compile_mode);
296 auto stages = compiler.get_stages();
297 auto stages2 = compiler2.get_stages();
298 auto i = stages.begin();
299 auto j = stages2.begin();
300 for(; (i!=stages.end() && j!=stages2.end() && *i==*j); ++i, ++j)
302 string output = compiler.get_stage_glsl(*i);
303 string output2 = compiler2.get_stage_glsl(*j);
305 verify_output(output2, output);
309 fail(format("Second pass didn't produce stage %s", GL::SL::Stage::get_stage_name(*i)));
311 fail(format("Second pass produced extra stage %s", GL::SL::Stage::get_stage_name(*j)));