mllib: convert common_utils_tests to oUnit

Covert common_utils_tests to use oUnit as testing framework, replacing
the hand-made assert in favour of structured unit tests and better
error reporting.

common_utils_tests is now built only when the oUnit module has been
found.
This commit is contained in:
Pino Toscano
2015-01-22 18:33:07 +01:00
parent 1f5e525533
commit d19eb31060
2 changed files with 108 additions and 64 deletions

View File

@@ -86,9 +86,13 @@ OCAMLPACKAGES = \
-I $(top_builddir)/src/.libs \
-I $(top_builddir)/gnulib/lib/.libs \
-I $(top_builddir)/ocaml
OCAMLPACKAGES_TESTS =
if HAVE_OCAML_PKG_GETTEXT
OCAMLPACKAGES += -package gettext-stub
endif
if HAVE_OCAML_PKG_OUNIT
OCAMLPACKAGES_TESTS += -package oUnit
endif
OCAMLFLAGS = $(OCAML_FLAGS) $(OCAML_WARN_ERROR)
@@ -115,6 +119,7 @@ dummy_LINK = \
if HAVE_OCAMLOPT
.ml.cmx:
$(OCAMLFIND) ocamlopt $(OCAMLFLAGS) $(OCAMLPACKAGES) -c $< -o $@
endif
# This OCaml module has to be generated by make (configure will put
@@ -130,18 +135,24 @@ check_SCRIPTS = \
common_utils_tests
if HAVE_OCAMLOPT
common_utils_tests.cmx: OCAMLPACKAGES += $(OCAMLPACKAGES_TESTS)
common_utils_tests: common_gettext.cmx common_utils.cmx common_utils_tests.cmx
$(OCAMLFIND) ocamlopt $(OCAMLFLAGS) $(OCAMLPACKAGES) \
$(OCAMLFIND) ocamlopt $(OCAMLFLAGS) $(OCAMLPACKAGES) $(OCAMLPACKAGES_TESTS) \
mlguestfs.cmxa -linkpkg $^ -cclib '$(LIBTINFO_LIBS)' -o $@
else
common_utils_tests.cmo: OCAMLPACKAGES += $(OCAMLPACKAGES_TESTS)
common_utils_tests: common_gettext.cmo common_utils.cmo common_utils_tests.cmo
$(OCAMLFIND) ocamlc $(OCAMLFLAGS) $(OCAMLPACKAGES) \
$(OCAMLFIND) ocamlc $(OCAMLFLAGS) $(OCAMLPACKAGES) $(OCAMLPACKAGES_TESTS) \
mlguestfs.cma -linkpkg $^ -cclib '$(LIBTINFO_LIBS)' -custom -o $@
endif
TESTS_ENVIRONMENT = $(top_builddir)/run --test
TESTS = common_utils_tests
TESTS =
if HAVE_OCAML_PKG_OUNIT
TESTS += common_utils_tests
endif
check-valgrind:
$(MAKE) VG="$(top_builddir)/run @VG@" check

View File

