File: std_utils_tests.ml

package info (click to toggle)
libguestfs 1%3A1.56.2-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 99,040 kB
  • sloc: ansic: 380,795; ml: 39,048; sh: 10,942; java: 9,681; cs: 6,404; haskell: 5,751; makefile: 5,174; python: 3,712; erlang: 2,472; perl: 2,467; ruby: 349; xml: 275; pascal: 259; javascript: 157; cpp: 10
file content (257 lines) | stat: -rw-r--r-- 10,845 bytes parent folder | download | duplicates (3)
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
(* Utilities for OCaml tools in libguestfs.
 * Copyright (C) 2011-2025 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *)

(* This file tests the Std_utils module. *)

open Printf

open Std_utils

let assert_equal ~printer a b =
  if a <> b then
    failwithf "FAIL: %s <> %s" (printer a) (printer b)

let assert_raises exn fn =
  try
    fn ();
    failwithf "FAIL: expected function to raise an exception"
  with exn' ->
    if exn <> exn' then (
      eprintf "FAIL: function raised the wrong exception:\n\
               expected %s\n\
               actual %s\n"
        (Printexc.to_string exn) (Printexc.to_string exn');
      exit 1
    )

let assert_equal_string =
  assert_equal ~printer:Fun.id
let assert_equal_int =
  assert_equal ~printer:(fun x -> string_of_int x)
let assert_equal_int64 =
  assert_equal ~printer:(fun x -> Int64.to_string x)
let assert_equal_stringlist =
  assert_equal ~printer:(fun x -> "(" ^ (String.escaped (String.concat "," x)) ^ ")")
let assert_equal_stringpair =
  assert_equal ~printer:(fun (x, y) -> sprintf "%S, %S" x y)
let assert_nonempty_string str =
  if str = "" then (
    eprintf "Expected empty string, got '%s'\n" str;
    exit 1
  )
let assert_raises_executable_not_found exe =
  assert_raises (Executable_not_found exe) (fun () -> which exe)

let assert_bool name b =
  if not b then failwithf "FAIL: %s" name

(* Test Std_utils.int_of_X and Std_utils.X_of_int byte swapping
 * functions.
 *)
let rec test_byteswap () =
  test_swap int_of_le16 le16_of_int 0x2040L "\x40\x20";
  test_swap int_of_le32 le32_of_int 0x20406080L "\x80\x60\x40\x20";
  test_swap int_of_le64 le64_of_int
            0x20406080A0C0E0F0L "\xF0\xE0\xC0\xA0\x80\x60\x40\x20";
  test_swap int_of_be16 be16_of_int 0x2040L "\x20\x40";
  test_swap int_of_be32 be32_of_int 0x20406080L "\x20\x40\x60\x80";
  test_swap int_of_be64 be64_of_int
            0x20406080A0C0E0F0L "\x20\x40\x60\x80\xA0\xC0\xE0\xF0"

and test_swap int_of_x x_of_int i s =
  assert_equal_int64 i (int_of_x s);
  assert_equal_string s (x_of_int i)

let () = test_byteswap ()

(* Test Std_utils.Char.mem. *)
let () =
  assert_bool "Char.mem" (Char.mem 'a' "abc");
  assert_bool "Char.mem" (Char.mem 'b' "abc");
  assert_bool "Char.mem" (Char.mem 'c' "abc");
  assert_bool "Char.mem" (not (Char.mem 'd' "abc"));
  assert_bool "Char.mem" (not (Char.mem 'a' ""))

(* Test Std_utils.String.starts_with. *)
let () =
  assert_bool "String.starts_with,," (String.starts_with "" "");
  assert_bool "String.starts_with,foo," (String.starts_with "" "foo");
  assert_bool "String.starts_with,foo,foo" (String.starts_with "foo" "foo");
  assert_bool "String.starts_with,foo123,foo"
    (String.starts_with "foo" "foo123");
  assert_bool "not (String.starts_with,,foo"
    (not (String.starts_with "foo" ""))

(* Test Std_utils.String.ends_with. *)
let () =
  assert_bool "String.ends_with,," (String.ends_with "" "");
  assert_bool "String.ends_with,foo," (String.ends_with "" "foo");
  assert_bool "String.ends_with,foo,foo" (String.ends_with "foo" "foo");
  assert_bool "String.ends_with,123foo,foo" (String.ends_with "foo" "123foo");
  assert_bool "not String.ends_with,,foo" (not (String.ends_with "foo" ""))

