summaryrefslogtreecommitdiffstats
path: root/hl/tools/h5watch/testh5watch.sh.in
blob: 84511511537ce98ad8ed6e3b14f21b13334340e7 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#! /bin/sh
#
# Copyright by The HDF Group.
# Copyright by the Board of Trustees of the University of Illinois.
# All rights reserved.
#
# This file is part of HDF5.  The full HDF5 copyright notice, including
# terms governing use, modification, and redistribution, is contained in
# the files COPYING and Copyright.html.  COPYING can be found at the root
# of the source code distribution tree; Copyright.html can be found at the
# root level of an installed copy of the electronic HDF5 document set and
# is linked from the top-level documents page.  It can also be found at
# http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have
# access to either file, you may request a copy from help@hdfgroup.org.
#
# Tests for the h5watch tool
#
H5WATCH=h5watch               	# The tool name
H5WATCH_BIN=`pwd`/$H5WATCH    	# The path of H5WATCH
EXTEND_DSET=extend_dset 	# Helper to extend the dataset when watching
EXTEND_BIN=`pwd`/$EXTEND_DSET 	# The path of EXTEND_DSET
#
EXIT_SUCCESS=0
EXIT_FAILURE=1
#
GEN_TEST=h5watchgentest	      	# Generate HDF5 file with various datasets
GEN_TEST_BIN=`pwd`/$GEN_TEST 	# Path of the binary GEN_TEST
#

CP='cp'
CMP='cmp -s'
DIFF='diff -c'
KILL='kill'
SLEEP='sleep'
NLINES=20			# Max. lines of output to display if test fails
#
# Mac OS: just to make sure echo "backslash backslash" behaves properly
if test `uname -s` = 'Darwin'; then
    ECHO='/bin/echo'
else
    ECHO='echo'
fi

# Global variables
nerrors=0
verbose=yes

# The build (current) directory might be different than the source directory.
if test -z "$srcdir"; then
    srcdir=.
fi
test -d ../testfiles || mkdir ../testfiles

# Print a line-line message left justified in a field of 70 characters
# beginning with the word "Testing".
TESTING() {
    SPACES="                                                               "
    $ECHO "Testing $* $SPACES" |cut -c1-70 |tr -d '\012'
}

#
# Overall algorithm:
#
# Run a test and print PASSED or FAILED
# If a test did not return with the expected return code, 
# increment the `nerrors' global variable and (if $verbose is set) display up to $NLINES
# lines of the actual output from the test.  
# If the test did return the expected code,
# compare the actual output with the expected output;
# If the outputs are the same, print PASSED,
# Otherwise print FAILED and the difference between the two outputs.
# The output files are not removed if $HDF5_NOCLEANUP has a non-zero value.
#
#
# TOOLTEST():
#
# Arguments:
#
# $1 -- expected output
# $2 -- expected return code
# $3 and on -- arguments for h5watch
TOOLTEST() {
    expect="$srcdir/../testfiles/$1"
    actual="../testfiles/`basename $1 .ddl`.out"
    actual_err="../testfiles/`basename $1 .ddl`.err"
    shift
    retvalexpect=$1
    shift
    # Run test.
    # Stderr is included in stdout so that the diff can detect
    # any unexpected output from that stream too.
    TESTING $H5WATCH $@
    (
	$ECHO "#############################"
	$ECHO " output for '$H5WATCH $@'" 
	$ECHO "#############################"
        $RUNSERIAL $H5WATCH_BIN "$@"
    ) > $actual 2>$actual_err
    exitcode=$?
    cat $actual_err >> $actual
    if [ $exitcode -ne $retvalexpect ]; then
	$ECHO "*FAILED*"
	nerrors="`expr $nerrors + 1`"
	if [ yes = "$verbose" ]; then
	    $ECHO "test returned with exit code $exitcode"
	    $ECHO "test output: (up to $NLINES lines)"
	    head -$NLINES $actual
	    $ECHO "***end of test output***"
	    $ECHO ""
	fi
    elif $CMP $expect $actual; then
        $ECHO " PASSED"
    else
        $ECHO "*FAILED*"
	$ECHO "    Expected result differs from actual result"
	nerrors="`expr $nerrors + 1`"
	test yes = "$verbose" && $DIFF $expect $actual |sed 's/^/    /'
    fi

    # Clean up output file
    if test -z "$HDF5_NOCLEANUP"; then
	rm -f $actual $actual_err
    fi
}

