1 #include "../misc/levenshtein.h"
2 #include "../misc/multiarray.h"
4 #include "../conversion/stringbuilder.h"
5 #include "../conversion/stringconversion.h"
7 #include "../io/misc.h"
9 #include "../tests/testutils.h"
11 #include "resources/version.h"
13 #include <cppunit/TestFixture.h>
14 #include <cppunit/extensions/HelperMacros.h>
21 using namespace CPPUNIT_NS;
24 #if CPP_UTILITIES_VERSION_CHECK(5, 2, 1) > CPP_UTILITIES_VERSION_CHECK(6, 0, 0)
25 #error "Check for major version doesn't work"
27 #if CPP_UTILITIES_VERSION_CHECK(5, 2, 2) > CPP_UTILITIES_VERSION_CHECK(5, 3, 0)
28 #error "Check for minor version doesn't work"
30 #if CPP_UTILITIES_VERSION_CHECK(5, 2, 1) > CPP_UTILITIES_VERSION_CHECK(5, 2, 2)
31 #error "Check for path version doesn't work"
33 #if CPP_UTILITIES_VERSION < CPP_UTILITIES_VERSION_CHECK(5, 0, 0)
34 #error "Library version seems wrongly defined, should be already >= 5.0.0"
42 CPPUNIT_TEST(testMultiArray);
43 CPPUNIT_TEST(testLevenshtein);
44 CPPUNIT_TEST(testTestUtilities);
45 CPPUNIT_TEST_SUITE_END();
55 void testMultiArray();
56 void testLevenshtein();
57 void testTestUtilities();
64 static_assert(decltype(makeMultiArray<char>(3))::dimensionCount() == 1,
"dimension count 1D");
65 static_assert(decltype(makeMultiArray<char>(3, 2))::dimensionCount() == 2,
"dimension count 2D");
66 static_assert(decltype(makeMultiArray<char>(3, 2, 3))::dimensionCount() == 3,
"dimension count 3D");
68 auto array1d(makeMultiArray<char>(3));
69 CPPUNIT_ASSERT_EQUAL(3_st, array1d.dimensionSize<0>());
70 CPPUNIT_ASSERT_EQUAL(3_st, array1d.totalSize());
74 CPPUNIT_ASSERT_EQUAL(
"abc"s,
string(array1d.data(), 3));
76 auto array2d(makeMultiArray<char>(3, 2));
77 CPPUNIT_ASSERT_EQUAL(3_st, array2d.dimensionSize<0>());
78 CPPUNIT_ASSERT_EQUAL(2_st, array2d.dimensionSize<1>());
79 CPPUNIT_ASSERT_EQUAL(6_st, array2d.totalSize());
80 const char *
const data(array2d.data());
81 array2d.at(0, 0) =
'a';
82 array2d.at(0, 1) =
'b';
83 array2d.at(1, 0) =
'c';
84 array2d.at(1, 1) =
'd';
85 array2d.at(2, 0) =
'e';
86 array2d.at(2, 1) =
'f';
87 CPPUNIT_ASSERT_EQUAL(
"abcdef"s,
string(data, 6));
89 auto array3d(makeMultiArray<char>(3, 2, 3));
90 CPPUNIT_ASSERT_EQUAL(3_st, array3d.dimensionSize<0>());
91 CPPUNIT_ASSERT_EQUAL(2_st, array3d.dimensionSize<1>());
92 CPPUNIT_ASSERT_EQUAL(3_st, array3d.dimensionSize<2>());
93 CPPUNIT_ASSERT_EQUAL(18_st, array3d.totalSize());
94 array3d.at(0, 0, 0) =
'a';
95 array3d.at(0, 0, 1) =
'b';
96 array3d.at(0, 0, 2) =
'c';
97 array3d.at(0, 1, 0) =
'd';
98 array3d.at(0, 1, 1) =
'e';
99 array3d.at(0, 1, 2) =
'f';
100 array3d.at(1, 0, 0) =
'g';
101 array3d.at(1, 0, 1) =
'h';
102 array3d.at(1, 0, 2) =
'i';
103 array3d.at(1, 1, 0) =
'j';
104 array3d.at(1, 1, 1) =
'k';
105 array3d.at(1, 1, 2) =
'l';
106 array3d.at(2, 0, 0) =
'm';
107 array3d.at(2, 0, 1) =
'n';
108 array3d.at(2, 0, 2) =
'o';
109 array3d.at(2, 1, 0) =
'p';
110 array3d.at(2, 1, 1) =
'q';
111 array3d.at(2, 1, 2) =
'r';
112 CPPUNIT_ASSERT_EQUAL(
"abcdefghijklmnopqr"s,
string(array3d.data(), 18));
114 auto stackMultiArray(makeFixedSizeMultiArray<char, 9>(3, 3));
115 CPPUNIT_ASSERT_EQUAL(3_st, stackMultiArray.dimensionSize<0>());
116 CPPUNIT_ASSERT_EQUAL(3_st, stackMultiArray.dimensionSize<1>());
117 CPPUNIT_ASSERT_EQUAL(9_st, stackMultiArray.totalSize());
118 stackMultiArray.at(0, 0) =
'a';
119 stackMultiArray.at(0, 1) =
'b';
120 stackMultiArray.at(0, 2) =
'c';
121 stackMultiArray.at(1, 0) =
'd';
122 stackMultiArray.at(1, 1) =
'e';
123 stackMultiArray.at(1, 2) =
'f';
124 stackMultiArray.at(2, 0) =
'g';
125 stackMultiArray.at(2, 1) =
'h';
126 stackMultiArray.at(2, 2) =
'i';
127 CPPUNIT_ASSERT_EQUAL(
"abcdefghi"s,
string(stackMultiArray.data(), 9));
157 const auto workingCopyPathForNestedTestFile =
workingCopyPath(
"subdir/nested-testfile.txt");
158 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"creation of subdirectories in working dir",
"some file\n"s,
readFile(workingCopyPathForNestedTestFile));
160 const auto workingCopyPathUnderDifferentNameForNestedTestFile =
workingCopyPathAs(
"subdir/nested-testfile.txt",
"subdir2/foo.txt");
161 const auto splittedPath = splitString<vector<string>>(workingCopyPathUnderDifferentNameForNestedTestFile,
"/", EmptyPartsTreat::Omit);
162 CPPUNIT_ASSERT_GREATEREQUAL(2_st, splittedPath.size());
163 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"different subdir",
"subdir2"s, splittedPath[splittedPath.size() - 2]);
164 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"different file name",
"foo.txt"s, splittedPath[splittedPath.size() - 1]);
165 CPPUNIT_ASSERT_EQUAL_MESSAGE(
166 "creation of subdirectories in working dir",
"some file\n"s,
readFile(workingCopyPathUnderDifferentNameForNestedTestFile));
170 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"printing hex numbers",
"0x10"s, ss.str());
The MiscTests class tests misc functions and classes (mainly of files contained by the misc directory...
void testTestUtilities()
Tests helper from TestUtilities namespace which aren't used in other tests anyways.
CPPUNIT_TEST_SUITE_REGISTRATION(MiscTests)
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 workingCopyPath(const std::string &relativeTestFilePath, WorkingCopyMode mode=WorkingCopyMode::CreateCopy)
Convenience function to invoke TestApplication::workingCopyPath().
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 std::string workingCopyPathAs(const std::string &relativeTestFilePath, const std::string &relativeWorkingCopyPath, WorkingCopyMode mode=WorkingCopyMode::CreateCopy)
Convenience function to invoke TestApplication::workingCopyPathAs().
CPP_UTILITIES_EXPORT std::size_t computeDamerauLevenshteinDistance(const char *str1, std::size_t size1, const char *str2, std::size_t size2)
#define TESTUTILS_ASSERT_LIKE(message, expectedRegex, actualString)
Asserts whether the specified string matches the specified regex.