From e563148ab9b441c0fcd83af28d19aca4021f17db Mon Sep 17 00:00:00 2001
From: Akuli <akuviljanen17@gmail.com>
Date: Sun, 3 Dec 2023 12:17:56 +0200
Subject: [PATCH] hack the test script

---
 examples/aoc2023/day01/part1.jou |  2 +-
 examples/aoc2023/day01/part2.jou |  3 +--
 examples/aoc2023/day02/part1.jou |  2 +-
 examples/aoc2023/day02/part2.jou |  2 +-
 examples/aoc2023/day03/part1.jou |  2 +-
 examples/aoc2023/day03/part2.jou |  3 +--
 runtests.sh                      | 30 +++++++++++++++++++++++-------
 7 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/examples/aoc2023/day01/part1.jou b/examples/aoc2023/day01/part1.jou
index 9181f6d4..47b234b5 100644
--- a/examples/aoc2023/day01/part1.jou
+++ b/examples/aoc2023/day01/part1.jou
@@ -21,5 +21,5 @@ def main() -> int:
         result += 10*(*first - '0')
         result += *last - '0'
 
-    printf("%d\n", result)
+    printf("%d\n", result)  # Output: 54630
     return 0
diff --git a/examples/aoc2023/day01/part2.jou b/examples/aoc2023/day01/part2.jou
index ea9fe8e2..050f62f4 100644
--- a/examples/aoc2023/day01/part2.jou
+++ b/examples/aoc2023/day01/part2.jou
@@ -33,6 +33,5 @@ def main() -> int:
         result += 10*parse_prefix_digit(first)
         result += parse_prefix_digit(last)
 
-    printf("%d\n", result)
+    printf("%d\n", result)  # Output: 54770
     return 0
-
diff --git a/examples/aoc2023/day02/part1.jou b/examples/aoc2023/day02/part1.jou
index dfdf3ce9..9bb5c0cf 100644
--- a/examples/aoc2023/day02/part1.jou
+++ b/examples/aoc2023/day02/part1.jou
@@ -47,5 +47,5 @@ def main() -> int:
         if game_is_possible(&id_end[1]):
             result += game_id
 
-    printf("%d\n", result)
+    printf("%d\n", result)  # Output: 1853
     return 0
diff --git a/examples/aoc2023/day02/part2.jou b/examples/aoc2023/day02/part2.jou
index 58b6f1c6..6e537672 100644
--- a/examples/aoc2023/day02/part2.jou
+++ b/examples/aoc2023/day02/part2.jou
@@ -56,5 +56,5 @@ def main() -> int:
         game = parse_game(&id_end[1])
         result += game.get_power()
 
-    printf("%d\n", result)
+    printf("%d\n", result)  # Output: 72706
     return 0
diff --git a/examples/aoc2023/day03/part1.jou b/examples/aoc2023/day03/part1.jou
index 78b8c60a..66833f3a 100644
--- a/examples/aoc2023/day03/part1.jou
+++ b/examples/aoc2023/day03/part1.jou
@@ -65,5 +65,5 @@ def main() -> int:
             start = end  # skip rest of number
 
     free(input)
-    printf("%d\n", sum)
+    printf("%d\n", sum)  # Output: 556057
     return 0
diff --git a/examples/aoc2023/day03/part2.jou b/examples/aoc2023/day03/part2.jou
index 780b0b1c..4aa14df7 100644
--- a/examples/aoc2023/day03/part2.jou
+++ b/examples/aoc2023/day03/part2.jou
@@ -75,11 +75,10 @@ def main() -> int:
             continue
 
         adjacent = find_adjacent_numbers(input, p)
-        printf("adjacent = [%d,%d,%d,%d,...]\n", adjacent[0], adjacent[1], adjacent[2], adjacent[3])
         if adjacent[0] != -1 and adjacent[1] != -1 and adjacent[2] == -1:
             # it is a gear
             sum += adjacent[0] * adjacent[1]
 
     free(input)
-    printf("%d\n", sum)
+    printf("%d\n", sum)  # Output: 82824352
     return 0
diff --git a/runtests.sh b/runtests.sh
index 13f7c202..ca804f3f 100755
--- a/runtests.sh
+++ b/runtests.sh
@@ -13,7 +13,8 @@ set -e -o pipefail
 
 function usage() {
     echo "Usage: $0 [--valgrind] [--verbose] [--dont-run-make] [TEMPLATE]" >&2
-    echo "TEMPLATE can be e.g. './jou %s', where %s will be replaced by a jou file." >&2
+    echo "TEMPLATE can be e.g. 'jou %s', where %s will be replaced by a jou file." >&2
+    echo "When the command runs, 'jou' points at the executable in repository root."
     exit 2
 }
 
@@ -33,9 +34,9 @@ done
 if [ $# == 0 ]; then
     # No arguments --> run tests in the basic/simple way
     if [[ "$OS" =~ Windows ]]; then
-        command_template='./jou.exe %s'
+        command_template='jou.exe %s'
     else
-        command_template='./jou %s'
+        command_template='jou %s'
     fi
 elif [ $# == 1 ]; then
     command_template="$1"
@@ -139,11 +140,21 @@ function run_test()
     local correct_exit_code="$2"
     local counter="$3"
 
+    local dir=.
+    if [[ "$joufile" =~ ^examples/aoc ]]; then
+        dir=$(dirname $joufile)
+        joufile=$(basename $joufile)
+    fi
+
     local command diffpath
     command="$(printf "$command_template" $joufile)"
     diffpath=tmp/tests/diff$(printf "%04d" $counter).txt  # consistent alphabetical order
 
-    printf "\n\n\x1b[33m*** Command: %s ***\x1b[0m\n\n" "$command" > $diffpath
+    local command_msg="Command: $command"
+    if [ $dir != . ]; then
+        command_msg="$command_msg [in $dir]"
+    fi
+    printf "\n\n\x1b[33m*** %s ***\x1b[0m\n\n" "$command_msg" > $diffpath
 
     # Skip tests when:
     #   * the test is supposed to crash, but optimizations are enabled (unpredictable by design)
@@ -160,8 +171,13 @@ function run_test()
 
     show_run $joufile
     if diff --text -u --color=always \
-        <(generate_expected_output $joufile $correct_exit_code | tr -d '\r') \
-        <(ulimit -v 500000 2>/dev/null; bash -c "$command; echo Exit code: \$?" 2>&1 | post_process_output $joufile | tr -d '\r') \
+        <(cd $dir; generate_expected_output $joufile $correct_exit_code | tr -d '\r') \
+        <(
+            export PATH="$PWD:$PATH"
+            cd $dir
+            ulimit -v 500000 2>/dev/null
+            bash -c "$command; echo Exit code: \$?" 2>&1 | post_process_output $joufile | tr -d '\r'
+        ) \
         &>> $diffpath
     then
         show_ok $joufile
@@ -173,7 +189,7 @@ function run_test()
 }
 
 counter=0
-for joufile in examples/*.jou tests/*/*.jou; do
+for joufile in examples/*.jou examples/aoc2023/day*/*.jou tests/*/*.jou; do
     case $joufile in
         examples/* | tests/should_succeed/*) correct_exit_code=0; ;;
         *) correct_exit_code=1; ;;  # compiler or runtime error