]> git.tdb.fi Git - libs/gl.git/blob - tests/glsl/glslcompiler.cpp
Check for extra error lines in compiler output
[libs/gl.git] / tests / glsl / glslcompiler.cpp
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>
9
10 #include <msp/io/print.h>
11
12 class GlslCompilerTest: public Msp::Test::RegisteredTest<GlslCompilerTest>
13 {
14 private:
15         struct TestCase
16         {
17                 std::string name;
18                 std::string source;
19                 std::map<Msp::GL::SL::Stage::Type, std::string> expected_output;
20                 std::string expected_error;
21         };
22
23         std::list<TestCase> test_cases;
24
25 public:
26         GlslCompilerTest();
27
28         static const char *get_name() { return "GLSL compiler"; }
29
30 private:
31         const TestCase &load_test_case(const std::string &);
32
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 &);
37 };
38
39 using namespace std;
40 using namespace Msp;
41
42 GlslCompilerTest::GlslCompilerTest()
43 {
44         FS::Path tests_dir = "glsl";
45         list<string> test_files = FS::list_filtered(tests_dir, "\\.glsl$");
46         test_files.sort();
47         for(const auto &fn: test_files)
48                 load_test_case((tests_dir/fn).str());
49
50         for(const auto &tc: test_cases)
51                 add(&GlslCompilerTest::run_test_case, &tc, tc.name);
52 }
53
54 const GlslCompilerTest::TestCase &GlslCompilerTest::load_test_case(const string &fn)
55 {
56         IO::BufferedFile file(fn);
57         TestCase test_case;
58         test_case.name = FS::basename(fn);
59         string *target = &test_case.source;
60         while(!file.eof())
61         {
62                 string line;
63                 if(!file.getline(line))
64                         break;
65
66                 if(line=="*/")
67                         continue;
68
69                 string::size_type expected = line.find("Expected output:");
70                 if(expected!=string::npos)
71                 {
72                         string stage = strip(line.substr(expected+16));
73                         if(stage=="vertex")
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];
79                         else
80                                 throw runtime_error("Unknown stage "+stage);
81                         continue;
82                 }
83
84                 expected = line.find("Expected error:");
85                 if(expected!=string::npos)
86                 {
87                         target = &test_case.expected_error;
88                         continue;
89                 }
90
91                 *target += line;
92                 *target += '\n';
93         }
94         test_cases.push_back(test_case);
95
96         return test_cases.back();
97 }
98
99 void GlslCompilerTest::run_test_case(const TestCase *test_case)
100 {
101         GL::SL::Compiler compiler(GL::SL::Features::all());
102         try
103         {
104                 compiler.set_source(test_case->source, "<test>");
105                 compiler.compile(GL::SL::Compiler::PROGRAM);
106         }
107         catch(const GL::SL::invalid_shader_source &exc)
108         {
109                 if(!test_case->expected_error.empty())
110                 {
111                         debug("Errors from compile:");
112                         debug(exc.what());
113                         verify_error(exc.what(), test_case->expected_error);
114                         return;
115                 }
116                 throw;
117         }
118
119         if(!test_case->expected_error.empty())
120                 fail("Error expected but none thrown");
121
122         auto stages = compiler.get_stages();
123         for(auto s: stages)
124         {
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)));
128
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]));
134
135                 verify_output(output, i->second);
136         }
137
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)));
141 }
142
143 void GlslCompilerTest::verify_output(const string &output, const string &expected)
144 {
145         GL::SL::Tokenizer tokenizer;
146         tokenizer.begin(output, "<output>");
147
148         GL::SL::Tokenizer expected_tkn;
149         expected_tkn.begin(expected, "<expected>");
150
151         while(1)
152         {
153                 string token = expected_tkn.parse_token();
154
155                 try
156                 {
157                         tokenizer.expect(token);
158                 }
159                 catch(const GL::SL::invalid_shader_source &exc)
160                 {
161                         fail(exc.what());
162                 }
163
164                 if(token.empty())
165                         break;
166         }
167 }
168
169 void GlslCompilerTest::verify_error(const string &output, const string &expected)
170 {
171         auto i = output.begin();
172         auto j = expected.begin();
173         bool space = true;
174         while(i!=output.end() && j!=expected.end())
175         {
176                 if(*i==*j)
177                 {
178                         ++i;
179                         ++j;
180                 }
181                 else if(isspace(*i) && isspace(*j))
182                 {
183                         ++i;
184                         ++j;
185                         space = true;
186                 }
187                 else if(space && isspace(*i))
188                         ++i;
189                 else if(space && isspace(*j))
190                         ++j;
191                 else
192                 {
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));
196                 }
197         }
198
199         while(i!=output.end() && isspace(*i))
200                 ++i;
201         while(j!=expected.end() && isspace(*j))
202                 ++j;
203
204         if(i!=output.end())
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)));
208 }
209
210 string GlslCompilerTest::extract_line(const string &text, const string::const_iterator &iter)
211 {
212         string::const_iterator begin = iter;
213         for(; (begin!=text.begin() && *begin!='\n'); --begin) ;
214         if(*begin=='\n')
215                 ++begin;
216         string::const_iterator end = iter;
217         for(; (end!=text.end() && *end!='\n'); ++end) ;
218         return string(begin, end);
219 }