X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=tests%2Fglsl%2Fglslcompiler.cpp;h=acca61d36a217e7bec5dcc572576209a5bfb3244;hb=1cdd7023ea6fa85bcd28176c504d0feb98b61cae;hp=f38db45caa6c5638e06044a624055ef2f0e63ed4;hpb=e195322eb1251f2eb0d6dfb8c2dc0fff02e5fcfc;p=libs%2Fgl.git diff --git a/tests/glsl/glslcompiler.cpp b/tests/glsl/glslcompiler.cpp index f38db45c..acca61d3 100644 --- a/tests/glsl/glslcompiler.cpp +++ b/tests/glsl/glslcompiler.cpp @@ -17,6 +17,7 @@ private: std::string name; std::string source; std::map expected_output; + std::string expected_error; }; std::list test_cases; @@ -30,6 +31,9 @@ private: const TestCase &load_test_case(const std::string &); void run_test_case(const TestCase *); + void verify_output(const std::string &, const std::string &); + void verify_error(const std::string &, const std::string &); + std::string extract_line(const std::string &, const std::string::const_iterator &); }; using namespace std; @@ -77,6 +81,13 @@ const GlslCompilerTest::TestCase &GlslCompilerTest::load_test_case(const string continue; } + expected = line.find("Expected error:"); + if(expected!=string::npos) + { + target = &test_case.expected_error; + continue; + } + *target += line; *target += '\n'; } @@ -88,8 +99,25 @@ const GlslCompilerTest::TestCase &GlslCompilerTest::load_test_case(const string void GlslCompilerTest::run_test_case(const TestCase *test_case) { GL::SL::Compiler compiler(GL::SL::Features::all()); - compiler.set_source(test_case->source, ""); - compiler.compile(GL::SL::Compiler::PROGRAM); + try + { + compiler.set_source(test_case->source, ""); + compiler.compile(GL::SL::Compiler::PROGRAM); + } + catch(const GL::SL::invalid_shader_source &exc) + { + if(!test_case->expected_error.empty()) + { + debug("Errors from compile:"); + debug(exc.what()); + verify_error(exc.what(), test_case->expected_error); + return; + } + throw; + } + + if(!test_case->expected_error.empty()) + fail("Error expected but none thrown"); auto stages = compiler.get_stages(); for(auto s: stages) @@ -104,31 +132,88 @@ void GlslCompilerTest::run_test_case(const TestCase *test_case) for(unsigned j=0; j"); + verify_output(output, i->second); + } + + for(const auto &s: test_case->expected_output) + if(find(stages, s.first)==stages.end()) + fail(format("Compiler didn't produce stage %s", GL::SL::Stage::get_stage_name(s.first))); +} + +void GlslCompilerTest::verify_output(const string &output, const string &expected) +{ + GL::SL::Tokenizer tokenizer; + tokenizer.begin(output, ""); + + GL::SL::Tokenizer expected_tkn; + expected_tkn.begin(expected, ""); + + while(1) + { + string token = expected_tkn.parse_token(); - GL::SL::Tokenizer expected_tkn; - expected_tkn.begin(i->second, ""); + try + { + tokenizer.expect(token); + } + catch(const GL::SL::invalid_shader_source &exc) + { + fail(exc.what()); + } - while(1) + if(token.empty()) + break; + } +} + +void GlslCompilerTest::verify_error(const string &output, const string &expected) +{ + auto i = output.begin(); + auto j = expected.begin(); + bool space = true; + while(i!=output.end() && j!=expected.end()) + { + if(*i==*j) { - string token = expected_tkn.parse_token(); - - try - { - tokenizer.expect(token); - } - catch(const GL::SL::invalid_shader_source &exc) - { - fail(exc.what()); - } - - if(token.empty()) - break; + ++i; + ++j; + } + else if(isspace(*i) && isspace(*j)) + { + ++i; + ++j; + space = true; + } + else if(space && isspace(*i)) + ++i; + else if(space && isspace(*j)) + ++j; + else + { + string out_line = extract_line(output, i); + string expect_line = extract_line(expected, j); + fail(format("Incorrect error line:\n%s\nExpected:\n%s", out_line, expect_line)); } } - for(const auto &s: test_case->expected_output) - if(find(stages, s.first)==stages.end()) - fail(format("Compiler didn't produce stage %s", GL::SL::Stage::get_stage_name(s.first))); + while(i!=output.end() && isspace(*i)) + ++i; + while(j!=expected.end() && isspace(*j)) + ++j; + + if(i!=output.end()) + fail(format("Extra error line: %s", extract_line(output, i))); + if(j!=expected.end()) + fail(format("Missing error line: %s", extract_line(expected, j))); +} + +string GlslCompilerTest::extract_line(const string &text, const string::const_iterator &iter) +{ + string::const_iterator begin = iter; + for(; (begin!=text.begin() && *begin!='\n'); --begin) ; + if(*begin=='\n') + ++begin; + string::const_iterator end = iter; + for(; (end!=text.end() && *end!='\n'); ++end) ; + return string(begin, end); }