diff options
| author | Julian Hurst <julian.hurst@digdash.com> | 2024-12-03 18:47:51 +0100 |
|---|---|---|
| committer | Julian Hurst <julian.hurst@digdash.com> | 2024-12-03 18:47:57 +0100 |
| commit | 190f522242b2e91eb3213e828a26886aa7847f3a (patch) | |
| tree | 04122a22aa1e92d6f3efe21436d2e75650c1c4c1 | |
| parent | 92524aa912db99becc1b3446d5b68b9cc089c721 (diff) | |
| download | aoc24-190f522242b2e91eb3213e828a26886aa7847f3a.tar.gz | |
Refactor and add tests
Folders need to have 'sensible' names for hare test to detect them.
| -rw-r--r-- | Makefile | 27 | ||||
| -rw-r--r-- | README | 1 | ||||
| -rw-r--r-- | one/+test.ha | 8 | ||||
| -rw-r--r-- | one/1.ha (renamed from 1/1.ha) | 8 | ||||
| -rw-r--r-- | one/in (renamed from 1/in) | 0 | ||||
| -rw-r--r-- | three/+test.ha | 8 | ||||
| -rw-r--r-- | three/3.ha (renamed from 3/3.ha) | 7 | ||||
| -rw-r--r-- | three/in (renamed from 3/in) | 0 | ||||
| -rw-r--r-- | two/+test.ha | 8 | ||||
| -rw-r--r-- | two/2.ha (renamed from 2/2.ha) | 8 | ||||
| -rw-r--r-- | two/in (renamed from 2/in) | 0 |
11 files changed, 72 insertions, 3 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..539e5f0 --- /dev/null +++ b/Makefile @@ -0,0 +1,27 @@ +.POSIX: +.SUFFIXES: +HARE=hare +HAREFLAGS= + +DESTDIR= +PREFIX=/usr/local +BINDIR=$(PREFIX)/bin + +all: example-cmd + +example-cmd: + $(HARE) build $(HAREFLAGS) -o $@ cmd/$@/ + +check: + $(HARE) test $(HAREFLAGS) + +clean: + rm -f example-cmd + +install: + install -Dm755 example-cmd $(DESTDIR)$(BINDIR)/example-cmd + +uninstall: + rm -f $(DESTDIR)$(BINDIR)/example-cmd + +.PHONY: all check clean install uninstall @@ -0,0 +1 @@ +Advent of Code 2024 in Hare diff --git a/one/+test.ha b/one/+test.ha new file mode 100644 index 0000000..7041261 --- /dev/null +++ b/one/+test.ha @@ -0,0 +1,8 @@ +use os; +use io; + +@test fn testone() void = { + const f = os::open("one/in")!; + defer io::close(f)!; + assert(do(f) == 11); +}; @@ -6,9 +6,14 @@ use strings; use strconv; use sort; use sort::cmp; +use io; export fn main() void = { - const sc = bufio::newscanner(os::stdin); + do(os::stdin); +}; + +fn do(h: io::handle) int = { + const sc = bufio::newscanner(h); defer bufio::finish(&sc); let l1: []int = []; @@ -46,4 +51,5 @@ export fn main() void = { sum += diff.2; }; fmt::println(sum)!; + return sum; }; diff --git a/three/+test.ha b/three/+test.ha new file mode 100644 index 0000000..5ec9745 --- /dev/null +++ b/three/+test.ha @@ -0,0 +1,8 @@ +use os; +use io; + +@test fn testthree() void = { + const f = os::open("three/in")!; + defer io::close(f)!; + assert(do(f) == 161); +}; @@ -6,7 +6,11 @@ use strings; use strconv; export fn main() void = { - const in = io::drain(os::stdin)!; + do(os::stdin); +}; + +fn do(h: io::handle) int = { + const in = io::drain(h)!; const sin = strings::trim(strings::fromutf8_unsafe(in)); const re = regex::compile(`mul\([0-9]+,[0-9]+\)`)!; defer regex::finish(&re); @@ -28,4 +32,5 @@ export fn main() void = { }; }; fmt::printfln("{}", sum)!; + return sum; }; diff --git a/two/+test.ha b/two/+test.ha new file mode 100644 index 0000000..8f61645 --- /dev/null +++ b/two/+test.ha @@ -0,0 +1,8 @@ +use os; +use io; + +@test fn testtwo() void = { + const f = os::open("two/in")!; + defer io::close(f)!; + assert(do(f) == 2); +}; @@ -3,9 +3,14 @@ use bufio; use os; use strings; use strconv; +use io; export fn main() void = { - const sc = bufio::newscanner(os::stdin); + do(os::stdin); +}; + +fn do(h: io::handle) int = { + const sc = bufio::newscanner(h); defer bufio::finish(&sc); let safereports = 0; @@ -53,4 +58,5 @@ export fn main() void = { }; }; fmt::printfln("{} reports are safe", safereports)!; + return safereports; }; |
