Skip to content

Commit

Permalink
Test all IP directions
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrk24 committed Sep 10, 2023
1 parent 6f8565c commit 2e306de
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ColumnLimit: 120
Cpp11BracedListStyle: false
EmptyLineBeforeAccessModifier: Never
ForEachMacros: # It doesn't particularly matter if these are ForEachMacros or IfMacros
# No idea why test_iter doesn't need to be here but the other two do.
- testcase
- testgroup
IndentWidth: 4
Expand All @@ -22,5 +23,7 @@ MaxEmptyLinesToKeep: 2
SpaceAfterTemplateKeyword: false
WhitespaceSensitiveMacros:
- STRING_NAME
- test_iter
- testcase
- testgroup
- TEST_ITEM
43 changes: 27 additions & 16 deletions tests/unit/ip_movement.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,36 @@
#include <program_walker.hh>
#include "test-framework/test_framework.hh"

testgroup (ip_advance) {
testcase (sample_program) {
/*
* From the README:
* 0
* 1 4
* 2 5 7
* 3 6 8 9
*/
program p("0142573689");
program_walker::instruction_pointer ip{ { SIZE_C(0), SIZE_C(0) }, direction::southwest };
namespace {
// Thank you clang-format very cool
#define TEST_ITEM(dir, prog) \
{ \
#dir, { direction::dir, program(prog) } \
}

// Each test program consists of every digit 0-9 exactly once, in the order they are hit if the IP is traveling in the
// given direction from the top corner.
std::initializer_list<std::pair<const char*, std::pair<direction, program>>> test_programs = {
TEST_ITEM(southwest, "0142573689"),
TEST_ITEM(west, "0215439876"),
TEST_ITEM(northwest, "0395286417"),
TEST_ITEM(northeast, "0968537421"),
TEST_ITEM(east, "0895671234"),
TEST_ITEM(southeast, "0715824693"),
};

#undef TEST_ITEM
} // namespace

for (int24_t i = '0'; i <= '9'; ++i) {
test_assert(p.at(ip.coords.first, ip.coords.second) == i);
program_walker::advance(ip, p.side_length());
}
test_iter(ip_advance, test_programs, input) {
program& p = input.second;
program_walker::instruction_pointer ip{ { SIZE_C(0), SIZE_C(0) }, input.first };

test_assert(ip.coords.first == 0 && ip.coords.second == 0);
for (int24_t i = '0'; i <= '9'; ++i) {
test_assert(p.at(ip.coords.first, ip.coords.second) == i);
program_walker::advance(ip, p.side_length());
}
test_assert(ip.coords.first == 0 && ip.coords.second == 0);
};

// TODO: Add tests for branch instructions
2 changes: 1 addition & 1 deletion tests/unit/test-framework

0 comments on commit 2e306de

Please sign in to comment.