diff --git a/cxx/Makefile.am b/cxx/Makefile.am index ce189f303..223478deb 100644 --- a/cxx/Makefile.am +++ b/cxx/Makefile.am @@ -27,16 +27,30 @@ run_nc4_tests.sh # create CDL files, which are compared with some reference files # shipped with the code. tst_failure is a program that is intended to # fail. -TESTFILES = nctst$(EXEEXT) tst_failure$(EXEEXT) tst_many_writes$(EXEEXT) +TESTFILES = nctst$(EXEEXT) tst_failure$(EXEEXT) tst_many_writes_SOURCES = tst_many_writes.cpp nctst_SOURCES = nctst.cpp tst_failure_SOURCES = tst_failure.cpp XFAIL_TESTS = tst_failure$(EXEEXT) +if USE_NETCDF4 +# This is for valgrind script. +TESTS_ENVIRONMENT=USE_NETCDF4=1 +# This is a netCDF-4 test. +TESTFILES += tst_many_writes$(EXEEXT) +tst_many_writes_SOURCES = tst_many_writes.cpp +endif # USE_NETCDF4 + # Build and run these tests. check_PROGRAMS = $(TESTFILES) TESTS = $(TESTFILES) +# This will run a bunch of the test programs with valgrind, the memory +# checking tool. (Valgrind must be present for this to work.) +if USE_VALGRIND_TESTS +TESTS += run_valgrind_tests.sh +endif # USE_VALGRIND_TESTS + # If the utilities have been built, run these extra tests. if BUILD_UTILITIES TESTS += run_nc_tests.sh diff --git a/cxx/run_valgrind_tests.sh b/cxx/run_valgrind_tests.sh new file mode 100755 index 000000000..4504007b7 --- /dev/null +++ b/cxx/run_valgrind_tests.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +# This shell runs the tests with valgrind. + +# $Id: run_valgrind_tests.sh,v 1.9 2010/01/26 20:24:18 ed Exp $ + +set -e +echo "" +echo "Testing programs with valgrind..." + +# These are my test programs. +list="" +# If we are running with netcdf4, then add tst_atts +if test "x$USE_NETCDF4" = "x1" ; then +list="$list tst_many_writes" +fi + +for tst in $list; do + echo "" + echo "Memory testing with $tst:" + valgrind -q --error-exitcode=2 --leak-check=full ./$tst +done + +echo "SUCCESS!!!" + +exit 0 diff --git a/cxx/tst_many_writes.cpp b/cxx/tst_many_writes.cpp index 5ff68ce53..35fb62aa3 100644 --- a/cxx/tst_many_writes.cpp +++ b/cxx/tst_many_writes.cpp @@ -36,10 +36,11 @@ int main(int argc, char** argv) int fileId, dimId, varId[NUMVARS]; string filename("tst_many_writes.nc"); + cout << "\n*** Testing netCDF-4 file with user-provided test (thanks Marica!)\n"; + try{ //create the netcdf-4 file handle_error ( nc_create(filename.c_str(), NC_NETCDF4, &fileId) ); - //cout << "Netcdf file " << filename << " successfully created..." << endl; //define the unlimited dimension "rec" handle_error ( nc_def_dim(fileId, "rec", NC_UNLIMITED, &dimId) ); //--> Segmentation Fault @@ -57,8 +58,6 @@ int main(int argc, char** argv) } handle_error ( nc_enddef(fileId) ); - cout << "Variables successfully defined." << endl; - //write data to the NUMVARS variables using nc_put_var1_double double data = 100; size_t index[1] ; @@ -67,26 +66,22 @@ int main(int argc, char** argv) for (int v=0; v" << endl; for (size_t i = 0; i< NUMREC ; i++) { - if( ( i% 2000 )==0) - cout << "Record n." << i << endl; index[0] = i; handle_error ( nc_put_var1_double(fileId, varId[v], index, &data ) ); } } + //close file handle_error ( nc_close(fileId) ); - + cout << "*** nctst SUCCESS!\n"; } catch(exception &ex) //exception handling { cerr << "Exception caught: " << ex.what() << endl; + cout << "*** nctst FAILURE!\n"; return -1; } - - cout << "Test passed: file " << filename << " successfully written." << endl; - }