From 8c37fe355d1d7915f6f1685256be2ae7ad4c0766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Georges=20Dup=C3=A9ron?= Date: Mon, 10 Jun 2019 21:39:43 +0200 Subject: [PATCH 1/4] test: contract for mligo version of the example on the website --- src/contracts/website2.mligo | 20 ++++++++++++++++++++ src/test/integration_tests.ml | 11 +++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/contracts/website2.mligo diff --git a/src/contracts/website2.mligo b/src/contracts/website2.mligo new file mode 100644 index 000000000..f972e9b47 --- /dev/null +++ b/src/contracts/website2.mligo @@ -0,0 +1,20 @@ +type storage = int + +(* variant defining pseudo multi-entrypoint actions *) + +type action = +| Increment of int +| Decrement of int + +let add (a: int) (b: int) : int = a + b + +let subtract (a: int) (b: int) : int = a - b + +(* real entrypoint that re-routes the flow based on the action provided *) + +let%entry main (p : action) storage = + let storage = + match p with + | Increment n -> add storage n + | Decrement n -> subtract storage n + in (([] : operation list), storage) diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index 16a4c7d69..8ee57494d 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -825,6 +825,16 @@ let tez_mligo () : unit result = let%bind _ = expect_eq_evaluate program "add_more_tez" (e_mutez 111111000) in ok () +let website2_mligo () : unit result = + let%bind program = mtype_file "./contracts/website2.mligo" in + let make_input = fun n -> + let action = if n mod 2 = 0 then "Increment" else "Decrement" in + e_pair (e_constructor action (e_int n)) (e_int 42) in + let make_expected = fun n -> + let op = if n mod 2 = 0 then (+) else (-) in + e_pair (e_typed_list [] t_operation) (e_int (op 42 n)) in + expect_eq_n program "main" make_input make_expected + let main = test_suite "Integration (End to End)" [ test "type alias" type_alias ; test "function" function_ ; @@ -888,4 +898,5 @@ let main = test_suite "Integration (End to End)" [ test "tez (mligo)" tez_mligo ; test "website1 ligo" website1_ligo ; test "website2 ligo" website2_ligo ; + test "website2 (mligo)" website2_mligo ; ] 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 2/4] 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 ; From 0e5c9802ec8cf0d1be93bf33c5104896273de9c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Georges=20Dup=C3=A9ron?= Date: Wed, 12 Jun 2019 00:50:48 +0200 Subject: [PATCH 3/4] More tests with lambdas --- src/contracts/fibo.mligo | 6 +----- src/contracts/fibo2.mligo | 7 +++++++ src/contracts/fibo3.mligo | 7 +++++++ src/contracts/fibo4.mligo | 6 ++++++ src/test/integration_tests.ml | 3 +++ 5 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 src/contracts/fibo2.mligo create mode 100644 src/contracts/fibo3.mligo create mode 100644 src/contracts/fibo4.mligo diff --git a/src/contracts/fibo.mligo b/src/contracts/fibo.mligo index ee7e2df12..efc437c30 100644 --- a/src/contracts/fibo.mligo +++ b/src/contracts/fibo.mligo @@ -1,12 +1,8 @@ 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 (f : (int * int) -> int) (x : int) (y : int) -> f (y, x)) (fun (x : int) (y : int) -> x + y) 0 1 diff --git a/src/contracts/fibo2.mligo b/src/contracts/fibo2.mligo new file mode 100644 index 000000000..1daa72adb --- /dev/null +++ b/src/contracts/fibo2.mligo @@ -0,0 +1,7 @@ +type storage = unit + +let%entry main (p:unit) storage = + (fun (f : int -> int) (x : int) (y : int) -> (f y)) + (fun (x : int) -> x) + 0 + 1 diff --git a/src/contracts/fibo3.mligo b/src/contracts/fibo3.mligo new file mode 100644 index 000000000..ebce6b862 --- /dev/null +++ b/src/contracts/fibo3.mligo @@ -0,0 +1,7 @@ +type storage = unit + +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/contracts/fibo4.mligo b/src/contracts/fibo4.mligo new file mode 100644 index 000000000..207d0c96c --- /dev/null +++ b/src/contracts/fibo4.mligo @@ -0,0 +1,6 @@ +type storage = unit + +let%entry main (p:unit) storage = + (fun (f : int -> int) (x : int) -> (f x)) + (fun (x : int) -> x) + 1 diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index 0de2e619c..3b6273f7f 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -918,6 +918,9 @@ let main = test_suite "Integration (End to End)" [ test "tez (mligo)" tez_mligo ; test "lambda2 mligo" lambda2_mligo ; (* test "fibo (mligo)" fibo_mligo ; *) + (* test "fibo2 (mligo)" fibo2_mligo ; *) + (* test "fibo3 (mligo)" fibo3_mligo ; *) + (* test "fibo4 (mligo)" fibo4_mligo ; *) test "website1 ligo" website1_ligo ; test "website2 ligo" website2_ligo ; test "website2 (mligo)" website2_mligo ; From e2ea89cf87132e8a9680c98d441f9493a7b5948f Mon Sep 17 00:00:00 2001 From: Tom Jack Date: Wed, 23 Oct 2019 09:37:51 -0500 Subject: [PATCH 4/4] Move tests, remove operators --- src/passes/operators/operators.ml | 18 ------------------ src/{ => test}/contracts/condition-annot.mligo | 0 .../contracts/condition-shadowing.mligo | 0 src/{ => test}/contracts/condition.mligo | 0 src/{ => test}/contracts/fibo.mligo | 0 src/{ => test}/contracts/fibo2.mligo | 0 src/{ => test}/contracts/fibo3.mligo | 0 src/{ => test}/contracts/fibo4.mligo | 0 src/{ => test}/contracts/for_fail.ligo | 0 src/{ => test}/contracts/incr_decr.mligo | 0 src/{ => test}/contracts/procedure.ligo | 0 src/{ => test}/contracts/website2.mligo | 0 12 files changed, 18 deletions(-) rename src/{ => test}/contracts/condition-annot.mligo (100%) rename src/{ => test}/contracts/condition-shadowing.mligo (100%) rename src/{ => test}/contracts/condition.mligo (100%) rename src/{ => test}/contracts/fibo.mligo (100%) rename src/{ => test}/contracts/fibo2.mligo (100%) rename src/{ => test}/contracts/fibo3.mligo (100%) rename src/{ => test}/contracts/fibo4.mligo (100%) rename src/{ => test}/contracts/for_fail.ligo (100%) rename src/{ => test}/contracts/incr_decr.mligo (100%) rename src/{ => test}/contracts/procedure.ligo (100%) rename src/{ => test}/contracts/website2.mligo (100%) diff --git a/src/passes/operators/operators.ml b/src/passes/operators/operators.ml index 1842b620d..15ed4928a 100644 --- a/src/passes/operators/operators.ml +++ b/src/passes/operators/operators.ml @@ -103,20 +103,9 @@ 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") ; @@ -715,13 +704,6 @@ 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/contracts/condition-annot.mligo b/src/test/contracts/condition-annot.mligo similarity index 100% rename from src/contracts/condition-annot.mligo rename to src/test/contracts/condition-annot.mligo diff --git a/src/contracts/condition-shadowing.mligo b/src/test/contracts/condition-shadowing.mligo similarity index 100% rename from src/contracts/condition-shadowing.mligo rename to src/test/contracts/condition-shadowing.mligo diff --git a/src/contracts/condition.mligo b/src/test/contracts/condition.mligo similarity index 100% rename from src/contracts/condition.mligo rename to src/test/contracts/condition.mligo diff --git a/src/contracts/fibo.mligo b/src/test/contracts/fibo.mligo similarity index 100% rename from src/contracts/fibo.mligo rename to src/test/contracts/fibo.mligo diff --git a/src/contracts/fibo2.mligo b/src/test/contracts/fibo2.mligo similarity index 100% rename from src/contracts/fibo2.mligo rename to src/test/contracts/fibo2.mligo diff --git a/src/contracts/fibo3.mligo b/src/test/contracts/fibo3.mligo similarity index 100% rename from src/contracts/fibo3.mligo rename to src/test/contracts/fibo3.mligo diff --git a/src/contracts/fibo4.mligo b/src/test/contracts/fibo4.mligo similarity index 100% rename from src/contracts/fibo4.mligo rename to src/test/contracts/fibo4.mligo diff --git a/src/contracts/for_fail.ligo b/src/test/contracts/for_fail.ligo similarity index 100% rename from src/contracts/for_fail.ligo rename to src/test/contracts/for_fail.ligo diff --git a/src/contracts/incr_decr.mligo b/src/test/contracts/incr_decr.mligo similarity index 100% rename from src/contracts/incr_decr.mligo rename to src/test/contracts/incr_decr.mligo diff --git a/src/contracts/procedure.ligo b/src/test/contracts/procedure.ligo similarity index 100% rename from src/contracts/procedure.ligo rename to src/test/contracts/procedure.ligo diff --git a/src/contracts/website2.mligo b/src/test/contracts/website2.mligo similarity index 100% rename from src/contracts/website2.mligo rename to src/test/contracts/website2.mligo