C++ Utilities  5.3.0
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(testWriteFile);
51  CPPUNIT_TEST(testAnsiEscapeCodes);
52 #ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
53  CPPUNIT_TEST(testNativeFileStream);
54 #endif
55  CPPUNIT_TEST_SUITE_END();
56 
57 public:
58  void setUp() override;
59  void tearDown() override;
60 
61  void testBinaryReader();
62  void testBinaryWriter();
63  void testBitReader();
64  void testPathUtilities();
65  void testIniFile();
66  void testCopy();
67  void testReadFile();
68  void testWriteFile();
69  void testAnsiEscapeCodes();
70 #ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
71  void testNativeFileStream();
72 #endif
73 };
74 
76 
78 {
79 }
80 
82 {
83 }
84 
89 {
90  // read test file
91  fstream testFile;
92  testFile.exceptions(ios_base::failbit | ios_base::badbit);
93  testFile.open(testFilePath("some_data"), ios_base::in | ios_base::binary);
94  BinaryReader reader(&testFile);
95  CPPUNIT_ASSERT_EQUAL(static_cast<istream::pos_type>(398), reader.readStreamsize());
96  CPPUNIT_ASSERT_EQUAL(static_cast<std::uint16_t>(0x0102u), reader.readUInt16LE());
97  CPPUNIT_ASSERT_EQUAL(static_cast<istream::pos_type>(396), reader.readRemainingBytes());
98  CPPUNIT_ASSERT_EQUAL(static_cast<std::uint16_t>(0x0102u), reader.readUInt16BE());
99  CPPUNIT_ASSERT_EQUAL(0x010203u, reader.readUInt24LE());
100  CPPUNIT_ASSERT_EQUAL(0x010203u, reader.readUInt24BE());
101  CPPUNIT_ASSERT_EQUAL(0x01020304u, reader.readUInt32LE());
102  CPPUNIT_ASSERT_EQUAL(0x01020304u, reader.readUInt32BE());
103  CPPUNIT_ASSERT_EQUAL(0x0102030405u, reader.readUInt40LE());
104  CPPUNIT_ASSERT_EQUAL(0x0102030405u, reader.readUInt40BE());
105  CPPUNIT_ASSERT_EQUAL(0x01020304050607u, reader.readUInt56LE());
106  CPPUNIT_ASSERT_EQUAL(0x01020304050607u, reader.readUInt56BE());
107  CPPUNIT_ASSERT_EQUAL(0x0102030405060708u, reader.readUInt64LE());
108  CPPUNIT_ASSERT_EQUAL(0x0102030405060708u, reader.readUInt64BE());
109  testFile.seekg(0);
110  CPPUNIT_ASSERT_EQUAL(reader.readInt16LE(), static_cast<std::int16_t>(0x0102));
111  CPPUNIT_ASSERT_EQUAL(reader.readInt16BE(), static_cast<std::int16_t>(0x0102));
112  CPPUNIT_ASSERT_EQUAL(0x010203, reader.readInt24LE());
113  CPPUNIT_ASSERT_EQUAL(0x010203, reader.readInt24BE());
114  CPPUNIT_ASSERT_EQUAL(0x01020304, reader.readInt32LE());
115  CPPUNIT_ASSERT_EQUAL(0x01020304, reader.readInt32BE());
116  CPPUNIT_ASSERT_EQUAL(0x0102030405, reader.readInt40LE());
117  CPPUNIT_ASSERT_EQUAL(0x0102030405, reader.readInt40BE());
118  CPPUNIT_ASSERT_EQUAL(0x01020304050607, reader.readInt56LE());
119  CPPUNIT_ASSERT_EQUAL(0x01020304050607, reader.readInt56BE());
120  CPPUNIT_ASSERT_EQUAL(0x0102030405060708, reader.readInt64LE());
121  CPPUNIT_ASSERT_EQUAL(0x0102030405060708, reader.readInt64BE());
122  CPPUNIT_ASSERT_EQUAL(1.125f, reader.readFloat32LE());
123  CPPUNIT_ASSERT_EQUAL(1.625, reader.readFloat64LE());
124  CPPUNIT_ASSERT_EQUAL(1.125f, reader.readFloat32BE());
125  CPPUNIT_ASSERT_EQUAL(reader.readFloat64BE(), 1.625);
126  CPPUNIT_ASSERT_EQUAL(false, reader.readBool());
127  CPPUNIT_ASSERT_EQUAL(true, reader.readBool());
128  CPPUNIT_ASSERT_EQUAL("abc"s, reader.readString(3));
129  CPPUNIT_ASSERT_EQUAL(reader.readLengthPrefixedString(), "ABC"s);
130  CPPUNIT_ASSERT_EQUAL("01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901"
131  "23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123"
132  "45678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"
133  "678901234567890123456789"s,
134  reader.readLengthPrefixedString());
135  CPPUNIT_ASSERT_EQUAL("def"s, reader.readTerminatedString());
136  testFile.seekg(-4, ios_base::cur);
137  CPPUNIT_ASSERT_EQUAL("def"s, reader.readTerminatedString(5, 0));
138  CPPUNIT_ASSERT_THROW(reader.readLengthPrefixedString(), ConversionException);
139  CPPUNIT_ASSERT_MESSAGE("pos in stream not advanced on conversion error", reader.readByte() == 0);
140 
141  // test ownership
142  reader.setStream(nullptr, true);
143  reader.setStream(new fstream(), true);
144  BinaryReader reader2(reader);
145  CPPUNIT_ASSERT(reader2.stream() == reader.stream());
146  CPPUNIT_ASSERT(!reader2.hasOwnership());
147  reader.setStream(&testFile, false);
148  reader.setStream(new fstream(), true);
149 }
150 
155 {
156  // prepare reading expected data
157  fstream testFile;
158  testFile.exceptions(ios_base::failbit | ios_base::badbit);
159  testFile.open(testFilePath("some_data"), ios_base::in | ios_base::binary);
160 
161  // prepare output stream
162  stringstream outputStream(ios_base::in | ios_base::out | ios_base::binary);
163  outputStream.exceptions(ios_base::failbit | ios_base::badbit);
164  char testData[397];
165  outputStream.rdbuf()->pubsetbuf(testData, sizeof(testData));
166 
167  // write test data
168  BinaryWriter writer(&outputStream);
169  writer.writeUInt16LE(0x0102u);
170  writer.writeUInt16BE(0x0102u);
171  writer.writeUInt24LE(0x010203u);
172  writer.writeUInt24BE(0x010203u);
173  writer.writeUInt32LE(0x01020304u);
174  writer.writeUInt32BE(0x01020304u);
175  writer.writeUInt40LE(0x0102030405u);
176  writer.writeUInt40BE(0x0102030405u);
177  writer.writeUInt56LE(0x01020304050607u);
178  writer.writeUInt56BE(0x01020304050607u);
179  writer.writeUInt64LE(0x0102030405060708u);
180  writer.writeUInt64BE(0x0102030405060708u);
181 
182  // test written values
183  for (char c : testData) {
184  CPPUNIT_ASSERT(c == static_cast<char>(testFile.get()));
185  if (testFile.tellg() >= 58) {
186  break;
187  }
188  }
189  testFile.seekg(0);
190  outputStream.seekp(0);
191 
192  // write more test data
193  writer.writeInt16LE(0x0102);
194  writer.writeInt16BE(0x0102);
195  writer.writeInt24LE(0x010203);
196  writer.writeInt24BE(0x010203);
197  writer.writeInt32LE(0x01020304);
198  writer.writeInt32BE(0x01020304);
199  writer.writeInt40LE(0x0102030405);
200  writer.writeInt40BE(0x0102030405);
201  writer.writeInt56LE(0x01020304050607);
202  writer.writeInt56BE(0x01020304050607);
203  writer.writeInt64LE(0x0102030405060708);
204  writer.writeInt64BE(0x0102030405060708);
205  writer.writeFloat32LE(1.125);
206  writer.writeFloat64LE(1.625);
207  writer.writeFloat32BE(1.125);
208  writer.writeFloat64BE(1.625);
209  writer.writeBool(false);
210  writer.writeBool(true);
211  writer.writeString("abc");
212  writer.writeLengthPrefixedString("ABC");
213  writer.writeLengthPrefixedString("012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
214  "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901"
215  "234567890123456789012345678901234567890123456789012345678901234567890123456789");
216  writer.writeTerminatedString("def");
217 
218  // test written values
219  for (char c : testData) {
220  CPPUNIT_ASSERT(c == static_cast<char>(testFile.get()));
221  }
222 
223  // test ownership
224  writer.setStream(nullptr, true);
225  writer.setStream(new fstream(), true);
226  BinaryWriter writer2(writer);
227  CPPUNIT_ASSERT(writer2.stream() == writer.stream());
228  CPPUNIT_ASSERT(!writer2.hasOwnership());
229  writer.setStream(&testFile, false);
230  writer.setStream(new fstream(), true);
231 }
232 
237 {
238  const std::uint8_t testData[] = { 0x81, 0x90, 0x3C, 0x44, 0x28, 0x00, 0x44, 0x10, 0x20, 0xFF, 0xFA };
239  BitReader reader(reinterpret_cast<const char *>(testData), sizeof(testData));
240  CPPUNIT_ASSERT(reader.readBit() == 1);
241  reader.skipBits(6);
242  CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(3), reader.showBits<std::uint8_t>(2));
243  CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(3), reader.readBits<std::uint8_t>(2));
244  CPPUNIT_ASSERT_EQUAL(static_cast<std::uint32_t>(0x103C4428 << 1), reader.readBits<std::uint32_t>(32));
245  reader.align();
246  CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0x44), reader.readBits<std::uint8_t>(8));
247  CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(7), reader.readUnsignedExpGolombCodedBits<std::uint8_t>());
248  CPPUNIT_ASSERT_EQUAL(static_cast<std::int8_t>(4), reader.readSignedExpGolombCodedBits<std::int8_t>());
249  CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0), reader.readBit());
250  CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0), reader.readBit());
251  reader.skipBits(8 + 4);
252  CPPUNIT_ASSERT_EQUAL(4_st, reader.bitsAvailable());
253  CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0xA), reader.readBits<std::uint8_t>(4));
254  CPPUNIT_ASSERT_THROW(reader.readBit(), std::ios_base::failure);
255  CPPUNIT_ASSERT_THROW(reader.skipBits(1), std::ios_base::failure);
256  reader.reset(reinterpret_cast<const char *>(testData), sizeof(testData));
257  CPPUNIT_ASSERT_EQUAL(static_cast<std::size_t>(8 * sizeof(testData)), reader.bitsAvailable());
258 }
259 
264 {
265  CPPUNIT_ASSERT_EQUAL("libc++utilities.so"s, fileName("C:\\libs\\libc++utilities.so"));
266  CPPUNIT_ASSERT_EQUAL("libc++utilities.so"s, fileName("C:\\libs/libc++utilities.so"));
267  CPPUNIT_ASSERT_EQUAL("libc++utilities.so"s, fileName("/usr/lib/libc++utilities.so"));
268  CPPUNIT_ASSERT_EQUAL("/usr/lib/"s, directory("/usr/lib/libc++utilities.so"));
269  CPPUNIT_ASSERT_EQUAL(string(), directory("libc++utilities.so"));
270  CPPUNIT_ASSERT_EQUAL("C:\\libs\\"s, directory("C:\\libs\\libc++utilities.so"));
271  CPPUNIT_ASSERT_EQUAL("C:\\libs/"s, directory("C:\\libs/libc++utilities.so"));
272  string invalidPath("lib/c++uti*lities.so?");
273  removeInvalidChars(invalidPath);
274  CPPUNIT_ASSERT(invalidPath == "libc++utilities.so");
275 }
276 
281 {
282  // prepare reading test file
283  fstream inputFile;
284  inputFile.exceptions(ios_base::failbit | ios_base::badbit);
285  inputFile.open(testFilePath("test.ini"), ios_base::in);
286 
287  IniFile ini;
288  ini.parse(inputFile);
289  const auto globalScope = ini.data().at(0);
290  const auto scope1 = ini.data().at(1);
291  const auto scope2 = ini.data().at(2);
292  CPPUNIT_ASSERT(globalScope.first.empty());
293  CPPUNIT_ASSERT(globalScope.second.find("key0") != globalScope.second.cend());
294  CPPUNIT_ASSERT(globalScope.second.find("key0")->second == "value 0");
295  CPPUNIT_ASSERT(globalScope.second.find("key1") == globalScope.second.cend());
296  CPPUNIT_ASSERT(scope1.first == "scope 1");
297  CPPUNIT_ASSERT(scope1.second.find("key1") != scope1.second.cend());
298  CPPUNIT_ASSERT(scope1.second.find("key1")->second == "value 1");
299  CPPUNIT_ASSERT(scope1.second.find("key2") != scope1.second.cend());
300  CPPUNIT_ASSERT(scope1.second.find("key2")->second == "value=2");
301  CPPUNIT_ASSERT(scope2.first == "scope 2");
302  CPPUNIT_ASSERT(scope2.second.find("key5") == scope2.second.cend());
303 
304  // write values to another file
305  fstream outputFile;
306  outputFile.exceptions(ios_base::failbit | ios_base::badbit);
307  outputFile.open(workingCopyPath("output.ini", WorkingCopyMode::NoCopy), ios_base::out | ios_base::trunc);
308  ini.make(outputFile);
309 
310  // parse written values (again)
311  outputFile.close();
312  outputFile.open(workingCopyPath("output.ini", WorkingCopyMode::NoCopy), ios_base::in);
313  IniFile ini2;
314  ini2.parse(outputFile);
315  CPPUNIT_ASSERT(ini.data() == ini2.data());
316 }
317 
322 {
323  // prepare streams
324  fstream testFile;
325  testFile.exceptions(ios_base::failbit | ios_base::badbit);
326  testFile.open(testFilePath("some_data"), ios_base::in | ios_base::binary);
327  stringstream outputStream(ios_base::in | ios_base::out | ios_base::binary);
328  outputStream.exceptions(ios_base::failbit | ios_base::badbit);
329 
330  // copy
331  CopyHelper<13> copyHelper;
332  copyHelper.copy(testFile, outputStream, 50);
333 
334  // test
335  testFile.seekg(0);
336  for (auto i = 0; i < 50; ++i) {
337  CPPUNIT_ASSERT(testFile.get() == outputStream.get());
338  }
339 }
340 
345 {
346  // read a file successfully
347  const string iniFilePath(testFilePath("test.ini"));
348  CPPUNIT_ASSERT_EQUAL("# file for testing INI parser\n"
349  "key0=value 0\n"
350  "\n"
351  "[scope 1]\n"
352  "key1=value 1 # comment\n"
353  "key2=value=2\n"
354  "key3=value 3\n"
355  "\n"
356  "[scope 2]\n"
357  "key4=value 4\n"
358  "#key5=value 5\n"
359  "key6=value 6\n"s,
360  readFile(iniFilePath));
361 
362  // fail by exceeding max size
363  CPPUNIT_ASSERT_THROW(readFile(iniFilePath, 10), std::ios_base::failure);
364 
365  // handle UTF-8 in path and file contents correctly via NativeFileStream
366 #if !defined(PLATFORM_WINDOWS) || defined(CPP_UTILITIES_USE_NATIVE_FILE_BUFFER)
367  CPPUNIT_ASSERT_EQUAL("file with non-ASCII character 'รค' in its name\n"s, readFile(testFilePath("tรคst.txt")));
368 #endif
369 }
370 
375 {
376  const string path(workingCopyPath("test.ini", WorkingCopyMode::NoCopy));
377  writeFile(path, "some contents");
378  CPPUNIT_ASSERT_EQUAL("some contents"s, readFile(path));
379 }
380 
385 {
386  stringstream ss1;
387  EscapeCodes::enabled = true;
388  ss1 << EscapeCodes::Phrases::Error << "some error" << EscapeCodes::Phrases::End;
389  ss1 << EscapeCodes::Phrases::Warning << "some warning" << EscapeCodes::Phrases::End;
390  ss1 << EscapeCodes::Phrases::Info << "some info" << EscapeCodes::Phrases::End;
391  ss1 << EscapeCodes::Phrases::ErrorMessage << "Arch-style error" << EscapeCodes::Phrases::End;
392  ss1 << EscapeCodes::Phrases::WarningMessage << "Arch-style warning" << EscapeCodes::Phrases::End;
393  ss1 << EscapeCodes::Phrases::PlainMessage << "Arch-style message" << EscapeCodes::Phrases::End;
394  ss1 << EscapeCodes::Phrases::SuccessMessage << "Arch-style success" << EscapeCodes::Phrases::End;
395  ss1 << EscapeCodes::Phrases::SubMessage << "Arch-style sub-message" << EscapeCodes::Phrases::End;
396  ss1 << EscapeCodes::color(EscapeCodes::Color::Blue, EscapeCodes::Color::Red, EscapeCodes::TextAttribute::Blink)
397  << "blue, blinking text on red background" << EscapeCodes::TextAttribute::Reset << '\n';
398  cout << "\noutput for formatting with ANSI escape codes:\n" << ss1.str() << "---------------------------------------------\n";
399  fstream("/tmp/test.txt", ios_base::out | ios_base::trunc) << ss1.str();
400  CPPUNIT_ASSERT_EQUAL("\e[1;31mError: \e[0m\e[1msome error\e[0m\n"
401  "\e[1;33mWarning: \e[0m\e[1msome warning\e[0m\n"
402  "\e[1;34mInfo: \e[0m\e[1msome info\e[0m\n"
403  "\e[1;31m==> ERROR: \e[0m\e[1mArch-style error\e[0m\n"
404  "\e[1;33m==> WARNING: \e[0m\e[1mArch-style warning\e[0m\n"
405  " \e[0m\e[1mArch-style message\e[0m\n"
406  "\e[1;32m==> \e[0m\e[1mArch-style success\e[0m\n"
407  "\e[1;32m -> \e[0m\e[1mArch-style sub-message\e[0m\n"
408  "\e[5;34;41mblue, blinking text on red background\e[0m\n"s,
409  ss1.str());
410 
411  stringstream ss2;
412  EscapeCodes::enabled = false;
413  ss2 << EscapeCodes::Phrases::Info << "some info" << EscapeCodes::Phrases::End;
414  CPPUNIT_ASSERT_EQUAL("Info: some info\n"s, ss2.str());
415 }
416 
417 #ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
418 
421 void IoTests::testNativeFileStream()
422 {
423  // open file by path
424  const auto txtFilePath(workingCopyPath("tรคst.txt"));
425  NativeFileStream fileStream;
426  fileStream.exceptions(ios_base::badbit | ios_base::failbit);
427  CPPUNIT_ASSERT(!fileStream.is_open());
428  fileStream.open(txtFilePath, ios_base::in);
429  CPPUNIT_ASSERT(fileStream.is_open());
430 #if defined(PLATFORM_WINDOWS) && defined(CPP_UTILITIES_USE_BOOST_IOSTREAMS)
431  CPPUNIT_ASSERT(fileStream.fileHandle() != nullptr);
432 #else
433  CPPUNIT_ASSERT(fileStream.fileDescriptor() != -1);
434 #endif
435  CPPUNIT_ASSERT_EQUAL(static_cast<char>(fileStream.get()), 'f');
436  fileStream.seekg(0, ios_base::end);
437  CPPUNIT_ASSERT_EQUAL(fileStream.tellg(), static_cast<NativeFileStream::pos_type>(47));
438  fileStream.close();
439  CPPUNIT_ASSERT(!fileStream.is_open());
440  try {
441  fileStream.open("non existing file", ios_base::in | ios_base::out | ios_base::binary);
442  CPPUNIT_FAIL("expected exception");
443  } catch (const std::ios_base::failure &failure) {
444 #ifdef PLATFORM_WINDOWS
445 #ifdef CPP_UTILITIES_USE_BOOST_IOSTREAMS
446  TESTUTILS_ASSERT_LIKE("expected error with some message", "CreateFileW failed: .+", failure.what());
447 #else
448  TESTUTILS_ASSERT_LIKE("expected error with some message", "_wopen failed: .+", failure.what());
449 #endif
450 #else
451  TESTUTILS_ASSERT_LIKE("expected error with some message", "open failed: .+", failure.what());
452 #endif
453  }
454  fileStream.clear();
455 
456  // open file from file descriptor
457 #ifndef PLATFORM_WINDOWS
458  auto readWriteFileDescriptor = open(txtFilePath.data(), O_RDWR);
459  CPPUNIT_ASSERT(readWriteFileDescriptor);
460  fileStream.open(readWriteFileDescriptor, ios_base::in | ios_base::out | ios_base::binary);
461  CPPUNIT_ASSERT(fileStream.is_open());
462  CPPUNIT_ASSERT_EQUAL(static_cast<char>(fileStream.get()), 'f');
463  fileStream.seekg(0, ios_base::end);
464  CPPUNIT_ASSERT_EQUAL(fileStream.tellg(), static_cast<NativeFileStream::pos_type>(47));
465  fileStream.flush();
466  fileStream.close();
467  CPPUNIT_ASSERT(!fileStream.is_open());
468 #endif
469  try {
470  fileStream.open(-1, ios_base::in | ios_base::out | ios_base::binary);
471  fileStream.get();
472  CPPUNIT_FAIL("expected exception");
473  } catch (const std::ios_base::failure &failure) {
474 #ifndef PLATFORM_WINDOWS
476  "expected error message", "(basic_ios::clear|failed reading: Bad file descriptor): iostream error"s, string(failure.what()));
477 #else
478  CPP_UTILITIES_UNUSED(failure)
479 #endif
480  }
481  fileStream.clear();
482 
483  // append + write file via path
484  NativeFileStream fileStream2;
485  fileStream2.exceptions(ios_base::failbit | ios_base::badbit);
486  fileStream2.open(txtFilePath, ios_base::in | ios_base::out | ios_base::app);
487  CPPUNIT_ASSERT(fileStream2.is_open());
488  fileStream2 << "foo";
489  fileStream2.flush();
490  fileStream2.close();
491  CPPUNIT_ASSERT(!fileStream2.is_open());
492  CPPUNIT_ASSERT_EQUAL("file with non-ASCII character 'รค' in its name\nfoo"s, readFile(txtFilePath, 50));
493 
494  // truncate + write file via path
495  fileStream2.open(txtFilePath, ios_base::out | ios_base::trunc);
496  CPPUNIT_ASSERT(fileStream2.is_open());
497  fileStream2 << "bar";
498  fileStream2.close();
499  CPPUNIT_ASSERT(!fileStream2.is_open());
500  CPPUNIT_ASSERT_EQUAL("bar"s, readFile(txtFilePath, 4));
501 
502  // append + write via file descriptor from file handle
503 #ifdef PLATFORM_WINDOWS
504  const auto wideTxtFilePath = NativeFileStream::makeWidePath(txtFilePath);
505  const auto appendFileHandle = _wfopen(wideTxtFilePath.get(), L"a+");
506 #else
507  const auto appendFileHandle = fopen(txtFilePath.data(), "a");
508 #endif
509  CPPUNIT_ASSERT(appendFileHandle);
510  fileStream2.open(fileno(appendFileHandle), ios_base::out | ios_base::app);
511  CPPUNIT_ASSERT(fileStream2.is_open());
512  fileStream2 << "foo";
513  fileStream2.close();
514  CPPUNIT_ASSERT(!fileStream2.is_open());
515  CPPUNIT_ASSERT_EQUAL("barfoo"s, readFile(txtFilePath, 7));
516 }
517 #endif
IoTests::testWriteFile
void testWriteFile()
Tests writeFile().
Definition: iotests.cpp:374
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:471
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:415
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:327
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:13
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:154
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:560
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::writeFile
CPP_UTILITIES_EXPORT void writeFile(std::string_view path, std::string_view contents)
Writes all contents to the specified file in a single call.
Definition: misc.cpp:39
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:542
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:505
CppUtilities::BinaryWriter
Writes primitive data types to a std::ostream.
Definition: binarywriter.h:14
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:439
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:154
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:570
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:243
IoTests::setUp
void setUp() override
Definition: iotests.cpp:77
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:16
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()
Tests formatting functions of CppUtilities::EscapeCodes namespace.
Definition: iotests.cpp:384
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:160
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:113
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:294
IoTests::testIniFile
void testIniFile()
Tests IniFile.
Definition: iotests.cpp:280
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:533
IoTests::testBitReader
void testBitReader()
Tests the BitReader.
Definition: iotests.cpp:236
CppUtilities::IniFile::parse
void parse(std::istream &inputStream)
Parses all data from the specified inputStream.
Definition: inifile.cpp:21
CppUtilities::BinaryReader::readStreamsize
std::istream::pos_type readStreamsize()
Returns the size of the assigned stream.
Definition: binaryreader.cpp:51
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:275
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:457
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:138
TESTUTILS_ASSERT_LIKE
#define TESTUTILS_ASSERT_LIKE(message, expectedRegex, actualString)
Asserts whether the specified string matches the specified regex.
Definition: testutils.h:275
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:351
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:481
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:88
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:252
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:379
CppUtilities
Contains all utilities provides by the c++utilities library.
Definition: argumentparser.h:17
i
constexpr int i
Definition: traitstests.cpp:103
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:448
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:429
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:495
CppUtilities::BinaryReader::readTerminatedString
std::string readTerminatedString(std::uint8_t termination=0)
Reads a terminated string from the current stream.
Definition: binaryreader.cpp:111
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:514
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:344
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:360
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:303
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:341
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:317
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::IniFile::data
ScopeList & data()
Returns the data of the file.
Definition: inifile.h:43
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:96
CppUtilities::Literals
Contains literals to ease asserting with CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:305
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:406
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:134
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:388
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::BinaryReader::readRemainingBytes
std::istream::pos_type readRemainingBytes()
Returns the number of remaining bytes in the stream from the current offset.
Definition: binaryreader.cpp:66
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
IoTests::tearDown
void tearDown() override
Definition: iotests.cpp:81
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:261
IoTests::testCopy
void testCopy()
Tests CopyHelper.
Definition: iotests.cpp:321
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:285
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:397
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:263
CppUtilities::BinaryReader::readLengthPrefixedString
std::string readLengthPrefixedString()
Reads a length prefixed string from the current stream.
Definition: binaryreader.h:580
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