11#ifdef CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN
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));
35#include <cppunit/TestFixture.h>
36#include <cppunit/extensions/HelperMacros.h>
43#ifdef PLATFORM_WINDOWS
55using namespace CPPUNIT_NS;
74#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
77 CPPUNIT_TEST_SUITE_END();
80 void setUp()
override;
95#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
117 testFile.exceptions(ios_base::failbit | ios_base::badbit);
155 CPPUNIT_ASSERT_EQUAL(
"01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901"
156 "23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123"
157 "45678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"
158 "678901234567890123456789"s,
159 reader.readLengthPrefixedString());
167 reader.setStream(
nullptr,
true);
168 reader.setStream(
new fstream(),
true);
173 reader.setStream(
new fstream(),
true);
183 testFile.exceptions(ios_base::failbit | ios_base::badbit);
187 stringstream
outputStream(ios_base::in | ios_base::out | ios_base::binary);
188 outputStream.exceptions(ios_base::failbit | ios_base::badbit);
190#if defined(__GLIBCXX__) && !defined(_LIBCPP_VERSION)
191#define USE_RDBUF_DIRECTLY
197 writer.writeUInt16LE(0x0102u);
198 writer.writeUInt16BE(0x0102u);
199 writer.writeUInt24LE(0x010203u);
200 writer.writeUInt24BE(0x010203u);
201 writer.writeUInt32LE(0x01020304u);
202 writer.writeUInt32BE(0x01020304u);
203 writer.writeUInt40LE(0x0102030405u);
204 writer.writeUInt40BE(0x0102030405u);
205 writer.writeUInt56LE(0x01020304050607u);
206 writer.writeUInt56BE(0x01020304050607u);
207 writer.writeUInt64LE(0x0102030405060708u);
208 writer.writeUInt64BE(0x0102030405060708u);
210#ifndef USE_RDBUF_DIRECTLY
217 const auto pos =
static_cast<std::size_t
>(
testFile.tellg());
229 writer.writeInt16LE(0x0102);
230 writer.writeInt16BE(0x0102);
231 writer.writeInt24LE(0x010203);
232 writer.writeInt24BE(0x010203);
233 writer.writeInt32LE(0x01020304);
234 writer.writeInt32BE(0x01020304);
235 writer.writeInt40LE(0x0102030405);
236 writer.writeInt40BE(0x0102030405);
237 writer.writeInt56LE(0x01020304050607);
238 writer.writeInt56BE(0x01020304050607);
239 writer.writeInt64LE(0x0102030405060708);
240 writer.writeInt64BE(0x0102030405060708);
241 writer.writeFloat32LE(1.125);
242 writer.writeFloat64LE(1.625);
243 writer.writeFloat32BE(1.125);
244 writer.writeFloat64BE(1.625);
247 writer.writeString(
"abc");
248 writer.writeLengthPrefixedString(
"ABC");
249 writer.writeLengthPrefixedString(
"012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
250 "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901"
251 "234567890123456789012345678901234567890123456789012345678901234567890123456789");
252 writer.writeTerminatedString(
"def");
254#ifndef USE_RDBUF_DIRECTLY
261 const auto pos =
static_cast<std::size_t
>(
testFile.tellg());
271 writer.setStream(
nullptr,
true);
272 writer.setStream(
new fstream(),
true);
277 writer.setStream(
new fstream(),
true);
285 const std::uint8_t
testData[] = { 0x81, 0x90, 0x3C, 0x44, 0x28, 0x00, 0x44, 0x10, 0x20, 0xFF, 0xFA };
322 char buffer[30] = { 0 };
325 std::strcpy(buffer,
"Starting Updated");
326 bs(std::string_view(buffer, 16));
328 std::strcpy(buffer,
" version: some ");
332 std::strcpy(buffer,
"version number\tmore chars");
336 std::strcpy(buffer,
"... Starting build ...");
358 const auto input = std::string_view(
"some/path/täst");
361#ifdef PLATFORM_WINDOWS
377 inputFile.exceptions(ios_base::failbit | ios_base::badbit);
399 outputFile.exceptions(ios_base::failbit | ios_base::badbit);
418 inputFile.exceptions(ios_base::failbit | ios_base::badbit);
429#if !defined(PLATFORM_WINDOWS) || defined(PLATFORM_MINGW) || defined(PLATFORM_CYGWIN)
430#define STD_REGEX_WORKS
432 "comment block before section",
"# Based on.*\n.*# GENERAL OPTIONS\n#\n"s, std::regex::extended,
options->precedingCommentBlock);
438#ifdef STD_REGEX_WORKS
440 "# The following paths are.*\n.*#HookDir = /etc/pacman\\.d/hooks/\n"s, std::regex::extended,
options->fields[0].precedingCommentBlock);
446#ifdef STD_REGEX_WORKS
448 options->fields[1].precedingCommentBlock);
454#ifdef STD_REGEX_WORKS
455 TESTUTILS_ASSERT_LIKE_FLAGS(
"empty lines in comments preserved",
"\n# Pacman.*\n.*\n\n#NoUpgrade =\n.*#TotalDownload\n"s, std::regex::extended,
456 options->fields[3].precedingCommentBlock);
465#ifdef STD_REGEX_WORKS
467 std::regex::extended,
ini.sections.back().precedingCommentBlock);
488#pragma GCC diagnostic push
489#pragma GCC diagnostic ignored "-Wnull-dereference"
493#pragma GCC diagnostic pop
505 testFile.exceptions(ios_base::failbit | ios_base::badbit);
507 auto outputStream = std::stringstream(ios_base::in | ios_base::out | ios_base::binary);
508 outputStream.exceptions(ios_base::failbit | ios_base::badbit);
520 for (
auto i = 0;
i < 50; ++
i) {
532 testFile.exceptions(ios_base::failbit | ios_base::badbit);
536 outputStream.exceptions(ios_base::failbit | ios_base::badbit);
559 for (
auto i = 0;
i < 50; ++
i) {
568 const auto isAborted = [] {
return false; };
587 for (
auto i = 0;
i < 50; ++
i) {
606 "key1=value 1 # comment\n"
620#if !defined(PLATFORM_WINDOWS) || defined(CPP_UTILITIES_USE_NATIVE_FILE_BUFFER)
630 const string path(
workingCopyPath(
"test.ini", WorkingCopyMode::NoCopy));
642 ss1 << EscapeCodes::Phrases::Error <<
"some error" << EscapeCodes::Phrases::End;
643 ss1 << EscapeCodes::Phrases::Warning <<
"some warning" << EscapeCodes::Phrases::End;
644 ss1 << EscapeCodes::Phrases::Info <<
"some info" << EscapeCodes::Phrases::End;
645 ss1 << EscapeCodes::Phrases::ErrorMessage <<
"Arch-style error" << EscapeCodes::Phrases::End;
646 ss1 << EscapeCodes::Phrases::WarningMessage <<
"Arch-style warning" << EscapeCodes::Phrases::End;
647 ss1 << EscapeCodes::Phrases::PlainMessage <<
"Arch-style message" << EscapeCodes::Phrases::End;
648 ss1 << EscapeCodes::Phrases::SuccessMessage <<
"Arch-style success" << EscapeCodes::Phrases::End;
649 ss1 << EscapeCodes::Phrases::SubMessage <<
"Arch-style sub-message" << EscapeCodes::Phrases::End;
650 ss1 <<
EscapeCodes::color(EscapeCodes::Color::Blue, EscapeCodes::Color::Red, EscapeCodes::TextAttribute::Blink)
651 <<
"blue, blinking text on red background" << EscapeCodes::TextAttribute::Reset <<
'\n';
652 cout <<
"\noutput for formatting with ANSI escape codes:\n" <<
ss1.str() <<
"---------------------------------------------\n";
654 "\033[1;33mWarning: \033[0m\033[1msome warning\033[0m\n"
655 "\033[1;34mInfo: \033[0m\033[1msome info\033[0m\n"
656 "\033[1;31m==> ERROR: \033[0m\033[1mArch-style error\033[0m\n"
657 "\033[1;33m==> WARNING: \033[0m\033[1mArch-style warning\033[0m\n"
658 " \033[0m\033[1mArch-style message\033[0m\n"
659 "\033[1;32m==> \033[0m\033[1mArch-style success\033[0m\n"
660 "\033[1;32m -> \033[0m\033[1mArch-style sub-message\033[0m\n"
661 "\033[5;34;41mblue, blinking text on red background\033[0m\n"s,
666 ss2 << EscapeCodes::Phrases::Info <<
"some info" << EscapeCodes::Phrases::End;
670#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
674void IoTests::testNativeFileStream()
680 fileStream.exceptions(ios_base::badbit | ios_base::failbit);
684#if defined(PLATFORM_WINDOWS) && defined(CPP_UTILITIES_USE_BOOST_IOSTREAMS)
695 fileStream.open(
"non existing file", ios_base::in | ios_base::out | ios_base::binary);
697 }
catch (
const std::ios_base::failure &failure) {
698#ifdef PLATFORM_WINDOWS
699#ifdef CPP_UTILITIES_USE_BOOST_IOSTREAMS
711#ifndef PLATFORM_WINDOWS
724 fileStream.open(-1, ios_base::in | ios_base::out | ios_base::binary);
727 }
catch (
const std::ios_base::failure &failure) {
728#ifndef PLATFORM_WINDOWS
730 "expected error message",
"(basic_ios::clear|failed reading: Bad file descriptor): iostream error"s,
string(failure.what()));
739 fileStream2.exceptions(ios_base::failbit | ios_base::badbit);
757#ifdef PLATFORM_WINDOWS
#define CPP_UTILITIES_UNUSED(x)
Prevents warnings about unused variables.
Reads primitive data types from a std::istream.
Writes primitive data types to a std::ostream.
The BitReader class provides bitwise reading of buffered data.
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 IniFile class allows parsing and writing INI files.
void parse(std::istream &inputStream)
Parses all data from the specified inputStream.
The IoTests class tests classes and functions provided by the files inside the io directory.
void testBufferSearch()
Tests the BufferSearch class.
void testBinaryReader()
Tests the most important methods of the BinaryReader.
void testAnsiEscapeCodes()
Tests formatting functions of CppUtilities::EscapeCodes namespace.
void testIniFile()
Tests IniFile.
void testBitReader()
Tests the BitReader class.
void testBinaryWriter()
Tests the most important methods of the BinaryWriter.
void testCopyWithNativeFileStream()
Tests CopyHelper in combination with NativeFileStream.
void testCopy()
Tests CopyHelper.
void testPathUtilities()
Tests fileName() and removeInvalidChars().
void testWriteFile()
Tests writeFile().
void testReadFile()
Tests readFile().
void testAdvancedIniFile()
Tests AdvancedIniFile.
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.
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.
CPP_UTILITIES_EXPORT std::string testFilePath(const std::string &relativeTestFilePath)
Convenience function to invoke TestApplication::testFilePath().
CPP_UTILITIES_EXPORT std::string workingCopyPath(const std::string &relativeTestFilePath, WorkingCopyMode mode=WorkingCopyMode::CreateCopy)
Convenience function to invoke TestApplication::workingCopyPath().
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)
IntegralType stringToNumber(const StringType &string, BaseType base=10)
Converts the given string to an unsigned/signed number assuming string uses the specified base.
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...
CPP_UTILITIES_EXPORT void writeFile(std::string_view path, std::string_view contents)
Writes all contents to the specified file in a single call.
CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName)
Removes invalid characters from the specified fileName.
CPP_UTILITIES_EXPORT std::string fileName(const std::string &path)
Returns the file name and extension of the specified path string.
NativePathStringView makeNativePath(PathStringView path)
Returns path in the platform's native encoding.
CPP_UTILITIES_EXPORT std::string directory(const std::string &path)
Returns the directory of the specified path string (including trailing slash).
The AdvancedIniFile class allows parsing and writing INI files.
void parse(std::istream &inputStream, IniFileParseOptions options=IniFileParseOptions::None)
Parses all data from the specified inputStream.
#define TESTUTILS_ASSERT_LIKE_FLAGS(message, expectedRegex, regexFlags, actualString)
Asserts whether the specified string matches the specified regex.
#define TESTUTILS_ASSERT_LIKE(message, expectedRegex, actualString)
Asserts whether the specified string matches the specified regex.