Browse Source

Add unit tests + separate out library from entrypoint

Getty Ritter 3 years ago
parent
commit
b8da1b0f3f
4 changed files with 71 additions and 38 deletions
  1. 12 2
      01/dune
  2. 32 0
      01/lib.ml
  3. 3 36
      01/main.ml
  4. 24 0
      01/test.ml

+ 12 - 2
01/dune

@@ -1,4 +1,14 @@
-;; This declares the hello_world executable implemented by hello_world.ml
+(library
+  (name Lib)
+  (modules Lib)
+  (libraries base stdio))
+
 (executable
  (name main)
- (libraries base stdio))
+ (modules Main)
+ (libraries Lib))
+
+(executable
+ (name test)
+ (modules Test)
+ (libraries Lib oUnit))

+ 32 - 0
01/lib.ml

@@ -0,0 +1,32 @@
+open Base
+
+(* load the provided filename *)
+let load_list filename =
+  let open Stdio.In_channel in
+  let lines = with_file ~f:input_lines filename
+  in List.map ~f:Int.of_string lines
+
+(* You might notice that this isn't actually >>=: I just wanted to
+   have a non-parenthesized foreach! *)
+let (>>=) list fn = let _ = List.map ~f:fn list in None
+
+let print_result = function
+  | None -> Stdio.printf "Unable to find result\n"
+  | Some xs ->
+     let lst = String.concat ~sep:" * " (List.map ~f:Int.to_string xs)
+     in Stdio.printf "Got (%s) = %d\n" lst (List.fold ~init:1 ~f:(fun x y -> x * y) xs)
+
+let part_one source =
+  With_return.with_return (fun r ->
+    source >>= fun a ->
+    source >>= fun b ->
+      if a <> b && a + b = 2020 then r.return (Some [a; b])
+    )
+
+let part_two source =
+  With_return.with_return (fun r ->
+    source >>= fun a ->
+    source >>= fun b ->
+    source >>= fun c ->
+      if a <> b && b <> c && a <> c && a + b + c = 2020 then r.return (Some [a; b; c])
+    )

+ 3 - 36
01/main.ml

@@ -1,39 +1,6 @@
-open Base
-
-(* load the provided filename *)
-let load_list filename =
-  let open Stdio.In_channel in
-  let lines = with_file ~f:input_lines filename
-  in List.map ~f:Int.of_string lines
-
-(* You might notice that this isn't actually >>=: I just wanted to
-   have a non-parenthesized foreach! *)
-let (>>=) list fn = let _ = List.map ~f:fn list in None
-
-let print_result = function
-  | None -> Stdio.printf "Unable to find result\n"
-  | Some xs ->
-     let lst = String.concat ~sep:" * " (List.map ~f:Int.to_string xs)
-     in Stdio.printf "Got (%s) = %d\n" lst (List.fold ~init:1 ~f:(fun x y -> x * y) xs)
-
-let part_one source =
-  With_return.with_return (fun r ->
-    source >>= fun a ->
-    source >>= fun b ->
-      if a <> b && a + b = 2020 then r.return (Some [a; b])
-    )
-
-let part_two source =
-  With_return.with_return (fun r ->
-    source >>= fun a ->
-    source >>= fun b ->
-    source >>= fun c ->
-      if a <> b && b <> c && a <> c && a + b + c = 2020 then r.return (Some [a; b; c])
-    )
-
 let main() =
-  let source = load_list "input" in
-  print_result (part_one source);
-  print_result (part_two source)
+  let source = Lib.load_list "input" in
+  Lib.print_result (Lib.part_one source);
+  Lib.print_result (Lib.part_two source)
 
 let () = main()

+ 24 - 0
01/test.ml

@@ -0,0 +1,24 @@
+open OUnit2
+
+let sample_list : int list = [
+    1721;
+    979;
+    366;
+    299;
+    675;
+    1456;
+]
+
+let test_part_one _ =
+  assert_equal (Lib.part_one sample_list) (Some [1721; 299])
+
+let test_part_two _ =
+  assert_equal (Lib.part_two sample_list) (Some [979; 366; 675])
+
+let () =
+  run_test_tt_main
+    ("day one" >:::
+       [
+         "part one" >:: test_part_one;
+         "part two" >:: test_part_two;
+    ])