(* Test Std_utils.String.find. *)
let () =
  assert_equal_int 0 (String.find "" "");
  assert_equal_int 0 (String.find "foo" "");
  assert_equal_int 1 (String.find "foo" "o");
  assert_equal_int 3 (String.find "foobar" "bar");
  assert_equal_int (-1) (String.find "" "baz");
  assert_equal_int (-1) (String.find "foobar" "baz")

(* Test Std_utils.String.break. *)
let () =
  assert_equal_stringpair ("a", "b") (String.break 1 "ab");
  assert_equal_stringpair ("", "ab") (String.break 0 "ab");
  assert_equal_stringpair ("", "ab") (String.break (-1) "ab");
  assert_equal_stringpair ("ab", "") (String.break 2 "ab");
  assert_equal_stringpair ("ab", "") (String.break 3 "ab");
  assert_equal_stringpair ("abc", "def") (String.break 3 "abcdef");
  assert_equal_stringpair ("", "") (String.break 0 "");
  assert_equal_stringpair ("", "") (String.break (-2) "");
  assert_equal_stringpair ("", "") (String.break 2 "")

(* Test Std_utils.String.split. *)
let () =
  assert_equal_stringpair ("a", "b") (String.split " " "a b");
  assert_equal_stringpair ("", "ab") (String.split " " " ab");
  assert_equal_stringpair ("", "abc") (String.split "" "abc");
  assert_equal_stringpair ("abc", "") (String.split " " "abc");
  assert_equal_stringpair ("", "") (String.split " " "")

(* Test Std_utils.String.nsplit. *)
let () =
  (* XXX Not clear if the next test case indicates an error in
   * String.nsplit.  However this is how it has historically worked.
   *)
  assert_equal_stringlist [""] (String.nsplit " " "");
  assert_equal_stringlist ["abc"] (String.nsplit " " "abc");
  assert_equal_stringlist ["a"; "b"; "c"] (String.nsplit " " "a b c");
  assert_equal_stringlist ["abc"; "d"; "e"] (String.nsplit " " "abc d e");
  assert_equal_stringlist ["a"; "b"; "c"; ""] (String.nsplit " " "a b c ");
  assert_equal_stringlist [""; "a"; "b"; "c"] (String.nsplit " " " a b c");
  assert_equal_stringlist [""; "a"; "b"; "c"; ""] (String.nsplit " " " a b c ");
  assert_equal_stringlist ["a b c d"] (String.nsplit ~max:1 " " "a b c d");
  assert_equal_stringlist ["a"; "b c d"] (String.nsplit ~max:2 " " "a b c d");
  assert_equal_stringlist ["a"; "b"; "c d"] (String.nsplit ~max:3 " " "a b c d");
  assert_equal_stringlist ["a"; "b"; "c"; "d"] (String.nsplit ~max:10 " " "a b c d");

  (* Test that nsplit can handle large strings. *)
  let xs = Array.to_list (Array.make 10_000_000 "xyz") in
  let xs_concat = String.concat " " xs in
  assert_equal_stringlist xs (String.nsplit " " xs_concat)

(* Test Std_utils.String.lines_split. *)
let () =
  assert_equal_stringlist [""] (String.lines_split "");
  assert_equal_stringlist ["A"] (String.lines_split "A");
  assert_equal_stringlist ["A"; ""] (String.lines_split "A\n");
  assert_equal_stringlist ["A"; "B"] (String.lines_split "A\nB");
  assert_equal_stringlist ["A"; "B"; "C"] (String.lines_split "A\nB\nC");
  assert_equal_stringlist ["A"; "B"; "C"; "D"] (String.lines_split "A\nB\nC\nD");
  assert_equal_stringlist ["A\n"] (String.lines_split "A\\");
  assert_equal_stringlist ["A\nB"] (String.lines_split "A\\\nB");
  assert_equal_stringlist ["A"; "B\nC"] (String.lines_split "A\nB\\\nC");
  assert_equal_stringlist ["A"; "B\nC"; "D"] (String.lines_split "A\nB\\\nC\nD");
  assert_equal_stringlist ["A"; "B\nC\nD"] (String.lines_split "A\nB\\\nC\\\nD");
  assert_equal_stringlist ["A\nB"; ""] (String.lines_split "A\\\nB\n");
  assert_equal_stringlist ["A\nB\n"] (String.lines_split "A\\\nB\\\n")

