3 #include "../io/binaryreader.h" 4 #include "../io/binarywriter.h" 5 #include "../io/bitreader.h" 6 #include "../io/catchiofailure.h" 7 #include "../io/copy.h" 8 #include "../io/inifile.h" 9 #include "../io/path.h" 11 #include <cppunit/TestFixture.h> 12 #include <cppunit/extensions/HelperMacros.h> 28 CPPUNIT_TEST(testFailure);
29 CPPUNIT_TEST(testBinaryReader);
30 CPPUNIT_TEST(testBinaryWriter);
31 CPPUNIT_TEST(testBitReader);
32 CPPUNIT_TEST(testPathUtilities);
33 CPPUNIT_TEST(testIniFile);
34 CPPUNIT_TEST(testCopy);
35 CPPUNIT_TEST_SUITE_END();
42 void testBinaryReader();
43 void testBinaryWriter();
45 void testPathUtilities();
72 map<string, string> testMap;
73 CPPUNIT_ASSERT_THROW(testVec.at(1), out_of_range);
74 CPPUNIT_ASSERT_THROW(testMap.at(
"test"), out_of_range);
79 stream.exceptions(ios_base::failbit | ios_base::badbit);
80 stream.open(
"path/to/file/which/does/not/exist", ios_base::in);
93 testFile.exceptions(ios_base::failbit | ios_base::badbit);
104 CPPUNIT_ASSERT(reader.
readUInt56LE() == 0x01020304050607u);
105 CPPUNIT_ASSERT(reader.
readUInt56BE() == 0x01020304050607u);
106 CPPUNIT_ASSERT(reader.
readUInt64LE() == 0x0102030405060708u);
107 CPPUNIT_ASSERT(reader.
readUInt64BE() == 0x0102030405060708u);
113 CPPUNIT_ASSERT(reader.
readInt32LE() == 0x01020304);
114 CPPUNIT_ASSERT(reader.
readInt32BE() == 0x01020304);
115 CPPUNIT_ASSERT(reader.
readInt40LE() == 0x0102030405);
116 CPPUNIT_ASSERT(reader.
readInt40BE() == 0x0102030405);
117 CPPUNIT_ASSERT(reader.
readInt56LE() == 0x01020304050607);
118 CPPUNIT_ASSERT(reader.
readInt56BE() == 0x01020304050607);
119 CPPUNIT_ASSERT(reader.
readInt64LE() == 0x0102030405060708);
120 CPPUNIT_ASSERT(reader.
readInt64BE() == 0x0102030405060708);
125 CPPUNIT_ASSERT(reader.
readBool() ==
false);
126 CPPUNIT_ASSERT(reader.
readBool() ==
true);
127 CPPUNIT_ASSERT(reader.
readString(3) ==
"abc");
139 testFile.exceptions(ios_base::failbit | ios_base::badbit);
143 stringstream outputStream(ios_base::in | ios_base::out | ios_base::binary);
144 outputStream.exceptions(ios_base::failbit | ios_base::badbit);
146 outputStream.rdbuf()->pubsetbuf(testData,
sizeof(testData));
164 for (
char c : testData) {
165 CPPUNIT_ASSERT(c == static_cast<char>(testFile.get()));
166 if (testFile.tellg() >= 58) {
171 outputStream.seekp(0);
197 for (
char c : testData) {
198 CPPUNIT_ASSERT(c == static_cast<char>(testFile.get()));
207 const byte testData[] = { 0x81, 0x90, 0x3C, 0x44, 0x28, 0x00, 0x44, 0x10, 0x20 };
208 BitReader reader(reinterpret_cast<const char *>(testData),
sizeof(testData));
209 CPPUNIT_ASSERT(reader.
readBit() == 1);
218 CPPUNIT_ASSERT(reader.
readBit() == 0);
219 CPPUNIT_ASSERT(reader.
readBit() == 0);
232 CPPUNIT_ASSERT_EQUAL(
"libc++utilities.so"s,
fileName(
"/usr/lib/libc++utilities.so"));
233 CPPUNIT_ASSERT_EQUAL(
"/usr/lib/"s,
directory(
"/usr/lib/libc++utilities.so"));
234 CPPUNIT_ASSERT(
directory(
"libc++utilities.so").empty());
235 string invalidPath(
"lib/c++uti*lities.so?");
237 CPPUNIT_ASSERT(invalidPath ==
"libc++utilities.so");
240 const string testFilesDir = iniFilePath.substr(0, iniFilePath.size() - 9);
241 auto testFilesDirEntries =
directoryEntries(testFilesDir.c_str(), DirectoryEntryType::All);
242 CPPUNIT_ASSERT(find(testFilesDirEntries.cbegin(), testFilesDirEntries.cend(),
"test.ini") != testFilesDirEntries.cend());
243 CPPUNIT_ASSERT(find(testFilesDirEntries.cbegin(), testFilesDirEntries.cend(),
".") != testFilesDirEntries.cend());
244 CPPUNIT_ASSERT(find(testFilesDirEntries.cbegin(), testFilesDirEntries.cend(),
"..") != testFilesDirEntries.cend());
245 testFilesDirEntries =
directoryEntries(testFilesDir.c_str(), DirectoryEntryType::Directory);
246 CPPUNIT_ASSERT(find(testFilesDirEntries.cbegin(), testFilesDirEntries.cend(),
"test.ini") == testFilesDirEntries.cend());
247 CPPUNIT_ASSERT(find(testFilesDirEntries.cbegin(), testFilesDirEntries.cend(),
".") != testFilesDirEntries.cend());
248 CPPUNIT_ASSERT(find(testFilesDirEntries.cbegin(), testFilesDirEntries.cend(),
"..") != testFilesDirEntries.cend());
249 testFilesDirEntries =
directoryEntries(testFilesDir.c_str(), DirectoryEntryType::File);
250 CPPUNIT_ASSERT(find(testFilesDirEntries.cbegin(), testFilesDirEntries.cend(),
"test.ini") != testFilesDirEntries.cend());
251 CPPUNIT_ASSERT(find(testFilesDirEntries.cbegin(), testFilesDirEntries.cend(),
".") == testFilesDirEntries.cend());
252 CPPUNIT_ASSERT(find(testFilesDirEntries.cbegin(), testFilesDirEntries.cend(),
"..") == testFilesDirEntries.cend());
263 inputFile.exceptions(ios_base::failbit | ios_base::badbit);
267 ini.
parse(inputFile);
268 const auto globalScope = ini.
data().at(0);
269 const auto scope1 = ini.
data().at(1);
270 const auto scope2 = ini.
data().at(2);
271 CPPUNIT_ASSERT(globalScope.first.empty());
272 CPPUNIT_ASSERT(globalScope.second.find(
"key0") != globalScope.second.cend());
273 CPPUNIT_ASSERT(globalScope.second.find(
"key0")->second ==
"value 0");
274 CPPUNIT_ASSERT(globalScope.second.find(
"key1") == globalScope.second.cend());
275 CPPUNIT_ASSERT(scope1.first ==
"scope 1");
276 CPPUNIT_ASSERT(scope1.second.find(
"key1") != scope1.second.cend());
277 CPPUNIT_ASSERT(scope1.second.find(
"key1")->second ==
"value 1");
278 CPPUNIT_ASSERT(scope1.second.find(
"key2") != scope1.second.cend());
279 CPPUNIT_ASSERT(scope1.second.find(
"key2")->second ==
"value=2");
280 CPPUNIT_ASSERT(scope2.first ==
"scope 2");
281 CPPUNIT_ASSERT(scope2.second.find(
"key5") == scope2.second.cend());
285 outputFile.exceptions(ios_base::failbit | ios_base::badbit);
287 ini.
make(outputFile);
293 ini2.
parse(outputFile);
294 CPPUNIT_ASSERT(ini.
data() == ini2.
data());
304 testFile.exceptions(ios_base::failbit | ios_base::badbit);
306 stringstream outputStream(ios_base::in | ios_base::out | ios_base::binary);
307 outputStream.exceptions(ios_base::failbit | ios_base::badbit);
311 copyHelper.
copy(testFile, outputStream, 50);
315 for (
byte i = 0; i < 50; ++i) {
316 CPPUNIT_ASSERT(testFile.get() == outputStream.get());
int64 readInt64LE()
Reads a 64-bit little endian signed 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 ...
void writeInt24LE(int32 value)
Writes a 24-bit little endian signed integer to the current stream and advances the current position ...
float32 readFloat32BE()
Reads a 32-bit big endian floating point value from the current stream and advances the current posit...
int64 readInt40BE()
Reads a 40-bit big endian signed integer from the current stream and advances the current position of...
int16 readInt16LE()
Reads a 16-bit little endian signed integer from the current stream and advances the current position...
void writeFloat64LE(float64 value)
Writes a 64-bit little endian floating point value to the current stream and advances the current pos...
void writeInt56LE(int64 value)
Writes a 56-bit big endian signed integer to the current stream and advances the current position of ...
Reads primitive data types from a std::istream.
float64 readFloat64LE()
Reads a 64-bit little endian floating point value from the current stream and advances the current po...
int32 readInt32LE()
Reads a 32-bit little endian signed integer from the current stream and advances the current position...
Writes primitive data types to a std::ostream.
float32 readFloat32LE()
Reads a 32-bit little endian floating point value from the current stream and advances the current po...
intType readSignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (signed).
uint32 readUInt24BE()
Reads a 24-bit big endian unsigned integer from the current stream and advances the current position ...
int16 readInt16BE()
Reads a 16-bit big endian signed integer from the current stream and advances the current position of...
intType readUnsignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (unsigned).
CPP_UTILITIES_EXPORT std::list< std::string > directoryEntries(const char *path, DirectoryEntryType types=DirectoryEntryType::All)
Returns the names of the directory entries in the specified path with the specified types...
void testBinaryReader()
Tests the most important methods of the BinaryReader.
void writeInt40LE(int64 value)
Writes a 40-bit big endian signed integer to the current stream and advances the current position of ...
The IoTests class tests classes and methods of the IoUtilities namespace.
void writeUInt24BE(uint32 value)
Writes a 24-bit big endian unsigned integer to the current stream and advances the current position o...
void make(std::ostream &outputStream)
Write the current data to the specified outputStream.
void testCopy()
Tests CopyHelper.
void writeUInt16BE(uint16 value)
Writes a 16-bit big endian unsigned integer to the current stream and advances the current position o...
The CopyHelper class helps to copy bytes from one stream to another.
void writeUInt56BE(uint64 value)
Writes a 56-bit big endian unsigned integer to the current stream and advances the current position o...
int64 readInt64BE()
Reads a 64-bit big endian signed integer from the current stream and advances the current position of...
void writeUInt40BE(uint64 value)
Writes a 40-bit big endian unsigned integer to the current stream and advances the current position o...
void writeUInt16LE(uint16 value)
Writes a 16-bit little endian unsigned integer to the current stream and advances the current positio...
void writeBool(bool value)
Writes a boolean value to the current stream and advances the current position of the stream by one b...
void writeUInt24LE(uint32 value)
Writes a 24-bit little endian unsigned integer to the current stream and advances the current positio...
The BitReader class provides bitwise reading of buffered data.
float64 readFloat64BE()
Reads a 64-bit big endian floating point value from the current stream and advances the current posit...
std::vector< std::pair< std::string, std::multimap< std::string, std::string > > > & data()
Returns the data of the file.
int64 readInt56BE()
Reads a 56-bit big endian signed integer from the current stream and advances the current position of...
uint64 readUInt56LE()
Reads a 56-bit little endian unsigned integer from the current stream and advances the current positi...
void writeInt16LE(int16 value)
Writes a 16-bit little endian signed integer to the current stream and advances the current position ...
void writeLengthPrefixedString(const std::string &value)
Writes the length of a string and the string itself to the current stream.
uint32 readUInt32LE()
Reads a 32-bit little endian unsigned integer from the current stream and advances the current positi...
CPPUNIT_TEST_SUITE_REGISTRATION(IoTests)
void writeFloat64BE(float64 value)
Writes a 64-bit big endian floating point value to the current stream and advances the current positi...
void writeFloat32BE(float32 value)
Writes a 32-bit big endian floating point value to the current stream and advances the current positi...
uint32 readUInt32BE()
Reads a 32-bit big endian unsigned integer from the current stream and advances the current position ...
Contains utility classes helping to read and write streams.
void writeUInt40LE(uint64 value)
Writes a 40-bit big endian unsigned integer to the current stream and advances the current position o...
std::int8_t sbyte
signed byte
void testBinaryWriter()
Tests the most important methods of the BinaryWriter.
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...
uint64 readUInt40BE()
Reads a 40-bit big endian unsigned integer from the current stream and advances the current position ...
void writeInt56BE(int64 value)
Writes a 56-bit big endian signed integer to the current stream and advances the current position of ...
void copy(std::istream &input, std::ostream &output, std::size_t count)
Copies count bytes from input to output.
uint64 readUInt64BE()
Reads a 64-bit big endian unsigned integer from the current stream and advances the current position ...
std::uint32_t uint32
unsigned 32-bit integer
void writeUInt56LE(uint64 value)
Writes a 56-bit big endian unsigned integer to the current stream and advances the current position o...
uint16 readUInt16LE()
Reads a 16-bit little endian unsigned integer from the current stream and advances the current positi...
uint16 readUInt16BE()
Reads a 16-bit big endian unsigned integer from the current stream and advances the current position ...
void writeUInt32LE(uint32 value)
Writes a 32-bit little endian unsigned integer to the current stream and advances the current positio...
int32 readInt24LE()
Reads a 24-bit little endian signed integer from the current stream and advances the current position...
void writeInt32LE(int32 value)
Writes a 32-bit little endian signed integer to the current stream and advances the current position ...
void writeInt16BE(int16 value)
Writes a 16-bit big endian signed integer to the current stream and advances the current position of ...
void testPathUtilities()
Tests fileName() and removeInvalidChars().
void testIniFile()
Tests IniFile.
void writeInt64LE(int64 value)
Writes a 64-bit little endian signed integer to the current stream and advances the current position ...
CPP_UTILITIES_EXPORT std::string directory(const std::string &path)
Returns the directory of the specified path string (including trailing slash).
std::string readTerminatedString(byte termination=0)
Reads a terminated string from the current stream.
void writeUInt32BE(uint32 value)
Writes a 32-bit big endian unsigned integer to the current stream and advances the current position o...
void writeInt24BE(int32 value)
Writes a 24-bit big endian signed integer to the current stream and advances the current position of ...
void skipBits(std::size_t bitCount)
Skips the specified number of bits without reading it.
intType readBits(byte bitCount)
Reads the specified number of bits from the buffer advancing the current position by bitCount bits...
void writeFloat32LE(float32 value)
Writes a 32-bit little endian floating point value to the current stream and advances the current pos...
void parse(std::istream &inputStream)
Parses all data from the specified inputStream.
int32 readInt32BE()
Reads a 32-bit big endian signed integer from the current stream and advances the current position of...
int32 readInt24BE()
Reads a 24-bit big endian signed integer from the current stream and advances the current position of...
std::uint8_t byte
unsigned byte
void writeInt64BE(int64 value)
Writes a 64-bit big endian signed integer to the current stream and advances the current position of ...
The IniFile class parses and makes INI files.
void testFailure()
Tests for GCC Bug 66145.
uint32 readUInt24LE()
Reads a 24-bit little endian unsigned integer from the current stream and advances the current positi...
int64 readInt56LE()
Reads a 56-bit little endian signed integer from the current stream and advances the current position...
void writeUInt64LE(uint64 value)
Writes a 64-bit little endian unsigned integer to the current stream and advances the current positio...
void writeInt32BE(int32 value)
Writes a 32-bit big endian signed integer to the current stream and advances the current position of ...
CPP_UTILITIES_EXPORT const char * catchIoFailure()
Provides a workaround for GCC Bug 66145.
CPP_UTILITIES_EXPORT std::string fileName(const std::string &path)
Returns the file name and extension of the specified path string.
uint64 readUInt64LE()
Reads a 64-bit little endian unsigned integer from the current stream and advances the current positi...
void testBitReader()
Tests the BitReader.
CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName)
Removes invalid characters from the specified fileName.
void align()
Re-establishes alignment.
std::string readLengthPrefixedString()
Reads a length prefixed string from the current stream.
byte readBit()
Reads the one bit from the buffer advancing the current position by one bit.
void writeUInt64BE(uint64 value)
Writes a 64-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...
uint64 readUInt56BE()
Reads a 56-bit big endian unsigned integer from the current stream and advances the current position ...
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...
int64 readInt40LE()
Reads a 40-bit little endian signed integer from the current stream and advances the current position...
CPP_UTILITIES_EXPORT std::string testFilePath(const std::string &name)
Convenience function which returns the full path of the test file with the specified name...
intType showBits(byte bitCount)
Reads the specified number of bits from the buffer without advancing the current position.
uint64 readUInt40LE()
Reads a 40-bit little endian unsigned integer from the current stream and advances the current positi...
void writeInt40BE(int64 value)
Writes a 40-bit big endian signed integer to the current stream and advances the current position of ...