Skip to content

Commit

Permalink
Merge pull request #12796 from MinaProtocol/gltrost/transaction_logic…
Browse files Browse the repository at this point in the history
…_stack_tests

Add tests for Inputs.Stack in transaction_logic/mina_transaction_logic.ml
  • Loading branch information
joaosreis authored Oct 26, 2023
2 parents d5ae586 + da0a3a4 commit ea6d94c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/lib/transaction_logic/mina_transaction_logic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,24 @@ module type S = sig
-> bool Or_error.t

module For_tests : sig
module Stack (Elt : sig
type t
end) : sig
type t = Elt.t list

val if_ : bool -> then_:t -> else_:t -> t

val empty : unit -> t

val is_empty : t -> bool

val pop_exn : t -> Elt.t * t

val pop : t -> (Elt.t * t) option

val push : Elt.t -> onto:t -> t
end

val validate_timing_with_min_balance :
account:Account.t
-> txn_amount:Amount.t
Expand Down Expand Up @@ -2503,6 +2521,8 @@ module Make (L : Ledger_intf.S) :
>>= Mina_stdlib.Result.List.map ~f:(apply_transaction_second_pass ledger)

module For_tests = struct
module Stack = Inputs.Stack

let validate_timing_with_min_balance = validate_timing_with_min_balance

let validate_timing = validate_timing
Expand Down
86 changes: 86 additions & 0 deletions src/lib/transaction_logic/test/zkapp_logic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -862,3 +862,89 @@ let%test_module "Test transaction logic." =
)
(run_zkapp_cmd ~fee_payer ~fee ~accounts txns) )
end )

(* This module tests Inputs.Stack *)
let%test_module "Test stack module" =
( module struct
module Stack = Transaction_logic.For_tests.Stack (Int)

let%test_unit "Ensure pop works on non-empty list." =
Quickcheck.test ~trials
(let open Quickcheck in
let open Generator.Let_syntax in
let%map stack = Generator.list_non_empty Generator.size in
let top = List.hd_exn stack in
let tail = List.tl_exn stack in
(stack, top, tail))
~f:(fun (stack, top, tail) ->
match Stack.pop stack with
| Some (x, xs) ->
assert (Int.equal x top && List.equal Int.equal xs tail)
| None ->
assert false )

let%test_unit "Ensure pop works on empty list." =
match Stack.pop (Stack.empty ()) with
| Some _ ->
assert false
| None ->
assert true

let%test_unit "Ensure push functionality works." =
Quickcheck.test ~trials
(let open Quickcheck in
let open Generator.Let_syntax in
let%bind stack = Generator.list_non_empty Generator.size in
let%bind stack' = Generator.list_non_empty Generator.size in
let pushed =
List.fold_right stack' ~init:stack ~f:(fun x s ->
Stack.push x ~onto:s )
in
let pushed' = List.append stack' stack in
return (pushed, pushed'))
~f:(fun (pushed, pushed') ->
assert (List.equal Int.equal pushed pushed') )
end )

(* This module tests Inputs.Stack *)
let%test_module "Test stack module" =
( module struct
module Stack = Transaction_logic.For_tests.Stack (Int)

let%test_unit "Ensure pop works on non-empty list." =
Quickcheck.test ~trials
(let open Quickcheck in
let open Generator.Let_syntax in
let%map stack = Generator.list_non_empty Generator.size in
let top = List.hd_exn stack in
let tail = List.tl_exn stack in
(stack, top, tail))
~f:(fun (stack, top, tail) ->
match Stack.pop stack with
| Some (x, xs) ->
assert (Int.equal x top && List.equal Int.equal xs tail)
| None ->
assert false )

let%test_unit "Ensure pop works on empty list." =
match Stack.pop (Stack.empty ()) with
| Some _ ->
assert false
| None ->
assert true

let%test_unit "Ensure push functionality works." =
Quickcheck.test ~trials
(let open Quickcheck in
let open Generator.Let_syntax in
let%bind stack = Generator.list_non_empty Generator.size in
let%bind stack' = Generator.list_non_empty Generator.size in
let pushed =
List.fold_right stack' ~init:stack ~f:(fun x s ->
Stack.push x ~onto:s )
in
let pushed' = List.append stack' stack in
return (pushed, pushed'))
~f:(fun (pushed, pushed') ->
assert (List.equal Int.equal pushed pushed') )
end )

0 comments on commit ea6d94c

Please sign in to comment.