diff --git a/src/test/contracts/annotation.ligo b/src/test/contracts/annotation.ligo index 1eaef7b0c..7f5e969f4 100644 --- a/src/test/contracts/annotation.ligo +++ b/src/test/contracts/annotation.ligo @@ -1,3 +1,5 @@ +(* Test that a string is cast to an address given a type annotation *) + const lst : list(int) = list [] ; const address : address = ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address) ; diff --git a/src/test/contracts/arithmetic.mligo b/src/test/contracts/arithmetic.mligo index 3dd91648a..e3bb8ba07 100644 --- a/src/test/contracts/arithmetic.mligo +++ b/src/test/contracts/arithmetic.mligo @@ -1,11 +1,30 @@ // Test CameLIGO arithmetic operators +let mod_op (n : int) : nat = + n mod 42 + +let plus_op (n : int) : int = + n + 42 + +let minus_op (n : int) : int = + n - 42 + +let times_op (n : int) : int = + n * 42 + +let div_op (n : int) : int = + n / 2 + +(* TODO (?): Support conversion from nat to int and back + +let int_op (n : nat) : int = + Int n + +*) + let neg_op (n : int) : int = -n let foo (n : int) : int = n + 10 let neg_op_2 (b: int) : int = -(foo b) - - - diff --git a/src/test/contracts/boolean_operators.mligo b/src/test/contracts/boolean_operators.mligo new file mode 100644 index 000000000..7eb3cfd69 --- /dev/null +++ b/src/test/contracts/boolean_operators.mligo @@ -0,0 +1,16 @@ +// Test CameLIGO boolean operators + +let or_true (b : bool) : bool = + b || true + +let or_false (b : bool) : bool = + b || false + +let and_true (b : bool) : bool = + b && true + +let and_false (b : bool) : bool = + b && false + +let not_bool (b: bool) : bool = + not b diff --git a/src/test/contracts/condition.mligo b/src/test/contracts/condition.mligo index 334ea46b0..3d9fdd0c6 100644 --- a/src/test/contracts/condition.mligo +++ b/src/test/contracts/condition.mligo @@ -1,3 +1,5 @@ +// Test if conditional in CameLIGO + let%entry main (i : int) = if i = 2 then 42 diff --git a/src/test/contracts/function-shared.mligo b/src/test/contracts/function-shared.mligo new file mode 100644 index 000000000..5fc3e0b29 --- /dev/null +++ b/src/test/contracts/function-shared.mligo @@ -0,0 +1,7 @@ +(* Test use of multiple subroutines in a CameLIGO function *) + +let foo (i: int) : int = i + 20 + +let bar (i: int) : int = i + 50 + +let foobar (i: int) : int = (foo i) + (bar i) diff --git a/src/test/contracts/match.mligo b/src/test/contracts/match.mligo index 1665e9f27..394925538 100644 --- a/src/test/contracts/match.mligo +++ b/src/test/contracts/match.mligo @@ -11,3 +11,18 @@ let%entry main (p : param) storage = Add n -> n | Sub n -> 0-n) in (([] : operation list), storage) + +let match_bool (b: bool) : int = + match b with + true -> 10 + | false -> 0 + +let match_list (l: int list) : int = + match l with + hd :: tl -> hd + | [] -> 10 + +let match_option (i : int option) : int = + match i with + Some n -> n + | None -> 0 diff --git a/src/test/contracts/multiple-parameters.mligo b/src/test/contracts/multiple-parameters.mligo new file mode 100644 index 000000000..5a6e51297 --- /dev/null +++ b/src/test/contracts/multiple-parameters.mligo @@ -0,0 +1,4 @@ +(* Test function with several parameters *) + +let abcde (a : int) (b : int) (c : int) (d : int) (e : int) : int = + (c + e + 3) diff --git a/src/test/contracts/set_arithmetic-1.mligo b/src/test/contracts/set_arithmetic-1.mligo new file mode 100644 index 000000000..811b5b7af --- /dev/null +++ b/src/test/contracts/set_arithmetic-1.mligo @@ -0,0 +1,6 @@ +// Test set iteration + +let aggregate (i : int) (j : int) : int = i + j + +let fold_op (s : int set) : int = + Set.fold s 15 aggregate diff --git a/src/test/contracts/set_arithmetic.mligo b/src/test/contracts/set_arithmetic.mligo index 23947077d..86d81fe67 100644 --- a/src/test/contracts/set_arithmetic.mligo +++ b/src/test/contracts/set_arithmetic.mligo @@ -1,4 +1,24 @@ (* Test set operations in CameLIGO *) +let add_op (s : string set) : string set = + Set.add "foobar" s + +let remove_op (s : string set) : string set = + Set.remove "foobar" s + +let remove_deep (s : string set * nat) : string set * nat = + Set.remove "foobar" s.(0) + +(* +let patch_op (s: string set) : string set = + begin patch s with set ["foobar"]; end with s + +let patch_op_deep (s: string set * nat) : string set * nat = + begin patch s.0 with set ["foobar"]; end with s +*) + +let mem_op (s : string set) : bool = + Set.mem "foobar" s + let size_op (s: string set) : nat = Set.size s diff --git a/src/test/contracts/string_arithmetic.mligo b/src/test/contracts/string_arithmetic.mligo index 89d77ff60..1e1db9750 100644 --- a/src/test/contracts/string_arithmetic.mligo +++ b/src/test/contracts/string_arithmetic.mligo @@ -1,4 +1,10 @@ (* Test that the string concatenation syntax in CameLIGO works *) +let size_op (s : string) : nat = + String.size s + +let slice_op (s : string) : string = + String.slice 1p 2p s + let concat_syntax (s: string) = s ^ "test_literal" diff --git a/src/test/contracts/tuple.mligo b/src/test/contracts/tuple.mligo new file mode 100644 index 000000000..178ecfe7b --- /dev/null +++ b/src/test/contracts/tuple.mligo @@ -0,0 +1,15 @@ +type abc = int * int * int + +let projection_abc (tpl : abc) : int = + tpl.(1) + +type foobar = int * int + +let fb : foobar = (0, 0) + +let projection (tpl : foobar) : int = + tpl.(0) + tpl.(1) + +type big_tuple = int * int * int * int * int + +let br : big_tuple = (23, 23, 23, 23, 23) diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index cbd750f1b..a77ff0bd3 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -150,6 +150,14 @@ let shared_function () : unit result = in ok () +let shared_function_mligo () : unit result = + let%bind program = mtype_file "./contracts/function-shared.mligo" in + let%bind () = + let make_expect = fun n -> (2 * n + 70) in + expect_eq_n_int program "foobar" make_expect + in + ok () + let bool_expression () : unit result = let%bind program = type_file "./contracts/boolean_operators.ligo" in let%bind _ = @@ -163,6 +171,19 @@ let bool_expression () : unit result = ] in ok () +let bool_expression_mligo () : unit result = + let%bind program = mtype_file "./contracts/boolean_operators.mligo" in + let%bind _ = + let aux (name, f) = expect_eq_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) ; + ("and_false", fun b -> b && false) ; + ("not_bool", fun b -> not b) ; + ] in + ok () + let arithmetic () : unit result = let%bind program = type_file "./contracts/arithmetic.ligo" in let%bind _ = @@ -181,11 +202,16 @@ let arithmetic () : unit result = let arithmetic_mligo () : unit result = let%bind program = mtype_file "./contracts/arithmetic.mligo" in let%bind _ = - let aux (name , f) = expect_eq_n_int program name f in + let aux (name, f) = expect_eq_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)) ; ("neg_op", fun n -> (-n)) ; ("neg_op_2", fun n -> -(n + 10)) ; ] in + let%bind () = expect_eq_n_pos program "mod_op" e_int (fun n -> e_nat (n mod 42)) in + let%bind () = expect_eq_n_pos program "div_op" e_int (fun n -> e_int (n / 2)) in ok () let bitwise_arithmetic () : unit result = @@ -231,6 +257,9 @@ let string_arithmetic () : unit result = let string_arithmetic_mligo () : unit result = let%bind program = mtype_file "./contracts/string_arithmetic.mligo" in + let%bind () = expect_eq program "size_op" (e_string "tata") (e_nat 4) in + let%bind () = expect_eq program "slice_op" (e_string "tata") (e_string "at") in + let%bind () = expect_eq program "slice_op" (e_string "foo") (e_string "oo") in let%bind () = expect_eq program "concat_syntax" (e_string "string_") (e_string "string_test_literal") in ok () @@ -319,10 +348,33 @@ let set_arithmetic () : unit result = let set_arithmetic_mligo () : unit result = let%bind program = mtype_file "./contracts/set_arithmetic.mligo" in + let%bind program_1 = type_file "./contracts/set_arithmetic-1.ligo" in let%bind () = expect_eq program "size_op" (e_set [e_string "foo"; e_string "bar"; e_string "foobar"]) - (e_nat 3) in ok () + (e_nat 3) in + let%bind () = + expect_eq program "add_op" + (e_set [e_string "foo" ; e_string "bar"]) + (e_set [e_string "foo" ; e_string "bar" ; e_string "foobar"]) in + let%bind () = + expect_eq program "add_op" + (e_set [e_string "foo" ; e_string "bar" ; e_string "foobar"]) + (e_set [e_string "foo" ; e_string "bar" ; e_string "foobar"]) in + let%bind () = + expect_eq program "remove_op" + (e_set [e_string "foo" ; e_string "bar"]) + (e_set [e_string "foo" ; e_string "bar"]) in + let%bind () = + expect_eq program "remove_op" + (e_set [e_string "foo" ; e_string "bar" ; e_string "foobar"]) + (e_set [e_string "foo" ; e_string "bar"]) in + let%bind () = + expect_eq program_1 "fold_op" + (e_set [ e_int 4 ; e_int 10 ]) + (e_int 29) + in + ok () let unit_expression () : unit result = let%bind program = type_file "./contracts/unit.ligo" in @@ -356,6 +408,18 @@ let multiple_parameters () : unit result = ] in ok () +let multiple_parameters_mligo () : unit result = + let%bind program = mtype_file "./contracts/multiple-parameters.mligo" in + let aux ((name : string) , make_input , make_output) = + let make_output' = fun n -> e_int @@ make_output n in + expect_eq_n program name make_input make_output' + in + let%bind _ = bind_list @@ List.map aux [ + (* Didn't include the other tests because they're probably not necessary *) + ("abcde", tuple_ez_int ["a";"b";"c";"d";"e"], fun n -> 2 * n + 3) ; + ] in + ok () + let record () : unit result = let%bind program = type_file "./contracts/record.ligo" in let%bind () = @@ -427,6 +491,30 @@ let tuple () : unit result = in ok () +let tuple_mligo () : unit result = + let%bind program = mtype_file "./contracts/tuple.mligo" in + let ez n = + e_tuple (List.map e_int n) in + let%bind () = + let expected = ez [0 ; 0] in + expect_eq_evaluate program "fb" expected + in + let%bind () = + let make_input = fun n -> ez [n ; n] in + let make_expected = fun n -> e_int (2 * n) in + expect_eq_n program "projection" make_input make_expected + in + let%bind () = + let make_input = fun n -> ez [n ; 2 * n ; n] in + let make_expected = fun n -> e_int (2 * n) in + expect_eq_n program "projection_abc" make_input make_expected + in + let%bind () = + let expected = ez [23 ; 23 ; 23 ; 23 ; 23] in + expect_eq_evaluate program "br" expected + in + ok () + let option () : unit result = let%bind program = type_file "./contracts/option.ligo" in let%bind () = @@ -888,11 +976,33 @@ let let_in_mligo () : unit result = let match_variant () : unit result = let%bind program = mtype_file "./contracts/match.mligo" in - let make_input n = - e_pair (e_constructor "Sub" (e_int n)) (e_int 3) in - let make_expected n = - e_pair (e_typed_list [] t_operation) (e_int (3-n)) - in expect_eq_n program "main" make_input make_expected + let%bind () = + let make_input n = + e_pair (e_constructor "Sub" (e_int n)) (e_int 3) in + let make_expected n = + e_pair (e_typed_list [] t_operation) (e_int (3-n)) + in expect_eq_n program "main" make_input make_expected in + let%bind () = + let input = e_bool true in + let expected = e_int 10 in + expect_eq program "match_bool" input expected in + let%bind () = + let input = e_bool false in + let expected = e_int 0 in + expect_eq program "match_bool" input expected in + let%bind () = + let input = e_list [e_int 3] in + let expected = e_int 3 in + expect_eq program "match_list" input expected in + let%bind () = + let input = e_typed_list [] t_int in + let expected = e_int 10 in + expect_eq program "match_list" input expected in + let%bind () = + let make_input n = e_some (e_int n) in + let make_expected n = e_int n in + expect_eq_n program "match_option" make_input make_expected in + ok () let match_matej () : unit result = let%bind program = mtype_file "./contracts/match_bis.mligo" in @@ -1006,11 +1116,13 @@ let main = test_suite "Integration (End to End)" [ test "complex function" complex_function ; test "closure" closure ; test "shared function" shared_function ; + test "shared function (mligo)" shared_function_mligo ; test "higher order" higher_order ; test "variant" variant ; test "variant (mligo)" variant_mligo ; test "variant matching" variant_matching ; test "tuple" tuple ; + test "tuple (mligo)" tuple_mligo ; test "record" record ; test "condition simple" condition_simple ; test "condition (ligo)" condition ; @@ -1018,7 +1130,9 @@ let main = test_suite "Integration (End to End)" [ test "shadow" shadow ; test "annotation" annotation ; test "multiple parameters" multiple_parameters ; + test "multiple parameters (mligo)" multiple_parameters_mligo ; test "bool" bool_expression ; + test "bool (mligo)" bool_expression_mligo ; test "arithmetic" arithmetic ; test "arithmetic (mligo)" arithmetic_mligo ; test "bitwise_arithmetic" bitwise_arithmetic ;