File: bytes_unit_tests.ml

package info (click to toggle)
ocaml-dune 3.20.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,564 kB
  • sloc: ml: 175,178; asm: 28,570; ansic: 5,251; sh: 1,096; lisp: 625; makefile: 148; python: 125; cpp: 48; javascript: 10
file content (70 lines) | stat: -rw-r--r-- 1,421 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
open Stdune

let () =
  (* We assert some properties of the conversion table here. It should be sorted
     by the values in the second component and the suffix list must be
     non-empty. *)
  let rec loop = function
    | [] -> ()
    | [ (units, _) ] -> assert (List.length units >= 1)
    | (units, value) :: ((_, value') :: _ as l) ->
      assert (List.length units >= 1);
      assert (value <= value');
      loop l
  in
  loop Bytes_unit.conversion_table
;;

let test bytes = List.iter ~f:(fun x -> Bytes_unit.pp x |> print_endline) bytes

let%expect_test "Testing significant digit boundaries" =
  test
    [ 0L
    ; 1L
    ; 12L
    ; 123L
    ; 1234L
    ; 12345L
    ; 123456L
    ; 1234567L
    ; 12345678L
    ; 123456789L
    ; 1234567890L
    ; 12345678901L
    ; 123456789012L
    ; 1234567890123L
    ; 12345678901234L
    ; 123456789012345L
    ; 1234567890123456L
    ];
  [%expect
    {|
      0B
      1B
      12B
      123B
      1.23kB
      12.35kB
      123.46kB
      1.23MB
      12.35MB
      123.46MB
      1.23GB
      12.35GB
      123.46GB
      1.23TB
      12.35TB
      123.46TB
      1234.57TB |}]
;;

(* CR-someday alizter: we should raise an exception here rather than giving a meaningless value. *)
(* Negative units get truncated but still printed as a negative. *)
let%expect_test "Negative units" =
  test [ -1L; -10L ];
  [%expect
    {|
    -0.00TB
    -0.00TB
    |}]
;;