C++ Utilities 5.27.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Loading...
Searching...
No Matches
misctests.cpp
Go to the documentation of this file.
4
7
8#include "../io/misc.h"
9
10#include "../tests/testutils.h"
11
12#include "resources/version.h"
13
14#include <cppunit/TestFixture.h>
15#include <cppunit/extensions/HelperMacros.h>
16
17#include <regex>
18
19using namespace std;
20using namespace CppUtilities;
21using namespace CppUtilities::Literals;
22using namespace CPPUNIT_NS;
23
24// test version check macro
25#if CPP_UTILITIES_VERSION_CHECK(5, 2, 1) > CPP_UTILITIES_VERSION_CHECK(6, 0, 0)
26#error "Check for major version doesn't work"
27#endif
28#if CPP_UTILITIES_VERSION_CHECK(5, 2, 2) > CPP_UTILITIES_VERSION_CHECK(5, 3, 0)
29#error "Check for minor version doesn't work"
30#endif
31#if CPP_UTILITIES_VERSION_CHECK(5, 2, 1) > CPP_UTILITIES_VERSION_CHECK(5, 2, 2)
32#error "Check for path version doesn't work"
33#endif
34#if CPP_UTILITIES_VERSION < CPP_UTILITIES_VERSION_CHECK(5, 0, 0)
35#error "Library version seems wrongly defined, should be already >= 5.0.0"
36#endif
37
41class MiscTests : public TestFixture {
42 CPPUNIT_TEST_SUITE(MiscTests);
43 CPPUNIT_TEST(testMultiArray);
44 CPPUNIT_TEST(testLevenshtein);
45 CPPUNIT_TEST(testTestUtilities);
46 CPPUNIT_TEST_SUITE_END();
47
48public:
49 void setUp()
50 {
51 }
52 void tearDown()
53 {
54 }
55
56 void testMultiArray();
57 void testLevenshtein();
58 void testTestUtilities();
59};
60
62
64{
65 static_assert(decltype(makeMultiArray<char>(3))::dimensionCount() == 1, "dimension count 1D");
66 static_assert(decltype(makeMultiArray<char>(3, 2))::dimensionCount() == 2, "dimension count 2D");
67 static_assert(decltype(makeMultiArray<char>(3, 2, 3))::dimensionCount() == 3, "dimension count 3D");
68
69 auto array1d(makeMultiArray<char>(3));
70 CPPUNIT_ASSERT_EQUAL(3_st, array1d.dimensionSize<0>());
71 CPPUNIT_ASSERT_EQUAL(3_st, array1d.totalSize());
72 array1d.at(0) = 'a';
73 array1d.at(1) = 'b';
74 array1d.at(2) = 'c';
75 CPPUNIT_ASSERT_EQUAL("abc"s, string(array1d.data(), 3));
76
77 auto array2d(makeMultiArray<char>(3, 2));
78 CPPUNIT_ASSERT_EQUAL(3_st, array2d.dimensionSize<0>());
79 CPPUNIT_ASSERT_EQUAL(2_st, array2d.dimensionSize<1>());
80 CPPUNIT_ASSERT_EQUAL(6_st, array2d.totalSize());
81 const char *const data(array2d.data());
82 array2d.at(0, 0) = 'a';
83 array2d.at(0, 1) = 'b';
84 array2d.at(1, 0) = 'c';
85 array2d.at(1, 1) = 'd';
86 array2d.at(2, 0) = 'e';
87 array2d.at(2, 1) = 'f';
88 CPPUNIT_ASSERT_EQUAL("abcdef"s, string(data, 6));
89
90 auto array3d(makeMultiArray<char>(3, 2, 3));
91 CPPUNIT_ASSERT_EQUAL(3_st, array3d.dimensionSize<0>());
92 CPPUNIT_ASSERT_EQUAL(2_st, array3d.dimensionSize<1>());
93 CPPUNIT_ASSERT_EQUAL(3_st, array3d.dimensionSize<2>());
94 CPPUNIT_ASSERT_EQUAL(18_st, array3d.totalSize());
95 array3d.at(0, 0, 0) = 'a';
96 array3d.at(0, 0, 1) = 'b';
97 array3d.at(0, 0, 2) = 'c';
98 array3d.at(0, 1, 0) = 'd';
99 array3d.at(0, 1, 1) = 'e';
100 array3d.at(0, 1, 2) = 'f';
101 array3d.at(1, 0, 0) = 'g';
102 array3d.at(1, 0, 1) = 'h';
103 array3d.at(1, 0, 2) = 'i';
104 array3d.at(1, 1, 0) = 'j';
105 array3d.at(1, 1, 1) = 'k';
106 array3d.at(1, 1, 2) = 'l';
107 array3d.at(2, 0, 0) = 'm';
108 array3d.at(2, 0, 1) = 'n';
109 array3d.at(2, 0, 2) = 'o';
110 array3d.at(2, 1, 0) = 'p';
111 array3d.at(2, 1, 1) = 'q';
112 array3d.at(2, 1, 2) = 'r';
113 CPPUNIT_ASSERT_EQUAL("abcdefghijklmnopqr"s, string(array3d.data(), 18));
114
115 auto stackMultiArray(makeFixedSizeMultiArray<char, 9>(3, 3));
116 CPPUNIT_ASSERT_EQUAL(3_st, stackMultiArray.dimensionSize<0>());
117 CPPUNIT_ASSERT_EQUAL(3_st, stackMultiArray.dimensionSize<1>());
118 CPPUNIT_ASSERT_EQUAL(9_st, stackMultiArray.totalSize());
119 stackMultiArray.at(0, 0) = 'a';
120 stackMultiArray.at(0, 1) = 'b';
121 stackMultiArray.at(0, 2) = 'c';
122 stackMultiArray.at(1, 0) = 'd';
123 stackMultiArray.at(1, 1) = 'e';
124 stackMultiArray.at(1, 2) = 'f';
125 stackMultiArray.at(2, 0) = 'g';
126 stackMultiArray.at(2, 1) = 'h';
127 stackMultiArray.at(2, 2) = 'i';
128 CPPUNIT_ASSERT_EQUAL("abcdefghi"s, string(stackMultiArray.data(), 9));
129}
130
132{
133 CPPUNIT_ASSERT_EQUAL(1_st, computeDamerauLevenshteinDistance("ab", "abc"));
134 CPPUNIT_ASSERT_EQUAL(1_st, computeDamerauLevenshteinDistance("abc", "ab"));
135 CPPUNIT_ASSERT_EQUAL(2_st, computeDamerauLevenshteinDistance("xzaby", "xbay"));
136 CPPUNIT_ASSERT_EQUAL(0_st, computeDamerauLevenshteinDistance("", ""));
137 CPPUNIT_ASSERT_EQUAL(1_st, computeDamerauLevenshteinDistance("ab", "ba"));
138 CPPUNIT_ASSERT_EQUAL(1_st, computeDamerauLevenshteinDistance("xaby", "xbay"));
139 CPPUNIT_ASSERT_EQUAL(0_st, computeDamerauLevenshteinDistance("abc", "abc"));
140 CPPUNIT_ASSERT_EQUAL(1_st, computeDamerauLevenshteinDistance("ab", "abc"));
141 CPPUNIT_ASSERT_EQUAL(2_st, computeDamerauLevenshteinDistance("ca", "abc"));
142 CPPUNIT_ASSERT_EQUAL(4_st, computeDamerauLevenshteinDistance("", "abcd"));
143 CPPUNIT_ASSERT_EQUAL(4_st, computeDamerauLevenshteinDistance("abcd", ""));
144 CPPUNIT_ASSERT_EQUAL(3_st, computeDamerauLevenshteinDistance("abcd", "d"));
145 CPPUNIT_ASSERT_EQUAL(2_st, computeDamerauLevenshteinDistance("abcd", "bc"));
146 CPPUNIT_ASSERT_EQUAL(3_st, computeDamerauLevenshteinDistance("abcd", "a"));
147 CPPUNIT_ASSERT_EQUAL(2_st, computeDamerauLevenshteinDistance("adb", "abc"));
148 CPPUNIT_ASSERT_EQUAL(2_st, computeDamerauLevenshteinDistance("xxaxx", "xxäxx"));
149 CPPUNIT_ASSERT_EQUAL(1_st, computeDamerauLevenshteinDistance("xxöxx", "xxäxx"));
150 CPPUNIT_ASSERT_EQUAL(11_st, computeDamerauLevenshteinDistance("this is a long text", "this is too long for stack"));
151}
152
157{
158 const auto workingCopyPathForNestedTestFile = workingCopyPath("subdir/nested-testfile.txt");
159 CPPUNIT_ASSERT_EQUAL_MESSAGE("creation of subdirectories in working dir", "some file\n"s, readFile(workingCopyPathForNestedTestFile));
160
161 const auto workingCopyPathUnderDifferentNameForNestedTestFile = workingCopyPathAs("subdir/nested-testfile.txt", "subdir2/foo.txt");
162 const auto splittedPath = splitString<vector<string>>(workingCopyPathUnderDifferentNameForNestedTestFile, "/", EmptyPartsTreat::Omit);
163 CPPUNIT_ASSERT_GREATEREQUAL(2_st, splittedPath.size());
164 CPPUNIT_ASSERT_EQUAL_MESSAGE("different subdir", "subdir2"s, splittedPath[splittedPath.size() - 2]);
165 CPPUNIT_ASSERT_EQUAL_MESSAGE("different file name", "foo.txt"s, splittedPath[splittedPath.size() - 1]);
166 CPPUNIT_ASSERT_EQUAL_MESSAGE(
167 "creation of subdirectories in working dir", "some file\n"s, readFile(workingCopyPathUnderDifferentNameForNestedTestFile));
168
169 stringstream ss;
170 ss << asHexNumber(16);
171 CPPUNIT_ASSERT_EQUAL_MESSAGE("printing hex numbers", "0x10"s, ss.str());
172
173 TESTUTILS_ASSERT_LIKE("assert like works", ".*foo.*", " foo ");
174}
175
176// test flagenumclass.h
177
178namespace FlagEnumTests {
179enum class TestFlags { None, Foo = 1, Bar = 2, Baz = 4, Biz = 8 };
180}
181
183
184namespace FlagEnumTests {
185
186constexpr bool testFlagEnumClass()
187{
188 // test const operations
189 const auto testFlags = TestFlags::Foo | TestFlags::Baz;
190 static_assert((testFlags & TestFlags::Foo) == TestFlags::Foo);
191 static_assert(testFlags && TestFlags::Foo);
192 static_assert(!(testFlags && TestFlags::Bar));
193 static_assert((testFlags & TestFlags::Bar) == TestFlags::None);
194 static_assert(testFlags && TestFlags::Baz);
195 static_assert((testFlags & TestFlags::Baz) == TestFlags::Baz);
196 static_assert(checkFlagEnum(testFlags, TestFlags::Foo | TestFlags::Baz));
197 static_assert(checkFlagEnum(testFlags, TestFlags::Foo));
198 static_assert(!checkFlagEnum(testFlags, TestFlags::Foo | TestFlags::Bar));
199
200 // test modifying
201 auto nonConstFlags = TestFlags::Foo | TestFlags::Biz;
202 modFlagEnum(nonConstFlags, TestFlags::Foo | TestFlags::Bar, false);
203 nonConstFlags += TestFlags::Bar;
204 modFlagEnum(nonConstFlags, TestFlags::Baz | TestFlags::Biz, true);
205 return nonConstFlags == (TestFlags::Bar | TestFlags::Baz | TestFlags::Biz);
206}
207static_assert(testFlagEnumClass());
208
209} // namespace FlagEnumTests
The MiscTests class tests misc functions and classes (mainly of files contained by the misc directory...
Definition misctests.cpp:41
void testTestUtilities()
Tests helper from TestUtilities namespace which aren't used in other tests anyways.
void setUp()
Definition misctests.cpp:49
void tearDown()
Definition misctests.cpp:52
void testLevenshtein()
void testMultiArray()
Definition misctests.cpp:63
#define CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(Namespace, EnumClassType)
CPPUNIT_TEST_SUITE_REGISTRATION(MiscTests)
Contains literals to ease asserting with CPPUNIT_ASSERT_EQUAL.
Definition testutils.h:368
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.
Definition misc.cpp:17
CPP_UTILITIES_EXPORT std::string workingCopyPath(const std::string &relativeTestFilePath, WorkingCopyMode mode=WorkingCopyMode::CreateCopy)
Convenience function to invoke TestApplication::workingCopyPath().
Definition testutils.h:179
constexpr bool checkFlagEnum(FlagEnumClass flagVariable, FlagEnumClass flagsToCheck)
Returns whether the specified flagVariable has set all flags specified via flagsToCheck to true.
auto makeFixedSizeMultiArray(DimensionSizes... dimensionSizes)
Constructs a new N-dimensional array using a fixed size array as underlying container....
Definition multiarray.h:177
auto makeMultiArray(DimensionSizes... dimensionSizes)
Constructs a new N-dimensional array using an std::vector with std::allocator as underlying container...
Definition multiarray.h:169
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:258
CPP_UTILITIES_EXPORT std::string workingCopyPathAs(const std::string &relativeTestFilePath, const std::string &relativeWorkingCopyPath, WorkingCopyMode mode=WorkingCopyMode::CreateCopy)
Convenience function to invoke TestApplication::workingCopyPathAs().
Definition testutils.h:188
constexpr FlagEnumClass & modFlagEnum(FlagEnumClass &flagVariable, FlagEnumClass relevantFlags, bool value)
Sets the specified relevantFlags in the specified flagVariable to the specified value.
CPP_UTILITIES_EXPORT std::size_t computeDamerauLevenshteinDistance(const char *str1, std::size_t size1, const char *str2, std::size_t size2)
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.
constexpr bool testFlagEnumClass()
STL namespace.
#define TESTUTILS_ASSERT_LIKE(message, expectedRegex, actualString)
Asserts whether the specified string matches the specified regex.
Definition testutils.h:338