open OUnit2 module TestList = struct let test_linearize ctx = begin let input = [ ['1'; '2'; '3']; ['A'; 'B']; ['W'; 'X'; 'Y'; 'Z']; ['O']; ] in let expected = [ '1'; 'A'; 'W'; 'O'; 'X'; 'B'; '2'; '3'; 'Y'; 'Z' ] in let result = Tools.List.linearize input in let to_string elems = begin let result_buffer = Buffer.create 16 in List.iter (Buffer.add_char result_buffer) elems; Buffer.contents result_buffer end in assert_equal ~msg:(Printf.sprintf "Expected %s but got %s" (to_string expected) (to_string result) ) expected result end let tests = "list_test">::: [ "test_list_linearize" >:: test_linearize; ] end module TestString = struct let _msg ~expected ~result = Printf.sprintf "Expected %s but got %s" expected result let test_string_of_ints ctx = begin let result = Tools.String.string_of_ints 127 in let expected = "\127" in assert_equal ~msg:(_msg ~expected ~result) expected result end let test_string_of_ints_512 ctx = begin let result = Tools.String.string_of_ints 512 in let expected = "\002\000" in assert_equal ~msg:(_msg ~expected ~result) expected result end let test_split ctx = begin let result = Tools.String.split ~by:' ' "abc 123 456" and expected = ("abc", "123 456") in assert_equal expected result end let test_filter_float src expected ctx = begin let result = Tools.String.filter_float src in assert_equal ~msg:(_msg ~expected ~result) expected result end let tests = "string_test">::: [ "test_string_of_ints" >:: test_string_of_ints; "test_string_of_ints_512" >:: test_string_of_ints_512; "test_string_split" >:: test_split; "filter_float" >:: test_filter_float "12." "12"; "filter_float2" >:: test_filter_float "12.5" "12.5"; "filter_float_empty" >:: test_filter_float "" ""; "filter_float_no_dot" >:: test_filter_float "123" "123"; ] end module TestDate = struct let _msg ~expected ~result = Printf.sprintf "Expected %s but got %s" (Num.string_of_num expected) (Num.string_of_num result) let test_get_julian_day ctx = begin let result = Date.get_julian_day 2016 01 01 and expected = (Num.num_of_int 42370) in (* Check that the num is round *) assert_equal true (Num.is_integer_num result); assert_equal ~cmp:Num.(=/) ~msg:(_msg ~expected ~result) expected result end let test_from_julian_day ctx = begin let _msg (y1, m1, d1) (y2, m2, d2) = Printf.sprintf "Expected %d-%d-%d but got %d-%d-%d" y1 m1 d1 y2 m2 d2 in let result = Date.date_from_julian_day @@ Num.num_of_int 734 and expected = (1902, 01, 03) in assert_equal ~msg:(_msg expected result) expected result; end let test_parse_time ctx = begin let result = Date.from_string "1902-01-03T12:34:56" and expected = (Num.num_of_string "3966431/5400") in (* =2415753.52425925925925925925 *) assert_equal ~cmp:Num.(=/) ~msg:(_msg ~expected ~result) expected result end let test_time_from_julian_day ctx = begin let _msg (h1, m1, s1) (h2, m2, s2) = Printf.sprintf "Expected %f:%f:%f but got %f:%f:%f" h1 m1 s1 h2 m2 s2 in let result = Date.time_from_julian_day @@ Date.from_string "1902-01-03T12:34:56" |> Tools.Tuple3.map (Num.float_of_num) and expected = (12., 34., 56.) in assert_equal ~msg:(_msg expected result) expected result end let test_time_add_hour ctx = begin let (result:string) = Date.from_string "1902-01-03T12:34:56" |> Num.(add_num ((num_of_int 1) // (num_of_int 2)) ) |> Date.to_string in let expected = "1902-01-04T00:34:56" in let msg = Printf.sprintf "Expected %s but got %s" expected result in assert_equal ~msg expected result end let test_time_add_hour2 ctx = begin let (result:string) = Date.from_string "1902-01-03T12:34:56" |> Num.(add_num ((num_of_int 3) // (num_of_int 4)) ) |> Date.to_string in let expected = "1902-01-04T00:34:56" in let msg = Printf.sprintf "Expected %s but got %s" expected result in assert_equal ~msg expected result end end (* module TestLocale = struct let test_empty_string_length ctx = begin Tools.Locale.set Tools.Locale.LC_CTYPE "en_US.UTF-8"; let result = Tools.Locale.length "" in let expected = 0 in Tools.Locale.set Tools.Locale.LC_CTYPE "C"; assert_equal expected result end let test_one_byte_length ctx = begin Tools.Locale.set Tools.Locale.LC_CTYPE "en_US.UTF-8"; let result = Tools.Locale.length "A" in let expected = 1 in Tools.Locale.set Tools.Locale.LC_CTYPE "C"; assert_equal expected result end (** Encode an two-bytes UTF-8 string and check that the length is only one character*) let test_two_byte_length ctx = begin Tools.Locale.set Tools.Locale.LC_CTYPE "en_US.UTF-8"; let result = Tools.Locale.length "\xc3\x80" in let expected = 1 in Tools.Locale.set Tools.Locale.LC_CTYPE "C"; assert_equal expected result end end *) let tests = "tools_test">::: [ TestString.tests; TestList.tests; "test_get_julian_day" >:: TestDate.test_get_julian_day; "test_from_julian_day" >:: TestDate.test_from_julian_day; "test_parse_time" >:: TestDate.test_parse_time; "test_time_from_julian_day" >:: TestDate.test_time_from_julian_day; "test_time_add_hour" >:: TestDate.test_time_add_hour; (* (** Locale test *) "test_locale_length0" >:: TestLocale.test_empty_string_length; "test_locale_length1" >:: TestLocale.test_one_byte_length; "test_locale_length2" >:: TestLocale.test_two_byte_length; *) ]