File: test_array_allocation.ml

package info (click to toggle)
janest-base 0.17.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,632 kB
  • sloc: ml: 48,653; ansic: 281; javascript: 126; makefile: 14
file content (32 lines) | stat: -rw-r--r-- 962 bytes parent folder | download | duplicates (2)
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
open! Base
open Expect_test_helpers_core

let%expect_test "Array.sort [||] only allocates when computing bounds" =
  require_allocation_does_not_exceed (Minor_words 3) [%here] (fun () ->
    Array.sort ~compare:Int.compare [||]);
  [%expect {| |}]
;;

let%expect_test "Array.sort [| 5; 2; 3; 4; 1 |] only allocates when computing bounds" =
  let arr = [| 5; 2; 3; 4; 1 |] in
  require_allocation_does_not_exceed (Minor_words 3) [%here] (fun () ->
    Array.sort ~compare:Int.compare arr);
  [%expect {| |}]
;;

let%expect_test "equal does not allocate" =
  let arr1 = [| 1; 2; 3; 4 |] in
  let arr2 = [| 1; 2; 4; 3 |] in
  require
    [%here]
    (require_no_allocation [%here] (fun () -> not (Array.equal Int.equal arr1 arr2)));
  [%expect {| |}]
;;

let%expect_test "foldi does not allocate" =
  let arr = [| 1; 2; 3; 4 |] in
  let f i x y = i + x + y in
  require
    [%here]
    (require_no_allocation [%here] (fun () -> 16 = Array.foldi ~init:0 ~f arr))
;;