@@ -18,84 +18,117 @@
(* This file tests the Common_utils module. *)
open OUnit
open Common_utils
let prog = "common_utils_tests"
(* Utils. *)
let assert_equal_string = assert_equal ~printer:(fun x -> x)
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)
(* Test Common_utils.int_of_le32 and Common_utils.le32_of_int. *)
let () =
assert (int_of_le32 "\x80\x60\x40\x20" = 0x20406080L);
assert (le32_of_int 0x20406080L = "\x80\x60\x40\x20")
let test_le32 () =
assert_equal_int64 0x20406080L (int_of_le32 "\x80\x60\x40\x20");
assert_equal_string "\x80\x60\x40\x20" (le32_of_int 0x20406080L)
(* Test Common_utils.parse_size. *)
let () =
let test_parse_resize () =
(* For absolute sizes, oldsize is ignored. *)
assert (parse_resize ~prog 100_L "100b" = 100_L);
assert (parse_resize ~prog 1000_L "100b" = 100_L);
assert (parse_resize ~prog 10000_L "100b" = 100_L);
assert (parse_resize ~prog 100_L "100K" = 102400_L);
assert_equal_int64 100_L (parse_resize ~prog 100_L "100b");
assert_equal_int64 100_L (parse_resize ~prog 1000_L "100b");
assert_equal_int64 100_L (parse_resize ~prog 10000_L "100b");
assert_equal_int64 102400_L (parse_resize ~prog 100_L "100K");
(* Fractions are always rounded down. *)
assert (parse_resize ~prog 100_L "1.1K" = 1126_L);
assert (parse_resize ~prog 100_L "100.1M" = 104962457_L);
assert (parse_resize ~prog 100_L "123.4G" = 132499741081_L);
assert_equal_int64 1126_L (parse_resize ~prog 100_L "1.1K");
assert_equal_int64 104962457_L (parse_resize ~prog 100_L "100.1M");
assert_equal_int64 132499741081_L (parse_resize ~prog 100_L "123.4G");
(* oldsize +/- a constant. *)
assert (parse_resize ~prog 100_L "+1b" = 101_L);
assert (parse_resize ~prog 100_L "-2b" = 98_L);
assert (parse_resize ~prog 100_L "+1K" = 1124_L);
assert (parse_resize ~prog 1024_L "-1K" = 0_L);
assert (parse_resize ~prog 1126_L "-1.1K" = 0_L);
assert (parse_resize ~prog 1024_L "+1.1M" = 1154457_L);
assert (parse_resize ~prog 132499741081_L "-123.3G" = 107374182_L);
assert_equal_int64 101_L (parse_resize ~prog 100_L "+1b");
assert_equal_int64 98_L (parse_resize ~prog 100_L "-2b");
assert_equal_int64 1124_L (parse_resize ~prog 100_L "+1K");
assert_equal_int64 0_L (parse_resize ~prog 1024_L "-1K");
assert_equal_int64 0_L (parse_resize ~prog 1126_L "-1.1K");
assert_equal_int64 1154457_L (parse_resize ~prog 1024_L "+1.1M");
assert_equal_int64 107374182_L (parse_resize ~prog 132499741081_L "-123.3G");
(* oldsize +/- a percentage. *)
assert (parse_resize ~prog 100_L "+1%" = 101_L);
assert (parse_resize ~prog 100_L "-1%" = 99_L);
assert (parse_resize ~prog 100000_L "+1%" = 101000_L);
assert (parse_resize ~prog 100000_L "-1%" = 99000_L);
assert (parse_resize ~prog 100000_L "+50%" = 150000_L);
assert (parse_resize ~prog 100000_L "-50%" = 50000_L);
assert (parse_resize ~prog 100000_L "+100%" = 200000_L);
assert (parse_resize ~prog 100000_L "-100%" = 0_L);
assert (parse_resize ~prog 100000_L "+200%" = 300000_L);
assert (parse_resize ~prog 100000_L "+300%" = 400000_L);
assert_equal_int64 101_L (parse_resize ~prog 100_L "+1%");
assert_equal_int64 99_L (parse_resize ~prog 100_L "-1%");
assert_equal_int64 101000_L (parse_resize ~prog 100000_L "+1%");
assert_equal_int64 99000_L (parse_resize ~prog 100000_L "-1%");
assert_equal_int64 150000_L (parse_resize ~prog 100000_L "+50%");
assert_equal_int64 50000_L (parse_resize ~prog 100000_L "-50%");
assert_equal_int64 200000_L (parse_resize ~prog 100000_L "+100%");
assert_equal_int64 0_L (parse_resize ~prog 100000_L "-100%");
assert_equal_int64 300000_L (parse_resize ~prog 100000_L "+200%");
assert_equal_int64 400000_L (parse_resize ~prog 100000_L "+300%");
(* Implementation rounds numbers so that only a single digit after
* the decimal point is significant.
*)
assert (parse_resize ~prog 100000_L "+1.1%" = 101100_L);
assert (parse_resize ~prog 100000_L "+1.12%" = 101100_L)
assert_equal_int64 101100_L (parse_resize ~prog 100000_L "+1.1%");
assert_equal_int64 101100_L (parse_resize ~prog 100000_L "+1.12%")
(* Test Common_utils.human_size. *)
let test_human_size () =
assert_equal_string "100" (human_size 100_L);
assert_equal_string "-100" (human_size (-100_L));
assert_equal_string "1.0K" (human_size 1024_L);
assert_equal_string "-1.0K" (human_size (-1024_L));
assert_equal_string "1.1K" (human_size 1126_L);
assert_equal_string "-1.1K" (human_size (-1126_L));
assert_equal_string "1.3M" (human_size 1363149_L);
assert_equal_string "-1.3M" (human_size (-1363149_L));
assert_equal_string "3.4G" (human_size 3650722201_L);
assert_equal_string "-3.4G" (human_size (-3650722201_L))
(* Test Common_utils.string_prefix. *)
let test_string_prefix () =
assert_bool "string_prefix,," (string_prefix "" "");
assert_bool "string_prefix,foo," (string_prefix "foo" "");
assert_bool "string_prefix,foo,foo" (string_prefix "foo" "foo");
assert_bool "string_prefix,foo123,foo" (string_prefix "foo123" "foo");
assert_bool "not (string_prefix,,foo" (not (string_prefix "" "foo"))
(* Test Common_utils.string_suffix. *)
let test_string_suffix () =
assert_bool "string_suffix,," (string_suffix "" "");
assert_bool "string_suffix,foo," (string_suffix "foo" "");
assert_bool "string_suffix,foo,foo" (string_suffix "foo" "foo");
assert_bool "string_suffix,123foo,foo" (string_suffix "123foo" "foo");
assert_bool "not string_suffix,,foo" (not (string_suffix "" "foo"))
(* Test Common_utils.string_find. *)
let test_string_find () =
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")
(* Suites declaration. *)
let suite =
TestList ([
"numeric" >::: [
"le32" >:: test_le32;
];
"sizes" >::: [
"parse_resize" >:: test_parse_resize;
"human_size" >:: test_human_size;
];
"strings" >::: [
"prefix" >:: test_string_prefix;
"suffix" >:: test_string_suffix;
"find" >:: test_string_find;
];
])
let _ =
run_test_tt_main suite
let () =
assert (human_size 100_L = "100");
assert (human_size (-100_L) = "-100");
assert (human_size 1024_L = "1.0K");
assert (human_size (-1024_L) = "-1.0K");
assert (human_size 1126_L = "1.1K");
assert (human_size (-1126_L) = "-1.1K");
assert (human_size 1363149_L = "1.3M");
assert (human_size (-1363149_L) = "-1.3M");
assert (human_size 3650722201_L = "3.4G");
assert (human_size (-3650722201_L) = "-3.4G")
(* Test Common_utils.string_prefix, string_suffix, string_find. *)
let () =
assert (string_prefix "" "");
assert (string_prefix "foo" "");
assert (string_prefix "foo" "foo");
assert (string_prefix "foo123" "foo");
assert (not (string_prefix "" "foo"));
assert (string_suffix "" "");
assert (string_suffix "foo" "");
assert (string_suffix "foo" "foo");
assert (string_suffix "123foo" "foo");
assert (not (string_suffix "" "foo"));
assert (string_find "" "" = 0);
assert (string_find "foo" "" = 0);
assert (string_find "foo" "o" = 1);
assert (string_find "foobar" "bar" = 3);
assert (string_find "" "baz" = -1);
assert (string_find "foobar" "baz" = -1)
Printf.fprintf stderr "\n"