Skip to content

Commit

Permalink
LibGLSL: Add tests for GLSL parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Poseydon42 committed Aug 3, 2023
1 parent b1407ca commit 7ba9478
Show file tree
Hide file tree
Showing 25 changed files with 425 additions and 0 deletions.
1 change: 1 addition & 0 deletions Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions Tests/LibGLSL/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set(TEST_SOURCES
test-parser.cpp
)

foreach(source IN LISTS TEST_SOURCES)
serenity_test("${source}" LibGLSL LIBS LibGLSL)
endforeach()
61 changes: 61 additions & 0 deletions Tests/LibGLSL/test-parser.cpp
Original file line number Diff line number Diff line change
@@ -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;
}));
}
2 changes: 2 additions & 0 deletions Userland/Libraries/LibGLSL/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
11 changes: 11 additions & 0 deletions Userland/Libraries/LibGLSL/Tests/parser/discard.ast
Original file line number Diff line number Diff line change
@@ -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]
}
4 changes: 4 additions & 0 deletions Userland/Libraries/LibGLSL/Tests/parser/discard.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
void foo()
{
discard;
}
63 changes: 63 additions & 0 deletions Userland/Libraries/LibGLSL/Tests/parser/expression.ast
Original file line number Diff line number Diff line change
@@ -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
]
}
63 changes: 63 additions & 0 deletions Userland/Libraries/LibGLSL/Tests/parser/expression.ast.1
Original file line number Diff line number Diff line change
@@ -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
]
}
4 changes: 4 additions & 0 deletions Userland/Libraries/LibGLSL/Tests/parser/expression.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
void foo()
{
int a = 1 + 2 * 3 + (4 - 2) / 2 + max(7, 8) - bar.xy + abc[13];
}
37 changes: 37 additions & 0 deletions Userland/Libraries/LibGLSL/Tests/parser/for-statement.ast
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions Userland/Libraries/LibGLSL/Tests/parser/for-statement.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
void main()
{
int b = 0;
for (int a = 0; true; a++)
b++;
}

15 changes: 15 additions & 0 deletions Userland/Libraries/LibGLSL/Tests/parser/function-declaration.ast
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
int main(int a, float b);

13 changes: 13 additions & 0 deletions Userland/Libraries/LibGLSL/Tests/parser/function-definition.ast
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
int main()
{
return 1;
}

26 changes: 26 additions & 0 deletions Userland/Libraries/LibGLSL/Tests/parser/if-else.ast
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions Userland/Libraries/LibGLSL/Tests/parser/if-else.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
bool foo()
{
if (1 == 2)
return true;
else
return false;
}
23 changes: 23 additions & 0 deletions Userland/Libraries/LibGLSL/Tests/parser/return.ast
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 9 additions & 0 deletions Userland/Libraries/LibGLSL/Tests/parser/return.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
void foo()
{
return;
}

int bar()
{
return 3;
}
7 changes: 7 additions & 0 deletions Userland/Libraries/LibGLSL/Tests/parser/struct.ast
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 7ba9478

Please sign in to comment.