summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib-bytes/test_bytes.ml
blob: 7349a0748749f99be1c0c0f7b2d67d2b941ce2ae (plain)
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
(* TEST
 include testing;
*)

let test_raises_invalid_argument f x =
  ignore
    (Testing.test_raises_exc_p
      (function Invalid_argument _ -> true | _ -> false) f x)

let check b offset s =
  let rec loop i =
    i = String.length s ||
    Bytes.get b (i + offset) = String.get s i && loop (i+1)
  in
  loop 0

let () =
  let abcde = Bytes.of_string "abcde" in
  let open Bytes in
  begin
    (*
           abcde
    ?????
    *)
    Testing.test
      (length (extend abcde 7 (-7)) = 5);

    (*
    abcde
           ?????
    *)
    Testing.test
      (length (extend abcde (-7) 7) = 5);

    (*
      abcde
      abcde
    *)
    Testing.test
      (let r = extend abcde 0 0 in
       length r = 5 && check r 0 "abcde"
       && r != abcde);

    (*
      abcde
    ??abc
    *)
    Testing.test
      (let r = extend abcde 2 (-2) in
       length r = 5 && check r 2 "abc");

    (*
      abcde
       bcd
    *)
    Testing.test
      (let r = extend abcde (-1) (-1) in
       length r = 3 && check r 0 "bcd");

    (*
      abcde
         de??
    *)
    Testing.test
      (let r = extend abcde (-3) 2 in
       length r = 4 && check r 0 "de");

    (*
      abcde
      abc
    *)
    Testing.test
      (let r = extend abcde 0 (-2) in
       length r = 3 && check r 0 "abc");

    (*
      abcde
        cde
    *)
    Testing.test
      (let r = extend abcde (-2) 0 in
       length r = 3 && check r 0 "cde");

    (*
      abcde
      abcde??
    *)
    Testing.test
      (let r = extend abcde 0 2 in
       length r = 7
       && check r 0 "abcde");

    (*
        abcde
      ??abcde
    *)
    Testing.test
      (let r = extend abcde 2 0 in
       length r = 7
       && check r 2 "abcde");

    (*
       abcde
      ?abcde?
    *)
    Testing.test
      (let r = extend abcde 1 1 in
       length r = 7
       && check r 1 "abcde");

    (*
       abcde
       edcba
    *)
    Testing.test
      (let r = copy abcde in
       let l = fold_left (fun acc x -> (make 1 x)::acc) [] r in
       let result = concat (Bytes.of_string "") l in
       length result = 5
       && check result 0 "edcba");

    (*
       abcde
       abcde
    *)
    Testing.test
      (let r = copy abcde in
       let l = fold_right (fun x acc -> (make 1 x)::acc) r [] in
       let result = concat (Bytes.of_string "") l in
       length result = 5
       && check result 0 "abcde");

    (*
       test exists and for_all
    *)
    Testing.test
      (exists (fun c -> c = 'b') abcde
      && not (exists (fun c -> c = 'f') abcde)
      && for_all (fun c -> c <> 'f') abcde
      && not (for_all (fun c -> c = 'b') abcde));

    (* length + left + right < 0 *)
    test_raises_invalid_argument
      (fun () -> extend abcde (-3) (-3)) ();

    (* length + left > max_int *)
    test_raises_invalid_argument
      (fun () -> extend abcde max_int 0) ();

    (* length + right > max_int *)
    test_raises_invalid_argument
      (fun () -> extend abcde 0 max_int) ();

    (* length + left + right > max_int *)
    test_raises_invalid_argument
      (fun () -> extend abcde max_int max_int) ();
  end