Skip to content

Commit b50d515

Browse files
authored
Create tests of NeXus::File from old napi tests -- ornl-next (#39025)
Create tests of `NeXus::File` from old `napi` tests (#39010) * use shared_ptr for file pointer * create test of NeXusFile * creare copy * finish creation of NeXusFileTest * copy napi_test_cpp as nexus file tests * copy napi leak tests for nexus file
1 parent d934714 commit b50d515

File tree

4 files changed

+514
-41
lines changed

4 files changed

+514
-41
lines changed

Framework/Nexus/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ set(INC_FILES
2424
inc/MantidNexus/nxstack.h
2525
)
2626

27-
set(TEST_FILES H5UtilTest.h NexusIOHelperTest.h)
27+
set(TEST_FILES H5UtilTest.h NexusIOHelperTest.h NeXusFileTest.h)
2828

2929
if(COVERAGE)
3030
foreach(loop_var ${SRC_FILES} ${INC_FILES})

Framework/Nexus/inc/MantidNexus/NeXusFile.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class MANTID_NEXUS_DLL File {
3232
std::string m_filename;
3333
NXaccess m_access;
3434
/** The handle for the C-API. */
35-
NXhandle m_file_id;
35+
std::shared_ptr<NXhandle> m_pfile_id;
3636
/** should be close handle on exit */
3737
bool m_close_handle;
3838

Framework/Nexus/src/NeXusFile.cpp

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,20 @@ File::File(const char *filename, const NXaccess access) : m_filename(filename),
102102
}
103103

104104
File::File(File const &f)
105-
: m_filename(f.m_filename), m_access(f.m_access), m_file_id(f.m_file_id), m_close_handle(false) {}
105+
: m_filename(f.m_filename), m_access(f.m_access), m_pfile_id(f.m_pfile_id), m_close_handle(false) {}
106106

107107
File::File(File const *const pf)
108-
: m_filename(pf->m_filename), m_access(pf->m_access), m_file_id(pf->m_file_id), m_close_handle(false) {}
108+
: m_filename(pf->m_filename), m_access(pf->m_access), m_pfile_id(pf->m_pfile_id), m_close_handle(false) {}
109109

110110
File::File(std::shared_ptr<File> pf)
111-
: m_filename(pf->m_filename), m_access(pf->m_access), m_file_id(pf->m_file_id), m_close_handle(false) {}
111+
: m_filename(pf->m_filename), m_access(pf->m_access), m_pfile_id(pf->m_pfile_id), m_close_handle(false) {}
112112

113113
File &File::operator=(File const &f) {
114114
if (this == &f) {
115115
} else {
116116
this->m_filename = f.m_filename;
117117
this->m_access = f.m_access;
118-
this->m_file_id = f.m_file_id;
118+
this->m_pfile_id = f.m_pfile_id;
119119
this->m_close_handle = f.m_close_handle;
120120
}
121121
return *this;
@@ -126,18 +126,21 @@ void File::initOpenFile(const string &filename, const NXaccess access) {
126126
throw Exception("Filename specified is empty constructor");
127127
}
128128

129-
NXstatus status = NXopen(filename.c_str(), access, &(this->m_file_id));
129+
NXhandle temp;
130+
NXstatus status = NXopen(filename.c_str(), access, &(temp));
130131
if (status != NXstatus::NX_OK) {
131132
stringstream msg;
132133
msg << "NXopen(" << filename << ", " << access << ") failed";
133134
throw Exception(msg.str(), status);
135+
} else {
136+
this->m_pfile_id = std::make_shared<NXhandle>(temp);
134137
}
135138
}
136139

137140
File::~File() {
138-
if (m_close_handle && m_file_id != NULL) {
139-
NXstatus status = NXclose(&(this->m_file_id));
140-
this->m_file_id = NULL;
141+
if (m_close_handle && m_pfile_id != NULL) {
142+
NXstatus status = NXclose(&(*this->m_pfile_id));
143+
this->m_pfile_id = NULL;
141144
if (status != NXstatus::NX_OK) {
142145
stringstream msg;
143146
msg << "NXclose failed with status: " << status << "\n";
@@ -147,17 +150,17 @@ File::~File() {
147150
}
148151

149152
void File::close() {
150-
if (this->m_file_id != NULL) {
151-
NXstatus status = NXclose(&(this->m_file_id));
152-
this->m_file_id = NULL;
153+
if (this->m_pfile_id != NULL) {
154+
NXstatus status = NXclose(&(*this->m_pfile_id));
155+
this->m_pfile_id = NULL;
153156
if (status != NXstatus::NX_OK) {
154157
throw Exception("NXclose failed", status);
155158
}
156159
}
157160
}
158161

159162
void File::flush() {
160-
NXstatus status = NXflush(&(this->m_file_id));
163+
NXstatus status = NXflush(&(*this->m_pfile_id));
161164
if (status != NXstatus::NX_OK) {
162165
throw Exception("NXflush failed", status);
163166
}
@@ -170,7 +173,7 @@ void File::makeGroup(const string &name, const string &class_name, bool open_gro
170173
if (class_name.empty()) {
171174
throw Exception("Supplied empty class name to makeGroup");
172175
}
173-
NXstatus status = NXmakegroup(this->m_file_id, name.c_str(), class_name.c_str());
176+
NXstatus status = NXmakegroup(*(this->m_pfile_id), name.c_str(), class_name.c_str());
174177
if (status != NXstatus::NX_OK) {
175178
stringstream msg;
176179
msg << "NXmakegroup(" << name << ", " << class_name << ") failed";
@@ -188,7 +191,7 @@ void File::openGroup(const string &name, const string &class_name) {
188191
if (class_name.empty()) {
189192
throw Exception("Supplied empty class name to openGroup");
190193
}
191-
NXstatus status = NXopengroup(this->m_file_id, name.c_str(), class_name.c_str());
194+
NXstatus status = NXopengroup(*(this->m_pfile_id), name.c_str(), class_name.c_str());
192195
if (status != NXstatus::NX_OK) {
193196
stringstream msg;
194197
msg << "NXopengroup(" << name << ", " << class_name << ") failed";
@@ -200,7 +203,7 @@ void File::openPath(const string &path) {
200203
if (path.empty()) {
201204
throw Exception("Supplied empty path to openPath");
202205
}
203-
NXstatus status = NXopenpath(this->m_file_id, path.c_str());
206+
NXstatus status = NXopenpath(*(this->m_pfile_id), path.c_str());
204207
if (status != NXstatus::NX_OK) {
205208
stringstream msg;
206209
msg << "NXopenpath(" << path << ") failed";
@@ -212,7 +215,7 @@ void File::openGroupPath(const string &path) {
212215
if (path.empty()) {
213216
throw Exception("Supplied empty path to openGroupPath");
214217
}
215-
NXstatus status = NXopengrouppath(this->m_file_id, path.c_str());
218+
NXstatus status = NXopengrouppath(*(this->m_pfile_id), path.c_str());
216219
if (status != NXstatus::NX_OK) {
217220
stringstream msg;
218221
msg << "NXopengrouppath(" << path << ") failed";
@@ -224,7 +227,7 @@ std::string File::getPath() {
224227
char cPath[2048];
225228

226229
memset(cPath, 0, sizeof(cPath));
227-
NXstatus status = NXgetpath(this->m_file_id, cPath, sizeof(cPath) - 1);
230+
NXstatus status = NXgetpath(*(this->m_pfile_id), cPath, sizeof(cPath) - 1);
228231
if (status != NXstatus::NX_OK) {
229232
stringstream msg;
230233
msg << "NXgetpath() failed";
@@ -234,7 +237,7 @@ std::string File::getPath() {
234237
}
235238

236239
void File::closeGroup() {
237-
NXstatus status = NXclosegroup(this->m_file_id);
240+
NXstatus status = NXclosegroup(*(this->m_pfile_id.get()));
238241
if (status != NXstatus::NX_OK) {
239242
throw Exception("NXclosegroup failed", status);
240243
}
@@ -244,7 +247,7 @@ void File::makeData(const string &name, NXnumtype type, const vector<int> &dims,
244247
this->makeData(name, type, toDimSize(dims), open_data);
245248
}
246249

247-
void File::makeData(const string &name, NXnumtype type, const vector<int64_t> &dims, bool open_data) {
250+
void File::makeData(const string &name, NXnumtype type, const DimVector &dims, bool open_data) {
248251
// error check the parameters
249252
if (name.empty()) {
250253
throw Exception("Supplied empty label to makeData");
@@ -254,7 +257,7 @@ void File::makeData(const string &name, NXnumtype type, const vector<int64_t> &d
254257
}
255258

256259
// do the work
257-
NXstatus status = NXmakedata64(this->m_file_id, name.c_str(), type, static_cast<int>(dims.size()),
260+
NXstatus status = NXmakedata64(*(this->m_pfile_id), name.c_str(), type, static_cast<int>(dims.size()),
258261
const_cast<int64_t *>(&(dims[0])));
259262
// report errors
260263
if (status != NXstatus::NX_OK) {
@@ -360,7 +363,7 @@ void File::makeCompData(const string &name, const NXnumtype type, const vector<i
360363
}
361364

362365
void File::makeCompData(const string &name, const NXnumtype type, const DimVector &dims, const NXcompression comp,
363-
const vector<int64_t> &bufsize, bool open_data) {
366+
const DimSizeVector &bufsize, bool open_data) {
364367
// error check the parameters
365368
if (name.empty()) {
366369
throw Exception("Supplied empty name to makeCompData");
@@ -381,7 +384,7 @@ void File::makeCompData(const string &name, const NXnumtype type, const DimVecto
381384
// do the work
382385
int i_type = static_cast<int>(type);
383386
int i_comp = static_cast<int>(comp);
384-
NXstatus status = NXcompmakedata64(this->m_file_id, name.c_str(), type, static_cast<int>(dims.size()),
387+
NXstatus status = NXcompmakedata64(*(this->m_pfile_id), name.c_str(), type, static_cast<int>(dims.size()),
385388
const_cast<int64_t *>(&(dims[0])), i_comp, const_cast<int64_t *>(&(bufsize[0])));
386389

387390
// report errors
@@ -414,14 +417,14 @@ void File::openData(const string &name) {
414417
if (name.empty()) {
415418
throw Exception("Supplied empty name to openData");
416419
}
417-
NXstatus status = NXopendata(this->m_file_id, name.c_str());
420+
NXstatus status = NXopendata(*(this->m_pfile_id), name.c_str());
418421
if (status != NXstatus::NX_OK) {
419422
throw Exception("NXopendata(" + name + ") failed", status);
420423
}
421424
}
422425

423426
void File::closeData() {
424-
NXstatus status = NXclosedata(this->m_file_id);
427+
NXstatus status = NXclosedata(*(this->m_pfile_id).get());
425428
if (status != NXstatus::NX_OK) {
426429
throw Exception("NXclosedata() failed", status);
427430
}
@@ -431,7 +434,7 @@ void File::putData(const void *data) {
431434
if (data == NULL) {
432435
throw Exception("Data specified as null in putData");
433436
}
434-
NXstatus status = NXputdata(this->m_file_id, const_cast<void *>(data));
437+
NXstatus status = NXputdata(*(this->m_pfile_id), const_cast<void *>(data));
435438
if (status != NXstatus::NX_OK) {
436439
throw Exception("NXputdata(void *) failed", status);
437440
}
@@ -451,7 +454,7 @@ void File::putAttr(const AttrInfo &info, const void *data) {
451454
if (info.name.empty()) {
452455
throw Exception("Supplied empty name to putAttr");
453456
}
454-
NXstatus status = NXputattr(this->m_file_id, info.name.c_str(), data, static_cast<int>(info.length), info.type);
457+
NXstatus status = NXputattr(*(this->m_pfile_id), info.name.c_str(), data, static_cast<int>(info.length), info.type);
455458
if (status != NXstatus::NX_OK) {
456459
stringstream msg;
457460
msg << "NXputattr(" << info.name << ", data, " << info.length << ", " << info.type << ") failed";
@@ -509,7 +512,7 @@ void File::putSlab(const void *data, const DimSizeVector &start, const DimSizeVe
509512
msg << "Supplied start rank=" << start.size() << " must match supplied size rank=" << size.size() << "in putSlab";
510513
throw Exception(msg.str());
511514
}
512-
NXstatus status = NXputslab64(this->m_file_id, data, &(start[0]), &(size[0]));
515+
NXstatus status = NXputslab64(*(this->m_pfile_id), data, &(start[0]), &(size[0]));
513516
if (status != NXstatus::NX_OK) {
514517
stringstream msg;
515518
msg << "NXputslab64(data, " << toString(start) << ", " << toString(size) << ") failed";
@@ -542,7 +545,7 @@ template <typename NumT> void File::putSlab(const vector<NumT> &data, dimsize_t
542545

543546
NXlink File::getDataID() {
544547
NXlink link;
545-
NXstatus status = NXgetdataID(this->m_file_id, &link);
548+
NXstatus status = NXgetdataID(*(this->m_pfile_id), &link);
546549
if (status != NXstatus::NX_OK) {
547550
throw Exception("NXgetdataID failed", status);
548551
}
@@ -551,7 +554,7 @@ NXlink File::getDataID() {
551554

552555
bool File::isDataSetOpen() {
553556
NXlink id;
554-
if (NXgetdataID(this->m_file_id, &id) == NXstatus::NX_ERROR) {
557+
if (NXgetdataID(*(this->m_pfile_id), &id) == NXstatus::NX_ERROR) {
555558
return false;
556559
} else {
557560
return true;
@@ -560,7 +563,7 @@ bool File::isDataSetOpen() {
560563
/*----------------------------------------------------------------------*/
561564

562565
void File::makeLink(NXlink &link) {
563-
NXstatus status = NXmakelink(this->m_file_id, &link);
566+
NXstatus status = NXmakelink(*(this->m_pfile_id), &link);
564567
if (status != NXstatus::NX_OK) {
565568
throw Exception("NXmakelink failed", status);
566569
}
@@ -570,7 +573,7 @@ void File::getData(void *data) {
570573
if (data == NULL) {
571574
throw Exception("Supplied null pointer to getData");
572575
}
573-
NXstatus status = NXgetdata(this->m_file_id, data);
576+
NXstatus status = NXgetdata(*(this->m_pfile_id), data);
574577
if (status != NXstatus::NX_OK) {
575578
throw Exception("NXgetdata failed", status);
576579
}
@@ -728,7 +731,7 @@ Info File::getInfo() {
728731
int64_t dims[NX_MAXRANK];
729732
NXnumtype type;
730733
int rank;
731-
NXstatus status = NXgetinfo64(this->m_file_id, &rank, dims, &type);
734+
NXstatus status = NXgetinfo64(*(this->m_pfile_id), &rank, dims, &type);
732735
if (status != NXstatus::NX_OK) {
733736
throw Exception("NXgetinfo failed", status);
734737
}
@@ -745,7 +748,7 @@ Entry File::getNextEntry() {
745748
NXname name, class_name;
746749
NXnumtype datatype;
747750

748-
NXstatus status = NXgetnextentry(this->m_file_id, name, class_name, &datatype);
751+
NXstatus status = NXgetnextentry(*(this->m_pfile_id), name, class_name, &datatype);
749752
if (status == NXstatus::NX_OK) {
750753
string str_name(name);
751754
string str_class(class_name);
@@ -796,7 +799,7 @@ void File::getSlab(void *data, const DimSizeVector &start, const DimSizeVector &
796799
throw Exception(msg.str());
797800
}
798801

799-
NXstatus status = NXgetslab64(this->m_file_id, data, &(start[0]), &(size[0]));
802+
NXstatus status = NXgetslab64(*(this->m_pfile_id), data, &(start[0]), &(size[0]));
800803
if (status != NXstatus::NX_OK) {
801804
throw Exception("NXgetslab failed", status);
802805
}
@@ -809,7 +812,7 @@ AttrInfo File::getNextAttr() {
809812

810813
int rank;
811814
int dim[NX_MAXRANK];
812-
NXstatus status = NXgetnextattra(this->m_file_id, name, &rank, dim, &type);
815+
NXstatus status = NXgetnextattra(*(this->m_pfile_id), name, &rank, dim, &type);
813816
if (status == NXstatus::NX_OK) {
814817
AttrInfo info;
815818
info.type = type;
@@ -859,7 +862,7 @@ void File::getAttr(const AttrInfo &info, void *data, int length) {
859862
if (length < 0) {
860863
length = static_cast<int>(info.length);
861864
}
862-
NXstatus status = NXgetattr(this->m_file_id, name, data, &length, &type);
865+
NXstatus status = NXgetattr(*(this->m_pfile_id), name, data, &length, &type);
863866
if (status != NXstatus::NX_OK) {
864867
throw Exception("NXgetattr(" + info.name + ") failed", status);
865868
}
@@ -952,22 +955,22 @@ bool File::hasAttr(const std::string &name) {
952955

953956
NXlink File::getGroupID() {
954957
NXlink link;
955-
NXstatus status = NXgetgroupID(this->m_file_id, &link);
958+
NXstatus status = NXgetgroupID(*(this->m_pfile_id), &link);
956959
if (status != NXstatus::NX_OK) {
957960
throw Exception("NXgetgroupID failed", status);
958961
}
959962
return link;
960963
}
961964

962965
void File::initGroupDir() {
963-
NXstatus status = NXinitgroupdir(this->m_file_id);
966+
NXstatus status = NXinitgroupdir(*(this->m_pfile_id));
964967
if (status != NXstatus::NX_OK) {
965968
throw Exception("NXinitgroupdir failed", status);
966969
}
967970
}
968971

969972
void File::initAttrDir() {
970-
NXstatus status = NXinitattrdir(this->m_file_id);
973+
NXstatus status = NXinitattrdir(*(this->m_pfile_id));
971974
if (status != NXstatus::NX_OK) {
972975
throw Exception("NXinitattrdir failed", status);
973976
}

0 commit comments

Comments
 (0)