Skip to content

Commit

Permalink
assembler: Add automated tests
Browse files Browse the repository at this point in the history
- Include expected output files generated from textbook's assembler
- Fix path issue that caused output files to always appear under
  working directory
- Log source and output paths
  • Loading branch information
lucas89901 committed Dec 31, 2023
1 parent 5a84bc5 commit 7902cd9
Show file tree
Hide file tree
Showing 9 changed files with 55,109 additions and 6 deletions.
47 changes: 47 additions & 0 deletions projects/06/assembler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,50 @@ install(
TARGETS assembler
DESTINATION ${CMAKE_SOURCE_DIR}/projects/bin
)

set(
test_programs
../add/Add.asm
../max/Max.asm
../max/MaxL.asm
../pong/Pong.asm
../pong/PongL.asm
../rect/Rect.asm
../rect/RectL.asm
)

if(WIN32)
set(TextComparer "${CMAKE_SOURCE_DIR}/tools/TextComparer.bat")
else()
set(TextComparer "${CMAKE_SOURCE_DIR}/tools/TextComparer.sh")
endif()

foreach(program ${test_programs})
message(STATUS "Adding test assembly program: " ${program})
file(
COPY ${program}
DESTINATION test_programs/
)

cmake_path(GET program STEM program_stem)
add_test(
NAME "Assemble: ${program}"
COMMAND assembler "test_programs/${program_stem}.asm"
)
add_test(
NAME "Comparison: ${program}"
COMMAND "${TextComparer}" "test_programs/${program_stem}.hack" "${CMAKE_CURRENT_SOURCE_DIR}/../expected/${program_stem}.hack"
)

set_tests_properties(
"Assemble: ${program}"
PROPERTIES
FIXTURES_SETUP "${program}"
)
set_tests_properties(
"Comparison: ${program}"
PROPERTIES
FIXTURES_REQUIRED "${program}"
PASS_REGULAR_EXPRESSION "Comparison ended successfully"
)
endforeach()
14 changes: 8 additions & 6 deletions projects/06/assembler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@ bool IsNumber(std::string_view str) {
}

int main(int argc, char *argv[]) {
CHECK_GE(argc, 2) << "No input file";
QCHECK_GE(argc, 2) << "Usage: " << argv[0] << " SOURCE";
std::ifstream asm_file(argv[1]);
CHECK(asm_file.is_open()) << "Failed to open input file '" << argv[1] << "'";
QCHECK(asm_file.is_open()) << "Failed to open input file '" << argv[1] << "'";
LOG(INFO) << "Source: " << argv[1];

std::string buffer;
std::vector<std::string> trimmed_lines;
Expand Down Expand Up @@ -182,11 +183,12 @@ int main(int argc, char *argv[]) {
}
}

std::string hack_filename = std::filesystem::path(argv[1]).stem().string();
absl::StrAppend(&hack_filename, ".hack");
std::ofstream hack_file(hack_filename);
std::filesystem::path hack_path(argv[1]);
hack_path.replace_extension(".hack");
std::ofstream hack_file(hack_path);
CHECK(hack_file.is_open())
<< "Failed to open output file '" << hack_filename << "'";
<< "Failed to open output file " << hack_path.string();
LOG(INFO) << "Output: " << hack_path.string();

uint16_t variable_address = 16;
for (const std::string &line : trimmed_lines) {
Expand Down
6 changes: 6 additions & 0 deletions projects/06/expected/Add.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
0000000000000010
1110110000010000
0000000000000011
1110000010010000
0000000000000000
1110001100001000
16 changes: 16 additions & 0 deletions projects/06/expected/Max.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
0000000000000000
1111110000010000
0000000000000001
1111010011010000
0000000000001010
1110001100000001
0000000000000001
1111110000010000
0000000000001100
1110101010000111
0000000000000000
1111110000010000
0000000000000010
1110001100001000
0000000000001110
1110101010000111
16 changes: 16 additions & 0 deletions projects/06/expected/MaxL.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
0000000000000000
1111110000010000
0000000000000001
1111010011010000
0000000000001010
1110001100000001
0000000000000001
1111110000010000
0000000000001100
1110101010000111
0000000000000000
1111110000010000
0000000000000010
1110001100001000
0000000000001110
1110101010000111
Loading

0 comments on commit 7902cd9

Please sign in to comment.