Sparse Virtual File System  0.4.0
A Sparse Virtual File System.
test_svf.h
Go to the documentation of this file.
1 
32 #ifndef CPPSVF_TEST_SVF_H
33 #define CPPSVF_TEST_SVF_H
34 
35 #include <string>
36 #include <sstream>
37 
38 #include "svf.h"
39 #include "test.h"
40 
41 
42 namespace SVFS {
43 
44  namespace Test {
45 
49  class TestCaseABC {
50  public:
58  m_writes(m_writes) {}
59 
60  virtual TestResult run() const = 0;
61 
63  size_t load_writes(SparseVirtualFile &svf, const char *data) const {
64  size_t bytes_written = 0;
65  for (const auto &write_test: m_writes) {
66  /* We limit ourselves to only half the 512 byte data and then a further offset of 256 maximum. */
67  assert(write_test.first < 256);
68  if (write_test.first >= 256) {
69  std::ostringstream os;
70  os << "Test file position " << write_test.first;
71  os << " >= 256";
72  throw ExceptionTestConfiguration(os.str());
73  }
74  assert(write_test.first + write_test.second < 256);
75  if (write_test.first + write_test.second >= 256) {
76  std::ostringstream os;
77  os << "Test file position + size " << write_test.first;
78  os << " >= 256";
79  throw ExceptionTestConfiguration(os.str());
80  }
81  svf.write(write_test.first, data + write_test.first, write_test.second);
82  bytes_written += write_test.second;
83  }
84  return bytes_written;
85  }
86 
87  virtual ~TestCaseABC() = default;
88 
90  const std::string &test_name() const noexcept { return m_test_name; }
91 
92  protected:
93  std::string m_test_name;
95  };
96 
97 
99  class TestCaseWrite : public TestCaseABC {
100  public:
101  TestCaseWrite(const std::string &m_test_name, const t_seek_reads &m_writes,
103 
104  TestResult run() const override;
105 
106  virtual ~TestCaseWrite() = default;
107 
108  private:
110  };
111 
114  public:
115  TestCaseWriteThrows(const std::string &m_test_name, const t_seek_reads &m_writes,
116  t_fpos fpos, size_t len, const char *data, const std::string &message);
117 
118  TestResult run() const override;
119 
120  virtual ~TestCaseWriteThrows() = default;
121 
122  protected:
124  const char *m_data;
125  size_t m_len;
126  std::string m_message;
127  };
128 
130  class TestCaseRead : public TestCaseABC {
131  public:
132  TestCaseRead(const std::string &m_test_name, const t_seek_reads &m_writes,
133  t_fpos fpos, size_t len);
134 
135  TestResult run() const override;
136 
137  virtual ~TestCaseRead() = default;
138 
139  protected:
141  size_t m_len;
142  };
143 
146  public:
147  TestCaseReadThrows(const std::string &m_test_name, const t_seek_reads &m_writes,
148  t_fpos fpos, size_t len, const std::string &message);
149 
150  TestResult run() const override;
151 
152  virtual ~TestCaseReadThrows() = default;
153 
154  protected:
155  std::string m_message;
156  };
157 
159  class TestCaseHas : public TestCaseABC {
160  public:
161  TestCaseHas(const std::string &m_test_name, const t_seek_reads &m_writes,
162  t_fpos fpos, size_t len, bool expected);
163 
164  TestResult run() const override;
165 
166  virtual ~TestCaseHas() = default;
167 
168  protected:
170  size_t m_len;
172  };
173 
175  class TestCaseNeed : public TestCaseABC {
176  public:
177  TestCaseNeed(const std::string &m_test_name, const t_seek_reads &m_writes,
178  t_fpos fpos, size_t len, const t_seek_reads &m_need);
179 
180  TestResult run() const override;
181 
182  virtual ~TestCaseNeed() = default;
183 
184  protected:
186  size_t m_len;
188  };
189 
192  public:
193  explicit TestCaseNeedGreedy(const std::string &m_test_name, const t_seek_reads &m_writes,
194  t_fpos fpos, size_t len, size_t greedy_length, const t_seek_reads &m_need);
195 
196  TestResult run() const override;
197 
198  virtual ~TestCaseNeedGreedy() = default;
199 
200  protected:
202  size_t m_len;
205  };
206 
208  class TestCaseErase : public TestCaseABC {
209  public:
210  TestCaseErase(const std::string &m_test_name, const t_seek_reads &m_writes, t_fpos fpos);
211 
212  TestResult run() const override;
213 
214  virtual ~TestCaseErase() = default;
215 
216  protected:
218 // size_t m_len;
219  };
220 
223  public:
224  TestCaseEraseThrows(const std::string &m_test_name, const t_seek_reads &m_writes,
225  t_fpos fpos, const std::string &message);
226 
227  TestResult run() const override;
228 
229  virtual ~TestCaseEraseThrows() = default;
230 
231  protected:
232  std::string m_message;
233  };
234 
236 
237  } // namespace Test
238 } // namespace SVFS
239 
240 #endif //CPPSVF_TEST_SVF_H
Implementation of a Sparse Virtual File.
Definition: svf.h:288
void write(t_fpos fpos, const char *data, size_t len)
Write the data a the given file position.
Definition: svf.cpp:453
Exception used where a test case is miss-configured.
Definition: test.h:60
Abstract base class for tests cases.
Definition: test_svf.h:49
std::string m_test_name
Definition: test_svf.h:93
virtual TestResult run() const =0
virtual ~TestCaseABC()=default
size_t load_writes(SparseVirtualFile &svf, const char *data) const
Load all the specified write seek/write blocks from the data (assumed to be 512 bytes long).
Definition: test_svf.h:63
t_seek_reads m_writes
Definition: test_svf.h:94
const std::string & test_name() const noexcept
The tests name.
Definition: test_svf.h:90
TestCaseABC(const std::string &m_test_name, const t_seek_reads &m_writes)
Definition: test_svf.h:57
Specialisation of a test case for erase() on a SVF.
Definition: test_svf.h:208
virtual ~TestCaseErase()=default
TestResult run() const override
Definition: test_svf.cpp:1093
TestCaseErase(const std::string &m_test_name, const t_seek_reads &m_writes, t_fpos fpos)
Definition: test_svf.cpp:1087
Specialisation of a test case where erase() on a SVF throws an exception.
Definition: test_svf.h:222
TestCaseEraseThrows(const std::string &m_test_name, const t_seek_reads &m_writes, t_fpos fpos, const std::string &message)
Definition: test_svf.cpp:1136
TestResult run() const override
Definition: test_svf.cpp:1143
virtual ~TestCaseEraseThrows()=default
Specialisation of a test case for a SVF has() .
Definition: test_svf.h:159
virtual ~TestCaseHas()=default
TestCaseHas(const std::string &m_test_name, const t_seek_reads &m_writes, t_fpos fpos, size_t len, bool expected)
Definition: test_svf.cpp:735
TestResult run() const override
Definition: test_svf.cpp:742
Specialisation of a test case for a SVF need() with a greedy length.
Definition: test_svf.h:191
TestResult run() const override
Definition: test_svf.cpp:977
virtual ~TestCaseNeedGreedy()=default
TestCaseNeedGreedy(const std::string &m_test_name, const t_seek_reads &m_writes, t_fpos fpos, size_t len, size_t greedy_length, const t_seek_reads &m_need)
Definition: test_svf.cpp:967
Specialisation of a test case for a SVF need() .
Definition: test_svf.h:175
TestResult run() const override
Definition: test_svf.cpp:816
virtual ~TestCaseNeed()=default
TestCaseNeed(const std::string &m_test_name, const t_seek_reads &m_writes, t_fpos fpos, size_t len, const t_seek_reads &m_need)
Definition: test_svf.cpp:808
t_seek_reads m_need
Definition: test_svf.h:187
Specialisation of a test case for writing to a SVF.
Definition: test_svf.h:130
TestCaseRead(const std::string &m_test_name, const t_seek_reads &m_writes, t_fpos fpos, size_t len)
Definition: test_svf.cpp:526
TestResult run() const override
Definition: test_svf.cpp:532
virtual ~TestCaseRead()=default
Specialisation of a test case where a write to a SVF throws an exception.
Definition: test_svf.h:145
virtual ~TestCaseReadThrows()=default
TestResult run() const override
Definition: test_svf.cpp:621
TestCaseReadThrows(const std::string &m_test_name, const t_seek_reads &m_writes, t_fpos fpos, size_t len, const std::string &message)
Definition: test_svf.cpp:611
Specialisation of a test case for writing to a SVF.
Definition: test_svf.h:99
virtual ~TestCaseWrite()=default
TestCaseWrite(const std::string &m_test_name, const t_seek_reads &m_writes, const t_seek_reads &m_expected_blocks)
Definition: test_svf.cpp:64
TestResult run() const override
Create a SVF, run the write tests and report the result.
Definition: test_svf.cpp:69
t_seek_reads m_expected_blocks
Definition: test_svf.h:109
Specialisation of a test case where a write to a SVF throws an exception.
Definition: test_svf.h:113
virtual ~TestCaseWriteThrows()=default
TestResult run() const override
Definition: test_svf.cpp:351
TestCaseWriteThrows(const std::string &m_test_name, const t_seek_reads &m_writes, t_fpos fpos, size_t len, const char *data, const std::string &message)
Definition: test_svf.cpp:340
Count of tests taht pass and fail.
Definition: test.h:137
Result of a test.
Definition: test.h:68
std::vector< TestResult > t_test_results
Definition: test.h:128
TestCount test_svf_all(t_test_results &results)
Definition: test_svf.cpp:1949
The namespace for all svfsc code.
Definition: svf.cpp:41
std::vector< t_seek_read > t_seek_reads
Definition: svf.h:251
size_t t_fpos
Definition: svf.h:247