add bitwise arithmetic and string arithmetic tests

This commit is contained in:
galfour 2019-07-19 12:42:01 +02:00
parent 7b9d861a34
commit 5c3d801c78
6 changed files with 65 additions and 7 deletions

View File

@ -185,8 +185,6 @@ and translate_expression ?push_var_name (expr:expression) (env:environment) : (m
| E_environment_return expr -> ( | E_environment_return expr -> (
let%bind (expr' , env) = translate_expression ~push_var_name:"return_clause" expr env in let%bind (expr' , env) = translate_expression ~push_var_name:"return_clause" expr env in
let%bind (code , cleared_env) = Compiler_environment.clear 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 [ return ~end_env:cleared_env @@ seq [
expr' ; expr' ;
code ; code ;

View File

@ -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)

View File

@ -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)

View File

@ -69,6 +69,11 @@ module Simplify = struct
("source" , "SOURCE") ; ("source" , "SOURCE") ;
("sender" , "SENDER") ; ("sender" , "SENDER") ;
("failwith" , "FAILWITH") ; ("failwith" , "FAILWITH") ;
("bitwise_or" , "OR") ;
("bitwise_and" , "AND") ;
("bitwise_xor" , "XOR") ;
("string_concat" , "CONCAT") ;
("string_slice" , "SLICE") ;
] ]
let type_constants = type_constants let type_constants = type_constants
@ -546,15 +551,17 @@ module Typer = struct
sub ; sub ;
none ; none ;
some ; some ;
concat ;
slice ;
comparator "EQ" ; comparator "EQ" ;
comparator "NEQ" ; comparator "NEQ" ;
comparator "LT" ; comparator "LT" ;
comparator "GT" ; comparator "GT" ;
comparator "LE" ; comparator "LE" ;
comparator "GE" ; comparator "GE" ;
boolean_operator_2 "OR" ; or_ ;
boolean_operator_2 "AND" ; and_ ;
boolean_operator_2 "XOR" ; xor ;
not_ ; not_ ;
map_remove ; map_remove ;
map_add ; map_add ;
@ -655,13 +662,14 @@ module Compiler = struct
("MAP_UPDATE" , simple_ternary @@ prim I_UPDATE) ; ("MAP_UPDATE" , simple_ternary @@ prim I_UPDATE) ;
("SET_MEM" , simple_binary @@ prim I_MEM) ; ("SET_MEM" , simple_binary @@ prim I_MEM) ;
("SET_ADD" , simple_binary @@ seq [dip (i_push (prim T_bool) (prim D_True)) ; prim I_UPDATE]) ; ("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) ; ("SHA256" , simple_unary @@ prim I_SHA256) ;
("SHA512" , simple_unary @@ prim I_SHA512) ; ("SHA512" , simple_unary @@ prim I_SHA512) ;
("BLAKE2B" , simple_unary @@ prim I_BLAKE2B) ; ("BLAKE2B" , simple_unary @@ prim I_BLAKE2B) ;
("CHECK_SIGNATURE" , simple_ternary @@ prim I_CHECK_SIGNATURE) ; ("CHECK_SIGNATURE" , simple_ternary @@ prim I_CHECK_SIGNATURE) ;
("HASH_KEY" , simple_unary @@ prim I_HASH_KEY) ; ("HASH_KEY" , simple_unary @@ prim I_HASH_KEY) ;
("PACK" , simple_unary @@ prim I_PACK) ; ("PACK" , simple_unary @@ prim I_PACK) ;
("CONCAT" , simple_binary @@ prim I_CONCAT) ;
] ]
(* Some complex predicates will need to be added in compiler/compiler_program *) (* Some complex predicates will need to be added in compiler/compiler_program *)

View File

@ -127,12 +127,38 @@ let arithmetic () : unit result =
("plus_op", fun n -> (n + 42)) ; ("plus_op", fun n -> (n + 42)) ;
("minus_op", fun n -> (n - 42)) ; ("minus_op", fun n -> (n - 42)) ;
("times_op", fun n -> (n * 42)) ; ("times_op", fun n -> (n * 42)) ;
("neg_op", fun n -> (-n)) ;
] in ] in
let%bind () = expect_eq_n_pos program "int_op" e_nat e_int 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 "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 let%bind () = expect_eq_n_pos program "div_op" e_int (fun n -> e_int (n / 2)) in
ok () 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 unit_expression () : unit result =
let%bind program = type_file "./contracts/unit.ligo" in let%bind program = type_file "./contracts/unit.ligo" in
expect_eq_evaluate program "u" (e_unit ()) 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 "multiple parameters" multiple_parameters ;
test "bool" bool_expression ; test "bool" bool_expression ;
test "arithmetic" arithmetic ; test "arithmetic" arithmetic ;
test "bitiwse_arithmetic" bitwise_arithmetic ;
test "string_arithmetic" string_arithmetic ;
test "unit" unit_expression ; test "unit" unit_expression ;
test "string" string_expression ; test "string" string_expression ;
test "option" option ; test "option" option ;

View File

@ -49,7 +49,7 @@ let test name f =
) )
let test_suite name lst = Test_suite (name , lst) let test_suite name lst = Test_suite (name , lst)
open Ast_simplified.Combinators open Ast_simplified.Combinators
let expect ?options program entry_point input expecter = 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 Ligo.Run.run_simplityped ~debug_michelson:true ?options program entry_point input in
expecter result 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 expect_eq ?options program entry_point input expected =
let expecter = fun result -> let expecter = fun result ->
let expect_error = let expect_error =