(* Test Std_utils.String.span and cspan. *)
let () =
  assert_equal_int 3 (String.span "aaabb" "a");
  assert_equal_int 3 (String.span "aaaba" "a");
  assert_equal_int 3 (String.span "aba" "ab");
  assert_equal_int 0 (String.span "" "ab");
  assert_equal_int 3 (String.cspan "defab" "ab");
  assert_equal_int 3 (String.cspan "defba" "ab");
  assert_equal_int 3 (String.cspan "def" "ab");
  assert_equal_int 0 (String.cspan "" "ab")

(* Test Std_utils.String.chomp. *)
let () =
  assert_equal_string "a" (String.chomp "a");
  assert_equal_string "a" (String.chomp "a\n");
  assert_equal_string "a\nb" (String.chomp "a\nb");
  assert_equal_string "" (String.chomp "");
  assert_equal_string "" (String.chomp "\n");
  assert_equal_string "\n" (String.chomp "\n\n") (* only removes one *)

(* Test Std_utils.String.common_prefix, longest_common_prefix. *)
let () =
  assert_equal_string "" (String.common_prefix "" "");
  assert_equal_string "" (String.common_prefix "" "abc");
  assert_equal_string "" (String.common_prefix "abc" "");
  assert_equal_string "" (String.common_prefix "hello" "world");
  assert_equal_string "abc" (String.common_prefix "abc" "abcde");
  assert_equal_string "abc" (String.common_prefix "abcde" "abc");
  assert_equal_string "abc" (String.common_prefix "abcde" "abcghi");
  assert_equal_string "" (String.longest_common_prefix []);
  assert_equal_string "" (String.longest_common_prefix [""]);
  assert_equal_string "abc" (String.longest_common_prefix ["abc"]);
  assert_equal_string "" (String.longest_common_prefix [""; "abc"]);
  assert_equal_string "" (String.longest_common_prefix ["abc"; ""]);
  assert_equal_string "" (String.longest_common_prefix ["hello"; "world"]);
  assert_equal_string ""
    (String.longest_common_prefix ["hello"; "there"; "world"]);
  assert_equal_string "abc" (String.longest_common_prefix ["abc"; "abcde"]);
  assert_equal_string "abc" (String.longest_common_prefix ["abcde"; "abc"]);
  assert_equal_string "abc" (String.longest_common_prefix ["abcde"; "abcghi"]);
  assert_equal_string "abc"
    (String.longest_common_prefix ["abcde"; "abcghi"; "abc123"]);
  assert_equal_string "abc"
    (String.longest_common_prefix ["abc"; "abcghi"; "abc123"]);
  assert_equal_string "abc"
    (String.longest_common_prefix ["abcde"; "abc"; "abc123"]);
  assert_equal_string "abc"
    (String.longest_common_prefix ["abcde"; "abcde"; "abc"]);
  assert_equal_string "abc"
    (String.longest_common_prefix ["abc"; "abc"; "abc"])

(* Test Std_utils.which. *)
let () =
  assert_nonempty_string (which "true");
  assert_raises_executable_not_found "this-command-does-not-really-exist";
  begin
    let exe_name = "true" in
    let exe = which exe_name in
    assert_equal_string exe (which exe);
    let pwd = Sys.getcwd () in
    Sys.chdir (Filename.dirname exe);
    let exe_relative = "./" ^ exe_name in
    assert_equal_string exe_relative (which exe_relative);
    Sys.chdir pwd
  end;
  ()

(* Test List.make. *)
let () =
  assert_equal_stringlist [] (List.make 0 "1");
  assert_equal_stringlist ["1"; "1"; "1"] (List.make 3 "1");
  assert_raises (Invalid_argument "make") (fun () -> List.make (-1) "1")

(* Test List.same. *)
let () =
  assert_bool "List.same []" (List.same (List.make 0 "1"));
  assert_bool "List.same [1; 1; ...; 1]" (List.same (List.make 10 1))

(* Test List.last. *)
let () =
  assert_equal_string "3" (List.last ["1"; "2"; "3"]);
  assert_equal_string "1" (List.last ["1"]);
  assert_raises (Invalid_argument "List.last") (fun () -> List.last [])