#
# TEST_WATCH(): TEST_WATCH $1 $2 $3 $4 $5 ...
# 
# Arguments:
#	$1 -- number of objects to watch and to extend
#	$2 -- the specified file to watch and to extend 
#	      (filename varies with $1 suffix)
#	$3 -- the specified dataset to watch and to extend
#	$4 -- the options to h5watch (can be NULL)
#	$5 -- expected output from watching the object
#	($3, $4, $5) can be repeated for $1 times
#
# This is done to reduce the sleeping time for testing
#
TEST_WATCH() {
    plist="" 	# plist: the list of h5watch process IDs
    dlist="" 	# dlist: the list of dataset names to watch/extend
    outlist="" 	# outlist: the list of expected output files with suffix ".ddl" stripped
    xlist="" 	# xlist: the list of exit codes from extending the dataset
    NUM=$1	# number of objects to watch/extend
    shift
    NFNAME="`basename $1 .h5`"		#the file to watch and to extend stripped of suffix ".h5"
    shift
    ctr=1				#counter for tracking the # of objects to test
    while [ $ctr -le $NUM ] ;		#loop till end of all objects to test
    do
	actual="../testfiles/`basename $3 .ddl`.out"	#the actual output
	outlist="$outlist`basename $3 .ddl` "		#store to the list of output names
	dlist="$dlist$1 "				#store to the list of datasets
	#
	$CP "$NFNAME.h5" "$NFNAME$ctr.h5"		#copy the file to a temporary file
	#
	if test -z "$2"; then				#compile options & object to h5watch
	    OBJ="$NFNAME$ctr.h5/$1"			#no options, just object
	else 
	    OBJ="$2 $NFNAME$ctr.h5/$1"			#option and object
	fi
	#
	# Run h5watch and put in background, output to a file
	#
	TESTING $H5WATCH $OBJ
	$ECHO ""
	$ECHO "#############################" 	> $actual
	$ECHO " output for '$H5WATCH $OBJ'"  	>>$actual
	$ECHO "#############################"	>>$actual
	$RUNSERIAL $H5WATCH_BIN $2 "$NFNAME$ctr.h5/$1" >> $actual &
	WATCH_PID=$!					#get h5watch process ID
	plist="$plist$! "				#store to the list of process IDs
	shift; shift; shift
	ctr=`expr $ctr + 1` 
    done;
    #
    # Extend the list of datasets corresponding to the list of files
    n=1;
    for d in $dlist; do	
	$EXTEND_BIN "$NFNAME$n.h5" $d
	xlist="$xlist$? "				#store to the list of exit codes from extending the dataset
	n=`expr $n + 1`
    done;
    #
    # Sleep to make sure output is flushed out before stopping h5watch
    $ECHO "Sleeping for 4 seconds..."
    $SLEEP 4
    #
    # Kill the list of h5watch processes
    $ECHO "Killing h5watch processes..."
    for p in $plist; do					#kill the list of h5watch processes
	$KILL $p	
    done
    #
    # Check the list of return codes from extending the dataset
    # Compare the list of actual output from h5watch with the corresponding expected output
    n=1
    for x in $xlist; do
	outname=`$ECHO $outlist|cut -d' ' -f$n`
	actual="../testfiles/$outname.out"
	expect="$srcdir/../testfiles/$outname.ddl"
	if [ $x -ne 0 ]; then
	$ECHO "*FAILED*"
	nerrors="`expr $nerrors + 1`"
	if [ yes = "$verbose" ]; then
	    $ECHO "test returned with exit code $exitcode"
	    $ECHO "test output: (up to $NLINES lines)"
	    
	    head -$NLINES $actual
	    $ECHO "***end of test output***"
	    $ECHO ""
	fi
	elif $CMP $expect $actual; then
	    $ECHO " PASSED"
	else
	    $ECHO "*FAILED*"
	    $ECHO "    Expected result differs from actual result"
	    nerrors="`expr $nerrors + 1`"
	    test yes = "$verbose" && $DIFF $expect $actual |sed 's/^/    /'
	fi
	#
	# Cleaning
	rm -f "$NFNAME$n.h5"
	if test -z "$HDF5_NOCLEANUP"; then
	    rm -f $actual
	fi
	n="`expr $n + 1`"
    done;
}

