From ef312564ca84a2b49fc291434d8fb2f8501bb618 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Tue, 15 Nov 2016 13:00:01 +0100 Subject: Initial commit --- tests/expressionParser_test.ml | 83 ++++++++++++++++ tests/expression_test.ml | 113 +++++++++++++++++++++ tests/sheet_test.ml | 136 +++++++++++++++++++++++++ tests/test.ml | 10 ++ tests/tools_test.ml | 220 +++++++++++++++++++++++++++++++++++++++++ tests/unicode_test.ml | 39 ++++++++ 6 files changed, 601 insertions(+) create mode 100755 tests/expressionParser_test.ml create mode 100755 tests/expression_test.ml create mode 100755 tests/sheet_test.ml create mode 100755 tests/test.ml create mode 100755 tests/tools_test.ml create mode 100755 tests/unicode_test.ml (limited to 'tests') diff --git a/tests/expressionParser_test.ml b/tests/expressionParser_test.ml new file mode 100755 index 0000000..7c16233 --- /dev/null +++ b/tests/expressionParser_test.ml @@ -0,0 +1,83 @@ +open OUnit2 + +let u = UTF8.from_utf8string + +let _msg ~(expected:Expression.t) ~(result:Expression.t) = + Printf.sprintf "Expected %s but got %s" + (UTF8.encode @@ Expression.show @@ Expression.load_expr expected) + (UTF8.encode @@ Expression.show @@ Expression.load_expr result) + +let load_expr str = + Expression.Formula ( + Expression.Expression( + ExpressionParser.value ExpressionLexer.read + @@ Lexing.from_string str ) + ) + + +let test_num ctx = begin + + let expected = Expression.Formula ( + Expression.Expression ( + ScTypes.Value ( + ScTypes.Num ( + Num.num_of_int 1, Some (u"1"))))) in + let result = load_expr "=1" in + + assert_equal + ~msg:(_msg ~expected ~result) + expected + result +end + +let test_call ctx = begin + + let expected = Expression.Formula ( + Expression.Expression ( + ScTypes.Call ( + u"sum", []))) in + let result = load_expr "=sum()" in + + assert_equal + ~msg:(_msg ~expected ~result) + expected + result +end + +let test_call2 ctx = begin + + (* The expression "foo2(" has to be parsed as a function call, and not as a + reference to cell "FOO2", followed by another expression. *) + + let expected = Expression.Formula ( + Expression.Expression ( + ScTypes.Call ( + u"foo2", [ScTypes.Value (ScTypes.Num (Num.num_of_int 4, Some (u"4")))]))) in + let result = load_expr "=foo2(4)" in + + assert_equal + ~msg:(_msg ~expected ~result) + expected + result +end + +let test_ref ctx = begin + + let expected = Expression.Formula ( + Expression.Expression ( + ScTypes.Ref( + ScTypes.Cell ((1, 3), (false, false))))) in + let result = load_expr "=A3" in + + assert_equal + ~msg:(_msg ~expected ~result) + expected + result +end + +let tests = "expression_parser_test">::: [ + "test_num" >:: test_num; + "test_call" >:: test_call; + "test_call2" >:: test_call2; + "test_ref" >:: test_ref; +] diff --git a/tests/expression_test.ml b/tests/expression_test.ml new file mode 100755 index 0000000..3674b9a --- /dev/null +++ b/tests/expression_test.ml @@ -0,0 +1,113 @@ +open OUnit2 +module T = Tools + +let u = UTF8.from_utf8string + +let _msg ~expected ~result = + + let get_type = function + | Expression.Basic ScTypes.Num _ -> "N" + | Expression.Basic ScTypes.Str _ -> "S" + | Expression.Basic ScTypes.Date _ -> "D" + | Expression.Basic ScTypes.Undefined -> "U" + | Expression.Basic ScTypes.Bool _ -> "B" + | Expression.Basic ScTypes.List _ -> "L" + | Expression.Formula _ -> "F" in + + Printf.sprintf "Expected %s:%s but got %s:%s" + (UTF8.encode @@ Expression.show expected) + (get_type expected) + (UTF8.encode @@ Expression.show result) + (get_type result) + +let assert_equal expected result = + OUnit2.assert_equal + ~cmp:(Expression.(=)) + ~msg:(_msg ~expected ~result) + expected result + + +let test_str ctx = begin + let result = Expression.load @@ u"cafe" in + let expected = Expression.load_expr @@ Expression.Basic ( + ScTypes.Str (u"cafe")) in + assert_equal expected result +end + +(** If the string start with space, it should be interpreted as a litteral *) +let test_str_space ctx = begin + let result = Expression.load @@ u" =cafe" in + let expected = Expression.load_expr @@ Expression.Basic ( + ScTypes.Str (u" =cafe")) in + assert_equal expected result +end + +let test_formula_str ctx = begin + let result = Expression.load @@ u"=\"cafe\"" in + let expected = Expression.load_expr @@ Expression.Formula ( + Expression.Expression ( + ScTypes.Value ( + ScTypes.Str (u"cafe")))) in + assert_equal expected result +end + +let test_num ctx = begin + let result = Expression.load @@ u"123" in + let expected = Expression.load_expr @@ Expression.Basic ( + ScTypes.Num (Num.num_of_int 123, None)) in + assert_equal expected result +end + +let test_float ctx = begin + let result = Expression.load @@ u"12.45" in + let expected = Expression.load_expr @@ Expression.Basic ( + ScTypes.Num ( + T.Num.of_float_string "12.45", None)) in + assert_equal expected result +end + +let test_relative ctx = begin + let result = Expression.load @@ u"-123" in + let expected = Expression.load_expr @@ Expression.Basic ( + ScTypes.Num (Num.num_of_int (-123), None)) in + assert_equal expected result +end + +let test_date ctx = begin + let result = Expression.load @@ u"1900/01/01" + and expected = Expression.load_expr @@ Expression.Basic ( + ScTypes.Date (Tools.Date.get_julian_day 1900 01 01)) in + assert_equal expected result +end + +let test_sources ctx = begin + let result = Expression.load @@ u"=A1" + |> Expression.collect_sources in + + let expected = Cell.Set.singleton (1, 1) in + + let msg = Printf.sprintf "Expected %s but got %s" + (UTF8.encode @@ Tools.String.print_buffer Cell.Set.printb expected) + (UTF8.encode @@ Tools.String.print_buffer Cell.Set.printb result) in + + OUnit2.assert_equal ~msg expected result + +end + +let tests = "expression_test">::: [ + (* Test litteral input *) + "test_str" >:: test_str; + "test_str_space" >:: test_str_space; + + "test_formula_str" >:: test_formula_str; + + (* Test numeric input *) + "test_num" >:: test_num; + "test_float" >:: test_float; + "test_relative" >:: test_relative; + + "test_date" >:: test_date; + + "test_sources" >:: test_sources; + +] diff --git a/tests/sheet_test.ml b/tests/sheet_test.ml new file mode 100755 index 0000000..f63d76f --- /dev/null +++ b/tests/sheet_test.ml @@ -0,0 +1,136 @@ +open OUnit2 + +let u = UTF8.from_utf8string + +let _msg ~expected ~result = + Printf.sprintf "Expected %s but got %s" + (UTF8.encode @@ ScTypes.Result.show expected) + (UTF8.encode @@ ScTypes.Result.show result) + +(** Test a simple references between two cells *) +let test_create_ref_1 ctx = begin + + let s = Sheet.Raw.create + |> Sheet.Raw.add (3,3) @@ Expression.load @@ u"=-1" + |> snd |> Sheet.Raw.add (0,0) @@ Expression.load @@ u"=C3" + |> snd in + let result = (Sheet.Raw.get_value (0, 0) s) in + let expected = (ScTypes.Result (ScTypes.Num (Num.num_of_int (-1), Some (u"-1")))) in + + assert_equal + ~msg:(_msg ~expected ~result) + expected + result +end + +let test_create_ref_2 ctx = begin + + let s = Sheet.Raw.create + |> Sheet.Raw.add (2,2) @@ Expression.load @@ u"=C3" + |> snd |> Sheet.Raw.add (3,3) @@ Expression.load @@ u"=A1" + |> snd |> Sheet.Raw.add (1,1) @@ Expression.load @@ u"123" + |> snd in + + + let result = (Sheet.Raw.get_value (2, 2) s) in + let expected = ScTypes.Result (ScTypes.Num (Num.num_of_int 123, Some (u"123"))) in + + assert_equal + ~msg:(_msg ~expected ~result) + expected + result +end + +let test_create_direct_cycle ctx = begin + + let s = Sheet.Raw.create + |> Sheet.Raw.add (2,2) @@ Expression.load @@ u"=B2" + |> snd in + let result = (Sheet.Raw.get_value (2, 2) s) in + let expected = ScTypes.Error Sheet.Raw.Cycle in + + assert_equal + ~msg:(_msg ~expected ~result) + expected + result +end + +let test_create_indirect_cycle ctx = begin + + let s = Sheet.Raw.create + |> Sheet.Raw.add (2,2) @@ Expression.load @@ u"=A1" + |> snd |> Sheet.Raw.add (1,1) @@ Expression.load @@ u"=B2" + |> snd |> Sheet.Raw.add (0,0) @@ Expression.load @@ u"=A1" + |> snd in + let result = (Sheet.Raw.get_value (0, 0) s) in + let expected = ScTypes.Result ScTypes.Undefined in + + assert_equal + ~msg:(_msg ~expected ~result) + expected + result +end + +let test_delete ctx = begin + + let s = Sheet.Raw.create + |> Sheet.Raw.add (2,2) @@ Expression.load @@ u"=C3" + |> snd |> Sheet.Raw.add (3,3) @@ Expression.load @@ u"=A1" + |> snd |> Sheet.Raw.remove (2,2) + |> snd |> Sheet.Raw.remove (3,3) + |> snd in + let result = (Sheet.Raw.get_value (3, 3) s) in + let expected = ScTypes.Result ScTypes.Undefined in + + assert_equal + ~msg:(_msg ~expected ~result) + expected + result +end + +let test_update_succs1 ctx = begin + + let result = Sheet.Raw.create + |> Sheet.Raw.add (1,1) @@ Expression.load @@ u" =1" + |> snd |> Sheet.Raw.add (2,2) @@ Expression.load @@ u"=A2" + |> snd |> Sheet.Raw.add (1,2) @@ Expression.load @@ u"=A1/1" + |> snd |> Sheet.Raw.add (1,1) @@ Expression.load @@ u"=2" + |> fst in + + (* All the cells are updated by the change *) + let expected = Cell.Set.of_list [(1,1); (1, 2); (2,2)] in + + let msg = Printf.sprintf "Expected %s but got %s" + (UTF8.encode @@ Tools.String.print_buffer Cell.Set.printb expected) + (UTF8.encode @@ Tools.String.print_buffer Cell.Set.printb result) in + + assert_equal ~msg + expected + result +end + +let test_update_succs2 ctx = begin + + let result = Sheet.Raw.create + |> Sheet.Raw.add (1,1) @@ Expression.load @@ u"=1" + |> snd |> Sheet.Raw.add (2,2) @@ Expression.load @@ u"=A2" + |> snd |> Sheet.Raw.add (1,2) @@ Expression.load @@ u"=A1/0" + |> snd |> Sheet.Raw.add (1,1) @@ Expression.load @@ u"=2" + |> fst in + (* Only (1, 1) is updated ; (1, 2) does not change, neither (2, 2) *) + let expected = Cell.Set.of_list [(1,1)] in + + assert_equal + expected + result +end + +let tests = "sheet_test">::: [ + "test_ref2" >:: test_create_ref_2; + "test_ref1" >:: test_create_ref_1; + "test_cycle1" >:: test_create_direct_cycle; + "test_cycle2" >:: test_create_indirect_cycle; + "test_delete" >:: test_delete; + "test_update_succs1" >:: test_update_succs1; + "test_update_succs2" >:: test_update_succs2; + ] diff --git a/tests/test.ml b/tests/test.ml new file mode 100755 index 0000000..1bfd6c1 --- /dev/null +++ b/tests/test.ml @@ -0,0 +1,10 @@ +let () = + let tests = OUnit2.test_list [ + Tools_test.tests; + ExpressionParser_test.tests; + Expression_test.tests; + Sheet_test.tests; + ] + in OUnit2.run_test_tt_main tests + + 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; +*) + ] diff --git a/tests/unicode_test.ml b/tests/unicode_test.ml new file mode 100755 index 0000000..8c58dc8 --- /dev/null +++ b/tests/unicode_test.ml @@ -0,0 +1,39 @@ +open OUnit2 + +let _msg ~expected ~result = + Printf.sprintf "Expected %s but got %s" + (String.escaped expected) + (String.escaped result) + +let test_decode env = begin + let result = Unicode.decode ~encoding:`ISO_8859_1 "caf\xE9" + |> Unicode.to_utf8 + and expected = "café" in + + assert_equal + ~msg:(_msg ~expected ~result) + expected + result +end + +let test_tolist env = begin + + + let result = Unicode.decode ~encoding:`UTF_8 "café" + |> Unicode.to_list in + + let expected = ['c'; 'a'; 'f'; '\xE9'] + |> List.map Uchar.of_char in + + assert_equal + expected + result + +end + + +let tests = "unicode_test">::: [ + "test_decode" >:: test_decode; + "test_tolist" >:: test_tolist + +] -- cgit v1.2.3