LibGLSL: Add tests for GLSL parser

This commit is contained in:
Poseydon42 2023-07-23 23:10:25 +01:00 committed by Jelle Raaijmakers
parent 29972876e4
commit d160ff2f8d
24 changed files with 368 additions and 0 deletions

View file

@ -9,6 +9,7 @@ add_subdirectory(LibEDID)
add_subdirectory(LibELF)
add_subdirectory(LibGfx)
add_subdirectory(LibGL)
add_subdirectory(LibGLSL)
add_subdirectory(LibIMAP)
add_subdirectory(LibJS)
add_subdirectory(LibLocale)

View file

@ -0,0 +1,7 @@
set(TEST_SOURCES
test-parser.cpp
)
foreach(source IN LISTS TEST_SOURCES)
serenity_test("${source}" LibGLSL LIBS LibGLSL)
endforeach()

View file

@ -0,0 +1,61 @@
/*
* Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/LexicalPath.h>
#include <AK/MemoryStream.h>
#include <LibCore/Directory.h>
#include <LibCore/File.h>
#include <LibGLSL/Parser.h>
#include <LibTest/TestCase.h>
constexpr StringView TESTS_ROOT_DIR = "/home/anon/Tests/glsl-tests/parser"sv;
static String read_all(String const& path)
{
auto file = MUST(Core::File::open(path, Core::File::OpenMode::Read));
auto file_size = MUST(file->size());
return MUST(String::from_stream(*file, file_size));
}
TEST_CASE(test_regression)
{
MUST(Core::Directory::for_each_entry(TESTS_ROOT_DIR, Core::DirIterator::Flags::SkipDots, [](auto const& entry, auto const& directory) -> ErrorOr<IterationDecision> {
auto path = LexicalPath::join(directory.path().string(), entry.name);
if (!path.has_extension(".glsl"sv))
return IterationDecision::Continue;
outln("Checking {}...", path.basename());
String file_path = MUST(String::from_deprecated_string(path.string()));
auto ast_file_path = MUST(String::formatted("{}.ast", MUST(file_path.substring_from_byte_offset(0, file_path.bytes_as_string_view().length() - sizeof(".glsl") + 1))));
auto source = read_all(file_path);
auto target_ast = read_all(ast_file_path);
GLSL::Preprocessor preprocessor(file_path, source);
GLSL::Parser parser(MUST(preprocessor.process_and_lex()), file_path);
auto root = MUST(parser.parse());
EXPECT(parser.errors().is_empty());
Vector<uint8_t> memory;
memory.resize(8 * 1024 * 1024);
AK::FixedMemoryStream output_stream(memory.span());
MUST(root->dump(output_stream));
auto written_bytes = MUST(output_stream.tell());
MUST(output_stream.seek(0));
String content = MUST(String::from_stream(output_stream, written_bytes));
auto equal = content == target_ast;
EXPECT(equal);
if (!equal)
outln("Failed on {}", path.basename());
return IterationDecision::Continue;
}));
}

View file

@ -10,3 +10,5 @@ set(SOURCES
serenity_lib(LibGLSL glsl)
target_link_libraries(LibGLSL PRIVATE LibGPU)
install(DIRECTORY Tests/ DESTINATION home/anon/Tests/glsl-tests)

View file

@ -0,0 +1,11 @@
TranslationUnit[0:0->3:0]
FunctionDeclaration[0:0->3:0]
Type[0:0->0:3]
void
foo
(
)
FunctionDefinition[1:0->3:0]
{
DiscardStatement[2:4->2:11]
}

View file

@ -0,0 +1,5 @@
void foo()
{
discard;
}

View file

@ -0,0 +1,63 @@
TranslationUnit[0:0->3:0]
FunctionDeclaration[0:0->3:0]
Type[0:0->0:3]
void
foo
(
)
FunctionDefinition[1:0->3:0]
{
VariableDeclaration[2:4->2:66]
Type[2:4->2:6]
int
a
BinaryExpression[2:12->2:66]
BinaryExpression[2:12->2:57]
BinaryExpression[2:12->2:48]
BinaryExpression[2:12->2:36]
BinaryExpression[2:12->2:22]
NumericLiteral[2:12->2:12]
1
+
BinaryExpression[2:16->2:22]
NumericLiteral[2:16->2:16]
2
*
NumericLiteral[2:20->2:20]
3
+
BinaryExpression[2:24->2:36]
BinaryExpression[2:25->2:30]
NumericLiteral[2:25->2:25]
4
-
NumericLiteral[2:29->2:29]
2
/
NumericLiteral[2:34->2:34]
2
+
FunctionCall[2:38->2:48]
Name[2:38->2:40]
max
(
NumericLiteral[2:42->2:42]
7
NumericLiteral[2:45->2:45]
8
)
-
MemberExpression[2:50->2:57]
Name[2:50->2:52]
bar
Name[2:54->2:55]
xy
+
ArrayElementExpression[2:59->2:66]
Name[2:59->2:61]
abc
[
NumericLiteral[2:63->2:64]
13
]
}

View file

@ -0,0 +1,5 @@
void foo()
{
int a = 1 + 2 * 3 + (4 - 2) / 2 + max(7, 8) - bar.xy + abc[13];
}

View file

@ -0,0 +1,37 @@
TranslationUnit[0:0->5:0]
FunctionDeclaration[0:0->5:0]
Type[0:0->0:3]
void
main
(
)
FunctionDefinition[1:0->5:0]
{
VariableDeclaration[2:4->2:13]
Type[2:4->2:6]
int
b
NumericLiteral[2:12->2:12]
0
ForStatement[3:4->4:11]
Initializer:
VariableDeclaration[3:9->3:18]
Type[3:9->3:11]
int
a
NumericLiteral[3:17->3:17]
0
Test expression:
BooleanLiteral[3:20->3:23]
true
Update expression:
UnaryExpression[3:26->3:29]
postfix ++
Name[3:26->3:26]
a
Body:
UnaryExpression[4:8->4:11]
postfix ++
Name[4:8->4:8]
b
}

View file

@ -0,0 +1,7 @@
void main()
{
int b = 0;
for (int a = 0; true; a++)
b++;
}

View file

@ -0,0 +1,15 @@
TranslationUnit[0:0->0:24]
FunctionDeclaration[0:0->0:24]
Type[0:0->0:2]
int
main
(
Parameter[0:9->0:13]
a
Type[0:9->0:11]
int
Parameter[0:16->0:22]
b
Type[0:16->0:20]
float
)

View file

@ -0,0 +1,2 @@
int main(int a, float b);

View file

@ -0,0 +1,13 @@
TranslationUnit[0:0->3:0]
FunctionDeclaration[0:0->3:0]
Type[0:0->0:2]
int
main
(
)
FunctionDefinition[1:0->3:0]
{
ReturnStatement[2:1->2:9]
NumericLiteral[2:8->2:8]
1
}

View file

@ -0,0 +1,5 @@
int main()
{
return 1;
}

View file

@ -0,0 +1,26 @@
TranslationUnit[0:0->6:0]
FunctionDeclaration[0:0->6:0]
Type[0:0->0:3]
bool
foo
(
)
FunctionDefinition[1:0->6:0]
{
IfStatement[2:4->5:20]
Predicate:
BinaryExpression[2:8->2:14]
NumericLiteral[2:8->2:8]
1
==
NumericLiteral[2:13->2:13]
2
Then:
ReturnStatement[3:8->3:19]
BooleanLiteral[3:15->3:18]
true
Else:
ReturnStatement[5:8->5:20]
BooleanLiteral[5:15->5:19]
false
}

View file

@ -0,0 +1,8 @@
bool foo()
{
if (1 == 2)
return true;
else
return false;
}

View file

@ -0,0 +1,23 @@
TranslationUnit[0:0->8:0]
FunctionDeclaration[0:0->3:0]
Type[0:0->0:3]
void
foo
(
)
FunctionDefinition[1:0->3:0]
{
ReturnStatement[2:4->2:10]
}
FunctionDeclaration[5:0->8:0]
Type[5:0->5:2]
int
bar
(
)
FunctionDefinition[6:0->8:0]
{
ReturnStatement[7:4->7:12]
NumericLiteral[7:11->7:11]
3
}

View file

@ -0,0 +1,10 @@
void foo()
{
return;
}
int bar()
{
return 3;
}

View file

@ -0,0 +1,7 @@
TranslationUnit[0:0->3:1]
StructDeclaration[0:7->3:1]
foo
VariableDeclaration[2:4->3:0]
Type[2:4->2:6]
int
bar

View file

@ -0,0 +1,5 @@
struct foo
{
int bar;
};

View file

@ -0,0 +1,32 @@
TranslationUnit[0:0->4:0]
FunctionDeclaration[0:0->4:0]
Type[0:0->0:3]
void
foo
(
)
FunctionDefinition[1:0->4:0]
{
VariableDeclaration[2:4->2:13]
Type[2:4->2:6]
int
a
NumericLiteral[2:12->2:12]
7
VariableDeclaration[3:4->3:22]
Type[3:4->3:6]
int
b
UnaryExpression[3:12->3:22]
prefix ~
UnaryExpression[3:14->3:21]
prefix !
UnaryExpression[3:15->3:21]
prefix ~
UnaryExpression[3:16->3:21]
prefix ++
UnaryExpression[3:18->3:21]
postfix ++
Name[3:18->3:18]
a
}

View file

@ -0,0 +1,6 @@
void foo()
{
int a = 7;
int b = ~(!~++a++);
}

View file

@ -0,0 +1,13 @@
TranslationUnit[0:0->2:16]
VariableDeclaration[0:0->1:0]
Type[0:0->0:11]
uniform vec3
u_Color
VariableDeclaration[1:0->2:0]
Type[1:0->1:6]
in vec3
i_VertexColor
VariableDeclaration[2:0->2:16]
Type[2:0->2:7]
out vec4
o_Color

View file

@ -0,0 +1,4 @@
uniform vec3 u_Color;
in vec3 i_VertexColor;
out vec4 o_Color;