resize: Add tests for some Utils functions.
authorRichard W.M. Jones <rjones@redhat.com>
Thu, 3 Nov 2011 10:32:02 +0000 (10:32 +0000)
committerRichard W.M. Jones <rjones@redhat.com>
Thu, 3 Nov 2011 10:32:02 +0000 (10:32 +0000)
.gitignore
resize/.depend
resize/Makefile.am
resize/utils_tests.ml [new file with mode: 0644]

index fc70e76..d8f4628 100644 (file)
@@ -311,6 +311,7 @@ rescue/stamp-virt-rescue.pod
 rescue/virt-rescue
 rescue/virt-rescue.1
 resize/stamp-virt-resize.pod
 rescue/virt-rescue
 rescue/virt-rescue.1
 resize/stamp-virt-resize.pod
+resize/utils_tests
 resize/virt-resize
 resize/virt-resize.1
 ruby/bindtests.rb
 resize/virt-resize
 resize/virt-resize.1
 ruby/bindtests.rb
index 17bdaca..14d8b51 100644 (file)
@@ -5,3 +5,5 @@ resize.cmo: utils.cmo progress.cmi ../ocaml/guestfs.cmi
 resize.cmx: utils.cmx progress.cmx ../ocaml/guestfs.cmx
 utils.cmo: ../ocaml/guestfs.cmi
 utils.cmx: ../ocaml/guestfs.cmx
 resize.cmx: utils.cmx progress.cmx ../ocaml/guestfs.cmx
 utils.cmo: ../ocaml/guestfs.cmi
 utils.cmx: ../ocaml/guestfs.cmx
+utils_tests.cmo: utils.cmo
+utils_tests.cmx: utils.cmx
index e8ffff0..32c5325 100644 (file)
@@ -33,7 +33,8 @@ SOURCES = \
        progress.mli \
        progress.ml \
        resize.ml \
        progress.mli \
        progress.ml \
        resize.ml \
-       utils.ml
+       utils.ml \
+       utils_tests.ml
 
 # Note this list must be in dependency order.
 OBJECTS = \
 
 # Note this list must be in dependency order.
 OBJECTS = \
@@ -91,6 +92,12 @@ CLEANFILES += stamp-virt-resize.pod
 
 # Tests.
 
 
 # Tests.
 
+check_SCRIPTS = utils_tests
+
+utils_tests: utils.cmx utils_tests.cmx
+       $(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) \
+         mlguestfs.cmxa -linkpkg $^ -cclib -lncurses -o $@
+
 random_val := $(shell awk 'BEGIN{srand(); print 1+int(255*rand())}' < /dev/null)
 
 TESTS_ENVIRONMENT = \
 random_val := $(shell awk 'BEGIN{srand(); print 1+int(255*rand())}' < /dev/null)
 
 TESTS_ENVIRONMENT = \
@@ -99,7 +106,7 @@ TESTS_ENVIRONMENT = \
        LIBGUESTFS_PATH=$(top_builddir)/appliance \
        TMPDIR=$(top_builddir)
 
        LIBGUESTFS_PATH=$(top_builddir)/appliance \
        TMPDIR=$(top_builddir)
 
-TESTS = test-virt-resize.sh
+TESTS = test-virt-resize.sh utils_tests
 
 # Dependencies.
 depend: .depend
 
 # Dependencies.
 depend: .depend
diff --git a/resize/utils_tests.ml b/resize/utils_tests.ml
new file mode 100644 (file)
index 0000000..9356d7b
--- /dev/null
@@ -0,0 +1,87 @@
+(* virt-resize
+ * Copyright (C) 2011 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 Utils module. *)
+
+open Utils
+
+(* Test Utils.int_of_le32 and Utils.le32_of_int. *)
+let () =
+  assert (int_of_le32 "\x80\x60\x40\x20" = 0x20406080L);
+  assert (le32_of_int 0x20406080L = "\x80\x60\x40\x20")
+
+(* Test Utils.canonicalize. *)
+let () =
+  assert (canonicalize "/dev/vda" = "/dev/sda");
+  assert (canonicalize "/dev/hda3" = "/dev/sda3");
+  assert (canonicalize "/dev/sda4" = "/dev/sda4");
+  assert (canonicalize "/dev/hdaa" = "/dev/sdaa");
+  assert (canonicalize "/dev/sdaa" = "/dev/sdaa");
+  assert (canonicalize "/dev/cciss/c0d0p1" = "/dev/cciss/c0d0p1")
+
+(* Test Utils.parse_size. *)
+let () =
+  (* For absolute sizes, oldsize is ignored. *)
+  assert (parse_size 100_L "100b" = 100_L);
+  assert (parse_size 1000_L "100b" = 100_L);
+  assert (parse_size 10000_L "100b" = 100_L);
+  assert (parse_size 100_L "100K" = 102400_L);
+  (* Fractions are always rounded down. *)
+  assert (parse_size 100_L "1.1K" = 1126_L);
+  assert (parse_size 100_L "100.1M" = 104962457_L);
+  assert (parse_size 100_L "123.4G" = 132499741081_L);
+
+  (* oldsize +/- a constant. *)
+  assert (parse_size 100_L "+1b" = 101_L);
+  assert (parse_size 100_L "-2b" = 98_L);
+  assert (parse_size 100_L "+1K" = 1124_L);
+  assert (parse_size 1024_L "-1K" = 0_L);
+  assert (parse_size 1126_L "-1.1K" = 0_L);
+  assert (parse_size 1024_L "+1.1M" = 1154457_L);
+  assert (parse_size 132499741081_L "-123.3G" = 107374182_L);
+
+  (* oldsize +/- a percentage. *)
+  assert (parse_size 100_L "+1%" = 101_L);
+  assert (parse_size 100_L "-1%" = 99_L);
+  assert (parse_size 100000_L "+1%" = 101000_L);
+  assert (parse_size 100000_L "-1%" = 99000_L);
+  assert (parse_size 100000_L "+50%" = 150000_L);
+  assert (parse_size 100000_L "-50%" = 50000_L);
+  assert (parse_size 100000_L "+100%" = 200000_L);
+  assert (parse_size 100000_L "-100%" = 0_L);
+  assert (parse_size 100000_L "+200%" = 300000_L);
+  assert (parse_size 100000_L "+300%" = 400000_L);
+
+  (* Implementation rounds numbers so that only a single digit after
+   * the decimal point is significant.
+   *)
+  assert (parse_size 100000_L "+1.1%" = 101100_L);
+  assert (parse_size 100000_L "+1.12%" = 101100_L)
+
+(* Test Utils.human_size. *)
+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")