tests: factor 350 fail=0 initializations into test-lib.sh
[platform/upstream/coreutils.git] / tests / du / 2g
1 #!/bin/sh
2 # Ensure that du can handle a 2GB file (i.e., a file of size 2^31 bytes)
3 # Before coreutils-5.93, on systems with a signed, 32-bit stat.st_blocks
4 # one of du's computations would overflow.
5
6 # Copyright (C) 2005-2009 Free Software Foundation, Inc.
7
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 if test "$VERBOSE" = yes; then
22   set -x
23   du --version
24 fi
25
26 . $srcdir/test-lib.sh
27
28 # Creating a 2GB file counts as `very expensive'.
29 very_expensive_
30
31
32 # Get number of free kilobytes on current partition, so we can
33 # skip this test if there is insufficient free space.
34
35 # This technique relies on the fact that the `Available' kilobyte
36 # count is the number just before the one with a trailing `%'.
37 free_kb=`df -kP .|tail -1|sed 's/ [0-9][0-9]*%.*//;s/ *$//;s/.* //'`
38 case "$free_kb" in
39   [0-9]*) ;;
40   *) skip_test_ "invalid size from df: $free_kb";;
41 esac
42
43 # Require about 3GB free.
44 min_kb=3000000
45 test $min_kb -lt $free_kb ||
46 {
47   skip_test_ "too little free space on current partition: $free_kb (need $min_kb KB)"
48 }
49
50 big=big
51 rm -f $big
52 test -t 1 || printf 'creating a 2GB file...\n'
53 for i in `seq 100`; do
54   # Note: 2147483648 == 2^31. Print floor(2^31/100) per iteration.
55   printf %21474836s x >> $big || fail=1
56   # On the final iteration, append the remaining 48 bytes.
57   test $i = 100 && { printf %48s x >> $big || fail=1; }
58   test -t 1 && printf 'creating a 2GB file: %d%% complete\r' $i
59 done
60 echo
61
62 du -k $big > out1 || fail=1
63 rm -f $big
64 sed 's/^2[0-9][0-9][0-9][0-9][0-9][0-9] '$big'$/~2M/' out1 > out
65
66 cat <<\EOF > exp || fail=1
67 ~2M
68 EOF
69
70 compare out exp || fail=1
71
72 Exit $fail