File: core_queue_test.ml

package info (click to toggle)
janest-core 107.01-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,440 kB
  • sloc: ml: 26,624; ansic: 2,498; sh: 49; makefile: 29
file content (104 lines) | stat: -rw-r--r-- 4,126 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
(******************************************************************************
 *                             Core                                           *
 *                                                                            *
 * Copyright (C) 2008- Jane Street Holding, LLC                               *
 *    Contact: opensource@janestreet.com                                      *
 *    WWW: http://www.janestreet.com/ocaml                                    *
 *                                                                            *
 *                                                                            *
 * This library is free software; you can redistribute it and/or              *
 * modify it under the terms of the GNU Lesser General Public                 *
 * License as published by the Free Software Foundation; either               *
 * version 2 of the License, or (at your option) any later version.           *
 *                                                                            *
 * This library is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU          *
 * Lesser General Public License for more details.                            *
 *                                                                            *
 * You should have received a copy of the GNU Lesser General Public           *
 * License along with this library; if not, write to the Free Software        *
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  *
 *                                                                            *
 ******************************************************************************)

open OUnit
open Core.Std
open Queue

let test =
  "queue" >:::
    [
      "create" >::
        (fun () ->
          let t = create () in
          assert (is_empty t);
        );
      "container" >::
        (fun () ->
          let module T = Container_test.Test_S1 (Queue) in
          T.test ();
        );
      "enqueue" >::
        (fun () ->
          let t = create () in
          enqueue t 1;
          enqueue t 2;
          enqueue t 3;
          assert (to_list t = [1; 2; 3]);
          assert (peek t = Some 1);
          assert (dequeue t = Some 1);
          assert (to_list t = [2; 3]);
          assert (peek t = Some 2);
          assert (dequeue_exn t = 2);
          assert (to_list t = [3]);
          assert (peek_exn t = 3);
          assert (dequeue t = Some 3);
          assert (to_list t = []);
          assert (peek t = None);
          assert (dequeue t = None);
        );
      "clear" >::
        (fun () ->
          for i = 0 to 5 do
            let t = of_list (List.init i ~f:ident) in
            clear t;
            assert (is_empty t);
          done
        );
      "copy" >::
        (fun () ->
          let t = of_list [1; 2; 3] in
          let t' = copy t in
          ignore (dequeue t);
          assert (to_list t = [2; 3]);
          assert (to_list t' = [1; 2; 3]);
          ignore (clear t');
          assert (to_list t = [2; 3]);
          assert (to_list t' = []);
        );
      "transfer" >::
        (fun () ->
          for i1 = 0 to 3 do
            let l1 = List.init i1 ~f:ident in
            for i2 = 0 to 3 do
              let l2 = List.init i2 ~f:ident in
              let t1 = of_list l1 in
              let t2 = of_list l2 in
              transfer ~src:t1 ~dst:t2;
              assert (is_empty t1);
              assert (to_list t2 = l2 @ l1);
            done
          done
        );
      "filter_inplace" >::
        (fun () ->
          let t = of_list [1; 2; 3; 4; 5; 6; 7] in
          filter_inplace t ~f:(fun n -> n mod 2 = 0);
          assert (to_list t = [2; 4; 6]);
          assert (for_all t ~f:(fun n -> n mod 2 = 0));
          assert (exists t ~f:(fun n -> n = 4 || n = 6));
          filter_inplace t ~f:(fun n -> n mod 2 <> 0);
          assert (to_list t = []);
        );
    ]