mirror of
https://github.com/Unidata/netcdf-c.git
synced 2024-12-09 08:11:38 +08:00
added user test to check for memory problems
This commit is contained in:
parent
a5861e57ec
commit
26a520147e
@ -17,7 +17,7 @@ AM_LDFLAGS =
|
||||
|
||||
CLEANFILES = nctst_classic.nc nctst_64bit_offset.nc nctst_netcdf4.nc \
|
||||
nctst_netcdf4_classic.nc nctst_classic.cdl nctst_64bit_offset.cdl \
|
||||
tst_failure.nc
|
||||
tst_*.nc
|
||||
|
||||
EXTRA_DIST = ref_nctst.cdl ref_nctst_64bit_offset.cdl \
|
||||
ref_nctst_netcdf4.cdl ref_nctst_netcdf4_classic.cdl run_nc_tests.sh \
|
||||
@ -27,7 +27,8 @@ 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)
|
||||
TESTFILES = nctst$(EXEEXT) tst_failure$(EXEEXT) tst_many_writes$(EXEEXT)
|
||||
tst_many_writes_SOURCES = tst_many_writes.cpp
|
||||
nctst_SOURCES = nctst.cpp
|
||||
tst_failure_SOURCES = tst_failure.cpp
|
||||
XFAIL_TESTS = tst_failure$(EXEEXT)
|
||||
@ -54,5 +55,3 @@ lib_LTLIBRARIES = libnetcdf_c++.la
|
||||
# Library source.
|
||||
libnetcdf_c___la_SOURCES = netcdf.cpp ncvalues.cpp
|
||||
|
||||
test: check
|
||||
|
||||
|
92
cxx/tst_many_writes.cpp
Normal file
92
cxx/tst_many_writes.cpp
Normal file
@ -0,0 +1,92 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
extern "C" {
|
||||
#include <netcdf.h>
|
||||
}
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
//Exception class
|
||||
class NcErrorException : public exception
|
||||
{
|
||||
public:
|
||||
NcErrorException(const string& descr) throw(): exception(), _descr(descr) {};
|
||||
~NcErrorException() throw() {};
|
||||
|
||||
const char* what() const throw() { ostringstream err; err << "NcErrorException: " << _descr; return err.str().c_str(); };
|
||||
|
||||
|
||||
private:
|
||||
string _descr;
|
||||
};
|
||||
|
||||
void handle_error(int status) {
|
||||
if (status != NC_NOERR) {
|
||||
throw NcErrorException(nc_strerror(status));
|
||||
}
|
||||
};
|
||||
|
||||
/******MAIN********/
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int NUMVARS = 5;
|
||||
size_t NUMREC=10000;
|
||||
int fileId, dimId, varId[NUMVARS];
|
||||
string filename("tst_many_writes.nc");
|
||||
|
||||
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
|
||||
//handle_error ( nc_def_dim(fileId, "rec", NUMREC, &dimId) ); //--> Good!!
|
||||
|
||||
int dimids[1] = {dimId};
|
||||
|
||||
//define NUMVARS variables named field_%i using a loop
|
||||
for (int v=0; v<NUMVARS; v++)
|
||||
{
|
||||
size_t chunkSize[1] = {100000};
|
||||
ostringstream varName; varName << "field_" << v;
|
||||
handle_error ( nc_def_var(fileId, varName.str().c_str(), NC_DOUBLE, 1, dimids , &varId[v]) );
|
||||
handle_error ( nc_def_var_chunking(fileId, varId[v], NC_CHUNKED, chunkSize ) );
|
||||
}
|
||||
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] ;
|
||||
char charName[NC_MAX_NAME+1];
|
||||
|
||||
for (int v=0; v<NUMVARS; v++)
|
||||
{
|
||||
handle_error ( nc_inq_varname(fileId, varId[v], charName ) );
|
||||
cout << "Writing data to variable <" << charName << ">" << 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) );
|
||||
|
||||
}
|
||||
catch(exception &ex) //exception handling
|
||||
{
|
||||
cerr << "Exception caught: " << ex.what() << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
cout << "Test passed: file " << filename << " successfully written." << endl;
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user