Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
demiazz committed Nov 21, 2024
1 parent e8a08f3 commit 20e6145
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 9 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,17 @@ jobs:
shell: bash
run: make init-ci

- name: Build docs
- name: Run tests
run: make test

- name: Build
run: make build

- name: Upload docs artifact
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: dist

- name: Deploy docs to Github Pages
- name: Deploy to Github Pages
id: deployment
uses: actions/deploy-pages@v4
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ init:

init-ci:
pnpm install
opam install -y . --deps-only
opam install -y . --deps-only --with-tests
opam exec opam-check-npm-deps

build-native:
Expand All @@ -30,4 +30,7 @@ dev-native:
opam exec -- dune build --watch

run: build-native
_build/default/native/main.exe -y $(YEAR) -d $(DAY) -p $(PART) -i input.txt
_build/default/bin/main.exe -y $(YEAR) -d $(DAY) -p $(PART) -i input.txt

test:
opam exec -- dune test
1 change: 1 addition & 0 deletions advent-of-ocaml.opam
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ depends: [
"ocaml-lsp-server" {with-dev-setup}
"ocamlformat" {with-dev-dev-setup}
"dot-merlin-reader" {with-dev-setup}
"alcotest" {with-tests}
"utop" {with-dev-setup}
"odoc" {with-doc}
]
Expand Down
4 changes: 4 additions & 0 deletions bin/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(executable
(public_name advent-of-ocaml)
(name main)
(libraries native))
2 changes: 1 addition & 1 deletion native/main.ml → bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ let () =
let day = day.contents in
let part = part.contents in
let start = Sys.time () in
let answer = Solutions.solve ~year ~day ~part input in
let answer = Native.Solutions.solve ~year ~day ~part input in
let finish = Sys.time () in
match answer with
| Some answer -> (
Expand Down
6 changes: 3 additions & 3 deletions native/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(executable
(public_name advent-of-ocaml)
(name main)
(library
(name native)
(modules reader solutions)
(libraries advent_of_code))
1 change: 1 addition & 0 deletions tests/data/2015_01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(((())))()((((((((())()(()))(()((((()(()(((()((()((()(()()()()()))(((()(()((((((((((())(()()((())()(((())))()(()(()((()(()))(()()()()((()((()(((()()(((((((()()())()((((()()(((((()(())()(())((())()()))()(((((((())(()())(()(((())(()))((())))(()((()())))()())((((())))(()(((((()(())(((()()((()((()((((((((((())(()())))))()))())()()((((()()()()()()((((((())())(((()())()((()()(((()()()))(((((()))(((()(()()()(()(()(((())()))(()(((()((())()(()())())))((()()()(()()(((()))(((()((((()(((((()()(()())((()())())(()((((((()(()()))((((()))))())((())()()((()(()))))((((((((()))(()()(((())())(())()((()()()()((()((()((()()(((())))(()((())()((((((((()((()(()()(((())())())))(())())))()((((()))))))())))()()))()())((()())()((()()()))(()()(((()(())((((())())((((((((()()()()())))()()()((((()()))))))()((((()(((()))(()()())))((()()(((()))()()())())(((())((()()(())()()()(((())))))()())((()))()))((())()()())()())()()(()))())))())()))(())((()(())))(()(())(()))))(()(())())(()(())(()(()))))((()())()))()((((()()))))())))()()())((())()((()()()))()(((()(()))))(())()()))(((()())))))))))(((())))()))())()))))()()(((())))))))()(()()(()))((()))))((())))((()((())))())))()()(()))())()(()((()())(()(()()())())(()()))()))))(()())()()))()()()()))(()(()(()))))))()(()))()))()()(()((())(()(())))()(((())(())())))))()(()(()))))()))(()()()(())()(()(())))()))))()()(((((())))))())()())())())()())()))))()))))))))())()()()()()()())))()))((())()))())))()((())()))))()))())))))))())()()()))()()(()((((()(((((((()(())((()())((()()))()))))(())))()()()(())((())()())))(())))(())))(((()()))()(())(((()(()))((())))())()))((((()))())()))))))))()(())())))(()))()(()()))())()()(())())))())()()(()())))()((()())(()(())(())))))))))))))(()))))()))))))()()())(()(((((()(()())))())()))(()))()))(()()))()())(()))())()(())((()()))))))())))())()(((())))(()(()))()()))()(()))))))((()())(()))))))()())))()()))))))))((((((((()()()(()))))))()())))())))()()((())()))((())(())))())())))()()()((()((()(())))())()(())))))))))()())))()()()()()()))()))((())())(()(()))))))(()()))()))(())))()))))))))))))(()))))))))()))))()))()())()))()()))))))()))))((()))))(()))())()(())))(()())((((()())))()))))(()))()(()()(())))))())))))()))))))())))())))))())))())())))())(()))))(())()(())))())()))((()()))))))())))((())))))))())))(())))))()()())))))())))))()))))))()))()()()(()(((()())())())(()))())))))((()(())(()))))))))(())))()()()())())(()))))()()()))()))())())())()(())))()(((()((((())))))))()))))))))))))))))))))((())()())(()))))()()))))))(()()(())())))())))((())))((())))))))))))))()))))()(()))))))())))))()))(()()())(()())))))))))()))))))(())))))()()))()())(((())))()))(()))))))))(())())))())))())())())()()))((())()(())()())()))()())(())(()))))()())))(()(((()))))))()(()())()()()))()))))))))()()()(())()())()(((((()))()())())(()))))()()()(())))())))()((()())))(()))())()(()())())(()))()()))((()()))((()()()()())))(())()))(()(())))((()()))))))))())))))))())()()))))))))))))))))(())()(())(())()())())()))()(()))))())())))))()())()(()))()()(())))(())())))))(()))))))))))))))())())(())(())))(((()))()))))())((())(()))())))))))())))))())))()))()))))))))))))())()))))()))))((()))(())))()(())))(())()))()))())))())))))))()(()())())))()()())))(())))))(()))))))))))))(()))()))()))())))(((()()()(())((()())))()())(((()))(())()))((()()()())))())(())(()))))()(((((())))(()))())())))))))((((()()()))())())()(()(()())))))))))()())())))(())))()())(((()(())())()()))())())))))))((()())((()()(()))(()(())))()))()))(()))(()))()()(()(((())((((()))()(()))((())()(()(()())()(()))()())))))(()))()))())()())))())))(())))((())(()())))))()))(())(()))()())()(()()((()(()))))))()(())(()())(())()))(((())()))(()()(()()()))))(()(())))()))))())))))())(()()()()()()(((())))(()()))()((())(((((()()())))(()))(()))()()))(((())())()(((()()()()))))(()))(())())))()())(()()())())))))))()))))((())))()())(()))(()(()))())))))())(())))))()()())())()))()()(())))(()))(())((((((())(()))(()))())()))(()()(())))()))(()()))()))()(())))(())))((()(()))(())()()())())))(((()()())(())()))))))()(((()(((((()()(((())(())))())()((()))))((()())()(())(((())))(((()((()(()(()))(()()))())(()))(())(())))()))))))((((()))()((((()(()))()))()()))))()(()(()))()(()((()(((()(()()(((()))))()(((()(()(()(((()(()())())()()(()(()())())(()((((())(()))()))(((((()()())(())()((()()())))()()(((()()))()((((((((()(())))())((()))))(())))(()))))((()((((()()(())(((((()))(((((((((((((()())))((((()(((()((())())()))((()))()(()()((()()()()(()()(()(()(((())()(()((((((()((()()((())()((((()((()()(()()())((()()()((()((())()(()(((()((())((((())(()))((()(()))(()())()((((((((()(((((((((((()))(()(((()(()()()((((())((())()())()))(())((())(()))(((()((()(())))(()))))((()()))))((((()(()(()())(()(())((((((((()((((()((()(((((()))())()(()))(()()((()(())(((((()(())()(((((()()))))))()(((())()(()()((((())()((())((()(((())(((()))((()()((((()(())))))((()((((()((()((()(((())((()))(((((((()(((()((((((((())()))((((())(((((()((((((((()(((()((()(((()()(((()((((((()()(()((((((((()()(()(()(())((((()())()))))(((()))((((())((((()())((()(())()((()((((((()((((((()(())))()())(((())())())()(())()(()())((()()((((())((((((())(()(((((()((((())()((((()(()(())(()())(((())()((())((((()))()((((((())(()(((()(((()((((((()(((()))(()()())())((()((()())()((((())(((()(()(((((((((())(())))()((()()()()(())((()))(((((((()(((((((((()(()))))(()((((((((()((((()((()()((((((()()(((((((()(()(())()(())((()()()((()(((((()())()(((((()())()()((()(()())(()()()(((()()(((((()((((((()()((()(()()()((((((((((((()((((((((()()(((()())))()(((()()(())())((((()((((()((((()()()(())(())((()(()(((((((((((((((()(())(())))))()()))((()(((()(())((()(((()(()()((((()()(((()(((()(((((()()((()(()(((()))((((((()((((((((()((()((())(((((()(((())(())())((()()))((((())()()((()(((()(((((()()(((()))(((()(()(((((((((((((()))((((((((()(((()))))())((((((((((((())((())((()())(((())((())(()((((((((((()(((())((()()(()((())(((((((((((()))((((((((((((()(()())((()((()((()(()(((()((((((((()()(()((()(()(((()))((()))(((((((((((((()(())((((((())(((()(())(()(()(()((()()))((((()((((()((((())))())((((()((((()))((((((()((((((()((()(((())))((())(()))(()((()((((()((()(((()()))((((()()()(((((((())(((())(()))())((((()())(((()(((((((((((()(()(()((()(((((((((((((((()()((((()((((((((()(((()()((()((((()))(((()(())((((((()((((())()((((()((()))(())()(()(((()((())())((((((()(()(())())(((())(()(()())(((((()((()((())()())(())))(((()(())))))))(((()(((()))()((()(((()()((()())()()))())))(((()))(()(((()(((((((((()(()(((((()()(((()())()()))))()(((()))(((()(()(()(()(()))()(())()))(()(((())))(()))))))))))(())((()((())((()(())()(())((()()((((()()((()()))((())(((()((()(())(())))()(()(((((()((()))())()(((((()()(((()(()((((((())(()))(())()))((()(()()))(())())()))(((())))(()((()(((())(())())))((()()((((((((((((((()((()(()()(()(((()))())()()((()()()(())(()))(()())(((())((())()(())()()(()()(())))((()(((()))))(((()()(()()))())((()((())()))((((()()()())((())))(((()(())(((((()(((((()((()(()((((()()(((()()()(((()())(((()()((((())(()))(((()))(())())((()))(((()((()))(((()()((())((()(((((()((((()()())((()))()((((()((()(()()()(
4 changes: 4 additions & 0 deletions tests/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(test
(name test_cases)
(modules test_cases)
(libraries native alcotest))
41 changes: 41 additions & 0 deletions tests/test_cases.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
type case =
| PartOne of int * string
| PartTwo of int * string
| BothParts of int * string * string
[@@ocaml.warning "-37"]

let test_with_files year day part expected =
let input_name = Printf.sprintf "../../../tests/data/%d_%02d.txt" year day in
let input = open_in input_name in
let answer = Native.Solutions.solve ~year ~day ~part input in
close_in input;
match answer with
| Some (Ok answer) -> Alcotest.(check string) "same string" expected answer
| Some (Error answer) -> Alcotest.fail answer
| None -> Alcotest.fail "Have no solution"

let to_test_case year day part expected =
let name = Printf.sprintf "Year %d / Day %02d / Part %02d" year day part in
let test () = test_with_files year day part expected in
(name, [ Alcotest.test_case "OK" `Quick test ])

let to_test_cases year cases =
match cases with
| PartOne (day, expected) -> [ to_test_case year day 1 expected ]
| PartTwo (day, expected) -> [ to_test_case year day 2 expected ]
| BothParts (day, expected_one, expected_two) ->
[
to_test_case year day 1 expected_one;
to_test_case year day 2 expected_two;
]

let to_test_suite (year, cases) =
let to_test_cases = to_test_cases year in
let test_cases = cases |> List.map to_test_cases |> List.flatten in
test_cases

let suites = [ (2015, [ BothParts (1, "232", "1783") ]) ]

let () =
let cases = suites |> List.map to_test_suite |> List.flatten in
Alcotest.run "Advent of OCaml" cases

0 comments on commit 20e6145

Please sign in to comment.