# Testing stack in qcheck

**URL:** <https://discuss.ocaml.org/t/testing-stack-in-qcheck/10129>\
**Category:** Learning\
**Tags:** unit-test, qcheck\
**Created:** [July 4, 2022, 6:18pm UTC](https://discuss.ocaml.org/t/testing-stack-in-qcheck/10129 "2022-07-04T18:18:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jazz](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jazz/32/4425_2.png) [@Jazz](https://discuss.ocaml.org/u/Jazz)\
**Post date:** [July 4, 2022, 6:18pm UTC](https://discuss.ocaml.org/t/testing-stack-in-qcheck/10129/1 "2022-07-04T18:18:08Z")

</div>

Hi,

I am exploring [qcheck](https://github.com/c-cube/qcheck) and i am not sure the way i test `stack` like below is correct or not.  
Appreciate your suggestion. Thanks.

```auto
let test_fun ~name = QCheck2.(Test.make ~name
~count:100
~print:Print.(list int)
Gen.(list int)) in
let create_stack l = Stack.of_seq (List.to_seq l) in
let flist = [
   ("length", (fun l -> Stack.length (create_stack l) = List.length l)) ;
   ("pop", (fun l -> try Stack.pop (create_stack l) = List.hd (List.rev l) with Failure _ -> true)) ;
   ("top", (fun l -> try Stack.top (create_stack l) = List.hd (List.rev l) with Failure _ -> true)) ;
   ("clear", (fun l -> let st = create_stack l in Stack.clear st; Stack.length st = 0)) ;
   ("push", (fun l -> let st = create_stack l in Stack.push 101 st; Stack.pop st = 101)) ;
] in
let tests = flist |> List.map (fun (n,f) -> test_fun ~name:n f) in
QCheck_runner.run_tests ~verbose:true tests ;;

```

Regards,  
Jazz

---

<div class="post-metadata">

**Author:** ![otini](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/otini/32/3533_2.png) [@otini](https://discuss.ocaml.org/u/otini)\
**Post date:** [July 5, 2022, 10:00am UTC](https://discuss.ocaml.org/t/testing-stack-in-qcheck/10129/2 "2022-07-05T10:00:12Z")

</div>

Your tests look reasonable to me. It seems that what you are doing is comparing the result of each stack operation with a model of what it should be, in the form of a list.

You can push that testing principle further by testing _sequences_ of operations—e.g. a push, two pops, and another push— and verifying that the result still coincides with the list model. This possibly covers more scenarios than testing each operation individually.

Such model-based testing can be automated using [qcstm](https://github.com/jmid/qcstm): you provide a list of operations and describe what their effect should be, and qcstm generates sequences of such operations and verifies their result.

---

<div class="post-metadata">

**Author:** ![Jazz](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jazz/32/4425_2.png) [@Jazz](https://discuss.ocaml.org/u/Jazz)\
**Post date:** [July 5, 2022, 10:30am UTC](https://discuss.ocaml.org/t/testing-stack-in-qcheck/10129/3 "2022-07-05T10:30:59Z")

</div>

Thank you for your suggestion @otini.  
qcstm is interesting.

Regards
