From 6f6ff0e39eb6d771ef5336394079646ccdc18bd5 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Tue, 7 Nov 2017 15:44:40 +0100 Subject: Use Zarith instead of Num for computing numbers --- tests/tools_test.ml | 117 ++++++++++++++++++++++------------------------------ 1 file changed, 50 insertions(+), 67 deletions(-) (limited to 'tests/tools_test.ml') diff --git a/tests/tools_test.ml b/tests/tools_test.ml index 5514404..74a3acd 100755 --- a/tests/tools_test.ml +++ b/tests/tools_test.ml @@ -98,21 +98,41 @@ 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) + module Float = struct + + type t = float + let add = ( +. ) + let sub = ( -. ) + let mult = ( *. ) + let div = ( /. ) + + let floor = Pervasives.floor + + let of_int = float_of_int + + let to_int = int_of_float + let to_float x = x + + end + + module FDate = Date.Make(Float) + + let epsilon = 1.0e-5 + + let (=.) a b = (abs_float (a-.b)) < epsilon let test_get_julian_day ctx = begin - let result = Date.get_julian_day 2016 01 01 - and expected = (Num.num_of_int 42370) in + let result = FDate.get_julian_day 2016 01 01 + and expected = 42370. in - (* Check that the num is round *) - assert_equal true (Num.is_integer_num result); + let _msg ~expected ~result = + Printf.sprintf "Expected %f but got %f" + expected + result in assert_equal - ~cmp:Num.(=/) + ~cmp:(=.) ~msg:(_msg ~expected ~result) expected result @@ -126,7 +146,7 @@ module TestDate = struct y2 m2 d2 in - let result = Date.date_from_julian_day @@ Num.num_of_int 734 + let result = FDate.date_from_julian_day 734. and expected = (1902, 01, 03) in assert_equal @@ -137,11 +157,18 @@ module TestDate = struct 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 + let result = FDate.from_string "1902-01-03T12:34:56" + + and expected = 3966431. /. 5400. (* =2415753.52425925925925925925 *) + + and _msg ~expected ~result = + Printf.sprintf "Expected %f but got %f" + expected + result in + assert_equal - ~cmp:Num.(=/) + ~cmp:(=.) ~msg:(_msg ~expected ~result) expected result @@ -150,17 +177,17 @@ module TestDate = struct 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" + Printf.sprintf "Expected %d:%d:%f but got %d:%d:%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 + let result = FDate.time_from_julian_day @@ FDate.from_string "1902-01-03T12:34:56" + and expected = (12, 34, 56.) in assert_equal ~msg:(_msg expected result) + ~cmp:(fun (a, b, c) (a', b', c') -> a = a' && b = b' && c =. c') expected result end @@ -168,9 +195,9 @@ module TestDate = struct 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 (result:string) = FDate.from_string "1902-01-03T12:34:56" + |> fun x -> (x +. (1. /. 2.)) + |> FDate.to_string in let expected = "1902-01-04T00:34:56" in @@ -185,9 +212,9 @@ module TestDate = struct 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 (result:string) = FDate.from_string "1902-01-03T12:34:56" + |> fun x -> (x +. (3. /. 4.)) + |> FDate.to_string in let expected = "1902-01-04T00:34:56" in @@ -201,44 +228,6 @@ module TestDate = struct 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; @@ -250,10 +239,4 @@ let tests = "tools_test">::: [ "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; -*) ] -- cgit v1.2.3