From 0b50d94071f45b5c5610fefbff631d73d24ef091 Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Fri, 22 Nov 2019 02:22:29 -0800 Subject: [PATCH] Add higher order function test to CameLIGO --- src/test/contracts/high-order.mligo | 46 +++++++++++++++++++++++++++++ src/test/integration_tests.ml | 12 ++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/test/contracts/high-order.mligo diff --git a/src/test/contracts/high-order.mligo b/src/test/contracts/high-order.mligo new file mode 100644 index 000000000..4bba30eb3 --- /dev/null +++ b/src/test/contracts/high-order.mligo @@ -0,0 +1,46 @@ +(* Test a function which takes another function as an argument *) +let foobar (i : int) : int = + let foo: (int -> int) = + fun (i : int) -> i + in + let bar: ((int -> int) -> int) = + fun (f : int -> int) -> f i + in + bar foo + +(* higher order function with more than one argument *) +let higher2 (i: int) (f: int -> int): int = + let ii: int = f i in ii + +let foobar2 (i : int) : int = + let foo2: (int -> int) = + fun (i : int) -> i + in + higher2 i foo2 + +let a : int = 0 + +let foobar3 (i : int) : int = + let foo2: (int -> int) = + fun (i : int) -> a + i + in + higher2 i foo2 + +let f (i : int) : int = i + +let g (i : int) : int = f i + +let foobar4 (i : int) : int = g (g i) + +let higher3 (i: int) (f: int -> int) (g: int -> int) : int = + let ii: int = f (g i) in ii + +let foobar5 (i : int) : int = + let a : int = 0 in + let foo: (int -> int) = + fun (i : int) -> a + i + in + let goo: (int -> int) = + fun (i : int) -> foo i + in + higher3 i foo goo diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index 3217642e9..1a2a09ea2 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -158,6 +158,17 @@ let higher_order () : unit result = let%bind _ = expect_eq_n_int program "foobar5" make_expect in ok () +let higher_order_mligo () : unit result = + let%bind program = mtype_file "./contracts/high-order.mligo" in + let make_expect = fun n -> n in + let%bind _ = expect_eq_n_int program "foobar" make_expect in + let%bind _ = expect_eq_n_int program "foobar2" make_expect in + let%bind _ = expect_eq_n_int program "foobar3" make_expect in + let%bind _ = expect_eq_n_int program "foobar4" make_expect in + let%bind _ = expect_eq_n_int program "foobar5" make_expect in + ok () + + let shared_function () : unit result = let%bind program = type_file "./contracts/function-shared.ligo" in Format.printf "inc\n" ; @@ -1305,6 +1316,7 @@ let main = test_suite "Integration (End to End)" [ test "shared function" shared_function ; test "shared function (mligo)" shared_function_mligo ; test "higher order" higher_order ; + test "higher order (mligo)" higher_order_mligo ; test "variant" variant ; test "variant (mligo)" variant_mligo ; test "variant matching" variant_matching ;