Skip to content

Commit

Permalink
test(p19): add tests
Browse files Browse the repository at this point in the history
	- test iterative, map, module, rec and tailrec
	- test data module
  • Loading branch information
pmpknu committed Oct 29, 2024
1 parent 31a7aa9 commit cd94927
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 1 deletion.
15 changes: 15 additions & 0 deletions p19/lib/date.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
let is_leap year = (year mod 4 = 0 && year mod 100 <> 0) || year mod 400 = 0

let%test "is_leap for leap year 2000" = is_leap 2000 = true
let%test "is_leap for leap year 2020" = is_leap 2020 = true
let%test "is_leap for non-leap year 1900" = is_leap 1900 = false
let%test "is_leap for non-leap year 2023" = is_leap 2023 = false

let days_in_month year month =
match month with
| 1 | 3 | 5 | 7 | 8 | 10 | 12 -> 31
Expand All @@ -8,10 +13,20 @@ let days_in_month year month =
| _ -> failwith "Invalid month"
;;

let%test "days_in_month for February in leap year" = days_in_month 2020 2 = 29
let%test "days_in_month for February in non-leap year" = days_in_month 2021 2 = 28
let%test "days_in_month for January" = days_in_month 2021 1 = 31
let%test "days_in_month for April" = days_in_month 2021 4 = 30

let zellers_congruence year month day =
let year, month = if month < 3 then year - 1, month + 12 else year, month in
let k = year mod 100 in
let j = year / 100 in
let f = day + (13 * (month + 1) / 5) + k + (k / 4) + (j / 4) - (2 * j) in
((f mod 7) + 7) mod 7
;;

let%test "zellers_congruence for 1 Jan 1900 (Monday)" = zellers_congruence 1900 1 1 = 2
let%test "zellers_congruence for 1 Jan 2000 (Saturday)" = zellers_congruence 2000 1 1 = 0
let%test "zellers_congruence for 1 Mar 2021 (Monday)" = zellers_congruence 2021 2 28 = 1
let%test "zellers_congruence for 31 Dec 1999 (Friday)" = zellers_congruence 1999 12 31 = 6
6 changes: 5 additions & 1 deletion p19/lib/dune
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
(library
(name p19_lib)
(modules date tailrec rec map iterative module))
(modules date tailrec rec map iterative module)
(inline_tests
(flags (-verbose)))
(preprocess
(pps ppx_inline_test)))
2 changes: 2 additions & 0 deletions p19/lib/iterative.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ let count_sundays start_year end_year =
done;
!count
;;

let%test _ = count_sundays 1901 2000 = 171
2 changes: 2 additions & 0 deletions p19/lib/map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ let count_sundays start_year end_year =
in
List.fold_left (fun acc year -> acc + aux year) 0 years
;;

let%test _ = count_sundays 1901 2000 = 171
2 changes: 2 additions & 0 deletions p19/lib/module.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ let count_sundays start_year end_year =
in
List.fold_left (fun acc year -> acc + aux year) 0 years
;;

let%test _ = count_sundays 1901 2000 = 171
2 changes: 2 additions & 0 deletions p19/lib/rec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ let rec count_sundays year month day_of_week =
then new_count + count_sundays (year + 1) 1 next_day_of_week
else new_count + count_sundays year (month + 1) next_day_of_week)
;;

let%test _ = count_sundays 1901 1 2 = 171
2 changes: 2 additions & 0 deletions p19/lib/tailrec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ let rec count_sundays year month day_of_week count =
then count_sundays (year + 1) 1 next_day_of_week new_count
else count_sundays year (month + 1) next_day_of_week new_count)
;;

let%test _ = count_sundays 1901 1 2 0 = 171

0 comments on commit cd94927

Please sign in to comment.