C++ Utilities  5.0.1
Useful C++ classes and routines such as argument parser, IO and conversion utilities
iotests.cpp
Go to the documentation of this file.
1 #include "./testutils.h"
2 
3 #include "../conversion/conversionexception.h"
4 #include "../conversion/stringbuilder.h"
5 
6 #include "../io/ansiescapecodes.h"
7 #include "../io/binaryreader.h"
8 #include "../io/binarywriter.h"
9 #include "../io/bitreader.h"
10 #include "../io/copy.h"
11 #include "../io/inifile.h"
12 #include "../io/misc.h"
13 #include "../io/nativefilestream.h"
14 #include "../io/path.h"
15 
16 #include <cppunit/TestFixture.h>
17 #include <cppunit/extensions/HelperMacros.h>
18 
19 #include <algorithm>
20 #include <fstream>
21 #include <regex>
22 #include <sstream>
23 
24 #ifdef PLATFORM_WINDOWS
25 #include <cstdio>
26 #endif
27 
28 #ifdef PLATFORM_UNIX
29 #include <sys/fcntl.h>
30 #include <sys/types.h>
31 #endif
32 
33 using namespace std;
34 using namespace CppUtilities;
35 using namespace CppUtilities::Literals;
36 using namespace CPPUNIT_NS;
37 
41 class IoTests : public TestFixture {
42  CPPUNIT_TEST_SUITE(IoTests);
43  CPPUNIT_TEST(testBinaryReader);
44  CPPUNIT_TEST(testBinaryWriter);
45  CPPUNIT_TEST(testBitReader);
46  CPPUNIT_TEST(testPathUtilities);
47  CPPUNIT_TEST(testIniFile);
48  CPPUNIT_TEST(testCopy);
49  CPPUNIT_TEST(testReadFile);
50  CPPUNIT_TEST(testAnsiEscapeCodes);
51 #ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
52  CPPUNIT_TEST(testNativeFileStream);
53 #endif
54  CPPUNIT_TEST_SUITE_END();
55 
56 public:
57  void setUp();
58  void tearDown();
59 
60  void testBinaryReader();
61  void testBinaryWriter();
62  void testBitReader();
63  void testPathUtilities();
64  void testIniFile();
65  void testCopy();
66  void testReadFile();
67  void testAnsiEscapeCodes();
68 #ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
69  void testNativeFileStream();
70 #endif
71 };
72 
74 
76 {
77 }
78 
80 {
81 }
82 
87 {
88  // read test file
89  fstream testFile;
90  testFile.exceptions(ios_base::failbit | ios_base::badbit);
91  testFile.open(testFilePath("some_data"), ios_base::in | ios_base::binary);
92  BinaryReader reader(&testFile);
93  CPPUNIT_ASSERT_EQUAL(reader.readStreamsize(), static_cast<istream::pos_type>(398));
94  CPPUNIT_ASSERT(reader.readUInt16LE() == 0x0102u);
95  CPPUNIT_ASSERT(reader.readUInt16BE() == 0x0102u);
96  CPPUNIT_ASSERT(reader.readUInt24LE() == 0x010203u);
97  CPPUNIT_ASSERT(reader.readUInt24BE() == 0x010203u);
98  CPPUNIT_ASSERT(reader.readUInt32LE() == 0x01020304u);
99  CPPUNIT_ASSERT(reader.readUInt32BE() == 0x01020304u);
100  CPPUNIT_ASSERT(reader.readUInt40LE() == 0x0102030405u);
101  CPPUNIT_ASSERT(reader.readUInt40BE() == 0x0102030405u);
102  CPPUNIT_ASSERT(reader.readUInt56LE() == 0x01020304050607u);
103  CPPUNIT_ASSERT(reader.readUInt56BE() == 0x01020304050607u);
104  CPPUNIT_ASSERT(reader.readUInt64LE() == 0x0102030405060708u);
105  CPPUNIT_ASSERT(reader.readUInt64BE() == 0x0102030405060708u);
106  testFile.seekg(0);
107  CPPUNIT_ASSERT(reader.readInt16LE() == 0x0102);
108  CPPUNIT_ASSERT(reader.readInt16BE() == 0x0102);
109  CPPUNIT_ASSERT(reader.readInt24LE() == 0x010203);
110  CPPUNIT_ASSERT(reader.readInt24BE() == 0x010203);
111  CPPUNIT_ASSERT(reader.readInt32LE() == 0x01020304);
112  CPPUNIT_ASSERT(reader.readInt32BE() == 0x01020304);
113  CPPUNIT_ASSERT(reader.readInt40LE() == 0x0102030405);
114  CPPUNIT_ASSERT(reader.readInt40BE() == 0x0102030405);
115  CPPUNIT_ASSERT(reader.readInt56LE() == 0x01020304050607);
116  CPPUNIT_ASSERT(reader.readInt56BE() == 0x01020304050607);
117  CPPUNIT_ASSERT(reader.readInt64LE() == 0x0102030405060708);
118  CPPUNIT_ASSERT(reader.readInt64BE() == 0x0102030405060708);
119  CPPUNIT_ASSERT(reader.readFloat32LE() == 1.125);
120  CPPUNIT_ASSERT(reader.readFloat64LE() == 1.625);
121  CPPUNIT_ASSERT(reader.readFloat32BE() == 1.125);
122  CPPUNIT_ASSERT(reader.readFloat64BE() == 1.625);
123  CPPUNIT_ASSERT(reader.readBool() == false);
124  CPPUNIT_ASSERT(reader.readBool() == true);
125  CPPUNIT_ASSERT(reader.readString(3) == "abc");
126  CPPUNIT_ASSERT(reader.readLengthPrefixedString() == "ABC");
127  CPPUNIT_ASSERT(reader.readLengthPrefixedString()
128  == "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901"
129  "23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123"
130  "45678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"
131  "678901234567890123456789");
132  CPPUNIT_ASSERT_EQUAL("def"s, reader.readTerminatedString());
133  testFile.seekg(-4, ios_base::cur);
134  CPPUNIT_ASSERT_EQUAL("def"s, reader.readTerminatedString(5, 0));
135  CPPUNIT_ASSERT_THROW(reader.readLengthPrefixedString(), ConversionException);
136  CPPUNIT_ASSERT_MESSAGE("pos in stream not advanced on conversion error", reader.readByte() == 0);
137 
138  // test ownership
139  reader.setStream(nullptr, true);
140  reader.setStream(new fstream(), true);
141  BinaryReader reader2(reader);
142  CPPUNIT_ASSERT(reader2.stream() == reader.stream());
143  CPPUNIT_ASSERT(!reader2.hasOwnership());
144  reader.setStream(&testFile, false);
145  reader.setStream(new fstream(), true);
146 }
147 
152 {
153  // prepare reading expected data
154  fstream testFile;
155  testFile.exceptions(ios_base::failbit | ios_base::badbit);
156  testFile.open(testFilePath("some_data"), ios_base::in | ios_base::binary);
157 
158  // prepare output stream
159  stringstream outputStream(ios_base::in | ios_base::out | ios_base::binary);
160  outputStream.exceptions(ios_base::failbit | ios_base::badbit);
161  char testData[397];
162  outputStream.rdbuf()->pubsetbuf(testData, sizeof(testData));
163 
164  // write test data
165  BinaryWriter writer(&outputStream);
166  writer.writeUInt16LE(0x0102u);
167  writer.writeUInt16BE(0x0102u);
168  writer.writeUInt24LE(0x010203u);
169  writer.writeUInt24BE(0x010203u);
170  writer.writeUInt32LE(0x01020304u);
171  writer.writeUInt32BE(0x01020304u);
172  writer.writeUInt40LE(0x0102030405u);
173  writer.writeUInt40BE(0x0102030405u);
174  writer.writeUInt56LE(0x01020304050607u);
175  writer.writeUInt56BE(0x01020304050607u);
176  writer.writeUInt64LE(0x0102030405060708u);
177  writer.writeUInt64BE(0x0102030405060708u);
178 
179  // test written values
180  for (char c : testData) {
181  CPPUNIT_ASSERT(c == static_cast<char>(testFile.get()));
182  if (testFile.tellg() >= 58) {
183  break;
184  }
185  }
186  testFile.seekg(0);
187  outputStream.seekp(0);
188 
189  // write more test data
190  writer.writeInt16LE(0x0102);
191  writer.writeInt16BE(0x0102);
192  writer.writeInt24LE(0x010203);
193  writer.writeInt24BE(0x010203);
194  writer.writeInt32LE(0x01020304);
195  writer.writeInt32BE(0x01020304);
196  writer.writeInt40LE(0x0102030405);
197  writer.writeInt40BE(0x0102030405);
198  writer.writeInt56LE(0x01020304050607);
199  writer.writeInt56BE(0x01020304050607);
200  writer.writeInt64LE(0x0102030405060708);
201  writer.writeInt64BE(0x0102030405060708);
202  writer.writeFloat32LE(1.125);
203  writer.writeFloat64LE(1.625);
204  writer.writeFloat32BE(1.125);
205  writer.writeFloat64BE(1.625);
206  writer.writeBool(false);
207  writer.writeBool(true);
208  writer.writeString("abc");
209  writer.writeLengthPrefixedString("ABC");
210  writer.writeLengthPrefixedString("012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
211  "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901"
212  "234567890123456789012345678901234567890123456789012345678901234567890123456789");
213  writer.writeTerminatedString("def");
214 
215  // test written values
216  for (char c : testData) {
217  CPPUNIT_ASSERT(c == static_cast<char>(testFile.get()));
218  }
219 
220  // test ownership
221  writer.setStream(nullptr, true);
222  writer.setStream(new fstream(), true);
223  BinaryWriter writer2(writer);
224  CPPUNIT_ASSERT(writer2.stream() == writer.stream());
225  CPPUNIT_ASSERT(!writer2.hasOwnership());
226  writer.setStream(&testFile, false);
227  writer.setStream(new fstream(), true);
228 }
229 
234 {
235  const std::uint8_t testData[] = { 0x81, 0x90, 0x3C, 0x44, 0x28, 0x00, 0x44, 0x10, 0x20, 0xFF, 0xFA };
236  BitReader reader(reinterpret_cast<const char *>(testData), sizeof(testData));
237  CPPUNIT_ASSERT(reader.readBit() == 1);
238  reader.skipBits(6);
239  CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(3), reader.showBits<std::uint8_t>(2));
240  CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(3), reader.readBits<std::uint8_t>(2));
241  CPPUNIT_ASSERT_EQUAL(static_cast<std::uint32_t>(0x103C4428 << 1), reader.readBits<std::uint32_t>(32));
242  reader.align();
243  CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0x44), reader.readBits<std::uint8_t>(8));
244  CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(7), reader.readUnsignedExpGolombCodedBits<std::uint8_t>());
245  CPPUNIT_ASSERT_EQUAL(static_cast<std::int8_t>(4), reader.readSignedExpGolombCodedBits<std::int8_t>());
246  CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0), reader.readBit());
247  CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0), reader.readBit());
248  reader.skipBits(8 + 4);
249  CPPUNIT_ASSERT_EQUAL(4_st, reader.bitsAvailable());
250  CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0xA), reader.readBits<std::uint8_t>(4));
251  CPPUNIT_ASSERT_THROW(reader.readBit(), std::ios_base::failure);
252  CPPUNIT_ASSERT_THROW(reader.skipBits(1), std::ios_base::failure);
253  reader.reset(reinterpret_cast<const char *>(testData), sizeof(testData));
254  CPPUNIT_ASSERT_EQUAL(static_cast<std::size_t>(8 * sizeof(testData)), reader.bitsAvailable());
255 }
256 
261 {
262  CPPUNIT_ASSERT_EQUAL("libc++utilities.so"s, fileName("C:\\libs\\libc++utilities.so"));
263  CPPUNIT_ASSERT_EQUAL("libc++utilities.so"s, fileName("C:\\libs/libc++utilities.so"));
264  CPPUNIT_ASSERT_EQUAL("libc++utilities.so"s, fileName("/usr/lib/libc++utilities.so"));
265  CPPUNIT_ASSERT_EQUAL("/usr/lib/"s, directory("/usr/lib/libc++utilities.so"));
266  CPPUNIT_ASSERT_EQUAL(string(), directory("libc++utilities.so"));
267  CPPUNIT_ASSERT_EQUAL("C:\\libs\\"s, directory("C:\\libs\\libc++utilities.so"));
268  CPPUNIT_ASSERT_EQUAL("C:\\libs/"s, directory("C:\\libs/libc++utilities.so"));
269  string invalidPath("lib/c++uti*lities.so?");
270  removeInvalidChars(invalidPath);
271  CPPUNIT_ASSERT(invalidPath == "libc++utilities.so");
272 }
273 
278 {
279  // prepare reading test file
280  fstream inputFile;
281  inputFile.exceptions(ios_base::failbit | ios_base::badbit);
282  inputFile.open(testFilePath("test.ini"), ios_base::in);
283 
284  IniFile ini;
285  ini.parse(inputFile);
286  const auto globalScope = ini.data().at(0);
287  const auto scope1 = ini.data().at(1);
288  const auto scope2 = ini.data().at(2);
289  CPPUNIT_ASSERT(globalScope.first.empty());
290  CPPUNIT_ASSERT(globalScope.second.find("key0") != globalScope.second.cend());
291  CPPUNIT_ASSERT(globalScope.second.find("key0")->second == "value 0");
292  CPPUNIT_ASSERT(globalScope.second.find("key1") == globalScope.second.cend());
293  CPPUNIT_ASSERT(scope1.first == "scope 1");
294  CPPUNIT_ASSERT(scope1.second.find("key1") != scope1.second.cend());
295  CPPUNIT_ASSERT(scope1.second.find("key1")->second == "value 1");
296  CPPUNIT_ASSERT(scope1.second.find("key2") != scope1.second.cend());
297  CPPUNIT_ASSERT(scope1.second.find("key2")->second == "value=2");
298  CPPUNIT_ASSERT(scope2.first == "scope 2");
299  CPPUNIT_ASSERT(scope2.second.find("key5") == scope2.second.cend());
300 
301  // write values to another file
302  fstream outputFile;
303  outputFile.exceptions(ios_base::failbit | ios_base::badbit);
304  outputFile.open(workingCopyPath("output.ini", WorkingCopyMode::NoCopy), ios_base::out | ios_base::trunc);
305  ini.make(outputFile);
306 
307  // parse written values (again)
308  outputFile.close();
309  outputFile.open(workingCopyPath("output.ini", WorkingCopyMode::NoCopy), ios_base::in);
310  IniFile ini2;
311  ini2.parse(outputFile);
312  CPPUNIT_ASSERT(ini.data() == ini2.data());
313 }
314 
319 {
320  // prepare streams
321  fstream testFile;
322  testFile.exceptions(ios_base::failbit | ios_base::badbit);
323  testFile.open(testFilePath("some_data"), ios_base::in | ios_base::binary);
324  stringstream outputStream(ios_base::in | ios_base::out | ios_base::binary);
325  outputStream.exceptions(ios_base::failbit | ios_base::badbit);
326 
327  // copy
328  CopyHelper<13> copyHelper;
329  copyHelper.copy(testFile, outputStream, 50);
330 
331  // test
332  testFile.seekg(0);
333  for (auto i = 0; i < 50; ++i) {
334  CPPUNIT_ASSERT(testFile.get() == outputStream.get());
335  }
336 }
337 
342 {
343  // read a file successfully
344  const string iniFilePath(testFilePath("test.ini"));
345  CPPUNIT_ASSERT_EQUAL("# file for testing INI parser\n"
346  "key0=value 0\n"
347  "\n"
348  "[scope 1]\n"
349  "key1=value 1 # comment\n"
350  "key2=value=2\n"
351  "key3=value 3\n"
352  "\n"
353  "[scope 2]\n"
354  "key4=value 4\n"
355  "#key5=value 5\n"
356  "key6=value 6\n"s,
357  readFile(iniFilePath));
358 
359  // fail by exceeding max size
360  CPPUNIT_ASSERT_THROW(readFile(iniFilePath, 10), std::ios_base::failure);
361 
362  // handle UTF-8 in path and file contents correctly via NativeFileStream
363 #if !defined(PLATFORM_WINDOWS) || defined(CPP_UTILITIES_USE_NATIVE_FILE_BUFFER)
364  CPPUNIT_ASSERT_EQUAL("file with non-ASCII character 'รค' in its name\n"s, readFile(testFilePath("tรคst.txt")));
365 #endif
366 }
367 
369 {
370  stringstream ss1;
371  EscapeCodes::enabled = true;
372  ss1 << EscapeCodes::Phrases::Error << "some error" << EscapeCodes::Phrases::End;
373  ss1 << EscapeCodes::Phrases::Warning << "some warning" << EscapeCodes::Phrases::End;
374  ss1 << EscapeCodes::Phrases::Info << "some info" << EscapeCodes::Phrases::End;
375  ss1 << EscapeCodes::Phrases::ErrorMessage << "Arch-style error" << EscapeCodes::Phrases::End;
376  ss1 << EscapeCodes::Phrases::WarningMessage << "Arch-style warning" << EscapeCodes::Phrases::End;
377  ss1 << EscapeCodes::Phrases::PlainMessage << "Arch-style message" << EscapeCodes::Phrases::End;
378  ss1 << EscapeCodes::Phrases::SuccessMessage << "Arch-style success" << EscapeCodes::Phrases::End;
379  ss1 << EscapeCodes::Phrases::SubMessage << "Arch-style sub-message" << EscapeCodes::Phrases::End;
380  ss1 << EscapeCodes::color(EscapeCodes::Color::Blue, EscapeCodes::Color::Red, EscapeCodes::TextAttribute::Blink)
381  << "blue, blinking text on red background" << EscapeCodes::TextAttribute::Reset << '\n';
382  cout << "\noutput for formatting with ANSI escape codes:\n" << ss1.str() << "---------------------------------------------\n";
383  fstream("/tmp/test.txt", ios_base::out | ios_base::trunc) << ss1.str();
384  CPPUNIT_ASSERT_EQUAL("\e[1;31mError: \e[0m\e[1msome error\e[0m\n"
385  "\e[1;33mWarning: \e[0m\e[1msome warning\e[0m\n"
386  "\e[1;34mInfo: \e[0m\e[1msome info\e[0m\n"
387  "\e[1;31m==> ERROR: \e[0m\e[1mArch-style error\e[0m\n"
388  "\e[1;33m==> WARNING: \e[0m\e[1mArch-style warning\e[0m\n"
389  " \e[0m\e[1mArch-style message\e[0m\n"
390  "\e[1;32m==> \e[0m\e[1mArch-style success\e[0m\n"
391  "\e[1;32m -> \e[0m\e[1mArch-style sub-message\e[0m\n"
392  "\e[5;34;41mblue, blinking text on red background\e[0m\n"s,
393  ss1.str());
394 
395  stringstream ss2;
396  EscapeCodes::enabled = false;
397  ss2 << EscapeCodes::Phrases::Info << "some info" << EscapeCodes::Phrases::End;
398  CPPUNIT_ASSERT_EQUAL("Info: some info\n"s, ss2.str());
399 }
400 
401 #ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
402 
405 void IoTests::testNativeFileStream()
406 {
407  // open file by path
408  const auto txtFilePath(workingCopyPath("tรคst.txt"));
409  NativeFileStream fileStream;
410  fileStream.exceptions(ios_base::badbit | ios_base::failbit);
411  CPPUNIT_ASSERT(!fileStream.is_open());
412  fileStream.open(txtFilePath, ios_base::in);
413  CPPUNIT_ASSERT(fileStream.is_open());
414 #if defined(PLATFORM_WINDOWS) && defined(CPP_UTILITIES_USE_BOOST_IOSTREAMS)
415  CPPUNIT_ASSERT(fileStream.fileHandle() != nullptr);
416 #else
417  CPPUNIT_ASSERT(fileStream.fileDescriptor() != -1);
418 #endif
419  CPPUNIT_ASSERT_EQUAL(static_cast<char>(fileStream.get()), 'f');
420  fileStream.seekg(0, ios_base::end);
421  CPPUNIT_ASSERT_EQUAL(fileStream.tellg(), static_cast<NativeFileStream::pos_type>(47));
422  fileStream.close();
423  CPPUNIT_ASSERT(!fileStream.is_open());
424  try {
425  fileStream.open("non existing file", ios_base::in | ios_base::out | ios_base::binary);
426  CPPUNIT_FAIL("expected exception");
427  } catch (const std::ios_base::failure &failure) {
428 #ifdef PLATFORM_WINDOWS
429 #ifdef CPP_UTILITIES_USE_BOOST_IOSTREAMS
430  CPPUNIT_ASSERT_EQUAL("CreateFileW failed: iostream error"s, string(failure.what()));
431 #else
432  CPPUNIT_ASSERT_EQUAL("_wopen failed: iostream error"s, string(failure.what()));
433 #endif
434 #else
435  CPPUNIT_ASSERT_EQUAL("open failed: iostream error"s, string(failure.what()));
436 #endif
437  }
438  fileStream.clear();
439 
440  // open file from file descriptor
441 #ifndef PLATFORM_WINDOWS
442  auto readWriteFileDescriptor = open(txtFilePath.data(), O_RDWR);
443  CPPUNIT_ASSERT(readWriteFileDescriptor);
444  fileStream.open(readWriteFileDescriptor, ios_base::in | ios_base::out | ios_base::binary);
445  CPPUNIT_ASSERT(fileStream.is_open());
446  CPPUNIT_ASSERT_EQUAL(static_cast<char>(fileStream.get()), 'f');
447  fileStream.seekg(0, ios_base::end);
448  CPPUNIT_ASSERT_EQUAL(fileStream.tellg(), static_cast<NativeFileStream::pos_type>(47));
449  fileStream.flush();
450  fileStream.close();
451  CPPUNIT_ASSERT(!fileStream.is_open());
452 #endif
453  try {
454  fileStream.open(-1, ios_base::in | ios_base::out | ios_base::binary);
455  fileStream.get();
456  CPPUNIT_FAIL("expected exception");
457  } catch (const std::ios_base::failure &failure) {
458 #ifndef PLATFORM_WINDOWS
460  "expected error message", "(basic_ios::clear|failed reading: Bad file descriptor): iostream error"s, string(failure.what()));
461 #else
462  CPP_UTILITIES_UNUSED(failure)
463 #endif
464  }
465  fileStream.clear();
466 
467  // append + write file via path
468  NativeFileStream fileStream2;
469  fileStream2.exceptions(ios_base::failbit | ios_base::badbit);
470  fileStream2.open(txtFilePath, ios_base::in | ios_base::out | ios_base::app);
471  CPPUNIT_ASSERT(fileStream2.is_open());
472  fileStream2 << "foo";
473  fileStream2.flush();
474  fileStream2.close();
475  CPPUNIT_ASSERT(!fileStream2.is_open());
476  CPPUNIT_ASSERT_EQUAL("file with non-ASCII character 'รค' in its name\nfoo"s, readFile(txtFilePath, 50));
477 
478  // truncate + write file via path
479  fileStream2.open(txtFilePath, ios_base::out | ios_base::trunc);
480  CPPUNIT_ASSERT(fileStream2.is_open());
481  fileStream2 << "bar";
482  fileStream2.close();
483  CPPUNIT_ASSERT(!fileStream2.is_open());
484  CPPUNIT_ASSERT_EQUAL("bar"s, readFile(txtFilePath, 4));
485 
486  // append + write via file descriptor from file handle
487 #ifdef PLATFORM_WINDOWS
488  const auto wideTxtFilePath = NativeFileStream::makeWidePath(txtFilePath);
489  const auto appendFileHandle = _wfopen(wideTxtFilePath.get(), L"a+");
490 #else
491  const auto appendFileHandle = fopen(txtFilePath.data(), "a");
492 #endif
493  CPPUNIT_ASSERT(appendFileHandle);
494  fileStream2.open(fileno(appendFileHandle), ios_base::out | ios_base::app);
495  CPPUNIT_ASSERT(fileStream2.is_open());
496  fileStream2 << "foo";
497  fileStream2.close();
498  CPPUNIT_ASSERT(!fileStream2.is_open());
499  CPPUNIT_ASSERT_EQUAL("barfoo"s, readFile(txtFilePath, 7));
500 }
501 #endif
CppUtilities::BinaryWriter::writeInt16LE
void writeInt16LE(std::int16_t value)
Writes a 16-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:391
CppUtilities::BitReader::readBit
std::uint8_t readBit()
Reads the one bit from the buffer advancing the current position by one bit.
Definition: bitreader.h:88
CppUtilities::NativeFileStream
std::fstream NativeFileStream
Definition: nativefilestream.h:108
CppUtilities::BinaryReader::readUInt40LE
std::uint64_t readUInt40LE()
Reads a 40-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:470
CppUtilities::BinaryWriter::writeInt64LE
void writeInt64LE(std::int64_t value)
Writes a 64-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:489
CppUtilities::BinaryReader::readInt24LE
std::int32_t readInt24LE()
Reads a 24-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:414
CppUtilities::BinaryReader::readInt56BE
std::int64_t readInt56BE()
Reads a 56-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:326
CppUtilities::BinaryWriter::writeBool
void writeBool(bool value)
Writes a boolean value to the current stream and advances the current position of the stream by one b...
Definition: binarywriter.h:241
CppUtilities::IniFile
The IniFile class parses and makes INI files.
Definition: inifile.h:12
CppUtilities::testFilePath
CPP_UTILITIES_EXPORT std::string testFilePath(const std::string &relativeTestFilePath)
Convenience function to invoke TestApplication::testFilePath().
Definition: testutils.h:147
CppUtilities::BitReader
The BitReader class provides bitwise reading of buffered data.
Definition: bitreader.h:13
CppUtilities::IniFile::make
void make(std::ostream &outputStream)
Write the current data to the specified outputStream.
Definition: inifile.cpp:150
CppUtilities::BinaryWriter::writeUInt32LE
void writeUInt32LE(std::uint32_t value)
Writes a 32-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:440
CppUtilities::BinaryWriter::writeUInt56LE
void writeUInt56LE(std::uint64_t value)
Writes a 56-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:480
CppUtilities::BinaryReader::readByte
std::uint8_t readByte()
Reads a single byte/unsigned character from the current stream and advances the current position of t...
Definition: binaryreader.h:559
CppUtilities::BinaryWriter::writeUInt56BE
void writeUInt56BE(std::uint64_t value)
Writes a 56-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:337
CppUtilities::BinaryWriter::writeInt16BE
void writeInt16BE(std::int16_t value)
Writes a 16-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:249
CppUtilities::BinaryWriter::writeUInt32BE
void writeUInt32BE(std::uint32_t value)
Writes a 32-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:297
CppUtilities::BinaryReader::readFloat64LE
double readFloat64LE()
Reads a 64-bit little endian floating point value from the current stream and advances the current po...
Definition: binaryreader.h:541
CppUtilities::BinaryReader::readInt64LE
std::int64_t readInt64LE()
Reads a 64-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:504
CppUtilities::BinaryWriter
Writes primitive data types to a std::ostream.
Definition: binarywriter.h:14
IoTests::setUp
void setUp()
Definition: iotests.cpp:75
CppUtilities::BinaryReader::readInt32LE
std::int32_t readInt32LE()
Reads a 32-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:438
CppUtilities::BinaryWriter::writeInt40BE
void writeInt40BE(std::int64_t value)
Writes a 40-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:307
IoTests::testBinaryWriter
void testBinaryWriter()
Tests the most important methods of the BinaryWriter.
Definition: iotests.cpp:151
CppUtilities::BinaryReader::readBool
bool readBool()
Reads a boolean value from the current stream and advances the current position of the stream by one ...
Definition: binaryreader.h:569
IoTests::tearDown
void tearDown()
Definition: iotests.cpp:79
CppUtilities::BitReader::align
void align()
Re-establishes alignment.
Definition: bitreader.h:170
CppUtilities::BinaryReader::readInt16BE
std::int16_t readInt16BE()
Reads a 16-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:242
CppUtilities::readFile
CPP_UTILITIES_EXPORT std::string readFile(const std::string &path, std::string::size_type maxSize=std::string::npos)
Reads all contents of the specified file in a single call.
Definition: misc.cpp:15
CppUtilities::BinaryWriter::writeUInt64BE
void writeUInt64BE(std::uint64_t value)
Writes a 64-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:355
CppUtilities::BinaryWriter::writeFloat32BE
void writeFloat32BE(float value)
Writes a 32-bit big endian floating point value to the current stream and advances the current positi...
Definition: binarywriter.h:373
IoTests::testAnsiEscapeCodes
void testAnsiEscapeCodes()
Definition: iotests.cpp:368
CppUtilities::BitReader::skipBits
void skipBits(std::size_t bitCount)
Skips the specified number of bits without reading it.
Definition: bitreader.cpp:18
CppUtilities::BinaryReader::hasOwnership
bool hasOwnership() const
Returns whether the reader takes ownership over the assigned stream.
Definition: binaryreader.h:159
CppUtilities::BinaryWriter::writeInt24LE
void writeInt24LE(std::int32_t value)
Writes a 24-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:410
CppUtilities::EscapeCodes::color
constexpr auto color(Color foreground, Color background, TextAttribute displayAttribute=TextAttribute::Reset)
Definition: ansiescapecodes.h:112
CppUtilities::BinaryWriter::writeInt56BE
void writeInt56BE(std::int64_t value)
Writes a 56-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:327
CppUtilities::BinaryWriter::writeUInt40BE
void writeUInt40BE(std::uint64_t value)
Writes a 40-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:317
IoTests
The IoTests class tests classes and functions provided by the files inside the io directory.
Definition: iotests.cpp:41
CppUtilities::BinaryWriter::writeFloat64LE
void writeFloat64LE(double value)
Writes a 64-bit little endian floating point value to the current stream and advances the current pos...
Definition: binarywriter.h:525
CppUtilities::BitReader::readSignedExpGolombCodedBits
intType readSignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (signed).
Definition: bitreader.h:118
CppUtilities::BinaryReader::readUInt32BE
std::uint32_t readUInt32BE()
Reads a 32-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:293
IoTests::testIniFile
void testIniFile()
Tests IniFile.
Definition: iotests.cpp:277
CppUtilities::BinaryWriter::writeTerminatedString
void writeTerminatedString(const std::string &value)
Writes a terminated string to the current stream and advances the current position of the stream by t...
Definition: binarywriter.h:542
CppUtilities::BinaryReader::readFloat32LE
float readFloat32LE()
Reads a 32-bit little endian floating point value from the current stream and advances the current po...
Definition: binaryreader.h:532
IoTests::testBitReader
void testBitReader()
Tests the BitReader.
Definition: iotests.cpp:233
CppUtilities::IniFile::parse
void parse(std::istream &inputStream)
Parses all data from the specified inputStream.
Definition: inifile.cpp:17
CppUtilities::BinaryReader::readStreamsize
std::istream::pos_type readStreamsize()
Returns the size of the assigned stream.
Definition: binaryreader.cpp:52
CppUtilities::BinaryWriter::writeUInt64LE
void writeUInt64LE(std::uint64_t value)
Writes a 64-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:498
CppUtilities::BinaryReader::readUInt24BE
std::uint32_t readUInt24BE()
Reads a 24-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:274
CppUtilities::BinaryWriter::writeInt40LE
void writeInt40LE(std::int64_t value)
Writes a 40-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:450
CPP_UTILITIES_UNUSED
#define CPP_UTILITIES_UNUSED(x)
Prevents warnings about unused variables.
Definition: global.h:92
CppUtilities::BinaryReader::readInt40LE
std::int64_t readInt40LE()
Reads a 40-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:456
CppUtilities::BinaryReader::stream
const std::istream * stream() const
Returns a pointer to the stream the reader will read from when calling one of the read-methods.
Definition: binaryreader.h:147
TESTUTILS_ASSERT_LIKE
#define TESTUTILS_ASSERT_LIKE(message, expectedRegex, actualString)
Asserts whether the specified string matches the specified regex.
Definition: testutils.h:265
CppUtilities::BinaryReader::readInt64BE
std::int64_t readInt64BE()
Reads a 64-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:350
CppUtilities::BitReader::showBits
intType showBits(std::uint8_t bitCount)
Reads the specified number of bits from the buffer without advancing the current position.
Definition: bitreader.h:127
CppUtilities::directory
CPP_UTILITIES_EXPORT std::string directory(const std::string &path)
Returns the directory of the specified path string (including trailing slash).
Definition: path.cpp:35
CppUtilities::CopyHelper::copy
void copy(std::istream &input, std::ostream &output, std::size_t count)
Copies count bytes from input to output.
Definition: copy.h:43
CppUtilities::BinaryReader::readInt56LE
std::int64_t readInt56LE()
Reads a 56-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:480
CppUtilities::BitReader::bitsAvailable
std::size_t bitsAvailable()
Returns the number of bits which are still available to read.
Definition: bitreader.h:136
IoTests::testBinaryReader
void testBinaryReader()
Tests the most important methods of the BinaryReader.
Definition: iotests.cpp:86
CppUtilities::BinaryWriter::writeFloat32LE
void writeFloat32LE(float value)
Writes a 32-bit little endian floating point value to the current stream and advances the current pos...
Definition: binarywriter.h:516
CppUtilities::BinaryReader::readUInt16BE
std::uint16_t readUInt16BE()
Reads a 16-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:251
CppUtilities::BinaryReader::readFloat32BE
float readFloat32BE()
Reads a 32-bit big endian floating point value from the current stream and advances the current posit...
Definition: binaryreader.h:378
CppUtilities
Contains all utilities provides by the c++utilities library.
Definition: argumentparser.h:17
i
constexpr int i
Definition: traitstests.cpp:103
CppUtilities::IniFile::data
std::vector< std::pair< std::string, std::multimap< std::string, std::string > > > & data()
Returns the data of the file.
Definition: inifile.h:38
CppUtilities::BinaryWriter::writeString
void writeString(const std::string &value)
Writes a string to the current stream and advances the current position of the stream by the length o...
Definition: binarywriter.h:534
CppUtilities::BinaryReader::readUInt32LE
std::uint32_t readUInt32LE()
Reads a 32-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:447
CppUtilities::BinaryReader::readUInt24LE
std::uint32_t readUInt24LE()
Reads a 24-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:428
CppUtilities::BinaryWriter::writeUInt16LE
void writeUInt16LE(std::uint16_t value)
Writes a 16-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:400
CppUtilities::BinaryReader::readUInt56LE
std::uint64_t readUInt56LE()
Reads a 56-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:494
CppUtilities::BinaryReader::readTerminatedString
std::string readTerminatedString(std::uint8_t termination=0)
Reads a terminated string from the current stream.
Definition: binaryreader.cpp:97
CppUtilities::BinaryReader::readUInt64LE
std::uint64_t readUInt64LE()
Reads a 64-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:513
CppUtilities::BinaryWriter::writeFloat64BE
void writeFloat64BE(double value)
Writes a 64-bit big endian floating point value to the current stream and advances the current positi...
Definition: binarywriter.h:382
IoTests::testReadFile
void testReadFile()
Tests readFile().
Definition: iotests.cpp:341
CppUtilities::BinaryReader::readUInt64BE
std::uint64_t readUInt64BE()
Reads a 64-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:359
CppUtilities::BinaryWriter::writeUInt40LE
void writeUInt40LE(std::uint64_t value)
Writes a 40-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:460
CppUtilities::CopyHelper
The CopyHelper class helps to copy bytes from one stream to another.
Definition: copy.h:16
CppUtilities::BinaryReader::readInt40BE
std::int64_t readInt40BE()
Reads a 40-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:302
testutils.h
CppUtilities::BinaryReader::readUInt56BE
std::uint64_t readUInt56BE()
Reads a 56-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:340
CppUtilities::BinaryWriter::writeUInt16BE
void writeUInt16BE(std::uint16_t value)
Writes a 16-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:258
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(IoTests)
CppUtilities::BinaryReader::readUInt40BE
std::uint64_t readUInt40BE()
Reads a 40-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:316
CppUtilities::ConversionException
The ConversionException class is thrown by the various conversion functions of this library when a co...
Definition: conversionexception.h:11
CppUtilities::BinaryReader::setStream
void setStream(std::istream *stream, bool giveOwnership=false)
Assigns the stream the reader will read from when calling one of the read-methods.
Definition: binaryreader.cpp:31
CppUtilities::BinaryWriter::writeUInt24LE
void writeUInt24LE(std::uint32_t value)
Writes a 24-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:421
CppUtilities::BinaryReader
Reads primitive data types from a std::istream.
Definition: binaryreader.h:11
CppUtilities::BinaryWriter::writeInt24BE
void writeInt24BE(std::int32_t value)
Writes a 24-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:268
CppUtilities::BinaryWriter::writeUInt24BE
void writeUInt24BE(std::uint32_t value)
Writes a 24-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:278
CppUtilities::BinaryWriter::writeInt56LE
void writeInt56LE(std::int64_t value)
Writes a 56-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:470
CppUtilities::BinaryReader::readString
std::string readString(std::size_t length)
Reads a string from the current stream of the given length from the stream and advances the current p...
Definition: binaryreader.cpp:82
CppUtilities::Literals
Contains literals to ease asserting with CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:298
CppUtilities::BinaryReader::readUInt16LE
std::uint16_t readUInt16LE()
Reads a 16-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:405
CppUtilities::BinaryWriter::stream
const std::ostream * stream() const
Returns a pointer to the stream the writer will write to when calling one of the write-methods.
Definition: binarywriter.h:144
CppUtilities::BinaryWriter::writeLengthPrefixedString
void writeLengthPrefixedString(const std::string &value)
Writes the length of a string and the string itself to the current stream.
Definition: binarywriter.h:554
CppUtilities::BinaryReader::readFloat64BE
double readFloat64BE()
Reads a 64-bit big endian floating point value from the current stream and advances the current posit...
Definition: binaryreader.h:387
CppUtilities::BinaryWriter::setStream
void setStream(std::ostream *stream, bool giveOwnership=false)
Assigns the stream the writer will write to when calling one of the write-methods.
Definition: binarywriter.cpp:30
CppUtilities::BinaryWriter::writeInt32LE
void writeInt32LE(std::int32_t value)
Writes a 32-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:431
CppUtilities::workingCopyPath
CPP_UTILITIES_EXPORT std::string workingCopyPath(const std::string &relativeTestFilePath, WorkingCopyMode mode=WorkingCopyMode::CreateCopy)
Convenience function to invoke TestApplication::workingCopyPath().
Definition: testutils.h:156
CppUtilities::BitReader::readUnsignedExpGolombCodedBits
intType readUnsignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (unsigned).
Definition: bitreader.h:101
CppUtilities::removeInvalidChars
CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName)
Removes invalid characters from the specified fileName.
Definition: path.cpp:57
CppUtilities::fileName
CPP_UTILITIES_EXPORT std::string fileName(const std::string &path)
Returns the file name and extension of the specified path string.
Definition: path.cpp:15
CppUtilities::BitReader::reset
void reset(const char *buffer, std::size_t bufferSize)
Resets the reader.
Definition: bitreader.h:147
CppUtilities::BitReader::readBits
intType readBits(std::uint8_t bitCount)
Reads the specified number of bits from the buffer advancing the current position by bitCount bits.
Definition: bitreader.h:67
CppUtilities::BinaryWriter::hasOwnership
bool hasOwnership() const
Returns whether the writer takes ownership over the assigned stream.
Definition: binarywriter.h:156
CppUtilities::BinaryWriter::writeInt64BE
void writeInt64BE(std::int64_t value)
Writes a 64-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:346
CppUtilities::BinaryReader::readInt24BE
std::int32_t readInt24BE()
Reads a 24-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:260
IoTests::testCopy
void testCopy()
Tests CopyHelper.
Definition: iotests.cpp:318
CppUtilities::BinaryReader::readInt32BE
std::int32_t readInt32BE()
Reads a 32-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:284
CppUtilities::BinaryReader::readInt16LE
std::int16_t readInt16LE()
Reads a 16-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:396
CppUtilities::EscapeCodes::enabled
CPP_UTILITIES_EXPORT bool enabled
Controls whether the functions inside the EscapeCodes namespace actually make use of escape codes.
Definition: ansiescapecodes.cpp:22
IoTests::testPathUtilities
void testPathUtilities()
Tests fileName() and removeInvalidChars().
Definition: iotests.cpp:260
CppUtilities::BinaryReader::readLengthPrefixedString
std::string readLengthPrefixedString()
Reads a length prefixed string from the current stream.
Definition: binaryreader.h:579
CppUtilities::BinaryWriter::writeInt32BE
void writeInt32BE(std::int32_t value)
Writes a 32-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:288