From 55bff7b530f0c289a4dbecd6aedd3b77f78789c7 Mon Sep 17 00:00:00 2001 From: Galfour Date: Mon, 22 Apr 2019 03:29:21 +0000 Subject: [PATCH] lift tests to ast_simplify --- src/lib_utils/function.ml | 4 + src/lib_utils/trace.ml | 2 + src/lib_utils/x_option.ml | 33 ++ src/ligo/ast_simplified/ast_simplified.ml | 3 + src/ligo/ast_simplified/combinators.ml | 62 ++- src/ligo/ligo.ml | 16 +- src/ligo/test/integration_tests.ml | 581 ++++++---------------- src/ligo/test/test_helpers.ml | 52 ++ 8 files changed, 324 insertions(+), 429 deletions(-) diff --git a/src/lib_utils/function.ml b/src/lib_utils/function.ml index d60c3b391..f567d55ee 100644 --- a/src/lib_utils/function.ml +++ b/src/lib_utils/function.ml @@ -1,2 +1,6 @@ let compose = fun f g x -> f (g x) let (>|) = compose + +let compose_2 = fun f g x y -> f (g x y) +let compose_3 = fun f g x y z -> f (g x y z) +let compose_4 = fun f g a b c d -> f (g a b c d) diff --git a/src/lib_utils/trace.ml b/src/lib_utils/trace.ml index c84e74af7..39197dead 100644 --- a/src/lib_utils/trace.ml +++ b/src/lib_utils/trace.ml @@ -159,6 +159,8 @@ let bind_map_smap f smap = bind_smap (X_map.String.map f smap) let bind_map_list f lst = bind_list (List.map f lst) let bind_map_ne_list : _ -> 'a X_list.Ne.t -> 'b X_list.Ne.t result = fun f lst -> bind_ne_list (X_list.Ne.map f lst) +let bind_iter_list : (_ -> unit result) -> _ list -> unit result = fun f lst -> + bind_map_list f lst >>? fun _ -> ok () let bind_location (x:_ Location.wrap) = x.wrap_content >>? fun wrap_content -> diff --git a/src/lib_utils/x_option.ml b/src/lib_utils/x_option.ml index 2063463db..5aa636cba 100644 --- a/src/lib_utils/x_option.ml +++ b/src/lib_utils/x_option.ml @@ -4,3 +4,36 @@ let lr (a , b) = match (a , b) with | Some x , _ -> Some (`Left x) | None , Some x -> Some (`Right x) | _ -> None + +(* TODO: recursive terminal *) +let rec bind_list = fun lst -> + match lst with + | [] -> Some [] + | hd :: tl -> ( + match hd with + | None -> None + | Some hd' -> ( + match bind_list tl with + | None -> None + | Some tl' -> Some (hd' :: tl') + ) + ) + +let bind_pair = fun (a , b) -> + a >>= fun a' -> + b >>= fun b' -> + Some (a' , b') + +let bind_map_list = fun f lst -> bind_list (X_list.map f lst) + +let bind_map_pair = fun f (a , b) -> bind_pair (f a , f b) + +let bind_smap (s:_ X_map.String.t) = + let open X_map.String in + let aux k v prev = + prev >>= fun prev' -> + v >>= fun v' -> + Some (add k v' prev') in + fold aux s (Some empty) + +let bind_map_smap f smap = bind_smap (X_map.String.map f smap) diff --git a/src/ligo/ast_simplified/ast_simplified.ml b/src/ligo/ast_simplified/ast_simplified.ml index a49ce9e8b..d2d6aaef7 100644 --- a/src/ligo/ast_simplified/ast_simplified.ml +++ b/src/ligo/ast_simplified/ast_simplified.ml @@ -1,4 +1,7 @@ include Types +include Misc + module Types = Types +module Misc = Misc module PP = PP module Combinators = Combinators diff --git a/src/ligo/ast_simplified/combinators.ml b/src/ligo/ast_simplified/combinators.ml index b567c4afd..f1e5b458b 100644 --- a/src/ligo/ast_simplified/combinators.ml +++ b/src/ligo/ast_simplified/combinators.ml @@ -2,16 +2,18 @@ open Types module SMap = Map.String +let get_type_annotation (x:annotated_expression) = x.type_annotation let t_bool : type_expression = T_constant ("bool", []) let t_string : type_expression = T_constant ("string", []) let t_bytes : type_expression = T_constant ("bytes", []) let t_int : type_expression = T_constant ("int", []) +let t_nat : type_expression = T_constant ("nat", []) let t_unit : type_expression = T_constant ("unit", []) let t_option o : type_expression = T_constant ("option", [o]) let t_list t : type_expression = T_constant ("list", [t]) let t_tuple lst : type_expression = T_tuple lst -let t_pair a b = t_tuple [a ; b] +let t_pair (a , b) = t_tuple [a ; b] let t_record m : type_expression = (T_record m) let t_ez_record (lst:(string * type_expression) list) : type_expression = let aux prev (k, v) = SMap.add k v prev in @@ -29,22 +31,72 @@ let ez_t_sum (lst:(string * type_expression) list) : type_expression = T_sum map let t_function param result : type_expression = T_function (param, result) +let t_map key value = (T_constant ("map", [key ; value])) let make_e_a ?type_annotation expression = {expression ; type_annotation} let make_e_a_full expression type_annotation = make_e_a ~type_annotation expression -let name (s : string) : name = s +let make_name (s : string) : name = s let e_var (s : string) : expression = E_variable s -let e_unit () : expression = E_literal (Literal_unit) +let e_unit () : expression = E_literal (Literal_unit) let e_int n : expression = E_literal (Literal_int n) let e_nat n : expression = E_literal (Literal_nat n) let e_bool b : expression = E_literal (Literal_bool b) let e_string s : expression = E_literal (Literal_string s) let e_bytes b : expression = E_literal (Literal_bytes (Bytes.of_string b)) +let e_record map : expression = E_record map +let e_tuple lst : expression = E_tuple lst +let e_some s : expression = E_constant ("SOME", [s]) +let e_none : expression = E_constant ("NONE", []) +let e_map lst : expression = E_map lst +let e_list lst : expression = E_list lst +let e_pair a b : expression = E_tuple [a; b] let e_a_int n : annotated_expression = make_e_a_full (e_int n) t_int +let e_a_nat n : annotated_expression = make_e_a_full (e_nat n) t_nat +let e_a_bool b : annotated_expression = make_e_a_full (e_bool b) t_bool +let e_a_unit : annotated_expression = make_e_a_full (e_unit ()) t_unit + +let e_a_record r = + let type_annotation = Option.( + map ~f:t_record (bind_map_smap get_type_annotation r) + ) in + make_e_a ?type_annotation (e_record r) + +let ez_e_a_record lst = + let aux prev (k, v) = SMap.add k v prev in + let map = List.fold_left aux SMap.empty lst in + e_a_record map + +let e_a_tuple lst = + let type_annotation = Option.( + map ~f:t_tuple (bind_map_list get_type_annotation lst) + ) in + make_e_a ?type_annotation (e_tuple lst) + +let e_a_pair a b = + let type_annotation = Option.( + map ~f:t_pair + @@ bind_map_pair get_type_annotation (a , b) + ) in + make_e_a ?type_annotation (e_pair a b) + +let e_a_some opt = + let type_annotation = Option.( + map ~f:t_option (get_type_annotation opt) + ) in + make_e_a ?type_annotation (e_some opt) + +let e_a_none t_opt = + let type_annotation = t_option t_opt in + make_e_a ~type_annotation e_none + +let e_a_list lst t = + make_e_a ~type_annotation:(t_list t) (e_list lst) + +let e_a_map lst k v = make_e_a ~type_annotation:(t_map k v) (e_map lst) let e_lambda (binder : string) (input_type : type_expression) @@ -53,7 +105,7 @@ let e_lambda (binder : string) (body : block) : expression = E_lambda { - binder = (name binder) ; + binder = (make_name binder) ; input_type = input_type ; output_type = output_type ; result = (make_e_a result) ; @@ -64,7 +116,7 @@ let e_tuple (lst : ae list) : expression = E_tuple lst let ez_e_tuple (lst : expression list) : expression = e_tuple (List.map make_e_a lst) -let e_constructor (s : string) (e : ae) : expression = E_constructor (name s, e) +let e_constructor (s : string) (e : ae) : expression = E_constructor (make_name s, e) let e_record (lst : (string * ae) list) : expression = let aux prev (k, v) = SMap.add k v prev in diff --git a/src/ligo/ligo.ml b/src/ligo/ligo.ml index d2c0446b0..6ad06f831 100644 --- a/src/ligo/ligo.ml +++ b/src/ligo/ligo.ml @@ -71,6 +71,17 @@ let easy_evaluate_typed (entry:string) (program:AST_Typed.program) : AST_Typed.a untranspile_value result typed_main.type_annotation in ok typed_result +let easy_evaluate_typed_simplified (entry:string) (program:AST_Typed.program) : Ast_simplified.annotated_expression result = + let%bind result = + let%bind mini_c_main = + transpile_entry program entry in + Run.Mini_c.run_entry mini_c_main (Mini_c.Combinators.d_unit) in + let%bind typed_result = + let%bind typed_main = Ast_typed.get_entry program entry in + untranspile_value result typed_main.type_annotation in + let%bind annotated_result = untype_expression typed_result in + ok annotated_result + let easy_evaluate_typed = trace_f_2_ez easy_evaluate_typed (thunk "easy evaluate typed") let easy_run_typed @@ -105,7 +116,7 @@ let easy_run_typed let easy_run_typed_simplified ?(debug_mini_c = false) (entry:string) - (program:AST_Typed.program) (input:Ast_simplified.annotated_expression) : AST_Typed.annotated_expression result = + (program:AST_Typed.program) (input:Ast_simplified.annotated_expression) : Ast_simplified.annotated_expression result = let%bind mini_c_main = trace (simple_error "transpile mini_c entry") @@ transpile_entry program entry in @@ -132,7 +143,8 @@ let easy_run_typed_simplified | T_function (_, result) -> ok result | _ -> simple_fail "main doesn't have fun type" in untranspile_value mini_c_result main_result_type in - ok typed_result + let%bind annotated_result = untype_expression typed_result in + ok annotated_result let easy_run_main_typed ?(debug_mini_c = false) diff --git a/src/ligo/test/integration_tests.ml b/src/ligo/test/integration_tests.ml index 57f64f254..de0267db6 100644 --- a/src/ligo/test/integration_tests.ml +++ b/src/ligo/test/integration_tests.ml @@ -2,148 +2,61 @@ open Trace open Ligo open Test_helpers +open Ast_simplified.Combinators + let function_ () : unit result = let%bind program = type_file "./contracts/function.ligo" in - let aux n = - let open Ast_simplified.Combinators in - let input = e_a_int n in - let%bind result = easy_run_typed_simplified "main" program input in - let expected = Ast_typed.Combinators.e_a_empty_int n in - Ast_typed.assert_value_eq (expected , result) - in - let%bind _ = bind_list - @@ List.map aux - @@ [0 ; 2 ; 42 ; 163 ; -1] in - ok () + let make_expect = fun n -> n in + expect_n_int program "main" make_expect let complex_function () : unit result = let%bind program = type_file "./contracts/function-complex.ligo" in - let aux n = - let open AST_Typed.Combinators in - let input = e_a_empty_int n in - let%bind result = easy_run_main_typed program input in - let%bind result' = - trace (simple_error "bad result") @@ - get_a_int result in - Assert.assert_equal_int (3 * n + 2) result' - in - let%bind _ = bind_list - @@ List.map aux - @@ [0 ; 2 ; 42 ; 163 ; -1] in - ok () + let make_expect = fun n -> (3 * n + 2) in + expect_n_int program "main" make_expect let closure () : unit result = let%bind program = type_file "./contracts/closure.ligo" in - let%bind _foo = trace (simple_error "test foo") @@ - let aux n = - let open AST_Typed.Combinators in - let input = e_a_empty_int n in - let%bind result = easy_run_typed "foo" program input in - let expected = e_a_empty_int ( 2 * n ) in - AST_Typed.assert_value_eq (expected, result) - in - bind_list - @@ List.map aux - @@ [0 ; 2 ; 42 ; 163 ; -1] in - let%bind _toto = trace (simple_error "toto") @@ - let aux n = - let open AST_Typed.Combinators in - let input = e_a_empty_int n in - let%bind result = easy_run_typed "toto" program input in - let expected = e_a_empty_int ( 4 * n ) in - AST_Typed.assert_value_eq (expected, result) - in - bind_list - @@ List.map aux - @@ [0 ; 2 ; 42 ; 163 ; -1] in + let%bind () = + let make_expect = fun n -> (2 * n) in + expect_n_int program "foo" make_expect + in + let%bind _ = + let make_expect = fun n -> (4 * n) in + expect_n_int program "toto" make_expect + in ok () let shadow () : unit result = let%bind program = type_file "./contracts/shadow.ligo" in - let%bind _foo = trace (simple_error "test foo") @@ - let aux n = - let open AST_Typed.Combinators in - let input = e_a_empty_int n in - let%bind result = easy_run_typed "foo" program input in - let expected = e_a_empty_int 0 in - AST_Typed.assert_value_eq (expected, result) - in - bind_list - @@ List.map aux - @@ [3 ; 2 ; 0 ; 42 ; 163 ; -1] in - ok () + let make_expect = fun _ -> 0 in + expect_n_int program "foo" make_expect let higher_order () : unit result = let%bind program = type_file "./contracts/high-order.ligo" in - let%bind _foo = trace (simple_error "test foo") @@ - let aux n = - let open AST_Typed.Combinators in - let input = e_a_empty_int n in - let%bind result = easy_run_typed "foobar" program input in - let expected = e_a_empty_int ( n ) in - AST_Typed.assert_value_eq (expected, result) - in - bind_list - @@ List.map aux - @@ [0 ; 2 ; 42 ; 163 ; -1] in - ok () + let make_expect = fun n -> n in + expect_n_int program "foobar" make_expect let shared_function () : unit result = let%bind program = type_file "./contracts/function-shared.ligo" in - let%bind _inc = trace (simple_error "test inc") @@ - let aux n = - let open AST_Typed.Combinators in - let input = e_a_empty_int n in - let%bind result = easy_run_typed "inc" program input in - let expected = e_a_empty_int ( n + 1 ) in - AST_Typed.assert_value_eq (expected, result) - in - bind_list - @@ List.map aux - @@ [0 ; 2 ; 42 ; 163 ; -1] in - let%bind _double_inc = trace (simple_error "test double_inc") @@ - let aux n = - let open AST_Typed.Combinators in - let input = e_a_empty_int n in - let%bind result = easy_run_typed "double_inc" program input in - let expected = e_a_empty_int ( n + 2 ) in - AST_Typed.assert_value_eq (expected, result) - in - bind_list - @@ List.map aux - @@ [0 ; 2 ; 42 ; 163 ; -1] in - let%bind _foo = trace (simple_error "test foo") @@ - let aux n = - let open AST_Typed.Combinators in - let input = e_a_empty_int n in - let%bind result = easy_run_typed "foo" program input in - let expected = e_a_empty_int ( 2 * n + 3 ) in - AST_Typed.assert_value_eq (expected, result) - in - bind_list - @@ List.map aux - @@ [0 ; 2 ; 42 ; 163 ; -1] in + let%bind () = + let make_expect = fun n -> (n + 1) in + expect_n_int program "inc" make_expect + in + let%bind () = + let make_expect = fun n -> (n + 2) in + expect_n_int program "double_inc" make_expect + in + let%bind () = + let make_expect = fun n -> (2 * n + 3) in + expect_n_int program "foo" make_expect + in ok () let bool_expression () : unit result = let%bind program = type_file "./contracts/boolean_operators.ligo" in - let aux (name, f) = - let aux b = - let open AST_Typed.Combinators in - let input = e_a_empty_bool b in - let%bind result = easy_run_typed name program input in - let%bind result' = - trace (simple_error "bad result") @@ - get_a_bool result in - Assert.assert_equal_bool (f b) result' - in - let%bind _ = bind_list - @@ List.map aux [true;false] in - ok () - in - let%bind _ = bind_list - @@ List.map aux - @@ [ + let%bind _ = + let aux (name , f) = expect_b_bool program name f in + bind_map_list aux [ ("or_true", fun b -> b || true) ; ("or_false", fun b -> b || false) ; ("and_true", fun b -> b && true) ; @@ -153,64 +66,32 @@ let bool_expression () : unit result = let arithmetic () : unit result = let%bind program = type_file "./contracts/arithmetic.ligo" in - let aux (name, f) = - let aux n = - let open AST_Typed.Combinators in - let input = if name = "int_op" then e_a_empty_nat n else e_a_empty_int n in - let%bind result = easy_run_typed name program input in - AST_Typed.assert_value_eq (f n, result) - in - let%bind _ = bind_list - @@ List.map aux [0 ; 42 ; 128] in - ok () - in let%bind _ = - let open AST_Typed.Combinators in - bind_list - @@ List.map aux - @@ [ - ("plus_op", fun n -> e_a_empty_int (n + 42)) ; - ("minus_op", fun n -> e_a_empty_int (n - 42)) ; - ("times_op", fun n -> e_a_empty_int (n * 42)) ; - ("int_op", fun n -> e_a_empty_int n) ; + let aux (name , f) = expect_n_int program name f in + bind_map_list aux [ + ("plus_op", fun n -> (n + 42)) ; + ("minus_op", fun n -> (n - 42)) ; + ("times_op", fun n -> (n * 42)) ; ] in + let%bind () = expect_n_pos program "int_op" e_a_nat e_a_int in ok () let unit_expression () : unit result = let%bind program = type_file "./contracts/unit.ligo" in - let open AST_Typed.Combinators in - let%bind result = easy_evaluate_typed "u" program in - let%bind () = - trace (simple_error "result isn't unit") @@ - get_a_unit result in - ok () + expect_evaluate program "u" e_a_unit let include_ () : unit result = let%bind program = type_file "./contracts/includer.ligo" in - let%bind result = easy_evaluate_typed "bar" program in - let%bind n = - trace (simple_error "Include failed") @@ - AST_Typed.Combinators.get_a_int result in - Assert.assert_equal_int 144 n + expect_evaluate program "bar" (e_a_int 144) let record_ez_int names n = - let open AST_Typed.Combinators in - ez_e_a_empty_record @@ List.map (fun x -> x, e_a_empty_int n) names + ez_e_a_record @@ List.map (fun x -> x, e_a_int n) names let multiple_parameters () : unit result = let%bind program = type_file "./contracts/multiple-parameters.ligo" in - let inputs = [0 ; 2 ; 42 ; 163 ; -1] in - let aux (name, input_f, output_f) = - let aux n = - let input = input_f n in - let%bind result = easy_run_typed name program input in - let%bind result' = AST_Typed.Combinators.get_a_int result in - let expected = output_f n in - let%bind _ = Assert.assert_equal_int expected result' in - ok () - in - let%bind _ = bind_list @@ List.map aux inputs in - ok () + let aux ((name : string) , make_input , make_output) = + let make_output' = fun n -> e_a_int @@ make_output n in + expect_n program name make_input make_output' in let%bind _ = bind_list @@ List.map aux [ ("ab", record_ez_int ["a";"b"], fun n -> 2 * n) ; @@ -221,336 +102,192 @@ let multiple_parameters () : unit result = let record () : unit result = let%bind program = type_file "./contracts/record.ligo" in - let%bind _foobar = - let%bind result = easy_evaluate_typed "fb" program in - let expect = record_ez_int ["foo";"bar"] 0 in - AST_Typed.assert_value_eq (expect, result) + let%bind () = + let expected = record_ez_int ["foo" ; "bar"] 0 in + expect_evaluate program "fb" expected in - let%bind _projection = - let aux n = - let input = record_ez_int ["foo";"bar"] n in - let%bind result = easy_run_typed "projection" program input in - let expect = AST_Typed.Combinators.e_a_empty_int (2 * n) in - AST_Typed.assert_value_eq (expect, result) - in - bind_list @@ List.map aux [0 ; -42 ; 144] + let%bind () = + let make_input = record_ez_int ["foo" ; "bar"] in + let make_expected = fun n -> e_a_int (2 * n) in + expect_n program "projection" make_input make_expected in - let%bind _big = - let%bind result = easy_evaluate_typed "br" program in - let expect = record_ez_int ["a";"b";"c";"d";"e"] 23 in - AST_Typed.assert_value_eq (expect, result) + let%bind () = + let expected = record_ez_int ["a";"b";"c";"d";"e"] 23 in + expect_evaluate program "br" expected in ok () let tuple () : unit result = let%bind program = type_file "./contracts/tuple.ligo" in let ez n = - let open AST_Typed.Combinators in - e_a_empty_tuple (List.map e_a_empty_int n) in - let%bind _foobar = - trace (simple_error "foobar") ( - let%bind result = easy_evaluate_typed "fb" program in - let expect = ez [0 ; 0] in - AST_Typed.assert_value_eq (expect, result) - ) + e_a_tuple (List.map e_a_int n) in + let%bind () = + let expected = ez [0 ; 0] in + expect_evaluate program "fb" expected in - let%bind _projection = trace (simple_error "projection") ( - let aux n = - let input = ez [n ; n] in - let%bind result = easy_run_typed "projection" program input in - let expect = AST_Typed.Combinators.e_a_empty_int (2 * n) in - AST_Typed.assert_value_eq (expect, result) - in - bind_list @@ List.map aux [0 ; -42 ; 144] - ) + let%bind () = + let make_input = fun n -> ez [n ; n] in + let make_expected = fun n -> e_a_int (2 * n) in + expect_n program "projection" make_input make_expected in - let%bind _big = - let%bind result = easy_evaluate_typed "br" program in - let expect = ez [23 ; 23 ; 23 ; 23 ; 23] in - AST_Typed.assert_value_eq (expect, result) + let%bind () = + let expected = ez [23 ; 23 ; 23 ; 23 ; 23] in + expect_evaluate program "br" expected in ok () let option () : unit result = let%bind program = type_file "./contracts/option.ligo" in - let open AST_Typed.Combinators in - let%bind _some = trace (simple_error "some") @@ - let%bind result = easy_evaluate_typed "s" program in - let expect = e_a_empty_some (e_a_empty_int 42) in - AST_Typed.assert_value_eq (expect, result) + let%bind () = + let expected = e_a_some (e_a_int 42) in + expect_evaluate program "s" expected in - let%bind _none = trace (simple_error "none") @@ - let%bind result = easy_evaluate_typed "n" program in - let expect = e_a_empty_none (t_int ()) in - AST_Typed.assert_value_eq (expect, result) + let%bind () = + let expected = e_a_none t_int in + expect_evaluate program "n" expected in ok () let map () : unit result = let%bind program = type_file "./contracts/map.ligo" in let ez lst = - let open AST_Typed.Combinators in - let lst' = List.map (fun (x, y) -> e_a_empty_int x, e_a_empty_int y) lst in - e_a_empty_map lst' (t_int ()) (t_int ()) + let open Ast_simplified.Combinators in + let lst' = List.map (fun (x, y) -> e_a_int x, e_a_int y) lst in + e_a_map lst' t_int t_int in - let%bind _get_force = trace (simple_error "get_force") @@ - let aux n = - let input = ez [(23, n) ; (42, 4)] in - let%bind result = easy_run_typed "gf" program input in - let expect = AST_Typed.Combinators.(e_a_empty_int n) in - AST_Typed.assert_value_eq (expect, result) + let%bind () = + let make_input = fun n -> ez [(23, n) ; (42, 4)] in + let make_expected = e_a_int in + expect_n program "gf" make_input make_expected + in + let%bind () = + let make_input = fun n -> ez List.(map (fun x -> (x, x)) @@ range n) in + let make_expected = e_a_nat in + expect_n_strict_pos_small program "size_" make_input make_expected + in + let%bind () = + let expected = ez [(23, 0) ; (42, 0)] in + expect_evaluate program "fb" expected + in + let%bind () = + let make_input = fun n -> + let m = ez [(23 , 0) ; (42 , 0)] in + e_a_tuple [(e_a_int n) ; m] in - bind_map_list aux [0 ; 42 ; 51 ; 421 ; -3] + let make_expected = fun n -> ez [(23 , n) ; (42 , 0)] in + expect_n_pos_small program "set_" make_input make_expected in - let%bind _size = trace (simple_error "size") @@ - let aux n = - let input = ez List.(map (fun x -> (x, x)) @@ range n) in - let%bind result = easy_run_typed "size_" program input in - let expect = AST_Typed.Combinators.(e_a_empty_nat n) in - AST_Typed.assert_value_eq (expect, result) - in - bind_map_list aux [1 ; 10 ; 3] + let%bind () = + let make_input = fun n -> ez [(23, n) ; (42, 4)] in + let make_expected = fun _ -> e_a_some @@ e_a_int 4 in + expect_n program "get" make_input make_expected in - let%bind _foobar = trace (simple_error "foobar") @@ - let%bind result = easy_evaluate_typed "fb" program in - let expect = ez [(23, 0) ; (42, 0)] in - AST_Typed.assert_value_eq (expect, result) + let%bind () = + let expected = ez @@ List.map (fun x -> (x, 23)) [144 ; 51 ; 42 ; 120 ; 421] in + expect_evaluate program "bm" expected in - let%bind _set = trace (simple_error "set") @@ - let aux n = - let input = - let m = ez [(23, 0) ; (42, 0)] in - AST_Typed.Combinators.(e_a_empty_tuple [ e_a_empty_int n ; m ]) - in - let%bind result = easy_run_typed "set_" program input in - let expect = ez [(23, n) ; (42, 0)] in - AST_Typed.assert_value_eq (expect, result) - in - bind_map_list aux [1 ; 10 ; 3] - in - let%bind _get = trace (simple_error "get") @@ - let aux n = - let input = ez [(23, n) ; (42, 4)] in - let%bind result = easy_run_typed "get" program input in - let expect = AST_Typed.Combinators.(e_a_empty_some @@ e_a_empty_int 4) in - AST_Typed.assert_value_eq (expect, result) - in - bind_map_list aux [0 ; 42 ; 51 ; 421 ; -3] - in - let%bind _bigmap = trace (simple_error "bigmap") @@ - let%bind result = easy_evaluate_typed "bm" program in - let expect = ez @@ List.map (fun x -> (x, 23)) [144 ; 51 ; 42 ; 120 ; 421] in - AST_Typed.assert_value_eq (expect, result) - in - let%bind _remove = trace (simple_error "rm") @@ + let%bind () = let input = ez [(23, 23) ; (42, 42)] in - let%bind result = easy_run_typed "rm" program input in - let expect = ez [23, 23] in - AST_Typed.assert_value_eq (expect, result) + let expected = ez [23, 23] in + expect program "rm" input expected in ok () let list () : unit result = let%bind program = type_file "./contracts/list.ligo" in let ez lst = - let open AST_Typed.Combinators in - let lst' = List.map e_a_empty_int lst in - e_a_empty_list lst' (t_int ()) + let lst' = List.map e_a_int lst in + e_a_list lst' t_int in - let%bind _size = trace (simple_error "size") @@ - let aux n = - let input = ez (List.range n) in - let%bind result = easy_run_typed "size_" program input in - let expect = AST_Typed.Combinators.(e_a_empty_nat n) in - AST_Typed.assert_value_eq (expect, result) - in - bind_map_list aux [1 ; 10 ; 3] + let%bind () = + let make_input = fun n -> (ez @@ List.range n) in + let make_expected = e_a_nat in + expect_n_strict_pos_small program "size_" make_input make_expected in - let%bind _foobar = trace (simple_error "foobar") @@ - let%bind result = easy_evaluate_typed "fb" program in - let expect = ez [23 ; 42] in - AST_Typed.assert_value_eq (expect, result) + let%bind () = + let expected = ez [23 ; 42] in + expect_evaluate program "fb" expected in - let%bind _biglist = trace (simple_error "biglist") @@ - let%bind result = easy_evaluate_typed "bl" program in - let expect = ez [144 ; 51 ; 42 ; 120 ; 421] in - AST_Typed.assert_value_eq (expect, result) + let%bind () = + let expected = ez [144 ; 51 ; 42 ; 120 ; 421] in + expect_evaluate program "bl" expected in ok () let condition () : unit result = let%bind program = type_file "./contracts/condition.ligo" in - let aux n = - let open AST_Typed.Combinators in - let input = e_a_empty_int n in - let%bind result = easy_run_main_typed program input in - let%bind result' = - trace (simple_error "bad result") @@ - get_a_int result in - Assert.assert_equal_int (if n = 2 then 42 else 0) result' - in - let%bind _ = bind_list - @@ List.map aux - @@ [0 ; 2 ; 42 ; 163 ; -1] in - ok () + let make_input = e_a_int in + let make_expected = fun n -> e_a_int (if n = 2 then 42 else 0) in + expect_n program "main" make_input make_expected let loop () : unit result = let%bind program = type_file "./contracts/loop.ligo" in - let%bind _dummy = trace (simple_error "dummy") @@ - let aux n = - let open AST_Typed.Combinators in - let input = e_a_empty_nat n in - let%bind result = easy_run_typed "dummy" program input in - let expected = e_a_empty_nat n in - AST_Typed.assert_value_eq (expected, result) - in - let%bind _ = bind_list - @@ List.map aux - @@ [0 ; 2 ; 42 ; 163] in - ok () + let%bind () = + let make_input = e_a_nat in + let make_expected = e_a_nat in + expect_n_pos program "dummy" make_input make_expected in - let%bind _counter = trace (simple_error "counter") @@ - let aux n = - let open AST_Typed.Combinators in - let input = e_a_empty_nat n in - let%bind result = easy_run_typed "counter" program input in - let expected = e_a_empty_nat n in - AST_Typed.assert_value_eq (expected, result) - in - let%bind _ = bind_list - @@ List.map aux - @@ [0 ; 2 ; 42 ; 12] in - ok () + let%bind () = + let make_input = e_a_nat in + let make_expected = e_a_nat in + expect_n_pos_mid program "counter" make_input make_expected in - let%bind _sum = trace (simple_error "sum") @@ - let aux n = - let open AST_Typed.Combinators in - let input = e_a_empty_nat n in - let%bind result = easy_run_typed "sum" program input in - let expected = e_a_empty_nat (n * (n + 1) / 2) in - AST_Typed.assert_value_eq (expected, result) - in - let%bind _ = bind_list - @@ List.map aux - @@ [0 ; 2 ; 42 ; 12] in - ok () + let%bind () = + let make_input = e_a_nat in + let make_expected = fun n -> e_a_nat (n * (n + 1) / 2) in + expect_n_pos_mid program "sum" make_input make_expected in ok() let matching () : unit result = let%bind program = type_file "./contracts/match.ligo" in - let%bind _bool = - let aux n = - let open AST_Typed.Combinators in - let input = e_a_empty_int n in - let%bind result = easy_run_typed "match_bool" program input in - let%bind result' = - trace (simple_error "bad result") @@ - get_a_int result in - Assert.assert_equal_int (if n = 2 then 42 else 0) result' - in - let%bind _ = bind_list - @@ List.map aux - @@ [0 ; 2 ; 42 ; 163 ; -1] in - ok () + let%bind () = + let make_input = e_a_int in + let make_expected = fun n -> e_a_int (if n = 2 then 42 else 0) in + expect_n program "match_bool" make_input make_expected in - let%bind _expr_bool = - let aux n = - let open AST_Typed.Combinators in - let input = e_a_empty_int n in - let%bind result = easy_run_typed "match_expr_bool" program input in - let%bind result' = - trace (simple_error "bad result") @@ - get_a_int result in - Assert.assert_equal_int (if n = 2 then 42 else 0) result' - in - let%bind _ = bind_list - @@ List.map aux - @@ [0 ; 2 ; 42 ; 163 ; -1] in - ok () + let%bind () = + let make_input = e_a_int in + let make_expected = fun n-> e_a_int (if n = 2 then 42 else 0) in + expect_n program "match_expr_bool" make_input make_expected in - let%bind _option = + let%bind () = let aux n = - let open AST_Typed.Combinators in let input = match n with - | Some s -> e_a_empty_some (e_a_empty_int s) - | None -> e_a_empty_none (t_int ()) in - let%bind result = easy_run_typed "match_option" program input in - let%bind result' = - trace (simple_error "bad result") @@ - get_a_int result in - Assert.assert_equal_int 23 result' - (* Assert.assert_equal_int (match n with None -> 23 | Some s -> s) result' *) + | Some s -> e_a_some (e_a_int s) + | None -> e_a_none t_int in + let expected = e_a_int 23 in + expect program "match_option" input expected in - let%bind _ = bind_list - @@ List.map aux - @@ [Some 0 ; Some 2 ; Some 42 ; Some 163 ; Some (-1) ; None] in - ok () + bind_iter_list aux + [Some 0 ; Some 2 ; Some 42 ; Some 163 ; Some (-1) ; None] in ok () let declarations () : unit result = let%bind program = type_file "./contracts/declarations.ligo" in - let aux n = - let open AST_Typed.Combinators in - let input = e_a_empty_int n in - let%bind result = easy_run_main_typed program input in - let%bind result' = - trace (simple_error "bad result") @@ - get_a_int result in - Assert.assert_equal_int (42 + n) result' - in - let%bind _ = bind_list - @@ List.map aux - @@ [0 ; 2 ; 42 ; 163 ; -1] in - ok () + let make_input = e_a_int in + let make_expected = fun n -> e_a_int (42 + n) in + expect_n program "main" make_input make_expected let quote_declaration () : unit result = let%bind program = type_file "./contracts/quote-declaration.ligo" in - let aux n = - let open AST_Typed.Combinators in - let input = e_a_empty_int n in - let%bind result = easy_run_main_typed program input in - let%bind result' = - trace (simple_error "bad result") @@ - get_a_int result in - Assert.assert_equal_int result' (42 + 2 * n) - in - let%bind _ = bind_list - @@ List.map aux - @@ [0 ; 2 ; 42 ; 163 ; -1] in - ok () + let make_input = e_a_int in + let make_expected = fun n -> e_a_int (42 + 2 * n) in + expect_n program "main" make_input make_expected let quote_declarations () : unit result = let%bind program = type_file "./contracts/quote-declarations.ligo" in - let aux n = - let open AST_Typed.Combinators in - let input = e_a_empty_int n in - let%bind result = easy_run_main_typed program input in - let%bind result' = - trace (simple_error "bad result") @@ - get_a_int result in - Assert.assert_equal_int result' (74 + 2 * n) - in - let%bind _ = bind_list - @@ List.map aux - @@ [0 ; 2 ; 42 ; 163 ; -1] in - ok () + let make_input = e_a_int in + let make_expected = fun n -> e_a_int (74 + 2 * n) in + expect_n program "main" make_input make_expected let counter_contract () : unit result = let%bind program = type_file "./contracts/counter.ligo" in - let aux n = - let open AST_Typed.Combinators in - let input = e_a_empty_pair (e_a_empty_int n) (e_a_empty_int 42) in - let%bind result = easy_run_main_typed program input in - let expected = e_a_empty_pair (e_a_empty_list [] (t_int ())) (e_a_empty_int (42 + n)) in - AST_Typed.assert_value_eq (result, expected) - in - let%bind _ = bind_list - @@ List.map aux - @@ [0 ; 2 ; 42 ; 163 ; -1] in - ok () + let make_input = fun n-> e_a_pair (e_a_int n) (e_a_int 42) in + let make_expected = fun n -> e_a_pair (e_a_list [] (t_int)) (e_a_int (42 + n)) in + expect_n program "main" make_input make_expected let main = "Integration (End to End)", [ test "function" function_ ; diff --git a/src/ligo/test/test_helpers.ml b/src/ligo/test/test_helpers.ml index e2aac4135..bb266e469 100644 --- a/src/ligo/test/test_helpers.ml +++ b/src/ligo/test/test_helpers.ml @@ -10,3 +10,55 @@ let test name f = | Errors errs -> Format.printf "Errors : {\n%a}\n%!" errors_pp (List.rev (List.rev_map (fun f -> f ()) errs)) ; raise Alcotest.Test_error + +open Ast_simplified.Combinators + +let expect program entry_point input expected = + let error = + let title () = "expect run" in + let content () = Format.asprintf "Entry_point: %s" entry_point in + error title content in + trace error @@ + let%bind result = Ligo.easy_run_typed_simplified entry_point program input in + Ast_simplified.assert_value_eq (expected , result) + +let expect_evaluate program entry_point expected = + let error = + let title () = "expect evaluate" in + let content () = Format.asprintf "Entry_point: %s" entry_point in + error title content in + trace error @@ + let%bind result = Ligo.easy_evaluate_typed_simplified entry_point program in + Ast_simplified.assert_value_eq (expected , result) + +let expect_n_aux lst program entry_point make_input make_expected = + let aux n = + let input = make_input n in + let expected = make_expected n in + expect program entry_point input expected + in + let%bind _ = bind_map_list aux lst in + ok () + +let expect_n = expect_n_aux [0 ; 2 ; 42 ; 163 ; -1] +let expect_n_pos = expect_n_aux [0 ; 2 ; 42 ; 163] +let expect_n_strict_pos = expect_n_aux [2 ; 42 ; 163] +let expect_n_pos_small = expect_n_aux [0 ; 2 ; 10] +let expect_n_strict_pos_small = expect_n_aux [2 ; 10] +let expect_n_pos_mid = expect_n_aux [0 ; 2 ; 10 ; 33] + +let expect_b program entry_point make_expected = + let aux b = + let input = e_a_bool b in + let expected = make_expected b in + expect program entry_point input expected + in + let%bind _ = bind_map_list aux [false ; true] in + ok () + +let expect_n_int a b c = + expect_n a b e_a_int (fun n -> e_a_int (c n)) + +let expect_b_bool a b c = + let open Ast_simplified.Combinators in + expect_b a b (fun bool -> e_a_bool (c bool))