Browse Source

First unit test added

master
Ludovic 'Archivist' Lagouardette 1 year ago
parent
commit
1c58c9f44b
5 changed files with 85 additions and 15 deletions
  1. +8
    -0
      CMakeLists.txt
  2. +1
    -0
      prototypes/molasses/basic_file.mol
  3. +26
    -15
      src/molasses/generator_primitives_x86_64_linux.cpp
  4. +45
    -0
      tests/001.exp
  5. +5
    -0
      tests/001/exit-with-1.mol

+ 8
- 0
CMakeLists.txt View File

@ -4,6 +4,7 @@ project(sugar)
# find_package(cppfront REQUIRED)
set(CMAKE_CXX_STANDARD 20)
enable_testing()
include_directories(include)
@ -15,3 +16,10 @@ add_executable(sugar
src/molasses/parser_primitives.cpp
include/molasses/parser_primitives.h
include/molasses/generator_primitives.h)
add_test(
NAME id001
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
COMMAND expect ./tests/001.exp
)
set_property(TEST id001 PROPERTY ENVIRONMENT "SUGAR_EXECUTABLE=$<TARGET_FILE:sugar>")

+ 1
- 0
prototypes/molasses/basic_file.mol View File

@ -1,4 +1,5 @@
__PROC__ main
__--__
__DO__
1 i32-to-i64 30 30 + i32-to-i64 syscall1 drop_i64
__END__

+ 26
- 15
src/molasses/generator_primitives_x86_64_linux.cpp View File

@ -40,7 +40,7 @@ namespace molasses {
std::vector<std::string>({
" popq %rax\n",
" popq %rbx\n",
" multl %ebx, %eax\n",
" imull %ebx, %eax\n",
" andl $0xFFFFFFFF, %eax\n",
" pushq %rax\n"
})
@ -67,25 +67,36 @@ namespace molasses {
std::vector<std::string>({"i64"}),
std::vector<std::string>({
" popq %rax\n",
" movslq %rax\n",
" movslq %eax, %rax\n",
" pushq %rax\n"
})
)
);
ctx.operations.emplace_back(
std::make_shared<molasses::primitive_operation>(
std::string{"syscall1"},
std::vector<std::string>({"i64", "i64"}),
std::vector<std::string>({"i64"}),
std::vector<std::string>({
" popq %rax\n",
" popq %rdi\n",
" syscall\n",
" pushq %rax\n"
})
)
);
ctx.operations.emplace_back(
std::make_shared<molasses::primitive_operation>(
std::string{"drop_i64"},
std::vector<std::string>({"i64"}),
std::vector<std::string>({}),
std::vector<std::string>({
" popq %rax\n"
})
)
);
ctx.operations.emplace_back(
std::make_shared<molasses::primitive_operation>(
std::string{"syscall1"},
std::vector<std::string>({"i64", "i64"}),
std::vector<std::string>({"i64"}),
std::vector<std::string>({
" popq %rax\n",
" popq %rdi\n",
" syscall\n",
" pushq %rax\n"
})
)
);
ctx.operations.emplace_back(
std::make_shared<molasses::primitive_operation>(
std::string{"syscall2"},

+ 45
- 0
tests/001.exp View File

@ -0,0 +1,45 @@
#!/usr/bin/expect
set SUGAR_EXECUTABLE $::env(SUGAR_EXECUTABLE)
proc abort {reason} {
puts "test failed $reason"
exit 1
}
spawn -noecho $SUGAR_EXECUTABLE tests/001/exit-with-1.mol lex parse
expect {
error { abort "failed to parse" }
eof { abort "cannot find the main" }
main
}
expect eof
lassign [wait] pid spawnid os_error_flag value
if {$value != 0} {
abort "compiler crashed"
}
spawn -noecho $SUGAR_EXECUTABLE tests/001/exit-with-1.mol lex parse /tmp/sugar.generated generate assemble
expect {
error { abort "failed to compile" }
eof { abort "didn't run clang" }
clang
}
expect {
error { abort "failed to link" }
eof { abort "didn't run ld" }
ld
}
expect eof
lassign [wait] pid spawnid os_error_flag value
if {$value != 0} {
abort "compiler crashed"
}
spawn -noecho /tmp/sugar.generated
expect eof
lassign [wait] pid spawnid os_error_flag value
if {$value != 1} {
abort "executable didn't return exit code 1 but $value instead"
}

+ 5
- 0
tests/001/exit-with-1.mol View File

@ -0,0 +1,5 @@
__PROC__ main
__--__
__DO__
1 i32-to-i64 30 30 + i32-to-i64 syscall1 drop_i64
__END__

Loading…
Cancel
Save