add bitwise arithmetic and string arithmetic tests
This commit is contained in:
parent
7b9d861a34
commit
5c3d801c78
@ -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 ;
|
||||
|
8
src/contracts/bitwise_arithmetic.ligo
Normal file
8
src/contracts/bitwise_arithmetic.ligo
Normal 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)
|
5
src/contracts/string_arithmetic.ligo
Normal file
5
src/contracts/string_arithmetic.ligo
Normal 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)
|
@ -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 *)
|
||||
|
@ -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 ;
|
||||
|
@ -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 =
|
||||
|
Loading…
Reference in New Issue
Block a user