C++ Utilities 5.25.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Loading...
Searching...
No Matches
iotests.cpp
Go to the documentation of this file.
1#include "./testutils.h"
2
4
8std::ostream &operator<<(std::ostream &out, const std::wstring &s)
9{
10 const auto utf8 = CppUtilities::
11#ifdef CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN
13#else
15#endif
16 (reinterpret_cast<const char *>(s.data()), s.size() * (sizeof(std::wstring::value_type) / sizeof(char)));
17 out.write(utf8.first.get(), static_cast<std::streamsize>(utf8.second));
18 return out;
19}
20
23
25#include "../io/binaryreader.h"
26#include "../io/binarywriter.h"
27#include "../io/bitreader.h"
28#include "../io/buffersearch.h"
29#include "../io/copy.h"
30#include "../io/inifile.h"
31#include "../io/misc.h"
33#include "../io/path.h"
34
35#include <cppunit/TestFixture.h>
36#include <cppunit/extensions/HelperMacros.h>
37
38#include <algorithm>
39#include <cstddef>
40#include <fstream>
41#include <regex>
42#include <sstream>
43
44#ifdef PLATFORM_WINDOWS
45#include <cstdio>
46#endif
47
48#ifdef PLATFORM_UNIX
49#include <sys/fcntl.h>
50#include <sys/types.h>
51#endif
52
53using namespace std;
54using namespace CppUtilities;
55using namespace CppUtilities::Literals;
56using namespace CPPUNIT_NS;
57
61class IoTests : public TestFixture {
62 CPPUNIT_TEST_SUITE(IoTests);
63 CPPUNIT_TEST(testBinaryReader);
64 CPPUNIT_TEST(testBinaryWriter);
65 CPPUNIT_TEST(testBitReader);
66 CPPUNIT_TEST(testBufferSearch);
67 CPPUNIT_TEST(testPathUtilities);
68 CPPUNIT_TEST(testIniFile);
69 CPPUNIT_TEST(testAdvancedIniFile);
70 CPPUNIT_TEST(testCopy);
71 CPPUNIT_TEST(testCopyWithNativeFileStream);
72 CPPUNIT_TEST(testReadFile);
73 CPPUNIT_TEST(testWriteFile);
74 CPPUNIT_TEST(testAnsiEscapeCodes);
75#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
76 CPPUNIT_TEST(testNativeFileStream);
77#endif
78 CPPUNIT_TEST_SUITE_END();
79
80public:
81 void setUp() override;
82 void tearDown() override;
83
84 void testBinaryReader();
85 void testBinaryWriter();
86 void testBitReader();
87 void testBufferSearch();
88 void testPathUtilities();
89 void testIniFile();
91 void testCopy();
93 void testReadFile();
94 void testWriteFile();
96#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
97 void testNativeFileStream();
98#endif
99};
100
102
104{
105}
106
108{
109}
110
115{
116 // read test file
117 fstream testFile;
118 testFile.exceptions(ios_base::failbit | ios_base::badbit);
119 testFile.open(testFilePath("some_data"), ios_base::in | ios_base::binary);
120 BinaryReader reader(&testFile);
121 CPPUNIT_ASSERT_EQUAL(static_cast<istream::pos_type>(398), reader.readStreamsize());
122 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint16_t>(0x0102u), reader.readUInt16LE());
123 CPPUNIT_ASSERT_EQUAL(static_cast<istream::pos_type>(396), reader.readRemainingBytes());
124 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint16_t>(0x0102u), reader.readUInt16BE());
125 CPPUNIT_ASSERT_EQUAL(0x010203u, reader.readUInt24LE());
126 CPPUNIT_ASSERT_EQUAL(0x010203u, reader.readUInt24BE());
127 CPPUNIT_ASSERT_EQUAL(0x01020304u, reader.readUInt32LE());
128 CPPUNIT_ASSERT_EQUAL(0x01020304u, reader.readUInt32BE());
129 CPPUNIT_ASSERT_EQUAL(0x0102030405u, reader.readUInt40LE());
130 CPPUNIT_ASSERT_EQUAL(0x0102030405u, reader.readUInt40BE());
131 CPPUNIT_ASSERT_EQUAL(0x01020304050607u, reader.readUInt56LE());
132 CPPUNIT_ASSERT_EQUAL(0x01020304050607u, reader.readUInt56BE());
133 CPPUNIT_ASSERT_EQUAL(0x0102030405060708u, reader.readUInt64LE());
134 CPPUNIT_ASSERT_EQUAL(0x0102030405060708u, reader.readUInt64BE());
135 testFile.seekg(0);
136 CPPUNIT_ASSERT_EQUAL(reader.readInt16LE(), static_cast<std::int16_t>(0x0102));
137 CPPUNIT_ASSERT_EQUAL(reader.readInt16BE(), static_cast<std::int16_t>(0x0102));
138 CPPUNIT_ASSERT_EQUAL(0x010203, reader.readInt24LE());
139 CPPUNIT_ASSERT_EQUAL(0x010203, reader.readInt24BE());
140 CPPUNIT_ASSERT_EQUAL(0x01020304, reader.readInt32LE());
141 CPPUNIT_ASSERT_EQUAL(0x01020304, reader.readInt32BE());
142 CPPUNIT_ASSERT_EQUAL(0x0102030405, reader.readInt40LE());
143 CPPUNIT_ASSERT_EQUAL(0x0102030405, reader.readInt40BE());
144 CPPUNIT_ASSERT_EQUAL(0x01020304050607, reader.readInt56LE());
145 CPPUNIT_ASSERT_EQUAL(0x01020304050607, reader.readInt56BE());
146 CPPUNIT_ASSERT_EQUAL(0x0102030405060708, reader.readInt64LE());
147 CPPUNIT_ASSERT_EQUAL(0x0102030405060708, reader.readInt64BE());
148 CPPUNIT_ASSERT_EQUAL(1.125f, reader.readFloat32LE());
149 CPPUNIT_ASSERT_EQUAL(1.625, reader.readFloat64LE());
150 CPPUNIT_ASSERT_EQUAL(1.125f, reader.readFloat32BE());
151 CPPUNIT_ASSERT_EQUAL(reader.readFloat64BE(), 1.625);
152 CPPUNIT_ASSERT_EQUAL(false, reader.readBool());
153 CPPUNIT_ASSERT_EQUAL(true, reader.readBool());
154 CPPUNIT_ASSERT_EQUAL("abc"s, reader.readString(3));
155 CPPUNIT_ASSERT_EQUAL(reader.readLengthPrefixedString(), "ABC"s);
156 CPPUNIT_ASSERT_EQUAL("01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901"
157 "23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123"
158 "45678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"
159 "678901234567890123456789"s,
160 reader.readLengthPrefixedString());
161 CPPUNIT_ASSERT_EQUAL("def"s, reader.readTerminatedString());
162 testFile.seekg(-4, ios_base::cur);
163 CPPUNIT_ASSERT_EQUAL("def"s, reader.readTerminatedString(5, 0));
164 CPPUNIT_ASSERT_THROW(reader.readLengthPrefixedString(), ConversionException);
165 CPPUNIT_ASSERT_MESSAGE("pos in stream not advanced on conversion error", reader.readByte() == 0);
166
167 // test ownership
168 reader.setStream(nullptr, true);
169 reader.setStream(new fstream(), true);
170 BinaryReader reader2(reader);
171 CPPUNIT_ASSERT(reader2.stream() == reader.stream());
172 CPPUNIT_ASSERT(!reader2.hasOwnership());
173 reader.setStream(&testFile, false);
174 reader.setStream(new fstream(), true);
175}
176
181{
182 // prepare reading expected data
183 fstream testFile;
184 testFile.exceptions(ios_base::failbit | ios_base::badbit);
185 testFile.open(testFilePath("some_data"), ios_base::in | ios_base::binary);
186
187 // prepare output stream
188 stringstream outputStream(ios_base::in | ios_base::out | ios_base::binary);
189 outputStream.exceptions(ios_base::failbit | ios_base::badbit);
190 char testData[397];
191#if defined(__GLIBCXX__) && !defined(_LIBCPP_VERSION)
192#define USE_RDBUF_DIRECTLY
193 outputStream.rdbuf()->pubsetbuf(testData, sizeof(testData));
194#endif
195
196 // write test data
197 BinaryWriter writer(&outputStream);
198 writer.writeUInt16LE(0x0102u);
199 writer.writeUInt16BE(0x0102u);
200 writer.writeUInt24LE(0x010203u);
201 writer.writeUInt24BE(0x010203u);
202 writer.writeUInt32LE(0x01020304u);
203 writer.writeUInt32BE(0x01020304u);
204 writer.writeUInt40LE(0x0102030405u);
205 writer.writeUInt40BE(0x0102030405u);
206 writer.writeUInt56LE(0x01020304050607u);
207 writer.writeUInt56BE(0x01020304050607u);
208 writer.writeUInt64LE(0x0102030405060708u);
209 writer.writeUInt64BE(0x0102030405060708u);
210
211#ifndef USE_RDBUF_DIRECTLY
212 outputStream.seekg(0);
213 outputStream.read(testData, 58);
214#endif
215
216 // test written values
217 for (char c : testData) {
218 const auto pos = static_cast<std::size_t>(testFile.tellg());
219 if (pos >= 58) {
220 break;
221 }
222 char expected;
223 testFile.read(&expected, 1);
224 CPPUNIT_ASSERT_EQUAL_MESSAGE(argsToString("offset ", pos), asHexNumber(expected), asHexNumber(c));
225 }
226 testFile.seekg(0);
227 outputStream.seekp(0);
228
229 // write more test data
230 writer.writeInt16LE(0x0102);
231 writer.writeInt16BE(0x0102);
232 writer.writeInt24LE(0x010203);
233 writer.writeInt24BE(0x010203);
234 writer.writeInt32LE(0x01020304);
235 writer.writeInt32BE(0x01020304);
236 writer.writeInt40LE(0x0102030405);
237 writer.writeInt40BE(0x0102030405);
238 writer.writeInt56LE(0x01020304050607);
239 writer.writeInt56BE(0x01020304050607);
240 writer.writeInt64LE(0x0102030405060708);
241 writer.writeInt64BE(0x0102030405060708);
242 writer.writeFloat32LE(1.125);
243 writer.writeFloat64LE(1.625);
244 writer.writeFloat32BE(1.125);
245 writer.writeFloat64BE(1.625);
246 writer.writeBool(false);
247 writer.writeBool(true);
248 writer.writeString("abc");
249 writer.writeLengthPrefixedString("ABC");
250 writer.writeLengthPrefixedString("012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
251 "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901"
252 "234567890123456789012345678901234567890123456789012345678901234567890123456789");
253 writer.writeTerminatedString("def");
254
255#ifndef USE_RDBUF_DIRECTLY
256 outputStream.seekg(0);
257 outputStream.read(testData, 58);
258#endif
259
260 // test written values
261 for (char c : testData) {
262 const auto pos = static_cast<std::size_t>(testFile.tellg());
263 if (pos >= 58) {
264 break;
265 }
266 char expected;
267 testFile.read(&expected, 1);
268 CPPUNIT_ASSERT_EQUAL_MESSAGE(argsToString("offset ", pos), asHexNumber(expected), asHexNumber(c));
269 }
270
271 // test ownership
272 writer.setStream(nullptr, true);
273 writer.setStream(new fstream(), true);
274 BinaryWriter writer2(writer);
275 CPPUNIT_ASSERT(writer2.stream() == writer.stream());
276 CPPUNIT_ASSERT(!writer2.hasOwnership());
277 writer.setStream(&testFile, false);
278 writer.setStream(new fstream(), true);
279}
280
285{
286 const std::uint8_t testData[] = { 0x81, 0x90, 0x3C, 0x44, 0x28, 0x00, 0x44, 0x10, 0x20, 0xFF, 0xFA };
287 BitReader reader(reinterpret_cast<const char *>(testData), sizeof(testData));
288 CPPUNIT_ASSERT(reader.readBit() == 1);
289 reader.skipBits(6);
290 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(3), reader.showBits<std::uint8_t>(2));
291 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(3), reader.readBits<std::uint8_t>(2));
292 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint32_t>(0x103C4428 << 1), reader.readBits<std::uint32_t>(32));
293 reader.align();
294 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0x44), reader.readBits<std::uint8_t>(8));
295 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(7), reader.readUnsignedExpGolombCodedBits<std::uint8_t>());
296 CPPUNIT_ASSERT_EQUAL(static_cast<std::int8_t>(4), reader.readSignedExpGolombCodedBits<std::int8_t>());
297 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0), reader.readBit());
298 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0), reader.readBit());
299 reader.skipBits(8 + 4);
300 CPPUNIT_ASSERT_EQUAL(4_st, reader.bitsAvailable());
301 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0xA), reader.readBits<std::uint8_t>(4));
302 CPPUNIT_ASSERT_THROW(reader.readBit(), std::ios_base::failure);
303 CPPUNIT_ASSERT_THROW(reader.skipBits(1), std::ios_base::failure);
304 reader.reset(reinterpret_cast<const char *>(testData), sizeof(testData));
305 CPPUNIT_ASSERT_EQUAL(static_cast<std::size_t>(8 * sizeof(testData)), reader.bitsAvailable());
306}
307
312{
313 // setup search to test
314 auto expectedResult = std::string();
315 auto hasResult = false;
316 auto bs = BufferSearch("Updated version: ", "\t\n", "Starting build", [&](BufferSearch &, std::string &&result) {
317 CPPUNIT_ASSERT_EQUAL(expectedResult, result);
318 CPPUNIT_ASSERT_MESSAGE("callback only invoked once", !hasResult);
319 hasResult = true;
320 });
321
322 // feed data into the search
323 char buffer[30] = { 0 };
324 bs(buffer, 0);
325 CPPUNIT_ASSERT(!hasResult);
326 std::strcpy(buffer, "Starting Updated");
327 CPPUNIT_ASSERT(bs.process(std::string_view(buffer, 16)) == nullptr);
328 CPPUNIT_ASSERT(!hasResult);
329 std::strcpy(buffer, " version: some ");
330 bs(buffer, 15);
331 CPPUNIT_ASSERT(!hasResult);
332 expectedResult = "some version number";
333 std::strcpy(buffer, "version number\tmore chars");
334 CPPUNIT_ASSERT_EQUAL(static_cast<std::ptrdiff_t>(14), bs.process(buffer, 25) - buffer);
335 CPPUNIT_ASSERT(hasResult);
336 hasResult = false;
337 std::strcpy(buffer, "... Starting build ...");
338 bs(buffer, 22);
339 CPPUNIT_ASSERT(!hasResult);
340}
341
346{
347 CPPUNIT_ASSERT_EQUAL("libc++utilities.so"s, fileName("C:\\libs\\libc++utilities.so"));
348 CPPUNIT_ASSERT_EQUAL("libc++utilities.so"s, fileName("C:\\libs/libc++utilities.so"));
349 CPPUNIT_ASSERT_EQUAL("libc++utilities.so"s, fileName("/usr/lib/libc++utilities.so"));
350 CPPUNIT_ASSERT_EQUAL("libc++utilities.so"s, fileName("libc++utilities.so"));
351 CPPUNIT_ASSERT_EQUAL("/usr/lib/"s, directory("/usr/lib/libc++utilities.so"));
352 CPPUNIT_ASSERT_EQUAL(string(), directory("libc++utilities.so"));
353 CPPUNIT_ASSERT_EQUAL("C:\\libs\\"s, directory("C:\\libs\\libc++utilities.so"));
354 CPPUNIT_ASSERT_EQUAL("C:\\libs/"s, directory("C:\\libs/libc++utilities.so"));
355 string invalidPath("lib/c++uti*lities.so?");
356 removeInvalidChars(invalidPath);
357 CPPUNIT_ASSERT(invalidPath == "libc++utilities.so");
358
359 const auto input = std::string_view("some/path/täst");
360 const auto expected = input;
361 const auto output = makeNativePath(input);
362#ifdef PLATFORM_WINDOWS
363 const auto outputAsUtf8 = convertUtf16LEToUtf8(reinterpret_cast<const char *>(output.data()), output.size() * 2);
364 const auto outputView = std::string_view(outputAsUtf8.first.get(), outputAsUtf8.second);
365 CPPUNIT_ASSERT_EQUAL_MESSAGE("makeNativePath()", expected, outputView);
366#else
367 CPPUNIT_ASSERT_EQUAL_MESSAGE("makeNativePath()", expected, output);
368#endif
369}
370
375{
376 // prepare reading test file
377 fstream inputFile;
378 inputFile.exceptions(ios_base::failbit | ios_base::badbit);
379 inputFile.open(testFilePath("test.ini"), ios_base::in);
380
381 IniFile ini;
382 ini.parse(inputFile);
383 const auto globalScope = ini.data().at(0);
384 const auto scope1 = ini.data().at(1);
385 const auto scope2 = ini.data().at(2);
386 CPPUNIT_ASSERT(globalScope.first.empty());
387 CPPUNIT_ASSERT(globalScope.second.find("key0") != globalScope.second.cend());
388 CPPUNIT_ASSERT(globalScope.second.find("key0")->second == "value 0");
389 CPPUNIT_ASSERT(globalScope.second.find("key1") == globalScope.second.cend());
390 CPPUNIT_ASSERT(scope1.first == "scope 1");
391 CPPUNIT_ASSERT(scope1.second.find("key1") != scope1.second.cend());
392 CPPUNIT_ASSERT(scope1.second.find("key1")->second == "value 1");
393 CPPUNIT_ASSERT(scope1.second.find("key2") != scope1.second.cend());
394 CPPUNIT_ASSERT(scope1.second.find("key2")->second == "value=2");
395 CPPUNIT_ASSERT(scope2.first == "scope 2");
396 CPPUNIT_ASSERT(scope2.second.find("key5") == scope2.second.cend());
397
398 // write values to another file
399 fstream outputFile;
400 outputFile.exceptions(ios_base::failbit | ios_base::badbit);
401 outputFile.open(workingCopyPath("output.ini", WorkingCopyMode::NoCopy), ios_base::out | ios_base::trunc);
402 ini.make(outputFile);
403
404 // parse written values (again)
405 outputFile.close();
406 outputFile.open(workingCopyPath("output.ini", WorkingCopyMode::NoCopy), ios_base::in);
407 IniFile ini2;
408 ini2.parse(outputFile);
409 CPPUNIT_ASSERT(ini.data() == ini2.data());
410}
411
416{
417 // prepare reading test file
418 fstream inputFile;
419 inputFile.exceptions(ios_base::failbit | ios_base::badbit);
420 inputFile.open(testFilePath("pacman.conf"), ios_base::in);
421
422 // parse the test file
423 AdvancedIniFile ini;
424 ini.parse(inputFile);
425
426 // check whether scope data is as expected
427 CPPUNIT_ASSERT_EQUAL_MESSAGE("5 scopes (taking implicit empty section at the end into account)", 5_st, ini.sections.size());
428 auto options = ini.findSection("options");
429 CPPUNIT_ASSERT(options != ini.sectionEnd());
430#if !defined(PLATFORM_WINDOWS) || defined(PLATFORM_MINGW) || defined(PLATFORM_CYGWIN)
431#define STD_REGEX_WORKS // the parsed data looks good, apparently std::regex behaves differently on MSVC
433 "comment block before section", "# Based on.*\n.*# GENERAL OPTIONS\n#\n"s, std::regex::extended, options->precedingCommentBlock);
434#endif
435 CPPUNIT_ASSERT_EQUAL(7_st, options->fields.size());
436 CPPUNIT_ASSERT_EQUAL("HoldPkg"s, options->fields[0].key);
437 CPPUNIT_ASSERT_EQUAL("pacman glibc"s, options->fields[0].value);
438 CPPUNIT_ASSERT_MESSAGE("value present", options->fields[0].flags & IniFileFieldFlags::HasValue);
439#ifdef STD_REGEX_WORKS
440 TESTUTILS_ASSERT_LIKE_FLAGS("comment block between section header and first field",
441 "# The following paths are.*\n.*#HookDir = /etc/pacman\\.d/hooks/\n"s, std::regex::extended, options->fields[0].precedingCommentBlock);
442#endif
443 CPPUNIT_ASSERT_EQUAL(""s, options->fields[0].followingInlineComment);
444 CPPUNIT_ASSERT_EQUAL("Foo"s, options->fields[1].key);
445 CPPUNIT_ASSERT_EQUAL("bar"s, options->fields[1].value);
446 CPPUNIT_ASSERT_MESSAGE("value present", options->fields[1].flags & IniFileFieldFlags::HasValue);
447#ifdef STD_REGEX_WORKS
448 TESTUTILS_ASSERT_LIKE_FLAGS("comment block between fields", "#XferCommand.*\n.*#CleanMethod = KeepInstalled\n"s, std::regex::extended,
449 options->fields[1].precedingCommentBlock);
450#endif
451 CPPUNIT_ASSERT_EQUAL("# inline comment"s, options->fields[1].followingInlineComment);
452 CPPUNIT_ASSERT_EQUAL("CheckSpace"s, options->fields[3].key);
453 CPPUNIT_ASSERT_EQUAL(""s, options->fields[3].value);
454 CPPUNIT_ASSERT_MESSAGE("no value present", !(options->fields[3].flags & IniFileFieldFlags::HasValue));
455#ifdef STD_REGEX_WORKS
456 TESTUTILS_ASSERT_LIKE_FLAGS("empty lines in comments preserved", "\n# Pacman.*\n.*\n\n#NoUpgrade =\n.*#TotalDownload\n"s, std::regex::extended,
457 options->fields[3].precedingCommentBlock);
458#endif
459 CPPUNIT_ASSERT_EQUAL(""s, options->fields[3].followingInlineComment);
460 auto extraScope = ini.findSection(options, "extra");
461 CPPUNIT_ASSERT(extraScope != ini.sectionEnd());
462 CPPUNIT_ASSERT_EQUAL_MESSAGE("comment block which is only an empty line", "\n"s, extraScope->precedingCommentBlock);
463 CPPUNIT_ASSERT_EQUAL_MESSAGE("inline comment after scope", "# an inline comment after a scope name"s, extraScope->followingInlineComment);
464 CPPUNIT_ASSERT_EQUAL(1_st, extraScope->fields.size());
465 CPPUNIT_ASSERT(ini.sections.back().flags & IniFileSectionFlags::Implicit);
466#ifdef STD_REGEX_WORKS
467 TESTUTILS_ASSERT_LIKE_FLAGS("comment block after last field present in implicitly added last scope", "\n# If you.*\n.*custompkgs\n"s,
468 std::regex::extended, ini.sections.back().precedingCommentBlock);
469#endif
470
471 // test finding a field from file level and const access
472 const auto *const constIniFile = &ini;
473 auto includeField = constIniFile->findField("extra", "Include");
474 CPPUNIT_ASSERT(includeField.has_value());
475 CPPUNIT_ASSERT_EQUAL("Include"s, includeField.value()->key);
476 CPPUNIT_ASSERT_EQUAL("/etc/pacman.d/mirrorlist"s, includeField.value()->value);
477 CPPUNIT_ASSERT_MESSAGE("field not present", !constIniFile->findField("extra", "Includ").has_value());
478 CPPUNIT_ASSERT_MESSAGE("scope not present", !constIniFile->findField("extr", "Includ").has_value());
479
480 // write values again; there shouldn't be a difference as the parser and the writer are supposed to
481 // preserve the order of all elements and comments
482 std::stringstream newFile;
483 ini.make(newFile);
484 std::string originalContents;
485 inputFile.clear();
486 inputFile.seekg(std::ios_base::beg);
487 // ignore warning about null pointer dereference from GCC 12 for now (which is *likely* not correct)
488#ifdef __GNUC__
489#pragma GCC diagnostic push
490#pragma GCC diagnostic ignored "-Wnull-dereference"
491#endif
492 originalContents.assign((istreambuf_iterator<char>(inputFile)), istreambuf_iterator<char>());
493#ifdef __GNUC__
494#pragma GCC diagnostic pop
495#endif
496 CPPUNIT_ASSERT_EQUAL(originalContents, newFile.str());
497}
498
503{
504 // prepare streams
505 auto testFile = std::fstream();
506 testFile.exceptions(ios_base::failbit | ios_base::badbit);
507 testFile.open(testFilePath("some_data"), ios_base::in | ios_base::binary);
508 auto outputStream = std::stringstream(ios_base::in | ios_base::out | ios_base::binary);
509 outputStream.exceptions(ios_base::failbit | ios_base::badbit);
510
511 // copy
512 auto copyHelper = CopyHelper<13>();
513 copyHelper.copy(testFile, outputStream, 50);
514 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(0), outputStream.tellg());
515 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), outputStream.tellp());
516 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), testFile.tellg());
517 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), testFile.tellp());
518
519 // verify
520 testFile.seekg(0);
521 for (auto i = 0; i < 50; ++i) {
522 CPPUNIT_ASSERT_EQUAL(testFile.get(), outputStream.get());
523 }
524}
525
530{
531 // prepare streams
532 auto testFile = NativeFileStream();
533 testFile.exceptions(ios_base::failbit | ios_base::badbit);
534 testFile.open(testFilePath("some_data"), ios_base::in | ios_base::binary);
535 auto outputPath = workingCopyPath("copied_data", WorkingCopyMode::Cleanup);
536 auto outputStream = NativeFileStream();
537 outputStream.exceptions(ios_base::failbit | ios_base::badbit);
538 outputStream.open(outputPath, ios_base::out | ios_base::binary);
539
540 // copy
541 auto copyHelper = CopyHelper<13>();
542 copyHelper.copy(testFile, outputStream, 50);
543 CPPUNIT_ASSERT(outputStream.is_open());
544 CPPUNIT_ASSERT(testFile.is_open());
545
546 // test seek positions (the expected values are from a run with normal std::fstream)
547 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), outputStream.tellg());
548 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), outputStream.tellp());
549 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), testFile.tellg());
550 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), testFile.tellp());
551
552 // add a few more bytes to output stream (to be sure it is still usable) and re-open for reading
553 const auto aFewMoreBytes = "a few more bytes"sv;
554 outputStream << aFewMoreBytes;
555 outputStream.close();
556 outputStream.open(outputPath, ios_base::in | ios_base::binary);
557
558 // verify
559 testFile.seekg(0);
560 for (auto i = 0; i < 50; ++i) {
561 CPPUNIT_ASSERT_EQUAL(testFile.get(), outputStream.get());
562 }
563 auto tail = std::string(aFewMoreBytes.size(), '0');
564 outputStream.read(tail.data(), static_cast<std::streamsize>(tail.size()));
565 CPPUNIT_ASSERT_EQUAL(aFewMoreBytes, std::string_view(tail.data(), tail.size()));
566
567 // repeat with callback version
568 auto percentage = 0.0;
569 const auto isAborted = [] { return false; };
570 const auto callback = [&percentage](double p) { percentage = p; };
571 testFile.seekg(0);
572 outputStream.close();
573 outputStream.open(outputPath, ios_base::out | ios_base::trunc | ios_base::binary);
574 copyHelper.callbackCopy(testFile, outputStream, 50, isAborted, callback);
575 CPPUNIT_ASSERT_EQUAL(1.0, percentage);
576
577 // verify again
578 CPPUNIT_ASSERT(outputStream.is_open());
579 CPPUNIT_ASSERT(testFile.is_open());
580 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), outputStream.tellg());
581 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), outputStream.tellp());
582 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), testFile.tellg());
583 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), testFile.tellp());
584 outputStream << aFewMoreBytes;
585 outputStream.close();
586 outputStream.open(outputPath, ios_base::in | ios_base::binary);
587 testFile.seekg(0);
588 for (auto i = 0; i < 50; ++i) {
589 CPPUNIT_ASSERT_EQUAL(testFile.get(), outputStream.get());
590 }
591 tail.assign(aFewMoreBytes.size(), '0');
592 outputStream.read(tail.data(), static_cast<std::streamsize>(tail.size()));
593 CPPUNIT_ASSERT_EQUAL(aFewMoreBytes, std::string_view(tail.data(), tail.size()));
594}
595
600{
601 // read a file successfully
602 const string iniFilePath(testFilePath("test.ini"));
603 CPPUNIT_ASSERT_EQUAL("# file for testing INI parser\n"
604 "key0=value 0\n"
605 "\n"
606 "[scope 1]\n"
607 "key1=value 1 # comment\n"
608 "key2=value=2\n"
609 "key3=value 3\n"
610 "\n"
611 "[scope 2]\n"
612 "key4=value 4\n"
613 "#key5=value 5\n"
614 "key6=value 6\n"s,
615 readFile(iniFilePath));
616
617 // fail by exceeding max size
618 CPPUNIT_ASSERT_THROW(readFile(iniFilePath, 10), std::ios_base::failure);
619
620 // handle UTF-8 in path and file contents correctly via NativeFileStream
621#if !defined(PLATFORM_WINDOWS) || defined(CPP_UTILITIES_USE_NATIVE_FILE_BUFFER)
622 CPPUNIT_ASSERT_EQUAL("file with non-ASCII character 'ä' in its name\n"s, readFile(testFilePath("täst.txt")));
623#endif
624}
625
630{
631 const string path(workingCopyPath("test.ini", WorkingCopyMode::NoCopy));
632 writeFile(path, "some contents");
633 CPPUNIT_ASSERT_EQUAL("some contents"s, readFile(path));
634}
635
640{
641 stringstream ss1;
643 ss1 << EscapeCodes::Phrases::Error << "some error" << EscapeCodes::Phrases::End;
644 ss1 << EscapeCodes::Phrases::Warning << "some warning" << EscapeCodes::Phrases::End;
645 ss1 << EscapeCodes::Phrases::Info << "some info" << EscapeCodes::Phrases::End;
646 ss1 << EscapeCodes::Phrases::ErrorMessage << "Arch-style error" << EscapeCodes::Phrases::End;
647 ss1 << EscapeCodes::Phrases::WarningMessage << "Arch-style warning" << EscapeCodes::Phrases::End;
648 ss1 << EscapeCodes::Phrases::PlainMessage << "Arch-style message" << EscapeCodes::Phrases::End;
649 ss1 << EscapeCodes::Phrases::SuccessMessage << "Arch-style success" << EscapeCodes::Phrases::End;
650 ss1 << EscapeCodes::Phrases::SubMessage << "Arch-style sub-message" << EscapeCodes::Phrases::End;
651 ss1 << EscapeCodes::color(EscapeCodes::Color::Blue, EscapeCodes::Color::Red, EscapeCodes::TextAttribute::Blink)
652 << "blue, blinking text on red background" << EscapeCodes::TextAttribute::Reset << '\n';
653 cout << "\noutput for formatting with ANSI escape codes:\n" << ss1.str() << "---------------------------------------------\n";
654 CPPUNIT_ASSERT_EQUAL("\033[1;31mError: \033[0m\033[1msome error\033[0m\n"
655 "\033[1;33mWarning: \033[0m\033[1msome warning\033[0m\n"
656 "\033[1;34mInfo: \033[0m\033[1msome info\033[0m\n"
657 "\033[1;31m==> ERROR: \033[0m\033[1mArch-style error\033[0m\n"
658 "\033[1;33m==> WARNING: \033[0m\033[1mArch-style warning\033[0m\n"
659 " \033[0m\033[1mArch-style message\033[0m\n"
660 "\033[1;32m==> \033[0m\033[1mArch-style success\033[0m\n"
661 "\033[1;32m -> \033[0m\033[1mArch-style sub-message\033[0m\n"
662 "\033[5;34;41mblue, blinking text on red background\033[0m\n"s,
663 ss1.str());
664
665 stringstream ss2;
666 EscapeCodes::enabled = false;
667 ss2 << EscapeCodes::Phrases::Info << "some info" << EscapeCodes::Phrases::End;
668 CPPUNIT_ASSERT_EQUAL("Info: some info\n"s, ss2.str());
669}
670
671#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
675void IoTests::testNativeFileStream()
676{
677 return;
678 // open file by path
679 const auto txtFilePath(workingCopyPath("täst.txt"));
680 NativeFileStream fileStream;
681 fileStream.exceptions(ios_base::badbit | ios_base::failbit);
682 CPPUNIT_ASSERT(!fileStream.is_open());
683 fileStream.open(txtFilePath, ios_base::in);
684 CPPUNIT_ASSERT(fileStream.is_open());
685#if defined(PLATFORM_WINDOWS) && defined(CPP_UTILITIES_USE_BOOST_IOSTREAMS)
686 CPPUNIT_ASSERT(fileStream.fileHandle() != nullptr);
687#else
688 CPPUNIT_ASSERT(fileStream.fileDescriptor() != -1);
689#endif
690 CPPUNIT_ASSERT_EQUAL(static_cast<char>(fileStream.get()), 'f');
691 fileStream.seekg(0, ios_base::end);
692 CPPUNIT_ASSERT_EQUAL(fileStream.tellg(), static_cast<NativeFileStream::pos_type>(47));
693 fileStream.close();
694 CPPUNIT_ASSERT(!fileStream.is_open());
695 try {
696 fileStream.open("non existing file", ios_base::in | ios_base::out | ios_base::binary);
697 CPPUNIT_FAIL("expected exception");
698 } catch (const std::ios_base::failure &failure) {
699#ifdef PLATFORM_WINDOWS
700#ifdef CPP_UTILITIES_USE_BOOST_IOSTREAMS
701 TESTUTILS_ASSERT_LIKE("expected error with some message", "CreateFileW failed: .+", failure.what());
702#else
703 TESTUTILS_ASSERT_LIKE("expected error with some message", "_wopen failed: .+", failure.what());
704#endif
705#else
706 TESTUTILS_ASSERT_LIKE("expected error with some message", "open failed: .+", failure.what());
707#endif
708 }
709 fileStream.clear();
710
711 // open file from file descriptor
712#ifndef PLATFORM_WINDOWS
713 auto readWriteFileDescriptor = open(txtFilePath.data(), O_RDWR);
714 CPPUNIT_ASSERT(readWriteFileDescriptor);
715 fileStream.open(readWriteFileDescriptor, ios_base::in | ios_base::out | ios_base::binary);
716 CPPUNIT_ASSERT(fileStream.is_open());
717 CPPUNIT_ASSERT_EQUAL(static_cast<char>(fileStream.get()), 'f');
718 fileStream.seekg(0, ios_base::end);
719 CPPUNIT_ASSERT_EQUAL(fileStream.tellg(), static_cast<NativeFileStream::pos_type>(47));
720 fileStream.flush();
721 fileStream.close();
722 CPPUNIT_ASSERT(!fileStream.is_open());
723#endif
724 try {
725 fileStream.open(-1, ios_base::in | ios_base::out | ios_base::binary);
726 fileStream.get();
727 CPPUNIT_FAIL("expected exception");
728 } catch (const std::ios_base::failure &failure) {
729#ifndef PLATFORM_WINDOWS
731 "expected error message", "(basic_ios::clear|failed reading: Bad file descriptor): iostream error"s, string(failure.what()));
732#else
733 CPP_UTILITIES_UNUSED(failure)
734#endif
735 }
736 fileStream.clear();
737
738 // append + write file via path
739 NativeFileStream fileStream2;
740 fileStream2.exceptions(ios_base::failbit | ios_base::badbit);
741 fileStream2.open(txtFilePath, ios_base::in | ios_base::out | ios_base::app);
742 CPPUNIT_ASSERT(fileStream2.is_open());
743 fileStream2 << "foo";
744 fileStream2.flush();
745 fileStream2.close();
746 CPPUNIT_ASSERT(!fileStream2.is_open());
747 CPPUNIT_ASSERT_EQUAL("file with non-ASCII character 'ä' in its name\nfoo"s, readFile(txtFilePath, 50));
748
749 // truncate + write file via path
750 fileStream2.open(txtFilePath, ios_base::out | ios_base::trunc);
751 CPPUNIT_ASSERT(fileStream2.is_open());
752 fileStream2 << "bar";
753 fileStream2.close();
754 CPPUNIT_ASSERT(!fileStream2.is_open());
755 CPPUNIT_ASSERT_EQUAL("bar"s, readFile(txtFilePath, 4));
756
757 // append + write via file descriptor from file handle
758#ifdef PLATFORM_WINDOWS
759 const auto wideTxtFilePath = NativeFileStream::makeWidePath(txtFilePath);
760 const auto appendFileHandle = _wfopen(wideTxtFilePath.get(), L"a+");
761#else
762 const auto appendFileHandle = fopen(txtFilePath.data(), "a");
763#endif
764 CPPUNIT_ASSERT(appendFileHandle);
765 fileStream2.open(fileno(appendFileHandle), ios_base::out | ios_base::app);
766 CPPUNIT_ASSERT(fileStream2.is_open());
767 fileStream2 << "foo";
768 fileStream2.close();
769 CPPUNIT_ASSERT(!fileStream2.is_open());
770 CPPUNIT_ASSERT_EQUAL("barfoo"s, readFile(txtFilePath, 7));
771}
772#endif
#define CPP_UTILITIES_UNUSED(x)
Prevents warnings about unused variables.
Definition global.h:92
Reads primitive data types from a std::istream.
std::int64_t readInt64LE()
Reads a 64-bit little endian signed integer from the current stream and advances the current position...
float readFloat32BE()
Reads a 32-bit big endian floating point value from the current stream and advances the current posit...
std::string readTerminatedString(std::uint8_t termination=0)
Reads a terminated string from the current stream.
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...
std::uint64_t readUInt64LE()
Reads a 64-bit little endian unsigned integer from the current stream and advances the current positi...
std::string readLengthPrefixedString()
Reads a length prefixed string from the current stream.
std::int64_t readInt40LE()
Reads a 40-bit little endian signed integer from the current stream and advances the current position...
std::int32_t readInt32BE()
Reads a 32-bit big endian signed integer from the current stream and advances the current position of...
std::int32_t readInt24LE()
Reads a 24-bit little endian signed integer from the current stream and advances the current position...
std::uint32_t readUInt32LE()
Reads a 32-bit little endian unsigned integer from the current stream and advances the current positi...
std::uint16_t readUInt16LE()
Reads a 16-bit little endian unsigned integer from the current stream and advances the current positi...
void setStream(std::istream *stream, bool giveOwnership=false)
Assigns the stream the reader will read from when calling one of the read-methods.
std::uint64_t readUInt56BE()
Reads a 56-bit big endian unsigned integer from the current stream and advances the current position ...
bool readBool()
Reads a boolean value from the current stream and advances the current position of the stream by one ...
std::uint32_t readUInt24BE()
Reads a 24-bit big endian unsigned integer from the current stream and advances the current position ...
std::int32_t readInt32LE()
Reads a 32-bit little endian signed integer from the current stream and advances the current position...
const std::istream * stream() const
Returns a pointer to the stream the reader will read from when calling one of the read-methods.
std::int16_t readInt16BE()
Reads a 16-bit big endian signed integer from the current stream and advances the current position of...
std::uint32_t readUInt24LE()
Reads a 24-bit little endian unsigned integer from the current stream and advances the current positi...
std::int64_t readInt56LE()
Reads a 56-bit little endian signed integer from the current stream and advances the current position...
std::istream::pos_type readRemainingBytes()
Returns the number of remaining bytes in the stream from the current offset.
std::uint64_t readUInt64BE()
Reads a 64-bit big endian unsigned integer from the current stream and advances the current position ...
bool hasOwnership() const
Returns whether the reader takes ownership over the assigned stream.
std::uint64_t readUInt40LE()
Reads a 40-bit little endian unsigned integer from the current stream and advances the current positi...
std::uint64_t readUInt56LE()
Reads a 56-bit little endian unsigned integer from the current stream and advances the current positi...
std::uint16_t readUInt16BE()
Reads a 16-bit big endian unsigned integer from the current stream and advances the current position ...
std::int32_t readInt24BE()
Reads a 24-bit big endian signed integer from the current stream and advances the current position of...
std::int64_t readInt40BE()
Reads a 40-bit big endian signed integer from the current stream and advances the current position of...
double readFloat64BE()
Reads a 64-bit big endian floating point value from the current stream and advances the current posit...
std::uint32_t readUInt32BE()
Reads a 32-bit big endian unsigned integer from the current stream and advances the current position ...
std::int64_t readInt56BE()
Reads a 56-bit big endian signed integer from the current stream and advances the current position of...
std::int16_t readInt16LE()
Reads a 16-bit little endian signed integer from the current stream and advances the current position...
double readFloat64LE()
Reads a 64-bit little endian floating point value from the current stream and advances the current po...
std::int64_t readInt64BE()
Reads a 64-bit big endian signed integer from the current stream and advances the current position of...
std::istream::pos_type readStreamsize()
Returns the size of the assigned stream.
std::uint64_t readUInt40BE()
Reads a 40-bit big endian unsigned integer from the current stream and advances the current position ...
float readFloat32LE()
Reads a 32-bit little endian floating point value from the current stream and advances the current po...
std::uint8_t readByte()
Reads a single byte/unsigned character from the current stream and advances the current position of t...
Writes primitive data types to a std::ostream.
void writeFloat32LE(float value)
Writes a 32-bit little endian floating point value to the current stream and advances the current pos...
void writeUInt40BE(std::uint64_t value)
Writes a 40-bit big endian unsigned integer to the current stream and advances the current position o...
void writeUInt64LE(std::uint64_t value)
Writes a 64-bit little endian unsigned integer to the current stream and advances the current positio...
void writeInt40LE(std::int64_t value)
Writes a 40-bit big endian signed integer to the current stream and advances the current position of ...
void writeUInt40LE(std::uint64_t value)
Writes a 40-bit big endian unsigned integer to the current stream and advances the current position o...
void writeInt64BE(std::int64_t value)
Writes a 64-bit big endian signed integer to the current stream and advances the current position of ...
void writeInt32LE(std::int32_t value)
Writes a 32-bit little endian signed integer to the current stream and advances the current position ...
bool hasOwnership() const
Returns whether the writer takes ownership over the assigned stream.
void writeUInt24LE(std::uint32_t value)
Writes a 24-bit little endian unsigned integer to the current stream and advances the current positio...
void writeFloat32BE(float value)
Writes a 32-bit big endian floating point value to the current stream and advances the current positi...
void writeInt24BE(std::int32_t value)
Writes a 24-bit big endian signed integer to the current stream and advances the current position of ...
void setStream(std::ostream *stream, bool giveOwnership=false)
Assigns the stream the writer will write to when calling one of the write-methods.
void writeUInt56BE(std::uint64_t value)
Writes a 56-bit big endian unsigned integer to the current stream and advances the current position o...
void writeUInt32BE(std::uint32_t value)
Writes a 32-bit big endian unsigned integer to the current stream and advances the current position o...
void writeFloat64BE(double value)
Writes a 64-bit big endian floating point value to the current stream and advances the current positi...
void writeFloat64LE(double value)
Writes a 64-bit little endian floating point value to the current stream and advances the current pos...
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...
void writeUInt56LE(std::uint64_t value)
Writes a 56-bit big endian unsigned integer to the current stream and advances the current position o...
void writeInt56LE(std::int64_t value)
Writes a 56-bit big endian signed integer to the current stream and advances the current position of ...
const std::ostream * stream() const
Returns a pointer to the stream the writer will write to when calling one of the write-methods.
void writeUInt16BE(std::uint16_t value)
Writes a 16-bit big endian unsigned integer to the current stream and advances the current position o...
void writeUInt64BE(std::uint64_t value)
Writes a 64-bit big endian unsigned integer to the current stream and advances the current position o...
void writeInt56BE(std::int64_t value)
Writes a 56-bit big endian signed integer to the current stream and advances the current position of ...
void writeInt16LE(std::int16_t value)
Writes a 16-bit little endian signed integer to the current stream and advances the current position ...
void writeUInt32LE(std::uint32_t value)
Writes a 32-bit little endian unsigned integer to the current stream and advances the current positio...
void writeUInt16LE(std::uint16_t value)
Writes a 16-bit little endian unsigned integer to the current stream and advances the current positio...
void writeInt16BE(std::int16_t value)
Writes a 16-bit big endian signed integer to the current stream and advances the current position of ...
void writeLengthPrefixedString(const std::string &value)
Writes the length of a string and the string itself to the current stream.
void writeInt24LE(std::int32_t value)
Writes a 24-bit little endian signed integer to the current stream and advances the current position ...
void writeUInt24BE(std::uint32_t value)
Writes a 24-bit big endian unsigned integer to the current stream and advances the current position o...
void writeTerminatedString(const std::string &value)
Writes a terminated string to the current stream and advances the current position of the stream by t...
void writeInt64LE(std::int64_t value)
Writes a 64-bit little endian signed integer to the current stream and advances the current position ...
void writeInt32BE(std::int32_t value)
Writes a 32-bit big endian signed integer to the current stream and advances the current position of ...
void writeInt40BE(std::int64_t value)
Writes a 40-bit big endian signed integer to the current stream and advances the current position of ...
void writeBool(bool value)
Writes a boolean value to the current stream and advances the current position of the stream by one b...
The BitReader class provides bitwise reading of buffered data.
Definition bitreader.h:13
void reset(const char *buffer, std::size_t bufferSize)
Resets the reader.
Definition bitreader.h:148
std::uint8_t readBit()
Reads the one bit from the buffer advancing the current position by one bit.
Definition bitreader.h:89
void skipBits(std::size_t bitCount)
Skips the specified number of bits without reading it.
Definition bitreader.cpp:43
void align()
Re-establishes alignment.
Definition bitreader.h:171
std::size_t bitsAvailable()
Returns the number of bits which are still available to read.
Definition bitreader.h:137
intType readSignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (signed).
Definition bitreader.h:119
intType readUnsignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (unsigned).
Definition bitreader.h:102
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
intType showBits(std::uint8_t bitCount)
Reads the specified number of bits from the buffer without advancing the current position.
Definition bitreader.h:128
The BufferSearch struct invokes a callback if an initially given search term occurs in consecutively ...
The ConversionException class is thrown by the various conversion functions of this library when a co...
The CopyHelper class helps to copy bytes from one stream to another.
Definition copy.h:30
The IniFile class allows parsing and writing INI files.
Definition inifile.h:16
void parse(std::istream &inputStream)
Parses all data from the specified inputStream.
Definition inifile.cpp:40
ScopeList & data()
Returns the data of the file.
Definition inifile.h:46
void make(std::ostream &outputStream)
Write the current data to the specified outputStream.
Definition inifile.cpp:159
The IoTests class tests classes and functions provided by the files inside the io directory.
Definition iotests.cpp:61
void testBufferSearch()
Tests the BufferSearch class.
Definition iotests.cpp:311
void testBinaryReader()
Tests the most important methods of the BinaryReader.
Definition iotests.cpp:114
void testAnsiEscapeCodes()
Tests formatting functions of CppUtilities::EscapeCodes namespace.
Definition iotests.cpp:639
void testIniFile()
Tests IniFile.
Definition iotests.cpp:374
void testBitReader()
Tests the BitReader class.
Definition iotests.cpp:284
void testBinaryWriter()
Tests the most important methods of the BinaryWriter.
Definition iotests.cpp:180
void testCopyWithNativeFileStream()
Tests CopyHelper in combination with NativeFileStream.
Definition iotests.cpp:529
void setUp() override
Definition iotests.cpp:103
void testCopy()
Tests CopyHelper.
Definition iotests.cpp:502
void testPathUtilities()
Tests fileName() and removeInvalidChars().
Definition iotests.cpp:345
void tearDown() override
Definition iotests.cpp:107
void testWriteFile()
Tests writeFile().
Definition iotests.cpp:629
void testReadFile()
Tests readFile().
Definition iotests.cpp:599
void testAdvancedIniFile()
Tests AdvancedIniFile.
Definition iotests.cpp:415
CPPUNIT_TEST_SUITE_REGISTRATION(IoTests)
constexpr auto color(Color foreground, Color background, TextAttribute displayAttribute=TextAttribute::Reset)
CPP_UTILITIES_EXPORT bool enabled
Controls whether the functions inside the EscapeCodes namespace actually make use of escape codes.
Contains literals to ease asserting with CPPUNIT_ASSERT_EQUAL.
Definition testutils.h:368
Contains all utilities provides by the c++utilities library.
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:17
CPP_UTILITIES_EXPORT std::string testFilePath(const std::string &relativeTestFilePath)
Convenience function to invoke TestApplication::testFilePath().
Definition testutils.h:161
CPP_UTILITIES_EXPORT std::string workingCopyPath(const std::string &relativeTestFilePath, WorkingCopyMode mode=WorkingCopyMode::CreateCopy)
Convenience function to invoke TestApplication::workingCopyPath().
Definition testutils.h:179
CPP_UTILITIES_EXPORT StringData convertUtf16LEToUtf8(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-16 (little-endian) string to UTF-8.
CPP_UTILITIES_EXPORT std::ostream & operator<<(std::ostream &out, Indentation indentation)
CPP_UTILITIES_EXPORT StringData convertUtf16BEToUtf8(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-16 (big-endian) string to UTF-8.
std::fstream NativeFileStream
StringType argsToString(Args &&...args)
AsHexNumber< T > asHexNumber(const T &value)
Wraps a value to be printed using the hex system in the error case when asserted with cppunit (or sim...
Definition testutils.h:258
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:58
CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName)
Removes invalid characters from the specified fileName.
Definition path.cpp:75
CPP_UTILITIES_EXPORT std::string fileName(const std::string &path)
Returns the file name and extension of the specified path string.
Definition path.cpp:17
NativePathStringView makeNativePath(PathStringView path)
Returns path in the platform's native encoding.
Definition path.h:82
CPP_UTILITIES_EXPORT std::string directory(const std::string &path)
Returns the directory of the specified path string (including trailing slash).
Definition path.cpp:25
STL namespace.
The AdvancedIniFile class allows parsing and writing INI files.
Definition inifile.h:79
void make(std::ostream &outputStream, IniFileMakeOptions options=IniFileMakeOptions::None)
Write the current data to the specified outputStream.
Definition inifile.cpp:412
std::optional< FieldList::iterator > findField(std::string_view sectionName, std::string_view key)
Returns an iterator to the first field within the first section with matching sectionName and key.
Definition inifile.h:170
void parse(std::istream &inputStream, IniFileParseOptions options=IniFileParseOptions::None)
Parses all data from the specified inputStream.
Definition inifile.cpp:217
SectionList::iterator sectionEnd()
Returns an iterator that points one past the last section.
Definition inifile.h:154
SectionList::iterator findSection(std::string_view sectionName)
Returns an iterator to the first section with the name sectionName.
Definition inifile.h:122
#define TESTUTILS_ASSERT_LIKE_FLAGS(message, expectedRegex, regexFlags, actualString)
Asserts whether the specified string matches the specified regex.
Definition testutils.h:328
#define TESTUTILS_ASSERT_LIKE(message, expectedRegex, actualString)
Asserts whether the specified string matches the specified regex.
Definition testutils.h:338
constexpr int i