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 #include <msp/io/print.h>
12 class GlslCompilerTest: public Msp::Test::RegisteredTest<GlslCompilerTest>
19 std::map<Msp::GL::SL::Stage::Type, std::string> expected_output;
20 std::string expected_error;
23 std::list<TestCase> test_cases;
28 static const char *get_name() { return "GLSL compiler"; }
31 const TestCase &load_test_case(const std::string &);
33 void run_test_case(const TestCase *);
34 void verify_output(const std::string &, const std::string &);
35 void verify_error(const std::string &, const std::string &);
36 std::string extract_line(const std::string &, const std::string::const_iterator &);
42 GlslCompilerTest::GlslCompilerTest()
44 FS::Path tests_dir = "glsl";
45 list<string> test_files = FS::list_filtered(tests_dir, "\\.glsl$");
47 for(const auto &fn: test_files)
48 load_test_case((tests_dir/fn).str());
50 for(const auto &tc: test_cases)
51 add(&GlslCompilerTest::run_test_case, &tc, tc.name);
54 const GlslCompilerTest::TestCase &GlslCompilerTest::load_test_case(const string &fn)
56 IO::BufferedFile file(fn);
58 test_case.name = FS::basename(fn);
59 string *target = &test_case.source;
63 if(!file.getline(line))
69 string::size_type expected = line.find("Expected output:");
70 if(expected!=string::npos)
72 string stage = strip(line.substr(expected+16));
74 target = &test_case.expected_output[GL::SL::Stage::VERTEX];
75 else if(stage=="geometry")
76 target = &test_case.expected_output[GL::SL::Stage::GEOMETRY];
77 else if(stage=="fragment")
78 target = &test_case.expected_output[GL::SL::Stage::FRAGMENT];
80 throw runtime_error("Unknown stage "+stage);
84 expected = line.find("Expected error:");
85 if(expected!=string::npos)
87 target = &test_case.expected_error;
94 test_cases.push_back(test_case);
96 return test_cases.back();
99 void GlslCompilerTest::run_test_case(const TestCase *test_case)
101 GL::SL::Compiler compiler(GL::SL::Features::all());
104 compiler.set_source(test_case->source, "<test>");
105 compiler.compile(GL::SL::Compiler::PROGRAM);
107 catch(const GL::SL::invalid_shader_source &exc)
109 if(!test_case->expected_error.empty())
111 debug("Errors from compile:");
113 verify_error(exc.what(), test_case->expected_error);
119 if(!test_case->expected_error.empty())
120 fail("Error expected but none thrown");
122 auto stages = compiler.get_stages();
125 auto i = test_case->expected_output.find(s);
126 if(i==test_case->expected_output.end())
127 fail(format("Compiler produced extra stage %s", GL::SL::Stage::get_stage_name(s)));
129 string output = compiler.get_stage_glsl(s);
130 debug(format("Output for stage %s:", GL::SL::Stage::get_stage_name(s)));
131 auto lines = split_fields(output, '\n');
132 for(unsigned j=0; j<lines.size(); ++j)
133 debug(format("%3d: %s", j+1, lines[j]));
135 verify_output(output, i->second);
138 for(const auto &s: test_case->expected_output)
139 if(find(stages, s.first)==stages.end())
140 fail(format("Compiler didn't produce stage %s", GL::SL::Stage::get_stage_name(s.first)));
143 void GlslCompilerTest::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 GlslCompilerTest::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))
204 if(j!=expected.end())
205 fail(format("Missing error line: %s", extract_line(expected, j)));
208 string GlslCompilerTest::extract_line(const string &text, const string::const_iterator &iter)
210 string::const_iterator begin = iter;
211 for(; (begin!=text.begin() && *begin!='\n'); --begin) ;
214 string::const_iterator end = iter;
215 for(; (end!=text.end() && *end!='\n'); ++end) ;
216 return string(begin, end);