forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileDescriptorTest.h
181 lines (148 loc) · 6.31 KB
/
FileDescriptorTest.h
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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include <cstdio>
#include <cxxtest/TestSuite.h>
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/FileDescriptor.h"
#include <Poco/File.h>
#include <Poco/Path.h>
using Mantid::Kernel::FileDescriptor;
class FileDescriptorTest : public CxxTest::TestSuite {
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static FileDescriptorTest *createSuite() { return new FileDescriptorTest(); }
static void destroySuite(FileDescriptorTest *suite) { delete suite; }
FileDescriptorTest() {
auto &cfg = Mantid::Kernel::ConfigService::Instance();
cfg.reset();
const auto &dataPaths = cfg.getDataSearchDirs();
for (const auto &dataPath : dataPaths) {
Poco::Path nxsPath(dataPath, "CNCS_7860_event.nxs");
if (Poco::File(nxsPath).exists())
m_testNexusPath = nxsPath.toString();
Poco::Path nonNxsPath(dataPath, "CSP79590.raw");
if (Poco::File(nonNxsPath).exists())
m_testNonNexusPath = nonNxsPath.toString();
Poco::Path asciiPath(dataPath, "AsciiExample.txt");
if (Poco::File(asciiPath).exists())
m_testAsciiPath = asciiPath.toString();
Poco::Path emptyFilePath(dataPath, "emptyFile.txt");
if (Poco::File(emptyFilePath).exists())
m_emptyFilePath = emptyFilePath.toString();
if (!m_testNexusPath.empty() && !m_testNonNexusPath.empty() && !m_testAsciiPath.empty() &&
!m_emptyFilePath.empty())
break;
}
if (m_testNexusPath.empty() || m_testNonNexusPath.empty() || m_testAsciiPath.empty() || m_emptyFilePath.empty()) {
throw std::runtime_error("Unable to find test files for FileDescriptorTest. "
"The AutoTestData directory needs to be in the search path");
}
}
//===================== Success cases
//============================================
void test_isAscii_Returns_True_For_Ascii_Filename() {
TS_ASSERT(FileDescriptor::isAscii(m_testAsciiPath)); // static method
FileDescriptor descr(m_testAsciiPath);
TS_ASSERT(descr.isAscii());
}
void test_isAscii_Returns_False_For_Binary_Filename() {
TS_ASSERT(!FileDescriptor::isAscii(m_testNonNexusPath)); // static method
FileDescriptor descr(m_testNonNexusPath);
TS_ASSERT(!descr.isAscii());
}
void test_isAscii_Returns_True_For_Stream_Pointing_At_Ascii_File_And_Stream_Is_Returned_To_Position_On_Entry() {
std::ifstream is(m_testAsciiPath.c_str(), std::ios::in | std::ios::binary);
// move stream along one to check it is returned to here
is.seekg(1);
TS_ASSERT(FileDescriptor::isAscii(is));
TS_ASSERT_EQUALS(1, is.tellg());
}
void test_isAscii_Returns_False_For_Stream_Pointing_At_Ascii_File_And_Stream_Is_Returned_To_Position_On_Entry() {
std::ifstream is(m_testNonNexusPath.c_str(), std::ios::in | std::ios::binary);
// move stream along one to check it is returned to here
is.seekg(1);
TS_ASSERT(!FileDescriptor::isAscii(is));
TS_ASSERT_EQUALS(1, is.tellg());
}
void test_isAscii_Returns_True_For_C_Handle() {
FILE *handle = fopen(m_testAsciiPath.c_str(), "r");
if (handle) {
TS_ASSERT(FileDescriptor::isAscii(handle));
TS_ASSERT_EQUALS(0, ftell(handle));
fclose(handle);
}
}
void test_isAscii_Returns_False_For_C_Handle() {
FILE *handle = fopen(m_testNonNexusPath.c_str(), "r");
if (handle) {
TS_ASSERT(!FileDescriptor::isAscii(handle));
TS_ASSERT_EQUALS(0, ftell(handle));
fclose(handle);
}
}
void test_Constructor_With_Existing_File_Initializes_Description_Fields() {
const std::string filename = m_testNexusPath;
FileDescriptor descr(filename);
TS_ASSERT_EQUALS(filename, descr.filename());
TS_ASSERT_EQUALS(".nxs", descr.extension());
}
void test_Intial_Stream_Is_Positioned_At_Start_Of_File() {
const std::string filename = m_testNexusPath;
FileDescriptor descr(filename);
auto &stream = descr.data();
std::streamoff streamPos = stream.tellg();
TS_ASSERT_EQUALS(0, streamPos);
}
void test_ResetStreamToStart_Repositions_Stream_Start_Of_File() {
const std::string filename = m_testNexusPath;
FileDescriptor descr(filename);
auto &stream = descr.data();
char byte('0');
stream >> byte; // read byte from stream
TS_ASSERT_EQUALS(1, stream.tellg());
descr.resetStreamToStart();
TS_ASSERT_EQUALS(0, stream.tellg());
}
void testEmptyFile() {
TS_ASSERT_EQUALS(false, FileDescriptor::isEmpty(m_testAsciiPath));
TS_ASSERT(FileDescriptor::isEmpty(m_emptyFilePath));
}
//===================== Failure cases
//============================================
void test_IsAscii_Throws_For_Inaccessible_Filename() {
TS_ASSERT_THROWS(FileDescriptor::isAscii(""), const std::invalid_argument &);
TS_ASSERT_THROWS(FileDescriptor::isAscii("__not_a_File.txt__"), const std::invalid_argument &);
}
void test_IsAscii_Returns_True_For_Ascii_Stream_Shorter_Than_NBytes_Requested_And_Clears_Error_Flags() {
// Fake data
std::istringstream is;
is.str("abcdef"); // 6 bytes
TS_ASSERT(FileDescriptor::isAscii(is, 6)); // Equal to length
TS_ASSERT_EQUALS(0, is.tellg());
TS_ASSERT(is.good());
TS_ASSERT(FileDescriptor::isAscii(is, 10)); // Larger
TS_ASSERT_EQUALS(0, is.tellg());
TS_ASSERT(is.good());
}
void test_Constructor_Throws_With_Empty_filename() {
TS_ASSERT_THROWS(FileDescriptor(""), const std::invalid_argument &);
}
void test_Constructor_Throws_With_NonExistant_filename() {
TS_ASSERT_THROWS(FileDescriptor("__ThisShouldBeANonExistantFile.txt"), const std::invalid_argument &);
}
void testIsEmptyThrowsForInaccessibleFileName() {
TS_ASSERT_THROWS(FileDescriptor::isEmpty(""), const std::invalid_argument &);
TS_ASSERT_THROWS(FileDescriptor::isEmpty("__not_a_File.txt__"), const std::invalid_argument &);
}
private:
std::string m_testNexusPath;
std::string m_testNonNexusPath;
std::string m_testAsciiPath;
std::string m_emptyFilePath;
};