File: conversions

package info (click to toggle)
partman-base 239
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,016 kB
  • sloc: sh: 2,351; ansic: 2,292; makefile: 33
file content (54 lines) | stat: -rwxr-xr-x 1,388 bytes parent folder | download | duplicates (2)
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
#! /bin/sh
set -e

# Tests for partman's long integer <-> human representation conversions.

export DEBIAN_HAS_FRONTEND=1
export PARTMAN_TEST=1

. ../lib/base.sh

[ "$TEST_VERBOSE" ] || export TEST_VERBOSE=0

testnum=1
failures=0

test_eq () {
	if [ "$1" = "$2" ]; then
		if [ "$TEST_VERBOSE" -ge 1 ]; then
			echo "PASS $testnum '$1' = '$2'" >&2
		fi
	else
		echo "FAIL $testnum '$1' != '$2'" >&2
		failures="$(($failures + 1))"
	fi
	testnum="$(($testnum + 1))"
}

test_eq "$(longint2human 1)" '1.0 B'
test_eq "$(longint2human 30)" '30.0 B'
test_eq "$(longint2human 1049)" '1.0 kB'
test_eq "$(longint2human 1050)" '1.1 kB'
test_eq "$(longint2human 999999)" '1000.0 kB'
test_eq "$(longint2human 1000000)" '1.0 MB'
test_eq "$(longint2human 4294967296)" '4.3 GB'

test_eq "$(human2longint 3.5)" 3500000000
test_eq "$(human2longint 2B)" 2
test_eq "$(human2longint '3.9 b')" 3
test_eq "$(human2longint '4294967296 B')" 4294967296
test_eq "$(human2longint 2k)" 2000
test_eq "$(human2longint 2ki)" 2048
test_eq "$(human2longint 2.5 KiB)" 2560
test_eq "$(human2longint '2 M')" 2000000
test_eq "$(human2longint '2 MiB')" 2097152
test_eq "$(human2longint 20000KB)" 20000000
test_eq "$(human2longint '500.7 TB')" 500700000000000
test_eq "$(human2longint '500.7 PB')" 500700000000000000
test_eq "$(human2longint '12 EiB')" 13835058055282163712

if [ "$failures" != 0 ]; then
	exit 1
else
	exit 0
fi