8#include <cppunit/TestFixture.h>
9#include <cppunit/extensions/HelperMacros.h>
12#include <initializer_list>
16#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
22using namespace CPPUNIT_NS;
26static_assert(
toNormalInt(383) == 255,
"toNormalInt()");
27static_assert(
swapOrder(
static_cast<std::uint16_t
>(0xABCD)) == 0xCDAB,
"swapOrder(std::uint16_t)");
28static_assert(
swapOrder(
static_cast<std::uint32_t
>(0xABCDEF12u)) == 0x12EFCDABu,
"swapOrder(std::uint32_t)");
29static_assert(
swapOrder(
static_cast<std::uint64_t
>(0xABCDEF1234567890ul)) == 0x9078563412EFCDABul,
"swapOrder(std::uint64_t)");
30static_assert(
swapOrder(
static_cast<std::int16_t
>(0xABCD)) ==
static_cast<std::int16_t
>(0xCDAB),
"swapOrder(std::int16_t)");
31static_assert(
swapOrder(
static_cast<std::int32_t
>(0xABCDEF12)) == 0x12EFCDAB,
"swapOrder(std::int32_t)");
32static_assert(
swapOrder(
static_cast<std::int64_t
>(0xABCDEF1234567890l)) ==
static_cast<std::int64_t
>(0x9078563412EFCDABl),
"swapOrder(std::int64_t)");
46 CPPUNIT_TEST_SUITE_END();
67 template <
typename intType>
79 , m_randomEngine(m_randomDevice())
96 }
test = { 0x01020304 };
97#if defined(CONVERSION_UTILITIES_BYTE_ORDER_BIG_ENDIAN)
103#elif defined(CONVERSION_UTILITIES_BYTE_ORDER_LITTLE_ENDIAN)
110 CPPUNIT_FAIL(
"There is not valid byte order assumption");
114template <
typename intType>
115void ConversionTests::testConversion(
125#define TEST_TYPE(endianness, function) decltype(endianness::function(m_buff))
127#define TEST_CONVERSION(function, endianness) \
128 testConversion<TEST_TYPE(endianness, function)>("testing " #endianness "::" #function, \
129 static_cast<void (*)(TEST_TYPE(endianness, function), char *)>(&endianness::getBytes), endianness::function, \
130 numeric_limits<TEST_TYPE(endianness, function)>::min(), numeric_limits<TEST_TYPE(endianness, function)>::max())
132#define TEST_BE_CONVERSION(function) TEST_CONVERSION(function, BE)
134#define TEST_LE_CONVERSION(function) TEST_CONVERSION(function, LE)
136#define TEST_CUSTOM_CONVERSION(vice, versa, endianness, min, max) \
137 testConversion<TEST_TYPE(endianness, versa)>("testing " #versa " (" #endianness ")", \
138 static_cast<void (*)(TEST_TYPE(endianness, versa), char *)>(&endianness::vice), endianness::versa, min, max)
149 for (
auto b = 1;
b < 100; ++
b) {
186 auto *
i =
reinterpret_cast<std::uint8_t *
>(
actualValues.first.get());
192#if CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN == true
193#define LE_STR_FOR_ENDIANNESS(name) name##LE##String
194#define BE_STR_FOR_ENDIANNESS(name) name##BE##String
195#elif CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN == true
196#define LE_STR_FOR_ENDIANNESS(name) name##BE##String
197#define BE_STR_FOR_ENDIANNESS(name) name##LE##String
216 const std::uint8_t
simpleString[] = {
'A',
'B',
'C',
'D' };
220 const std::uint8_t
latin1String[] = {
'A',
'B', 0xD6,
'C',
'D' };
221 const std::uint8_t
utf8String[] = {
'A',
'B', 0xC3, 0x96,
'C',
'D' };
222 const std::uint16_t
utf16LEString[] = { 0x0041, 0x0042, 0x00D6, 0x0043, 0x0044 };
223 const std::uint16_t
utf16BEString[] = { 0x4100, 0x4200, 0xD600, 0x4300, 0x4400 };
258 for (std::uint8_t
b = 1;
b < 100; ++
b) {
309 "empty string (simple)", vector<string_view>({ string_view() }),
splitStringSimple<vector<string_view>>(string_view(),
","));
321 splitTestActual =
splitString<vector<string>>(
"1,2,,3,4,,5"s,
","s, EmptyPartsTreat::Merge, 3);
323 string splitJoinTest =
joinStrings(
splitString<vector<string>>(
",a,,ab,ABC,s"s,
","s, EmptyPartsTreat::Keep),
" "s,
false,
"("s,
")"s);
325 splitJoinTest =
joinStrings(
splitString<vector<string>>(
",a,,ab,ABC,s"s,
","s, EmptyPartsTreat::Keep),
" "s,
true,
"("s,
")"s);
327 splitJoinTest =
joinStrings(
splitStringSimple<vector<string>>(
",a,,ab,ABC,s"s,
","s),
" "s,
true,
"("s,
")"s);
329 splitJoinTest =
joinStrings(
splitString<vector<string>>(
",a,,ab,ABC,s"s,
","s, EmptyPartsTreat::Omit),
" "s,
false,
"("s,
")"s);
331 splitJoinTest =
joinStrings(
splitString<vector<string>>(
",a,,ab,ABC,s"s,
","s, EmptyPartsTreat::Merge),
" "s,
false,
"("s,
")"s);
411 operator std::string()
const;
436 static_assert(Helper::IsStringType<std::string, std::string>::value);
437 static_assert(!Helper::IsStringType<std::string, std::wstring>::value);
438 static_assert(Helper::IsStringType<std::wstring, std::wstring>::value);
439 static_assert(Helper::IsStringViewType<std::string, std::string_view>::value);
440 static_assert(!Helper::IsStringViewType<std::wstring, std::string_view>::value);
441 static_assert(Helper::IsStringViewType<std::wstring, std::wstring_view>::value);
442 static_assert(Helper::IsConvertibleToConstStringRef<std::string, ConvertibleToString>::value);
443#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
444 static_assert(!Helper::IsConvertibleToConstStringRef<std::filesystem::path::string_type, std::filesystem::path>::value,
445 "conversion via native() preferred");
448 !Helper::IsConvertibleToConstStringRef<std::string, std::string>::value,
"yes, in this context this should not be considered convertible");
449 static_assert(!Helper::IsConvertibleToConstStringRef<std::wstring, ConvertibleToString>::value);
450#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
451 static_assert(Helper::IsConvertibleToConstStringRefViaNative<std::filesystem::path::string_type, std::filesystem::path>::value);
453 static_assert(!Helper::IsConvertibleToConstStringRefViaNative<std::string, std::string>::value);
461#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
462 if constexpr (std::is_same_v<std::filesystem::path::value_type, std::string::value_type>) {
473 "regular + operator still works (no problems with ambiguity)"s,
"regular + still works"s,
"regular"s +
" + still works");
The ConversionTests class tests classes and functions provided by the files inside the conversion dir...
void testSwapOrderFunctions()
Tests swap order functions.
void testBinaryConversions()
Tests most important binary conversions.
void testStringEncodingConversions()
Tests string encoding conversions.
void testConversionException()
void testEndianness()
Tests whether macros for endianness are correct.
void testStringConversions()
Tests miscellaneous string conversions.
The ConversionException class is thrown by the various conversion functions of this library when a co...
void assertEqual(const char *message, const std::uint8_t *expectedValues, size_t expectedSize, const StringData &actualValues)
Internally used for string encoding tests to check results.
#define TEST_BE_CONVERSION(function)
#define TEST_LE_CONVERSION(function)
#define TEST_CUSTOM_CONVERSION(vice, versa, endianness, min, max)
CPPUNIT_TEST_SUITE_REGISTRATION(ConversionTests)
Contains all utilities provides by the c++utilities library.
CPP_UTILITIES_EXPORT StringData convertUtf8ToUtf16BE(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-8 string to UTF-16 (big-endian).
CPP_UTILITIES_EXPORT StringData convertString(const char *fromCharset, const char *toCharset, const char *inputBuffer, std::size_t inputBufferSize, float outputBufferSizeFactor=1.0f)
Converts the specified string from one character set to another.
std::pair< std::unique_ptr< char[], StringDataDeleter >, std::size_t > StringData
Type used to return string encoding conversion result.
StringType numberToString(IntegralType number, BaseType base=10)
Converts the given number to its equivalent string representation using the specified base.
CPP_UTILITIES_EXPORT constexpr std::uint32_t toNormalInt(std::uint32_t synchsafeInt)
Returns a normal 32-bit integer converted from a 32-bit synchsafe integer.
CPP_UTILITIES_EXPORT StringData convertLatin1ToUtf8(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified Latin-1 string to UTF-8.
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::pair< std::unique_ptr< std::uint8_t[]>, std::uint32_t > decodeBase64(const char *encodedStr, const std::uint32_t strSize)
Decodes the specified Base64 encoded string.
ReturnType joinStrings(const Container &strings, Detail::StringParamForContainer< Container > delimiter=Detail::StringParamForContainer< Container >(), bool omitEmpty=false, Detail::StringParamForContainer< Container > leftClosure=Detail::StringParamForContainer< Container >(), Detail::StringParamForContainer< Container > rightClosure=Detail::StringParamForContainer< Container >())
Joins the given strings using the specified delimiter.
CPP_UTILITIES_EXPORT constexpr std::uint16_t swapOrder(std::uint16_t value)
Swaps the byte order of the specified 16-bit unsigned integer.
bool startsWith(const StringType &str, const StringType &phrase)
Returns whether str starts with phrase.
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 void truncateString(std::string &str, char terminationChar='\0')
Truncates all characters after the first occurrence of the specified terminationChar and the terminat...
CPP_UTILITIES_EXPORT StringData convertUtf16BEToUtf8(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-16 (big-endian) string to UTF-8.
Container splitStringSimple(Detail::StringParamForContainer< Container > string, Detail::StringParamForContainer< Container > delimiter, int maxParts=-1)
Splits the given string (which might also be a string view) at the specified delimiter.
StringType argsToString(Args &&...args)
constexpr T max(T first, T second)
Returns the greatest of the given items.
StringType tupleToString(const std::tuple< Args... > &tuple)
Concatenates all strings hold by the specified tuple.
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 StringData convertUtf8ToLatin1(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-8 string to Latin-1.
bool endsWith(const StringType &str, const StringType &phrase)
Returns whether str ends with phrase.
CPP_UTILITIES_EXPORT constexpr std::uint32_t toSynchsafeInt(std::uint32_t normalInt)
Returns a 32-bit synchsafe integer converted from a normal 32-bit integer.
CPP_UTILITIES_EXPORT StringData convertUtf8ToUtf16LE(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-8 string to UTF-16 (little-endian).
CPP_UTILITIES_EXPORT std::string bitrateToString(double speedInKbitsPerSecond, bool useByteInsteadOfBits=false)
Converts the specified bitrate in kbit/s to its equivalent std::string representation.
constexpr T min(T first, T second)
Returns the smallest of the given items.
CPP_UTILITIES_EXPORT std::string encodeBase64(const std::uint8_t *data, std::uint32_t dataSize)
Encodes the specified data to Base64.
CPP_UTILITIES_EXPORT std::string dataSizeToString(std::uint64_t sizeInByte, bool includeByte=false)
Converts the specified data size in byte to its equivalent std::string representation.
Container splitString(Detail::StringParamForContainer< Container > string, Detail::StringParamForContainer< Container > delimiter, EmptyPartsTreat emptyPartsRole=EmptyPartsTreat::Keep, int maxParts=-1)
Splits the given string at the specified delimiter.