From 5c3d801c78e120e4ca1f619d9d9298896cf0d65d Mon Sep 17 00:00:00 2001 From: galfour Date: Fri, 19 Jul 2019 12:42:01 +0200 Subject: [PATCH] add bitwise arithmetic and string arithmetic tests --- src/compiler/compiler_program.ml | 2 -- src/contracts/bitwise_arithmetic.ligo | 8 ++++++++ src/contracts/string_arithmetic.ligo | 5 +++++ src/operators/operators.ml | 16 +++++++++++---- src/test/integration_tests.ml | 28 +++++++++++++++++++++++++++ src/test/test_helpers.ml | 13 ++++++++++++- 6 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 src/contracts/bitwise_arithmetic.ligo create mode 100644 src/contracts/string_arithmetic.ligo diff --git a/src/compiler/compiler_program.ml b/src/compiler/compiler_program.ml index f7f0f50a3..986b675a9 100644 --- a/src/compiler/compiler_program.ml +++ b/src/compiler/compiler_program.ml @@ -185,8 +185,6 @@ and translate_expression ?push_var_name (expr:expression) (env:environment) : (m | E_environment_return expr -> ( let%bind (expr' , env) = translate_expression ~push_var_name:"return_clause" expr env in let%bind (code , cleared_env) = Compiler_environment.clear env in - Format.printf "pre env %a\n" PP.environment env ; - Format.printf "post clean env %a\n" PP.environment cleared_env ; return ~end_env:cleared_env @@ seq [ expr' ; code ; diff --git a/src/contracts/bitwise_arithmetic.ligo b/src/contracts/bitwise_arithmetic.ligo new file mode 100644 index 000000000..0711b5854 --- /dev/null +++ b/src/contracts/bitwise_arithmetic.ligo @@ -0,0 +1,8 @@ +function or_op (const n : nat) : nat is + begin skip end with bitwise_or(n , 4n) + +function and_op (const n : nat) : nat is + begin skip end with bitwise_and(n , 7n) + +function xor_op (const n : nat) : nat is + begin skip end with bitwise_xor(n , 7n) diff --git a/src/contracts/string_arithmetic.ligo b/src/contracts/string_arithmetic.ligo new file mode 100644 index 000000000..c8ceacb01 --- /dev/null +++ b/src/contracts/string_arithmetic.ligo @@ -0,0 +1,5 @@ +function concat_op (const s : string) : string is + begin skip end with string_concat(s , "toto") + +function slice_op (const s : string) : string is + begin skip end with string_slice(1n , 2n , s) diff --git a/src/operators/operators.ml b/src/operators/operators.ml index 6a85aaa37..7f69f392b 100644 --- a/src/operators/operators.ml +++ b/src/operators/operators.ml @@ -69,6 +69,11 @@ module Simplify = struct ("source" , "SOURCE") ; ("sender" , "SENDER") ; ("failwith" , "FAILWITH") ; + ("bitwise_or" , "OR") ; + ("bitwise_and" , "AND") ; + ("bitwise_xor" , "XOR") ; + ("string_concat" , "CONCAT") ; + ("string_slice" , "SLICE") ; ] let type_constants = type_constants @@ -546,15 +551,17 @@ module Typer = struct sub ; none ; some ; + concat ; + slice ; comparator "EQ" ; comparator "NEQ" ; comparator "LT" ; comparator "GT" ; comparator "LE" ; comparator "GE" ; - boolean_operator_2 "OR" ; - boolean_operator_2 "AND" ; - boolean_operator_2 "XOR" ; + or_ ; + and_ ; + xor ; not_ ; map_remove ; map_add ; @@ -655,13 +662,14 @@ module Compiler = struct ("MAP_UPDATE" , simple_ternary @@ prim I_UPDATE) ; ("SET_MEM" , simple_binary @@ prim I_MEM) ; ("SET_ADD" , simple_binary @@ seq [dip (i_push (prim T_bool) (prim D_True)) ; prim I_UPDATE]) ; - ("SLICE" , simple_ternary @@ prim I_SLICE) ; + ("SLICE" , simple_ternary @@ seq [prim I_SLICE ; i_assert_some_msg (i_push_string "SLICE")]) ; ("SHA256" , simple_unary @@ prim I_SHA256) ; ("SHA512" , simple_unary @@ prim I_SHA512) ; ("BLAKE2B" , simple_unary @@ prim I_BLAKE2B) ; ("CHECK_SIGNATURE" , simple_ternary @@ prim I_CHECK_SIGNATURE) ; ("HASH_KEY" , simple_unary @@ prim I_HASH_KEY) ; ("PACK" , simple_unary @@ prim I_PACK) ; + ("CONCAT" , simple_binary @@ prim I_CONCAT) ; ] (* Some complex predicates will need to be added in compiler/compiler_program *) diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index 2f4af5adb..29235f2a7 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -127,12 +127,38 @@ let arithmetic () : unit result = ("plus_op", fun n -> (n + 42)) ; ("minus_op", fun n -> (n - 42)) ; ("times_op", fun n -> (n * 42)) ; + ("neg_op", fun n -> (-n)) ; ] in let%bind () = expect_eq_n_pos program "int_op" e_nat e_int 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 = + let%bind program = type_file "./contracts/bitwise_arithmetic.ligo" in + let%bind () = expect_eq program "or_op" (e_nat 7) (e_nat 7) in + let%bind () = expect_eq program "or_op" (e_nat 3) (e_nat 7) in + let%bind () = expect_eq program "or_op" (e_nat 2) (e_nat 6) in + let%bind () = expect_eq program "or_op" (e_nat 14) (e_nat 14) in + let%bind () = expect_eq program "or_op" (e_nat 10) (e_nat 14) in + let%bind () = expect_eq program "and_op" (e_nat 7) (e_nat 7) in + let%bind () = expect_eq program "and_op" (e_nat 3) (e_nat 3) in + let%bind () = expect_eq program "and_op" (e_nat 2) (e_nat 2) in + let%bind () = expect_eq program "and_op" (e_nat 14) (e_nat 6) in + let%bind () = expect_eq program "and_op" (e_nat 10) (e_nat 2) in + let%bind () = expect_eq program "xor_op" (e_nat 0) (e_nat 7) in + let%bind () = expect_eq program "xor_op" (e_nat 7) (e_nat 0) in + ok () + +let string_arithmetic () : unit result = + let%bind program = type_file "./contracts/string_arithmetic.ligo" in + let%bind () = expect_eq program "concat_op" (e_string "foo") (e_string "foototo") in + let%bind () = expect_eq program "concat_op" (e_string "") (e_string "toto") 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_fail program "slice_op" (e_string "ba") in + ok () + let unit_expression () : unit result = let%bind program = type_file "./contracts/unit.ligo" in expect_eq_evaluate program "u" (e_unit ()) @@ -562,6 +588,8 @@ let main = test_suite "Integration (End to End)" [ test "multiple parameters" multiple_parameters ; test "bool" bool_expression ; test "arithmetic" arithmetic ; + test "bitiwse_arithmetic" bitwise_arithmetic ; + test "string_arithmetic" string_arithmetic ; test "unit" unit_expression ; test "string" string_expression ; test "option" option ; diff --git a/src/test/test_helpers.ml b/src/test/test_helpers.ml index f178adcd2..e2f8896ce 100644 --- a/src/test/test_helpers.ml +++ b/src/test/test_helpers.ml @@ -49,7 +49,7 @@ let test name f = ) let test_suite name lst = Test_suite (name , lst) - + open Ast_simplified.Combinators let expect ?options program entry_point input expecter = @@ -62,6 +62,17 @@ let expect ?options program entry_point input expecter = Ligo.Run.run_simplityped ~debug_michelson:true ?options program entry_point input in expecter result +let expect_fail ?options program entry_point input = + let run_error = + let title () = "expect run" in + let content () = Format.asprintf "Entry_point: %s" entry_point in + error title content + in + trace run_error @@ + Assert.assert_fail + @@ Ligo.Run.run_simplityped ~debug_michelson:true ?options program entry_point input + + let expect_eq ?options program entry_point input expected = let expecter = fun result -> let expect_error =