[OCaml] High Intensity Training Online
1
module Calendar = Hito_web.Calendar
2
3
let invalid_month f =
4
try
5
ignore (f ());
6
false
7
with Invalid_argument _ -> true
8
9
let suite =
10
[
11
( "calendar",
12
[
13
( "recognizes Gregorian leap years",
14
`Quick,
15
fun () ->
16
Alcotest.(check bool)
17
"1900 is not leap" false (Calendar.is_leap 1900);
18
Alcotest.(check bool) "2000 is leap" true (Calendar.is_leap 2000);
19
Alcotest.(check bool) "2024 is leap" true (Calendar.is_leap 2024) );
20
( "counts every month",
21
`Quick,
22
fun () ->
23
let expected = [ 31; 29; 31; 30; 31; 30; 31; 31; 30; 31; 30; 31 ] in
24
List.iteri
25
(fun index days ->
26
Alcotest.(check int)
27
(Printf.sprintf "month %d" (index + 1))
28
days
29
(Calendar.days_in_month ~year:2024 ~month:(index + 1)))
30
expected );
31
( "rejects an invalid month",
32
`Quick,
33
fun () ->
34
Alcotest.(check bool)
35
"month 13" true
36
(invalid_month (fun () ->
37
Calendar.days_in_month ~year:2024 ~month:13)) );
38
( "converts Gregorian dates to UTC seconds",
39
`Quick,
40
fun () ->
41
Alcotest.(check int)
42
"Unix epoch" 0
43
(Calendar.unix_seconds_of_date ~year:1970 ~month:1 ~day:1);
44
Alcotest.(check int)
45
"day after epoch" 86_400
46
(Calendar.unix_seconds_of_date ~year:1970 ~month:1 ~day:2) );
47
( "returns Monday-first weekdays",
48
`Quick,
49
fun () ->
50
Alcotest.(check int)
51
"1970-01-01 was Thursday" 3
52
(Calendar.monday_first_weekday ~year:1970 ~month:1 ~day:1);
53
Alcotest.(check int)
54
"2024-01-01 was Monday" 0
55
(Calendar.monday_first_weekday ~year:2024 ~month:1 ~day:1) );
56
] );
57
]
58