C++ Utilities  4.6.1
Common C++ classes and routines used by my applications such as argument parser, IO and conversion utilities
conversiontests.cpp
Go to the documentation of this file.
1 #include "../conversion/binaryconversion.h"
2 #include "../conversion/stringconversion.h"
3 #include "../conversion/stringbuilder.h"
4 #include "../tests/testutils.h"
5 
6 #include <cppunit/extensions/HelperMacros.h>
7 #include <cppunit/TestFixture.h>
8 
9 #include <random>
10 #include <sstream>
11 #include <functional>
12 #include <initializer_list>
13 
14 using namespace std;
15 using namespace ConversionUtilities;
16 using namespace TestUtilities;
17 
18 using namespace CPPUNIT_NS;
19 
23 class ConversionTests : public TestFixture
24 {
25  CPPUNIT_TEST_SUITE(ConversionTests);
26  CPPUNIT_TEST(testEndianness);
27  CPPUNIT_TEST(testBinaryConversions);
28  CPPUNIT_TEST(testSwapOrderFunctions);
29  CPPUNIT_TEST(testStringEncodingConversions);
30  CPPUNIT_TEST(testStringConversions);
31  CPPUNIT_TEST(testStringBuilder);
32  CPPUNIT_TEST_SUITE_END();
33 
34 public:
36 
37  void setUp() {}
38  void tearDown() {}
39 
40  void testEndianness();
41  void testBinaryConversions();
42  void testSwapOrderFunctions();
43  void testStringEncodingConversions();
44  void testStringConversions();
45  void testStringBuilder();
46 
47 private:
48  template<typename intType>
49  void testConversion(const char *message, function<void (intType, char *)> vice, function<intType (const char *)> verca, intType min, intType max);
50 
51  char m_buff[8];
52  random_device m_randomDevice;
53  mt19937 m_randomEngine;
54 };
55 
57 
59  m_randomDevice(),
60  m_randomEngine(m_randomDevice())
61 {}
62 
67 {
68  union {
69  uint32_t integer;
70  char characters[4];
71  } test = {0x01020304};
72 #if defined(CONVERSION_UTILITIES_BYTE_ORDER_BIG_ENDIAN)
73  // test whether macro definitions are consistent
74  CPPUNIT_ASSERT(CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN == true);
75  CPPUNIT_ASSERT(CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN == false);
76  // test whether byte order assumption is correct
77  CPPUNIT_ASSERT_MESSAGE("Byte order assumption (big-endian) is wrong", test.characters[0] == 0x01);
78 #elif defined(CONVERSION_UTILITIES_BYTE_ORDER_LITTLE_ENDIAN)
79  // test whether macro definitions are consistent
80  CPPUNIT_ASSERT(CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN == false);
81  CPPUNIT_ASSERT(CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN == true);
82  // test whether byte order assumption is correct
83  CPPUNIT_ASSERT_MESSAGE("Byte order assumption (little-endian) is wrong", test.characters[0] == 0x04);
84 #else
85  CPPUNIT_FAIL("There is not valid byte order assumption");
86 #endif
87 }
88 
89 template<typename intType>
90 void ConversionTests::testConversion(const char *message, function<void (intType, char *)> vice, function<intType (const char *)> versa, intType min, intType max)
91 {
92  const intType random = uniform_int_distribution<intType>(min, max)(m_randomEngine);
93  stringstream msg;
94  msg << message << '(' << hex << '0' << 'x' << random << ')';
95  vice(random, m_buff);
96  CPPUNIT_ASSERT_MESSAGE(msg.str(), versa(m_buff) == random);
97 }
98 
99 #define TEST_TYPE(endianness, function) \
100  decltype(endianness::function(m_buff))
101 
102 #define TEST_CONVERSION(function, endianness) \
103  testConversion<TEST_TYPE(endianness, function)>( \
104  "testing " #function, \
105  static_cast<void(*)(TEST_TYPE(endianness, function), char *)>(&endianness::getBytes), \
106  endianness::function, \
107  numeric_limits<TEST_TYPE(endianness, function)>::min(), \
108  numeric_limits<TEST_TYPE(endianness, function)>::max() \
109  )
110 
111 #define TEST_BE_CONVERSION(function) \
112  TEST_CONVERSION( \
113  function, BE \
114  )
115 
116 #define TEST_LE_CONVERSION(function) \
117  TEST_CONVERSION( \
118  function, LE \
119  )
120 
121 #define TEST_CUSTOM_CONVERSION(vice, versa, endianness, min, max) \
122  testConversion<TEST_TYPE(endianness, versa)>( \
123  "testing " #versa, \
124  static_cast<void(*)(TEST_TYPE(endianness, versa), char *)>(&endianness::vice), \
125  endianness::versa, \
126  min, max \
127  )
128 
136 {
137  // test to...() / getBytes() with random numbers
138  for(byte b = 1; b < 100; ++b) {
139  TEST_BE_CONVERSION(toUInt16);
140  TEST_BE_CONVERSION(toUInt32);
141  TEST_BE_CONVERSION(toUInt64);
142  TEST_LE_CONVERSION(toUInt16);
143  TEST_LE_CONVERSION(toUInt32);
144  TEST_LE_CONVERSION(toUInt64);
145  TEST_BE_CONVERSION(toInt16);
146  TEST_BE_CONVERSION(toInt32);
147  TEST_BE_CONVERSION(toInt64);
148  TEST_LE_CONVERSION(toInt16);
149  TEST_LE_CONVERSION(toInt32);
150  TEST_LE_CONVERSION(toInt64);
151  TEST_CUSTOM_CONVERSION(getBytes24, toUInt24, BE, 0, 0xFFFFFF);
152  TEST_CUSTOM_CONVERSION(getBytes24, toUInt24, LE, 0, 0xFFFFFF);
153  }
154 }
155 
160 {
161  CPPUNIT_ASSERT(swapOrder(static_cast<uint16>(0x7825)) == 0x2578);
162  CPPUNIT_ASSERT(swapOrder(static_cast<uint32>(0x12345678)) == 0x78563412);
163  CPPUNIT_ASSERT(swapOrder(static_cast<uint64>(0x1122334455667788)) == 0x8877665544332211);
164 }
165 
169 void assertEqual(const char *message, const byte *expectedValues, size_t expectedSize, const StringData &actualValues)
170 {
171  // check whether number of elements matches
172  CPPUNIT_ASSERT_EQUAL_MESSAGE(message, expectedSize, actualValues.second);
173  // check whether contents match
174  auto *end = expectedValues + expectedSize;
175  auto *i = reinterpret_cast<byte *>(actualValues.first.get());
176  for(; expectedValues != end; ++expectedValues, ++i) {
177  CPPUNIT_ASSERT_EQUAL_MESSAGE(message, asHexNumber(*expectedValues), asHexNumber(*i));
178  }
179 }
180 
181 #if CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN == true
182 # define LE_STR_FOR_ENDIANNESS(name) name ## LE ## String
183 # define BE_STR_FOR_ENDIANNESS(name) name ## BE ## String
184 #elif CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN == true
185 # define LE_STR_FOR_ENDIANNESS(name) name ## BE ## String
186 # define BE_STR_FOR_ENDIANNESS(name) name ## LE ## String
187 #endif
188 
203 {
204  // define test string "ABCD" for the different encodings
205  const byte simpleString[] = {'A', 'B', 'C', 'D'};
206  const uint16 simpleUtf16LEString[] = {0x0041, 0x0042, 0x0043, 0x0044};
207  const uint16 simpleUtf16BEString[] = {0x4100, 0x4200, 0x4300, 0x4400};
208  // define test string "ABĂ–CD" for the different encodings
209  const byte latin1String[] = {'A', 'B', 0xD6, 'C', 'D'};
210  const byte utf8String[] = {'A', 'B', 0xC3, 0x96, 'C', 'D'};
211  const uint16 utf16LEString[] = {0x0041, 0x0042, 0x00D6, 0x0043, 0x0044};
212  const uint16 utf16BEString[] = {0x4100, 0x4200, 0xD600, 0x4300, 0x4400};
213  // test conversion to UTF-8
214  assertEqual("Latin-1 to UTF-8 (simple)", simpleString, 4, convertLatin1ToUtf8(reinterpret_cast<const char *>(simpleString), 4));
215  assertEqual("Latin-1 to UTF-8", utf8String, 6, convertLatin1ToUtf8(reinterpret_cast<const char *>(latin1String), 5));
216  assertEqual("UTF-16LE to UTF-8 (simple)", simpleString, 4, convertUtf16LEToUtf8(reinterpret_cast<const char *>(LE_STR_FOR_ENDIANNESS(simpleUtf16)), 8));
217  assertEqual("UTF-16LE to UTF-8", utf8String, 6, convertUtf16LEToUtf8(reinterpret_cast<const char *>(LE_STR_FOR_ENDIANNESS(utf16)), 10));
218  assertEqual("UTF-16BE to UTF-8 (simple)", simpleString, 4, convertUtf16BEToUtf8(reinterpret_cast<const char *>(BE_STR_FOR_ENDIANNESS(simpleUtf16)), 8));
219  assertEqual("UTF-16BE to UTF-8", utf8String, 6, convertUtf16BEToUtf8(reinterpret_cast<const char *>(BE_STR_FOR_ENDIANNESS(utf16)), 10));
220  // test conversion from UTF-8
221  assertEqual("UTF-8 to Latin-1 (simple)", simpleString, 4, convertUtf8ToLatin1(reinterpret_cast<const char *>(simpleString), 4));
222  assertEqual("UTF-8 to Latin-1", latin1String, 5, convertUtf8ToLatin1(reinterpret_cast<const char *>(utf8String), 6));
223  assertEqual("UTF-8 to UFT-16LE (simple)", reinterpret_cast<const byte *>(LE_STR_FOR_ENDIANNESS(simpleUtf16)), 8, convertUtf8ToUtf16LE(reinterpret_cast<const char *>(simpleString), 4));
224  assertEqual("UTF-8 to UFT-16LE", reinterpret_cast<const byte *>(LE_STR_FOR_ENDIANNESS(utf16)), 10, convertUtf8ToUtf16LE(reinterpret_cast<const char *>(utf8String), 6));
225  assertEqual("UTF-8 to UFT-16BE (simple)", reinterpret_cast<const byte *>(BE_STR_FOR_ENDIANNESS(simpleUtf16)), 8, convertUtf8ToUtf16BE(reinterpret_cast<const char *>(simpleString), 4));
226  assertEqual("UTF-8 to UFT-16BE", reinterpret_cast<const byte *>(BE_STR_FOR_ENDIANNESS(utf16)), 10, convertUtf8ToUtf16BE(reinterpret_cast<const char *>(utf8String), 6));
227 }
228 
233 {
234  // stringToNumber() / numberToString() with zero and random numbers
235  CPPUNIT_ASSERT_EQUAL(string("0"), numberToString<unsigned int>(0));
236  CPPUNIT_ASSERT_EQUAL(string("0"), numberToString<signed int>(0));
237  uniform_int_distribution<int64> randomDistSigned(numeric_limits<int64>::min());
238  uniform_int_distribution<uint64> randomDistUnsigned(0);
239  for(byte b = 1; b < 100; ++b) {
240  auto signedRandom = randomDistSigned(m_randomEngine);
241  auto unsignedRandom = randomDistUnsigned(m_randomEngine);
242  for(const auto base : initializer_list<byte>{2, 8, 10, 16}) {
243  auto resultString = stringToNumber<uint64, string>(numberToString<uint64, string>(unsignedRandom, base), base);
244  auto resultWideString = stringToNumber<uint64, wstring>(numberToString<uint64, wstring>(unsignedRandom, base), base);
245  CPPUNIT_ASSERT_EQUAL(unsignedRandom, resultString);
246  CPPUNIT_ASSERT_EQUAL(unsignedRandom, resultWideString);
247  }
248  for(const auto base : initializer_list<byte>{10}) {
249  auto resultString = stringToNumber<int64, string>(numberToString<int64, string>(signedRandom, base), base);
250  auto resultWideString = stringToNumber<int64, wstring>(numberToString<int64, wstring>(signedRandom, base), base);
251  CPPUNIT_ASSERT_EQUAL(signedRandom, resultString);
252  CPPUNIT_ASSERT_EQUAL(signedRandom, resultWideString);
253  }
254  }
255 
256  // stringToNumber() with leading zeroes and different types
257  int32 res = stringToNumber<int32, string>("01");
258  CPPUNIT_ASSERT_EQUAL(1, res);
259  res = stringToNumber<int32, wstring>(L"01");
260  CPPUNIT_ASSERT_EQUAL(1, res);
261  res = stringToNumber<int32, u16string>(u"01");
262  CPPUNIT_ASSERT_EQUAL(1, res);
263 
264  // interpretIntegerAsString()
265  CPPUNIT_ASSERT(interpretIntegerAsString<uint32>(0x54455354) == "TEST");
266 
267  // splitString() / joinStrings()
268  string splitJoinTest = joinStrings(splitString<vector<string> >(",a,,ab,ABC,s"s, ","s, EmptyPartsTreat::Keep), " "s, false, "("s, ")"s);
269  CPPUNIT_ASSERT_EQUAL("() (a) () (ab) (ABC) (s)"s, splitJoinTest);
270  splitJoinTest = joinStrings(splitString<vector<string> >(",a,,ab,ABC,s"s, ","s, EmptyPartsTreat::Keep), " "s, true, "("s, ")"s);
271  CPPUNIT_ASSERT_EQUAL("(a) (ab) (ABC) (s)"s, splitJoinTest);
272  splitJoinTest = joinStrings(splitString<vector<string> >(",a,,ab,ABC,s"s, ","s, EmptyPartsTreat::Omit), " "s, false, "("s, ")"s);
273  CPPUNIT_ASSERT_EQUAL("(a) (ab) (ABC) (s)"s, splitJoinTest);
274  splitJoinTest = joinStrings(splitString<vector<string> >(",a,,ab,ABC,s"s, ","s, EmptyPartsTreat::Merge), " "s, false, "("s, ")"s);
275  CPPUNIT_ASSERT_EQUAL("(a,ab) (ABC) (s)"s, splitJoinTest);
276 
277  // findAndReplace()
278  string findReplaceTest("findAndReplace()");
279  findAndReplace<string>(findReplaceTest, "And", "Or");
280  CPPUNIT_ASSERT_EQUAL("findOrReplace()"s, findReplaceTest);
281 
282  // startsWith()
283  CPPUNIT_ASSERT(!startsWith<string>(findReplaceTest, "findAnd"));
284  CPPUNIT_ASSERT(startsWith<string>(findReplaceTest, "findOr"));
285 
286  // containsSubstrings()
287  CPPUNIT_ASSERT(containsSubstrings<string>("this string contains foo and bar", {"foo", "bar"}));
288  CPPUNIT_ASSERT(!containsSubstrings<string>("this string contains foo and bar", {"bar", "foo"}));
289 
290  // encodeBase64() / decodeBase64() with random data
291  uniform_int_distribution<byte> randomDistChar;
292  byte originalBase64Data[4047];
293  for(byte &c : originalBase64Data) {
294  c = randomDistChar(m_randomEngine);
295  }
296  const auto encodedBase64Data = encodeBase64(originalBase64Data, sizeof(originalBase64Data));
297  auto decodedBase64Data = decodeBase64(encodedBase64Data.data(), encodedBase64Data.size());
298  CPPUNIT_ASSERT(decodedBase64Data.second == sizeof(originalBase64Data));
299  for(unsigned int i = 0; i < sizeof(originalBase64Data); ++i) {
300  CPPUNIT_ASSERT(decodedBase64Data.first[i] == originalBase64Data[i]);
301  }
302 }
303 
304 string functionTakingString(const string &str)
305 {
306  return str;
307 }
308 
310 {
311  // conversion of string-tuple to string (the actual string builder)
312  const tuple<const char *, string, int, const char *> tuple("string1", "string2", 1234, "string3");
313  CPPUNIT_ASSERT_EQUAL(string("string1string21234string3"), tupleToString(tuple));
314  CPPUNIT_ASSERT_EQUAL(string("foobarfoo2bar2"), tupleToString(string("foo") % "bar" % string("foo2") % "bar2"));
315  CPPUNIT_ASSERT_EQUAL(string("v2.3.0"), argsToString("v2.", 3, '.', 0));
316 
317  // construction of string-tuple and final conversion to string works
318  CPPUNIT_ASSERT_EQUAL_MESSAGE("result can be passed to any function taking a std::string"s, "123456789"s, functionTakingString("12" % string("34") % '5' % 67 + "89"));
319  constexpr double velocityExample = 27.0;
320  CPPUNIT_ASSERT_EQUAL_MESSAGE("real-word example"s, "velocity: 27 km/h (7.5 m/s)"s, functionTakingString("velocity: " % numberToString(velocityExample) % " km/h (" % numberToString(velocityExample / 3.6) + " m/s)"));
321  CPPUNIT_ASSERT_EQUAL_MESSAGE("regular + operator still works (no problems with ambiguity)"s, "regular + still works"s, "regular"s + " + still works");
322 }
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 encodeBase64(const byte *data, uint32 dataSize)
Encodes the specified data to Base64.
void testSwapOrderFunctions()
Tests swap order functions.
CPP_UTILITIES_EXPORT StringData convertUtf16BEToUtf8(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-16 (big-endian) string to UTF-8.
void testBinaryConversions()
Tests most important binary conversions.
constexpr StringType argsToString(Args &&... args)
STL namespace.
#define TEST_LE_CONVERSION(function)
CPPUNIT_TEST_SUITE_REGISTRATION(ConversionTests)
void testStringConversions()
Tests miscellaneous string conversions.
StringType numberToString(IntegralType number, typename StringType::value_type base=10)
Converts the given number to its equivalent string representation using the specified base...
void testEndianness()
Tests whether macros for endianness are correct.
#define TEST_CUSTOM_CONVERSION(vice, versa, endianness, min, max)
CPP_UTILITIES_EXPORT int random(int lowerbounds, int upperbounds)
Returns a pseudo random number between lowerbounds and upperbounds.
Definition: math.cpp:15
#define BE_STR_FOR_ENDIANNESS(name)
Selects right string for big-endian checks.
CPP_UTILITIES_EXPORT StringData convertUtf8ToUtf16BE(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-8 string to UTF-16 (big-endian).
string functionTakingString(const string &str)
CPP_UTILITIES_EXPORT constexpr uint16 swapOrder(uint16 value)
Swaps the byte order of the specified 16-bit unsigned integer.
Contains classes and functions utilizing creating of test applications.
Definition: testutils.h:9
#define TEST_BE_CONVERSION(function)
CPP_UTILITIES_EXPORT StringData convertUtf8ToLatin1(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-8 string to Latin-1.
Contains several functions providing conversions between different data types.
std::pair< std::unique_ptr< char[], StringDataDeleter >, std::size_t > StringData
Type used to return string encoding conversion result.
CPP_UTILITIES_EXPORT StringData convertUtf16LEToUtf8(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-16 (little-endian) string to UTF-8.
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:161
void testStringEncodingConversions()
Tests string encoding conversions.
std::int32_t int32
signed 32-bit integer
Definition: types.h:24
void assertEqual(const char *message, const byte *expectedValues, size_t expectedSize, const StringData &actualValues)
Internally used for string encoding tests to check results.
Container::value_type joinStrings(const Container &strings, const typename Container::value_type &delimiter=typename Container::value_type(), bool omitEmpty=false, const typename Container::value_type &leftClosure=typename Container::value_type(), const typename Container::value_type &rightClosure=typename Container::value_type())
Joins the given strings using the specified delimiter.
std::uint8_t byte
unsigned byte
Definition: types.h:14
CPP_UTILITIES_EXPORT StringData convertLatin1ToUtf8(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified Latin-1 string to UTF-8.
#define LE_STR_FOR_ENDIANNESS(name)
Selects right string for little-endian checks.
StringType tupleToString(const std::tuple< Args... > &tuple)
Concatenates all strings hold by the specified tuple.
Container splitString(const typename Container::value_type &string, const typename Container::value_type &delimiter, EmptyPartsTreat emptyPartsRole=EmptyPartsTreat::Keep, int maxParts=-1)
Splits the given string at the specified delimiter.
CPP_UTILITIES_EXPORT std::pair< std::unique_ptr< byte[]>, uint32 > decodeBase64(const char *encodedStr, const uint32 strSize)
Decodes the specified Base64 encoded string.
The ConversionTests class tests classes and methods of the ConversionUtilities namespace.
std::uint16_t uint16
unsigned 16-bit integer
Definition: types.h:39