##############################################################################
##############################################################################
###			  T H E   T E S T S                                ###
##############################################################################
##############################################################################
#
#
#################################################################################################
#												#
# WATCH.h5: file with various types of datasets for testing--					#
#   The following datasets are chunked, H5D_ALLOC_TIME_INCR, max. dimensional setting:		#
#   	DSET_ONE: one-dimensional dataset							#
#   	DSET_TWO: two-dimensional dataset							#
#   	DSET_CMPD: one-dimensional dataset with compound type 					#
#   	DSET_CMPD_ESC: one-dimensional dataset with compound type & escape/separator characters	#
#   	DSET_CMPD_TWO: two-dimensional dataset with compound type 				#
#												#
#   The following datasets are one-dimensional, chunked, max. dimension setting:		#
#   	DSET_ALLOC_EARLY: dataset with H5D_ALLOC_TIME_EARLY					#
#   	DSET_ALLOC_LATE: dataset H5D_ALLOC_TIME_LATE						#
#												#
#   The following datasets are one-dimensional:							#
#	DSET_NONE: fixed dimension setting, contiguous, H5D_ALLOC_TIME_LATE			#
#	DSET_NOMAX: fixed dimension setting, chunked, H5D_ALLOC_TIME_INCR			#
#											 	#
#################################################################################################
#
#
#################################################################################################
#												#
# Tests on expected failures:									#
#	Invalid file name									#
#	Unable to find dataset, invalid dataset							#
#	  DSET_NONE and DSET_NOMAX								#
#	Invalid input to options --width and --polling						#
#	Invalid field names for -f option							#
#												#
#################################################################################################
#
# Generate file with various types of datasets
$GEN_TEST_BIN
# Test on --help options
TOOLTEST w-help1.ddl 0 --help
#
# Tests on expected failures
TOOLTEST w-err-dset1.ddl 1 WATCH.h5
TOOLTEST w-err-dset2.ddl 1 WATCH.h5/group/DSET_CMPD
TOOLTEST w-err-dset-none.ddl 1 WATCH.h5/DSET_NONE
TOOLTEST w-err-dset-nomax.ddl 1 WATCH.h5/DSET_NOMAX
TOOLTEST w-err-file.ddl 1 ../WATCH.h5/DSET_CMPD
TOOLTEST w-err-width.ddl 1 --width=-8 WATCH.h5/DSET_ONE
TOOLTEST w-err-poll.ddl 1 --polling=-8 WATCH.h5/DSET_ONE
TOOLTEST w-err-poll0.ddl 1 --polling=0 WATCH.h5/DSET_ONE
#
# Tests on invalid field names via --fields option for a compound typed dataset: DSET_CMPD
TOOLTEST w-err-cmpd1.ddl 1 --fields=fieldx WATCH.h5/DSET_CMPD
TOOLTEST w-err-cmpd2.ddl 1 --fields=field1,field2. WATCH.h5/DSET_CMPD
TOOLTEST w-err-cmpd3.ddl 1 --fields=field1,field2, WATCH.h5/DSET_CMPD
TOOLTEST w-err-cmpd4.ddl 1 --fields=field1,field2.b.k WATCH.h5/DSET_CMPD
TOOLTEST w-err-cmpd5.ddl 1 --fields=field1 --fields=field2.b.k WATCH.h5/DSET_CMPD
#
#
#################################################################################################
#												#
# Tests on extending datasets--									#
#   	DSET_ONE, DSET_CMPD,  DSET_CMPD_ESC							#
#	DSET_ALLOC_EARLY, DSET_ALLOC_LATE							#
#   	DSET_TWO, DSET_CMPD_TWO									#
#											 	#
#################################################################################################
#
# Generate file with various types of datasets
$GEN_TEST_BIN
#
# Watching and extending: WATCH1.h5/DSET_ONE, WATCH2.h5/DSET_ALLOC_EARLY, WATCH3.h5/DSET_ALLOC_LATE WATCH4.h5/DSET_CMPD
# 			: WATCH5.h5/DSET_TWO, WATCH6.h5/DSET_CMPD_TWO, WATCH7.h5/DDSET_CMPD_ESC
TEST_WATCH 7 WATCH.h5 DSET_ONE '' w-ext-one.ddl DSET_ALLOC_EARLY '' w-ext-early.ddl DSET_ALLOC_LATE '' w-ext-late.ddl DSET_CMPD '' w-ext-cmpd.ddl DSET_TWO '' w-ext-two.ddl DSET_CMPD_TWO '' w-ext-cmpd-two.ddl DSET_CMPD_ESC '' w-ext-cmpd-esc.ddl
#
############################
# Tests on --fields option #
############################
#
# Watching and extending:
# 	WATCH1.h5/DSET_CMPD with --fields=field1,field2
#	WATCH2.h5/DSET_CMP_TWO with --fields=field1,field2
#	WATCH3.h5/DSET_CMPD_ESC with --fields=field\,1,field2\.
#	WATCH4.h5/DSET_CMPD with --fields=field1 --fields=field2 
#	WATCH5.h5/DSET_CMPD_ESC with --fields=field\,1 --fields=field2\.
#	WATCH6.h5/DSET_CMPD_TWO with --fields=field1 --fields=field2
#
TEST_WATCH 6 WATCH.h5 DSET_CMPD --fields=field1,field2 w-ext-cmpd-f1.ddl DSET_CMPD_TWO --fields=field1,field2 w-ext-cmpd-two-f1.ddl DSET_CMPD_ESC '--fields=field\,1,field2\.' w-ext-cmpd-esc-f1.ddl DSET_CMPD '--fields=field1 --fields=field2' w-ext-cmpd-ff1.ddl DSET_CMPD_ESC '--fields=field\,1 --fields=field2\.' w-ext-cmpd-esc-ff1.ddl DSET_CMPD_TWO '--fields=field1 --fields=field2' w-ext-cmpd-two-ff1.ddl
#
#
#
# Watching and extending: 
#	WATCH1.h5/DSET_CMPD with --fields=field2.b,field4 
#	WATCH2.h5/DSET_CMPD_ESC with --fields=field2\..\,b,field4\ 
#	WATCH3.h5/DSET_CMPD_TWO with --fields=field2.b,field4
#	WATCH4.h5/DSET_CMPD with --fields=field2.b --fields=field4
# 	WATCH5.h5/DSET_CMPD_ESC with --fields=field2\..\,b --fields=field4\,
# 	WATCH6.h5/DSET_CMPD_TWO with --fields=field2.b --fields=field4
TEST_WATCH 6 WATCH.h5 DSET_CMPD --fields=field2.b,field4 w-ext-cmpd-f2.ddl DSET_CMPD_ESC '--fields=field2\..\,b,field4\,' w-ext-cmpd-esc-f2.ddl DSET_CMPD_TWO --fields=field2.b,field4 w-ext-cmpd-two-f2.ddl DSET_CMPD '--fields=field2.b --fields=field4' w-ext-cmpd-ff2.ddl DSET_CMPD_ESC '--fields=field2\..\,b --fields=field4\,' w-ext-cmpd-esc-ff2.ddl DSET_CMPD_TWO '--fields=field2.b --fields=field4' w-ext-cmpd-two-ff2.ddl 
#
#
#
# Watching and extending:
#	WATCH1.h5/DSET_CMPD with --fields=field2.b.a,field2.c
# 	WATCH2.h5/DSET_CMPD_ESC with --fields=field2\..\,b.a,field2\..\\K
# 	WATCH3.h5/DSET_CMPD_TWO with --fields=field2.b.a,field2.c
#	WATCH4.h5/DSET_CMPD with --fields=field2.b.a --fields=field2.c
# 	WATCH5.h5/DSET_CMPD_ESC with --fields=field2\..\,b.a --fields=field2\..\\K
# 	WATCH6.h5/DSET_CMPD_TWO with --fields=field2.b.a --fields=field2.c
TEST_WATCH 6 WATCH.h5 DSET_CMPD --fields=field2.b.a,field2.c w-ext-cmpd-f3.ddl DSET_CMPD_ESC '--fields=field2\..\,b.a,field2\..\\K' w-ext-cmpd-esc-f3.ddl DSET_CMPD_TWO --fields=field2.b.a,field2.c w-ext-cmpd-two-f3.ddl DSET_CMPD '--fields=field2.b.a --fields=field2.c' w-ext-cmpd-ff3.ddl DSET_CMPD_ESC '--fields=field2\..\,b.a --fields=field2\..\\K' w-ext-cmpd-esc-ff3.ddl DSET_CMPD_TWO '--fields=field2.b.a --fields=field2.c' w-ext-cmpd-two-ff3.ddl 
#
#
#
#########################################################
#							#
# Tests on options:					#
#	--dim 						#
#	--width, --label, --simple, --help  		#
#							#
#########################################################
#
# Watching and extending:
#	WATCH1.h5/DSET_ONE with -d option
# 	WATCH2.h5/DSET_TWO with --dim option
#	WATCH3.h5/DSET_TWO with --width=60 option
# 	WATCH4.h5/DSET_CMPD with --label option
# 	WATCH5.h5/DSET_ONE with --simple option
TEST_WATCH 5 WATCH.h5 DSET_ONE --dim w-ext-one-d.ddl DSET_TWO --dim w-ext-two-d.ddl DSET_TWO --width=30 w-ext-two-width.ddl DSET_CMPD --label w-ext-cmpd-label.ddl DSET_ONE --simple w-ext-one-simple.ddl
#
#
#
if test $nerrors -eq 0 ; then
    $ECHO "All h5watch tests passed."
    exit $EXIT_SUCCESS
else
    $ECHO "h5watch tests failed with $nerrors errors."
    exit $EXIT_FAILURE
fi