2 * Copyright (C) 2011 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 (* This file tests the Utils module. *)
23 (* Test Utils.int_of_le32 and Utils.le32_of_int. *)
25 assert (int_of_le32 "\x80\x60\x40\x20" = 0x20406080L);
26 assert (le32_of_int 0x20406080L = "\x80\x60\x40\x20")
28 (* Test Utils.canonicalize. *)
30 assert (canonicalize "/dev/vda" = "/dev/sda");
31 assert (canonicalize "/dev/hda3" = "/dev/sda3");
32 assert (canonicalize "/dev/sda4" = "/dev/sda4");
33 assert (canonicalize "/dev/hdaa" = "/dev/sdaa");
34 assert (canonicalize "/dev/sdaa" = "/dev/sdaa");
35 assert (canonicalize "/dev/cciss/c0d0p1" = "/dev/cciss/c0d0p1")
37 (* Test Utils.parse_size. *)
39 (* For absolute sizes, oldsize is ignored. *)
40 assert (parse_size 100_L "100b" = 100_L);
41 assert (parse_size 1000_L "100b" = 100_L);
42 assert (parse_size 10000_L "100b" = 100_L);
43 assert (parse_size 100_L "100K" = 102400_L);
44 (* Fractions are always rounded down. *)
45 assert (parse_size 100_L "1.1K" = 1126_L);
46 assert (parse_size 100_L "100.1M" = 104962457_L);
47 assert (parse_size 100_L "123.4G" = 132499741081_L);
49 (* oldsize +/- a constant. *)
50 assert (parse_size 100_L "+1b" = 101_L);
51 assert (parse_size 100_L "-2b" = 98_L);
52 assert (parse_size 100_L "+1K" = 1124_L);
53 assert (parse_size 1024_L "-1K" = 0_L);
54 assert (parse_size 1126_L "-1.1K" = 0_L);
55 assert (parse_size 1024_L "+1.1M" = 1154457_L);
56 assert (parse_size 132499741081_L "-123.3G" = 107374182_L);
58 (* oldsize +/- a percentage. *)
59 assert (parse_size 100_L "+1%" = 101_L);
60 assert (parse_size 100_L "-1%" = 99_L);
61 assert (parse_size 100000_L "+1%" = 101000_L);
62 assert (parse_size 100000_L "-1%" = 99000_L);
63 assert (parse_size 100000_L "+50%" = 150000_L);
64 assert (parse_size 100000_L "-50%" = 50000_L);
65 assert (parse_size 100000_L "+100%" = 200000_L);
66 assert (parse_size 100000_L "-100%" = 0_L);
67 assert (parse_size 100000_L "+200%" = 300000_L);
68 assert (parse_size 100000_L "+300%" = 400000_L);
70 (* Implementation rounds numbers so that only a single digit after
71 * the decimal point is significant.
73 assert (parse_size 100000_L "+1.1%" = 101100_L);
74 assert (parse_size 100000_L "+1.12%" = 101100_L)
76 (* Test Utils.human_size. *)
78 assert (human_size 100_L = "100");
79 assert (human_size (-100_L) = "-100");
80 assert (human_size 1024_L = "1.0K");
81 assert (human_size (-1024_L) = "-1.0K");
82 assert (human_size 1126_L = "1.1K");
83 assert (human_size (-1126_L) = "-1.1K");
84 assert (human_size 1363149_L = "1.3M");
85 assert (human_size (-1363149_L) = "-1.3M");
86 assert (human_size 3650722201_L = "3.4G");
87 assert (human_size (-3650722201_L) = "-3.4G")