Source file test_map.ml

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
open! Core
open Quickcheck
open Expect_test_helpers_core

module Test (Scheduler : Common.Scheduler) : Parallel.Map.S = struct
  type ('key, 'data, 'cmp) map = ('key, 'data, 'cmp) Map.t
  type ('key, 'cmp, 'fn) with_comparator = 'fn

  open struct
    (* Test helpers, not for export. *)

    let quickcheck_config : Base_quickcheck.Test.Config.t =
      (* Rather than 10k maps of small size, generate 1k maps of size up to 1k. This way
         we test large enough inputs that the work actually parallelizes. *)
      { seed = Base_quickcheck.Test.default_config.seed
      ; test_count = 100
      ; shrink_count = 1_000_000
      ; sizes = Sequence.unfold ~init:0 ~f:(fun n -> Some (n, n + 1))
      }
    ;;

    let test
      (type (a : value mod contended portable) b)
      (module Input : Base_quickcheck.Test.S with type t = a)
      (module Output : With_equal with type t = b)
      seq_f
      par_f
      =
      quickcheck_m (module Input) ~config:quickcheck_config ~f:(fun input ->
        Scheduler.parallel (fun par ->
          require_equal (module Output) (seq_f input) (par_f par input)))
    ;;

    module Int = struct
      include Int

      let sexp_of_t n = Sexp.Atom (Int.to_string_hum n)
      let quickcheck_generator = Generator.small_positive_int

      let quickcheck_shrinker =
        Shrinker.create (fun n ->
          if n <= 1 then Sequence.empty else Sequence.singleton (n - 1))
      ;;
    end

    type int = Int.t [@@deriving quickcheck ~generator ~shrinker, sexp_of]

    module Int2_alist = struct
      type t = (int * int) list [@@deriving equal, sexp_of]

      (* Use the whole [size] as list length so we get some large maps. Only generates
         keys in sorted order, but that's fine, we don't use them for anything but
         generating maps. *)
      let quickcheck_generator_sorted =
        Generator.create (fun ~size ~random ->
          List.init size ~f:(fun _ ->
            ( Splittable_random.int random ~lo:0 ~hi:size
            , Splittable_random.int random ~lo:0 ~hi:size ))
          |> List.folding_map ~init:0 ~f:(fun min_key (k, data) ->
            let key = min_key + k in
            key + 1, (key, data)))
      ;;
    end

    module Int2_map = struct
      type t = int Map.M(Int).t [@@deriving equal, quickcheck ~shrinker, sexp_of]

      let quickcheck_generator =
        let%map.Generator alist = Int2_alist.quickcheck_generator_sorted in
        Int.Map.of_alist_exn alist
      ;;

      let check t =
        let comparator = Map.comparator t in
        let tree = Map.to_tree t in
        let ordered = Map.Tree.Expert.order_invariants ~comparator tree in
        let balanced = Map.Tree.Expert.balance_invariants tree in
        match ordered && balanced with
        | true -> ()
        | false ->
          let sexp_of_key = Comparator.sexp_of_t comparator in
          print_cr
            [%message
              "invariants violated"
                (ordered : bool)
                (balanced : bool)
                (tree : (key, _, _) Map.Tree.Expert.t)]
      ;;

      (* We put invariant checks in [equal] so that all maps in our output have their
         invariants checked. *)
      let equal a b =
        check a;
        check b;
        equal a b
      ;;
    end

    module Int2_map2 = struct
      type t = Int2_map.t * Int2_map.t [@@deriving equal, quickcheck ~shrinker, sexp_of]

      let quickcheck_generator =
        (* Generate some maps with shared structure and some without. *)
        let generate_independently = [%generator: Int2_map.t * Int2_map.t] in
        let generate_right_from_left =
          let%map.Generator map1 = [%generator: Int2_map.t]
          and alist = Int2_alist.quickcheck_generator_sorted in
          let map2 =
            List.fold alist ~init:map1 ~f:(fun acc (key, data) -> Map.set acc ~key ~data)
          in
          map1, map2
        in
        let generate_left_from_right =
          Generator.map ~f:(fun (map2, map1) -> map1, map2) generate_right_from_left
        in
        [ generate_independently; generate_right_from_left; generate_left_from_right ]
        |> Generator.union
      ;;
    end
  end

  let to_sequence = Parallel.Map.to_sequence

  let%expect_test "to_sequence" =
    test
      (module Int2_map)
      (module Int2_alist)
      (fun map -> Map.to_sequence map |> Sequence.to_list)
      (fun par map ->
        (Parallel.Map.to_sequence map |> Parallel.Sequence.With_length.to_list par)
        [@nontail])
  ;;

  let traverse = Parallel.Map.traverse

  let%expect_test "traverse" =
    test
      (module Int2_map)
      (module Int2_alist)
      (fun map -> Map.to_alist map)
      (fun par map ->
        Parallel.Map.traverse
          par
          map
          ~on_empty:(fun () -> [])
          ~on_data:(fun _ ~key ~data -> [ key, data ])
          ~on_leaf:(fun _ ~key:_ alist -> alist)
          ~on_node:(fun _ ~key:_ mid left right -> left @ mid @ right))
  ;;

  let fold = Parallel.Map.fold

  let%expect_test "fold" =
    let f ~key ~data acc = acc + (key * data) in
    test
      (module Int2_map)
      (module Int)
      (fun map -> Map.fold map ~init:0 ~f)
      (fun par map ->
        Parallel.Map.fold
          par
          map
          ~init:(fun () -> 0)
          ~f:(fun _ -> f)
          ~combine:(fun _ -> ( + )))
  ;;

  let iter = Parallel.Map.iter

  let%expect_test "iter" =
    let with_atomic (f @ local) =
      let atomic = Atomic.make ([] : (int * int) list) in
      f ~f:(fun ~key ~data ->
        Atomic.update atomic ~pure_f:(fun list -> (key, data) :: list));
      List.sort ~compare:[%compare: int * int] (Atomic.get atomic)
    in
    test
      (module Int2_map)
      (module Int2_alist)
      (fun map -> with_atomic (fun ~f -> Map.iteri map ~f))
      (fun par map ->
        with_atomic (fun ~f -> Parallel.Map.iter par map ~f:(fun _ -> f)) [@nontail])
  ;;

  let map = Parallel.Map.map

  let%expect_test "map" =
    let f ~key ~data = (key * data) + 1 in
    test
      (module Int2_map)
      (module Int2_map)
      (fun map -> Map.mapi map ~f)
      (fun par map -> Parallel.Map.map par map ~f:(fun _ -> f))
  ;;

  let filter = Parallel.Map.filter

  let%expect_test "filter" =
    let f ~key ~data = key * data % 2 = 1 in
    test
      (module Int2_map)
      (module Int2_map)
      (fun map -> Map.filteri map ~f)
      (fun par map -> Parallel.Map.filter par map ~f:(fun _ -> f))
  ;;

  let filter_map = Parallel.Map.filter_map

  let%expect_test "filter_map" =
    let f ~key ~data = Option.some_if (key * data % 2 = 1) (key + data) in
    test
      (module Int2_map)
      (module Int2_map)
      (fun map -> Map.filter_mapi map ~f)
      (fun par map -> Parallel.Map.filter_map par map ~f:(fun _ -> f))
  ;;

  let partition_tf = Parallel.Map.partition_tf

  let%expect_test "partition_tf" =
    let f ~key ~data = key * data % 2 = 1 in
    test
      (module Int2_map)
      (module Int2_map2)
      (fun map -> Map.partitioni_tf map ~f)
      (fun par map -> Parallel.Map.partition_tf par map ~f:(fun _ -> f))
  ;;

  let partition_map = Parallel.Map.partition_map

  let%expect_test "partition_map" =
    let f ~key ~data =
      if key * data % 2 = 1 then First (key + data) else Second (key + data)
    in
    test
      (module Int2_map)
      (module Int2_map2)
      (fun map -> Map.partition_mapi map ~f)
      (fun par map -> Parallel.Map.partition_map par map ~f:(fun _ -> f))
  ;;

  let to_sequence2 = Parallel.Map.to_sequence2

  let%expect_test "to_sequence2" =
    test
      (module Int2_map2)
      (module struct
        type t = (int * [ `Both of int * int | `Left of int | `Right of int ]) list
        [@@deriving equal, sexp_of]
      end)
      (fun (map1, map2) ->
        Map.merge map1 map2 ~f:(fun ~key:_ data -> Some data) |> Map.to_alist)
      (fun par (map1, map2) ->
        (Parallel.Map.to_sequence2 map1 map2 |> Parallel.Sequence.to_list par) [@nontail]);
    [%expect {| |}]
  ;;

  let merge_filter_map = Parallel.Map.merge_filter_map

  let%expect_test "merge_filter_map" =
    let f ~key = function
      | `Left data -> Option.some_if (key mod 3 <> 2) data
      | `Right data -> Option.some_if (key mod 3 <> 1) data
      | `Both (data1, data2) -> Option.some_if (key mod 3 <> 0) (data1 + data2)
    in
    test
      (module Int2_map2)
      (module Int2_map)
      (fun (map1, map2) -> Map.merge map1 map2 ~f)
      (fun par (map1, map2) ->
        Parallel.Map.merge_filter_map par map1 map2 ~f:(fun _ -> f));
    [%expect {| |}]
  ;;
end

include Common.Test_schedulers (Test)