aboutsummaryrefslogtreecommitdiff
path: root/tests/tools_test.ml
diff options
context:
space:
mode:
Diffstat (limited to 'tests/tools_test.ml')
-rwxr-xr-xtests/tools_test.ml220
1 files changed, 220 insertions, 0 deletions
diff --git a/tests/tools_test.ml b/tests/tools_test.ml
new file mode 100755
index 0000000..9afc611
--- /dev/null
+++ b/tests/tools_test.ml
@@ -0,0 +1,220 @@
+open OUnit2
+
+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 = Tools.Date.get_julian_day 2016 01 01
+ and expected = (Num.num_of_int 2457389) 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 = Tools.Date.date_from_julian_day @@ Num.num_of_int 2415753
+ and expected = (1902, 01, 03) in
+
+ assert_equal
+ ~msg:(_msg expected result)
+ expected
+ result;
+
+ end
+
+ let test_parse_time ctx = begin
+ let result = Tools.Date.from_string "1902-01-03T12:34:56"
+ and expected = (Num.num_of_string "13045069031/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 = Tools.Date.time_from_julian_day @@ Tools.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) = Tools.Date.from_string "1902-01-03T12:34:56"
+ |> Num.(add_num ((num_of_int 1) // (num_of_int 2)) )
+ |> Tools.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) = Tools.Date.from_string "1902-01-03T12:34:56"
+ |> Num.(add_num ((num_of_int 3) // (num_of_int 4)) )
+ |> Tools.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;
+
+ "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;
+*)
+ ]