From 606f7ca907331def3c5b7e322042e5c2a2b1a2ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Georges=20Dup=C3=A9ron?= Date: Tue, 11 Jun 2019 01:27:59 +0200 Subject: [PATCH] More tests, integration of some of the operators --- src/contracts/condition-annot.mligo | 5 +++++ src/contracts/condition-shadowing.mligo | 9 +++++++++ src/contracts/condition.mligo | 5 +++++ src/contracts/fibo.mligo | 12 ++++++++++++ src/passes/operators/operators.ml | 18 +++++++++++++++++ src/test/integration_tests.ml | 26 +++++++++++++++++++++++-- 6 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 src/contracts/condition-annot.mligo create mode 100644 src/contracts/condition-shadowing.mligo create mode 100644 src/contracts/condition.mligo create mode 100644 src/contracts/fibo.mligo diff --git a/src/contracts/condition-annot.mligo b/src/contracts/condition-annot.mligo new file mode 100644 index 000000000..b5b87ef68 --- /dev/null +++ b/src/contracts/condition-annot.mligo @@ -0,0 +1,5 @@ +let%entry main (i : int) = + if (i = 2 : bool) then + (42 : int) + else + (0 : int) diff --git a/src/contracts/condition-shadowing.mligo b/src/contracts/condition-shadowing.mligo new file mode 100644 index 000000000..099b9fd7c --- /dev/null +++ b/src/contracts/condition-shadowing.mligo @@ -0,0 +1,9 @@ +(* TODO : make a test using mutation, not shadowing *) +let%entry main (i : int) = + let result = 0 in + if i = 2 then + let result = 42 in + result + else + let result = 0 in + result diff --git a/src/contracts/condition.mligo b/src/contracts/condition.mligo new file mode 100644 index 000000000..334ea46b0 --- /dev/null +++ b/src/contracts/condition.mligo @@ -0,0 +1,5 @@ +let%entry main (i : int) = + if i = 2 then + 42 + else + 0 diff --git a/src/contracts/fibo.mligo b/src/contracts/fibo.mligo new file mode 100644 index 000000000..ee7e2df12 --- /dev/null +++ b/src/contracts/fibo.mligo @@ -0,0 +1,12 @@ +type storage = unit + +(* not supported yet +let%entry main (p:unit) storage = + (fun x -> ()) () +*) + +let%entry main (p:unit) storage = + (fun (f : int -> int -> int) (x : int) (y : int) -> f y (x + y)) + (fun (x : int) (y : int) -> x + y) + 0 + 1 diff --git a/src/passes/operators/operators.ml b/src/passes/operators/operators.ml index 15ed4928a..1842b620d 100644 --- a/src/passes/operators/operators.ml +++ b/src/passes/operators/operators.ml @@ -103,9 +103,20 @@ module Simplify = struct module Camligo = struct let constants = [ ("Bytes.pack" , "PACK") ; + + ("Map.remove" , "MAP_REMOVE") ; + ("Map.update" , "MAP_UPDATE") ; + ("Map.add" , "MAP_ADD") ; + ("Map.mem" , "MAP_MEM") ; + ("Map.find" , "MAP_FIND") ; + ("Map.fold" , "MAP_FOLD") ; + ("Map.map" , "MAP_MAP") ; + ("Crypto.hash" , "HASH") ; + ("Operation.transaction" , "CALL") ; ("Operation.get_contract" , "CONTRACT") ; + ("sender" , "SENDER") ; ("unit" , "UNIT") ; ("source" , "SOURCE") ; @@ -704,6 +715,13 @@ module Compiler = struct ("MAP_FIND_OPT" , simple_binary @@ prim I_GET) ; ("MAP_ADD" , simple_ternary @@ seq [dip (i_some) ; prim I_UPDATE]) ; ("MAP_UPDATE" , simple_ternary @@ prim I_UPDATE) ; + (* ("GET_CONTRACT" , simple_constant @@ prim I_CONTRACT) ; *) + (* ( "MAP_REMOVE" , simple_binary @@ seq [prim I_NONE TODO: + annotation ; prim I_UPDATE ]) ; *) + ( "MAP_MEM" , simple_binary @@ prim I_MEM) ; + (* ( "MAP_FOLD" , simple_ternary @@ prim TODO I_ITER?) ; *) + ( "MAP_MAP" , simple_binary @@ prim I_MAP) ; + (* ( "MAP_MAP_FOLD" , simple_ternary @@ prim TODO I_ITER?) ; *) + (* ( "MAP_ITER" , simple_binary @@ prim TODO I_ITER?) ; *) ("SIZE" , simple_unary @@ prim I_SIZE) ; ("FAILWITH" , simple_unary @@ prim I_FAILWITH) ; ("ASSERT_INFERRED" , simple_binary @@ i_if (seq [i_failwith]) (seq [i_drop ; i_push_unit])) ; diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index 8ee57494d..0de2e619c 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -533,6 +533,20 @@ let condition () : unit result = let make_expected = fun n -> e_int (if n = 2 then 42 else 0) in expect_eq_n program "main" make_input make_expected +let condition_mligo () : unit result = + let%bind _ = + let aux file = + let%bind program = mtype_file file in + let make_input = e_int in + let make_expected = fun n -> e_int (if n = 2 then 42 else 0) in + expect_eq_n program "main" make_input make_expected in + bind_map_list aux [ + "./contracts/condition.mligo"; + "./contracts/condition-shadowing.mligo"; + "./contracts/condition-annot.mligo"; + ] in + ok () + let condition_simple () : unit result = let%bind program = type_file "./contracts/condition-simple.ligo" in let make_input = e_int in @@ -794,6 +808,12 @@ let lambda2_mligo () : unit result = let make_expected = (e_unit ()) in expect_eq program "main" make_input make_expected +let fibo_mligo () : unit result = + let%bind program = mtype_file "./contracts/fibo.mligo" in + let make_input = e_pair (e_unit ()) (e_unit ()) in + let make_expected = (e_int 42) in + expect_eq program "main" make_input make_expected + let website1_ligo () : unit result = let%bind program = type_file "./contracts/website1.ligo" in let make_input = fun n-> e_pair (e_int n) (e_int 42) in @@ -851,7 +871,8 @@ let main = test_suite "Integration (End to End)" [ test "tuple" tuple ; test "record" record ; test "condition simple" condition_simple ; - test "condition" condition ; + test "condition (ligo)" condition ; + test "condition (mligo)" condition_mligo ; test "shadow" shadow ; test "annotation" annotation ; test "multiple parameters" multiple_parameters ; @@ -893,9 +914,10 @@ let main = test_suite "Integration (End to End)" [ (* test "guess string mligo" guess_string_mligo ; WIP? *) test "lambda mligo" lambda_mligo ; test "lambda ligo" lambda_ligo ; - (* test "lambda2 mligo" lambda2_mligo ; *) test "tez (ligo)" tez_ligo ; test "tez (mligo)" tez_mligo ; + test "lambda2 mligo" lambda2_mligo ; + (* test "fibo (mligo)" fibo_mligo ; *) test "website1 ligo" website1_ligo ; test "website2 ligo" website2_ligo ; test "website2 (mligo)" website2_mligo ;