added tests for ast

This commit is contained in:
Sven Vogel 2024-05-07 15:22:52 +02:00
parent 86c74f66c2
commit f32a16ea8e
6 changed files with 174 additions and 1 deletions

View File

@ -8,3 +8,4 @@ set(CTEST_BINARY_DIRECTORY ${PROJECT_BINARY_DIR}/tests)
add_subdirectory(logging) add_subdirectory(logging)
add_subdirectory(input_file) add_subdirectory(input_file)
add_subdirectory(ast)

35
tests/ast/CMakeLists.txt Normal file
View File

@ -0,0 +1,35 @@
include(CTest)
include_directories(${PROJECT_SOURCE_DIR}/src)
# ------------------------------------------------------- #
# CTEST 1
# test building the syntax tree
add_executable(ast_build_tree
${PROJECT_SOURCE_DIR}/src/ast/ast.c
${PROJECT_SOURCE_DIR}/src/sys/log.c
build_tree.c)
set_target_properties(ast_build_tree
PROPERTIES
OUTPUT_NAME "build_tree"
RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/ast)
add_test(NAME ast_build_tree
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMAND python ${GEMSTONE_TEST_DIR}/ast/test_ast.py check_build_tree)
# ------------------------------------------------------- #
# CTEST 2
# test node to string output
add_executable(ast_print_node
${PROJECT_SOURCE_DIR}/src/ast/ast.c
print_node.c)
set_target_properties(ast_build_tree
PROPERTIES
OUTPUT_NAME "build_tree"
RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/ast)
add_test(NAME ast_print_node
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMAND python ${GEMSTONE_TEST_DIR}/ast/test_ast.py check_print_node)

40
tests/ast/build_tree.c Normal file
View File

@ -0,0 +1,40 @@
//
// Created by servostar on 5/7/24.
//
#include <ast/ast.h>
#include <sys/log.h>
void generate_statement(const AST_NODE_PTR stmt) {
const AST_NODE_PTR add = AST_new_node(AST_Add, NULL);
AST_push_node(add, AST_new_node(AST_Int, "3"));
AST_push_node(add, AST_new_node(AST_Int, "6"));
AST_push_node(stmt, add);
}
void generate_branch(const AST_NODE_PTR stmt) {
const AST_NODE_PTR branch = AST_new_node(AST_If, NULL);
const AST_NODE_PTR gt = AST_new_node(AST_Greater, NULL);
AST_push_node(branch, gt);
AST_push_node(gt, AST_new_node(AST_Float, "2.3"));
AST_push_node(gt, AST_new_node(AST_Float, "0.79"));
AST_push_node(stmt, branch);
generate_statement(branch);
}
int main(void) {
const AST_NODE_PTR root = AST_new_node(AST_Stmt, NULL);
generate_branch(root);
AST_delete_node(root);
return 0;
}

32
tests/ast/gen_graph.c Normal file
View File

@ -0,0 +1,32 @@
//
// Created by servostar on 5/7/24.
//
#include <ast/ast.h>
#include <sys/log.h>
int main(void) {
struct AST_Node_t* node = AST_new_node(AST_If, NULL);
struct AST_Node_t* child = AST_new_node(AST_Add, NULL);
AST_push_node(child, AST_new_node(AST_Int, "43"));
AST_push_node(child, AST_new_node(AST_Int, "9"));
AST_push_node(node, child);
AST_push_node(node, AST_new_node(AST_Expr, NULL));
AST_push_node(node, AST_new_node(AST_Expr, NULL));
FILE* out = fopen("ast.gv", "w+");
// convert this file ^^^^^^
// to an svg with: `dot -Tsvg ast.gv > graph.svg`
AST_fprint_graphviz(out, node);
AST_delete_node(node);
fflush(out);
fclose(out);
return 0;
}

19
tests/ast/print_node.c Normal file
View File

@ -0,0 +1,19 @@
//
// Created by servostar on 5/7/24.
//
#include <ast/ast.h>
int main(void) {
const AST_NODE_PTR node = AST_new_node(0, "value");
for (size_t i = 0; i < AST_ELEMENT_COUNT; i++) {
// set kind
node->kind = i;
// print symbol
printf("%ld %s\n", i, AST_node_to_string(node));
}
AST_delete_node(node);
}

46
tests/ast/test_ast.py Normal file
View File

@ -0,0 +1,46 @@
import subprocess
import sys
import logging
from logging import info, error
import os
BIN_DIR = "bin/tests/ast/"
def run_check_build_tree():
info("started check tree build...")
p = subprocess.run(BIN_DIR + "build_tree", capture_output=True, text=True)
info("checking exit code...")
# check exit code
assert p.returncode == 0
def run_check_print_node():
info("started check node print...")
p = subprocess.run(BIN_DIR + "print_node", capture_output=True, text=True)
info("checking exit code...")
# check exit code
assert p.returncode == 0
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
target = sys.argv[1]
info(f"starting ast test suite with target: {target}")
match target:
case "check_build_tree":
run_check_build_tree()
case "check_print_node":
run_check_print_node()
case _:
error(f"unknown target: {target}")
exit(1)