1#! /bin/sh
2#
3# Copyright by The HDF Group.
4# Copyright by the Board of Trustees of the University of Illinois.
5# All rights reserved.
6#
7# This file is part of HDF5.  The full HDF5 copyright notice, including
8# terms governing use, modification, and redistribution, is contained in
9# the COPYING file, which can be found at the root of the source code
10# distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.
11# If you do not have access to either file, you may request a copy from
12# help@hdfgroup.org.
13#
14# Tests for the h5stat tool
15#
16# Modifcations:
17#     Vailin Choi; July 2013
18#     Add tests for -l, -m, -a options
19#
20
21srcdir=@srcdir@
22
23# Determine which filters are available
24USE_FILTER_SZIP="@USE_FILTER_SZIP@"
25USE_FILTER_DEFLATE="@USE_FILTER_DEFLATE@"
26
27TESTNAME=h5stat
28EXIT_SUCCESS=0
29EXIT_FAILURE=1
30
31STAT=../../src/h5stat/h5stat               # The tool name
32STAT_BIN=`pwd`/$STAT    # The path of the tool binary
33
34RM='rm -rf'
35CMP='cmp -s'
36DIFF='diff -c'
37CP='cp'
38DIRNAME='dirname'
39LS='ls'
40AWK='awk'
41
42nerrors=0
43verbose=yes
44
45# source dirs
46SRC_TOOLS="$srcdir/../.."
47SRC_TOOLS_TESTFILES="$SRC_TOOLS/testfiles"
48
49# testfiles source dirs for tools
50SRC_H5LS_TESTFILES="$SRC_TOOLS_TESTFILES"
51SRC_H5DUMP_TESTFILES="$SRC_TOOLS_TESTFILES"
52SRC_H5DIFF_TESTFILES="$SRC_TOOLS/test/h5diff/testfiles"
53SRC_H5COPY_TESTFILES="$SRC_TOOLS/test/h5copy/testfiles"
54SRC_H5REPACK_TESTFILES="$SRC_TOOLS/test/h5repack/testfiles"
55SRC_H5JAM_TESTFILES="$SRC_TOOLS/test/h5jam/testfiles"
56SRC_H5STAT_TESTFILES="$SRC_TOOLS/test/h5stat/testfiles"
57SRC_H5IMPORT_TESTFILES="$SRC_TOOLS/test/h5import/testfiles"
58
59TESTDIR=./testfiles
60test -d $TESTDIR || mkdir $TESTDIR
61
62######################################################################
63# test files
64# --------------------------------------------------------------------
65# All the test files copy from source directory to test directory
66# NOTE: Keep this framework to add/remove test files.
67#       Any test files from other tools can be used in this framework.
68#       This list are also used for checking exist.
69#       Comment '#' without space can be used.
70# --------------------------------------------------------------------
71LIST_HDF5_TEST_FILES="
72$SRC_H5STAT_TESTFILES/h5stat_err_refcount.h5
73$SRC_H5STAT_TESTFILES/h5stat_err_old_layout.h5
74$SRC_H5STAT_TESTFILES/h5stat_err_old_fill.h5
75$SRC_H5STAT_TESTFILES/h5stat_filters.h5
76$SRC_H5STAT_TESTFILES/h5stat_tsohm.h5
77$SRC_H5STAT_TESTFILES/h5stat_newgrat.h5
78$SRC_H5STAT_TESTFILES/h5stat_idx.h5
79$SRC_H5STAT_TESTFILES/h5stat_threshold.h5
80"
81
82LIST_ERR_TEST_FILES="
83$SRC_H5STAT_TESTFILES/h5stat_err_refcount.err
84$SRC_H5STAT_TESTFILES/h5stat_err_old_layout.err
85$SRC_H5STAT_TESTFILES/h5stat_err_old_fill.err
86$SRC_H5STAT_TESTFILES/h5stat_err1_links.err
87$SRC_H5STAT_TESTFILES/h5stat_err1_dims.err
88$SRC_H5STAT_TESTFILES/h5stat_err1_numattrs.err
89$SRC_H5STAT_TESTFILES/h5stat_err2_numattrs.err
90$SRC_H5STAT_TESTFILES/h5stat_notexist.err
91$SRC_H5STAT_TESTFILES/h5stat_nofile.err
92"
93
94LIST_OTHER_TEST_FILES="
95$SRC_H5STAT_TESTFILES/h5stat_err_refcount.ddl
96$SRC_H5STAT_TESTFILES/h5stat_err_old_layout.ddl
97$SRC_H5STAT_TESTFILES/h5stat_err_old_fill.ddl
98$SRC_H5STAT_TESTFILES/h5stat_help1.ddl
99$SRC_H5STAT_TESTFILES/h5stat_help2.ddl
100$SRC_H5STAT_TESTFILES/h5stat_notexist.ddl
101$SRC_H5STAT_TESTFILES/h5stat_nofile.ddl
102$SRC_H5STAT_TESTFILES/h5stat_filters.ddl
103$SRC_H5STAT_TESTFILES/h5stat_filters-file.ddl
104$SRC_H5STAT_TESTFILES/h5stat_filters-F.ddl
105$SRC_H5STAT_TESTFILES/h5stat_filters-d.ddl
106$SRC_H5STAT_TESTFILES/h5stat_filters-g.ddl
107$SRC_H5STAT_TESTFILES/h5stat_filters-dT.ddl
108$SRC_H5STAT_TESTFILES/h5stat_filters-UD.ddl
109$SRC_H5STAT_TESTFILES/h5stat_filters-UT.ddl
110$SRC_H5STAT_TESTFILES/h5stat_tsohm.ddl
111$SRC_H5STAT_TESTFILES/h5stat_newgrat.ddl
112$SRC_H5STAT_TESTFILES/h5stat_newgrat-UG.ddl
113$SRC_H5STAT_TESTFILES/h5stat_newgrat-UA.ddl
114$SRC_H5STAT_TESTFILES/h5stat_idx.ddl
115$SRC_H5STAT_TESTFILES/h5stat_links1.ddl
116$SRC_H5STAT_TESTFILES/h5stat_links2.ddl
117$SRC_H5STAT_TESTFILES/h5stat_links3.ddl
118$SRC_H5STAT_TESTFILES/h5stat_links4.ddl
119$SRC_H5STAT_TESTFILES/h5stat_links5.ddl
120$SRC_H5STAT_TESTFILES/h5stat_dims1.ddl
121$SRC_H5STAT_TESTFILES/h5stat_dims2.ddl
122$SRC_H5STAT_TESTFILES/h5stat_numattrs1.ddl
123$SRC_H5STAT_TESTFILES/h5stat_numattrs2.ddl
124$SRC_H5STAT_TESTFILES/h5stat_numattrs3.ddl
125$SRC_H5STAT_TESTFILES/h5stat_numattrs4.ddl
126"
127
128#
129# copy test files and expected output files from source dirs to test dir
130#
131COPY_TESTFILES="$LIST_HDF5_TEST_FILES $LIST_ERR_TEST_FILES $LIST_OTHER_TEST_FILES"
132
133COPY_TESTFILES_TO_TESTDIR()
134{
135    # copy test files. Used -f to make sure get a new copy
136    for tstfile in $COPY_TESTFILES
137    do
138        # ignore '#' comment
139        echo $tstfile | tr -d ' ' | grep '^#' > /dev/null
140        RET=$?
141        if [ $RET -eq 1 ]; then
142            # skip cp if srcdir is same as destdir
143            # this occurs when build/test performed in source dir and
144            # make cp fail
145            SDIR=`$DIRNAME $tstfile`
146            INODE_SDIR=`$LS -i -d $SDIR | $AWK -F' ' '{print $1}'`
147            INODE_DDIR=`$LS -i -d $TESTDIR | $AWK -F' ' '{print $1}'`
148            if [ "$INODE_SDIR" != "$INODE_DDIR" ]; then
149                $CP -f $tstfile $TESTDIR
150                if [ $? -ne 0 ]; then
151                    echo "Error: FAILED to copy $tstfile ."
152
153                    # Comment out this to CREATE expected file
154                    exit $EXIT_FAILURE
155                fi
156            fi
157        fi
158    done
159}
160
161CLEAN_TESTFILES_AND_TESTDIR()
162{
163    # skip rm if srcdir is same as destdir
164    # this occurs when build/test performed in source dir and
165    # make cp fail
166    SDIR=$SRC_H5STAT_TESTFILES
167    INODE_SDIR=`$LS -i -d $SDIR | $AWK -F' ' '{print $1}'`
168    INODE_DDIR=`$LS -i -d $TESTDIR | $AWK -F' ' '{print $1}'`
169    if [ "$INODE_SDIR" != "$INODE_DDIR" ]; then
170        $RM $TESTDIR
171    fi
172}
173
174# Print a line-line message left justified in a field of 70 characters
175# beginning with the word "Testing".
176#
177TESTING() {
178   SPACES="                                                               "
179   echo "Testing $* $SPACES" | cut -c1-70 | tr -d '\012'
180}
181
182# Source in the output filter function definitions.
183. $srcdir/../../../bin/output_filter.sh
184
185# Run a test and print PASS or *FAIL*.  If a test fails then increment
186# the `nerrors' global variable and (if $verbose is set) display the
187# difference between the actual output and the expected output. The
188# expected output is given as the first argument to this function and
189# the actual output file is calculated by replacing the `.ddl' with
190# `.out'.  The actual output is not removed if $HDF5_NOCLEANUP has a
191# non-zero value.
192#
193TOOLTEST() {
194   expect="$TESTDIR/$1"
195   expect_err="$TESTDIR/`basename $1 .ddl`.err"
196   actual="$TESTDIR/`basename $1 .ddl`.out"
197   actual_err="$TESTDIR/`basename $1 .ddl`.out.err"
198   actual_sav=${actual}-sav
199   actual_err_sav=${actual_err}-sav
200   shift
201
202   # Run test.
203   TESTING $STAT $@
204   (
205      cd $TESTDIR
206      $RUNSERIAL $STAT_BIN $@
207   ) >$actual 2>$actual_err
208
209   # save actual and actual_err in case they are needed later.
210   cp $actual $actual_sav
211   STDOUT_FILTER $actual
212   cp $actual_err $actual_err_sav
213   STDERR_FILTER $actual_err
214
215   if [ ! -f $expect ]; then
216      # Compare error files if the expect file doesn't exist.
217      if $CMP $expect_err $actual_err; then
218         echo " PASSED"
219      else
220         echo "*FAILED*"
221         echo "    Expected result (*.err) differs from actual result (*.out.err)"
222         nerrors="`expr $nerrors + 1`"
223         test yes = "$verbose" && $DIFF $expect_err $actual_err |sed 's/^/    /'
224      fi
225   elif $CMP $expect $actual; then
226      echo " PASSED"
227   else
228      echo "*FAILED*"
229      echo "    Expected result (*.ddl) differs from actual result (*.out)"
230      nerrors="`expr $nerrors + 1`"
231      test yes = "$verbose" && $DIFF $expect $actual |sed 's/^/    /'
232   fi
233
234   # Clean up output file
235   if test -z "$HDF5_NOCLEANUP"; then
236      rm -f $actual $actual_err $actual_sav $actual_err_sav
237   fi
238}
239
240
241# Print a "SKIP" message
242SKIP() {
243    TESTING $STAT $@
244    echo  " -SKIP-"
245}
246
247
248
249##############################################################################
250##############################################################################
251###              T H E   T E S T S                                ###
252##############################################################################
253##############################################################################
254# prepare for test
255COPY_TESTFILES_TO_TESTDIR
256
257# Test for help flag
258TOOLTEST h5stat_help1.ddl -h
259TOOLTEST h5stat_help2.ddl --help
260# Test when h5stat a file that does not exist
261TOOLTEST h5stat_notexist.ddl notexist.h5
262TOOLTEST h5stat_nofile.ddl ''
263
264# Test file with groups, compressed datasets, user-applied fileters, etc.
265# h5stat_filters.h5 is a copy of ../../testfiles/tfilters.h5 as of release 1.8.0-alpha4
266TOOLTEST h5stat_filters.ddl h5stat_filters.h5
267TOOLTEST h5stat_filters-file.ddl  -f   h5stat_filters.h5
268TOOLTEST h5stat_filters-F.ddl  -F   h5stat_filters.h5
269TOOLTEST h5stat_filters-d.ddl  -d   h5stat_filters.h5
270TOOLTEST h5stat_filters-g.ddl  -g   h5stat_filters.h5
271TOOLTEST h5stat_filters-dT.ddl -dT  h5stat_filters.h5
272TOOLTEST h5stat_filters-UD.ddl -D h5stat_filters.h5
273TOOLTEST h5stat_filters-UT.ddl -T h5stat_filters.h5
274#
275# h5stat_tsohm.h5 is a copy of ../../../test/tsohm.h5 generated by tsohm.c
276# as of release 1.8.7-snap0 (on a 64-bit machine)
277TOOLTEST h5stat_tsohm.ddl h5stat_tsohm.h5
278# h5stat_newgrat.h5 is generated by h5stat_gentest.c
279TOOLTEST h5stat_newgrat.ddl h5stat_newgrat.h5
280TOOLTEST h5stat_newgrat-UG.ddl -G h5stat_newgrat.h5
281TOOLTEST h5stat_newgrat-UA.ddl -A h5stat_newgrat.h5
282# h5stat_idx.h5 is generated by h5stat_gentest.c
283TOOLTEST h5stat_idx.ddl h5stat_idx.h5
284#
285# Tests for -l (--links) option on h5stat_threshold.h5:
286#     -l 0 (incorrect threshold value)
287#     -g -l 8
288#     --links=8
289#     --links=20 -g
290TOOLTEST h5stat_err1_links.ddl -l 0 h5stat_threshold.h5
291TOOLTEST h5stat_links1.ddl -g -l 8 h5stat_threshold.h5
292TOOLTEST h5stat_links2.ddl --links=8 h5stat_threshold.h5
293TOOLTEST h5stat_links3.ddl --links=20 -g h5stat_threshold.h5
294#
295# Tests for -l (--links) option on h5stat_newgrat.h5:
296#     -g
297#     -g -l 40000
298TOOLTEST h5stat_links4.ddl -g h5stat_newgrat.h5
299TOOLTEST h5stat_links5.ddl -g -l 40000 h5stat_newgrat.h5
300#
301# Tests for -m (--dims) option on h5stat_threshold.h5
302#     -d --dims=-1 (incorrect threshold value)
303#     -gd -m 5
304#     -d --di=15
305TOOLTEST h5stat_err1_dims.ddl -d --dims=-1 h5stat_threshold.h5
306TOOLTEST h5stat_dims1.ddl -gd -m 5 h5stat_threshold.h5
307TOOLTEST h5stat_dims2.ddl -d --di=15 h5stat_threshold.h5
308#
309# Tests for -a option on h5stat_threshold.h5
310#     -a -2 (incorrect threshold value)
311#    --numattrs (without threshold value)
312#    -AS -a 10
313#    -a 1
314#    -A --numattrs=25
315TOOLTEST h5stat_err1_numattrs.ddl -a -2 h5stat_threshold.h5
316TOOLTEST h5stat_err2_numattrs.ddl --numattrs h5stat_threshold.h5
317TOOLTEST h5stat_numattrs1.ddl -AS -a 10 h5stat_threshold.h5
318TOOLTEST h5stat_numattrs2.ddl -a 1 h5stat_threshold.h5
319TOOLTEST h5stat_numattrs3.ddl -A --numattrs=25 h5stat_threshold.h5
320#
321# Tests for -a option on h5stat_newgrat.h5
322#    -A -a 100
323TOOLTEST h5stat_numattrs4.ddl -A -a 100 h5stat_newgrat.h5
324#
325#
326# Tests to verify HDFFV-10333
327#   h5stat_err_refcount.h5 is generated by h5stat_gentest.c
328#   h5stat_err_old_layout.h5 and h5stat_err_old_fill.h5: see explanation in h5stat_gentest.c
329TOOLTEST h5stat_err_refcount.ddl h5stat_err_refcount.h5
330TOOLTEST h5stat_err_old_layout.ddl h5stat_err_old_layout.h5
331TOOLTEST h5stat_err_old_fill.ddl h5stat_err_old_fill.h5
332#
333#
334# Clean up temporary files/directories
335CLEAN_TESTFILES_AND_TESTDIR
336
337if test $nerrors -eq 0 ; then
338    echo "All $TESTNAME tests passed."
339    exit $EXIT_SUCCESS
340else
341    echo "$TESTNAME tests failed with $nerrors errors."
342    exit $EXIT_FAILURE
343fi
344
345