1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
23 #include <gtest/gtest.h>
26 #include <MUtils/Global.h>
27 #include <MUtils/OSSupport.h>
28 #include <MUtils/Version.h>
38 #define ASSERT_QSTR(X,Y) ASSERT_EQ((X).compare(QLatin1String(Y)), 0);
40 //===========================================================================
42 //===========================================================================
44 class Global
: public ::testing::Test
51 virtual void TearDown()
55 QString
makeTempFolder(const char *const suffix
)
57 const QString tempPath
= MUtils::temp_folder();
58 if (!tempPath
.isEmpty())
60 const QString
tempSuffix(QString(suffix
).simplified().replace(QRegExp("[^\\w]+"), "_"));
61 QDir
tempDir(tempPath
);
62 if (tempDir
.mkpath(tempSuffix
) && tempDir
.cd(tempSuffix
))
64 return tempDir
.absolutePath();
71 //-----------------------------------------------------------------
73 //-----------------------------------------------------------------
75 #define TEST_RANDOM_MAX 99991
76 #define TEST_RANDOM(X,Y) do \
78 MUtils::seed_rand(); \
81 while(test.count() != TEST_RANDOM_MAX) \
83 if(++attempts <= 64) \
87 MUtils::OS::sleep_ms(1); \
90 for (size_t i = 0; i < TEST_RANDOM_MAX; ++i) \
92 test.insert(MUtils::next_rand_##Y()); \
97 FAIL(); /*too many attempts!*/ \
103 TEST_F(Global
, RandomU32
)
105 TEST_RANDOM(quint32
, u32
);
108 TEST_F(Global
, RandomU64
)
110 TEST_RANDOM(quint64
, u64
);
113 TEST_F(Global
, RandomStr
)
115 TEST_RANDOM(QString
, str
);
121 //-----------------------------------------------------------------
123 //-----------------------------------------------------------------
125 #define TEST_TRIM_STR(X,Y,Z) do \
129 MUtils::trim_##X(test); \
130 ASSERT_QSTR(test, (Z)); \
133 const QString test((Y)); \
134 ASSERT_QSTR(MUtils::trim_##X(test), (Z)); \
139 TEST_F(Global
, TrimStringLeft
)
141 TEST_TRIM_STR(left
, "", "");
142 TEST_TRIM_STR(left
, " ", "");
143 TEST_TRIM_STR(left
, "! test !", "! test !");
144 TEST_TRIM_STR(left
, " test ", "test ");
145 TEST_TRIM_STR(left
, " ! test ! ", "! test ! ");
148 TEST_F(Global
, TrimStringRight
)
150 TEST_TRIM_STR(right
, "", "");
151 TEST_TRIM_STR(right
, " ", "");
152 TEST_TRIM_STR(right
, "! test !", "! test !");
153 TEST_TRIM_STR(right
, " test ", " test");
154 TEST_TRIM_STR(right
, " ! test ! ", " ! test !");
159 //-----------------------------------------------------------------
161 //-----------------------------------------------------------------
163 #define TEST_CLEAN_FILE(X,Y,Z) do \
165 ASSERT_QSTR(MUtils::clean_file_##X((Y)), (Z)); \
169 static const char *const VALID_FILENAME_CHARS
= "!#$%&'()+,-.0123456789;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{}~";
171 TEST_F(Global
, CleanFileName
)
173 TEST_CLEAN_FILE(name
, "", "");
174 TEST_CLEAN_FILE(name
, VALID_FILENAME_CHARS
, VALID_FILENAME_CHARS
);
175 TEST_CLEAN_FILE(name
, "example.txt", "example.txt");
176 TEST_CLEAN_FILE(name
, " example.txt", " example.txt");
177 TEST_CLEAN_FILE(name
, "example.txt ", "example.txt");
178 TEST_CLEAN_FILE(name
, ".example.txt", ".example.txt");
179 TEST_CLEAN_FILE(name
, "example.txt.", "example.txt");
180 TEST_CLEAN_FILE(name
, "foo<>:\"/\\|?*\t\r\nbar", "foo____________bar");
181 TEST_CLEAN_FILE(name
, "NUL", "___");
182 TEST_CLEAN_FILE(name
, "xNUL", "xNUL");
183 TEST_CLEAN_FILE(name
, "NULx", "NULx");
184 TEST_CLEAN_FILE(name
, "NUL.txt", "___.txt");
185 TEST_CLEAN_FILE(name
, "NULx.txt", "NULx.txt");
186 TEST_CLEAN_FILE(name
, "xNUL.txt", "xNUL.txt");
189 TEST_F(Global
, CleanFilePath
)
191 TEST_CLEAN_FILE(path
, "", "");
192 TEST_CLEAN_FILE(path
, VALID_FILENAME_CHARS
, VALID_FILENAME_CHARS
);
193 TEST_CLEAN_FILE(path
, "c:\\foo\\bar\\example.txt", "c:/foo/bar/example.txt");
194 TEST_CLEAN_FILE(path
, "c:/foo/bar/example.txt", "c:/foo/bar/example.txt");
195 TEST_CLEAN_FILE(path
, "foo\\bar\\example.txt", "foo/bar/example.txt");
196 TEST_CLEAN_FILE(path
, "\\foo\\bar\\example.txt", "/foo/bar/example.txt");
197 TEST_CLEAN_FILE(path
, "\\\\hostname\\share\\example.txt", "//hostname/share/example.txt");
198 TEST_CLEAN_FILE(path
, "\\\\?\\c:\\very long path", "//?/c:/very long path");
199 TEST_CLEAN_FILE(path
, "c:\\foo<>:\"|?*\t\r\nbar\\example.txt", "c:/foo__________bar/example.txt");
200 TEST_CLEAN_FILE(path
, "c:\\example\\foo<>:\"|?*\t\r\nbar.txt", "c:/example/foo__________bar.txt");
201 TEST_CLEAN_FILE(path
, "c:\\ foo\\ bar\\ example.txt", "c:/ foo/ bar/ example.txt");
202 TEST_CLEAN_FILE(path
, "c:\\foo \\bar \\example.txt ", "c:/foo/bar/example.txt");
203 TEST_CLEAN_FILE(path
, "c:\\foo bar\\exa mple.txt", "c:/foo bar/exa mple.txt");
204 TEST_CLEAN_FILE(path
, "c:\\example\\NUL", "c:/example/___");
205 TEST_CLEAN_FILE(path
, "c:\\example\\xNUL", "c:/example/xNUL");
206 TEST_CLEAN_FILE(path
, "c:\\example\\NULx", "c:/example/NULx");
207 TEST_CLEAN_FILE(path
, "c:\\example\\NUL.txt", "c:/example/___.txt");
208 TEST_CLEAN_FILE(path
, "c:\\example\\xNUL.txt", "c:/example/xNUL.txt");
209 TEST_CLEAN_FILE(path
, "c:\\example\\NULx.txt", "c:/example/NULx.txt");
212 //-----------------------------------------------------------------
214 //-----------------------------------------------------------------
216 #define TEST_FILE_NAME(X, Y, ...) \
218 const QString workDir = makeTempFolder(__FUNCTION__); \
219 ASSERT_FALSE(workDir.isEmpty()); \
220 QSet<QString> test; \
221 const QRegExp pattern((Y)); \
222 for (int i = 0; i < 997; ++i) \
224 const QString name = MUtils::make_##X##_file(workDir, __VA_ARGS__); \
225 ASSERT_FALSE(name.isEmpty()); \
226 ASSERT_FALSE(test.contains(name)); \
227 ASSERT_GE(pattern.indexIn(name), 0); \
230 ASSERT_TRUE(file.open(QIODevice::ReadWrite)); \
231 ASSERT_GT(file.write("foo"), 0); \
234 for (QSet<QString>::const_iterator iter = test.constBegin(); iter != test.constEnd(); iter++) \
236 ASSERT_TRUE(QFile::exists(*iter)); \
237 QFile::remove(*iter); \
241 TEST_F(Global
, TempFileName
)
243 TEST_FILE_NAME(temp
, "/\\w+\\.txt$", "txt", true);
246 TEST_F(Global
, UniqFileName
)
248 TEST_FILE_NAME(unique
, "/example.\\w+\\.txt$", "example", "txt");
251 #undef TEST_FILE_NAME
253 //-----------------------------------------------------------------
255 //-----------------------------------------------------------------
257 TEST_F(Global
, Parity
)
259 ASSERT_EQ(MUtils::parity(0x00000000), false);
260 ASSERT_EQ(MUtils::parity(0x11111111), false);
261 ASSERT_EQ(MUtils::parity(0xFFFFFFFF), false);
262 ASSERT_EQ(MUtils::parity(0x00000001), true );
263 ASSERT_EQ(MUtils::parity(0x00000010), true );
264 ASSERT_EQ(MUtils::parity(0x00000100), true );
265 ASSERT_EQ(MUtils::parity(0x00001000), true );
266 ASSERT_EQ(MUtils::parity(0x00010000), true );
267 ASSERT_EQ(MUtils::parity(0x00100000), true );
268 ASSERT_EQ(MUtils::parity(0x01000000), true );
269 ASSERT_EQ(MUtils::parity(0x10000000), true );
270 ASSERT_EQ(MUtils::parity(0xEFFFFFFF), true);
271 ASSERT_EQ(MUtils::parity(0xFEFFFFFF), true);
272 ASSERT_EQ(MUtils::parity(0xFFEFFFFF), true);
273 ASSERT_EQ(MUtils::parity(0xFFFEFFFF), true);
274 ASSERT_EQ(MUtils::parity(0xFFFFEFFF), true);
275 ASSERT_EQ(MUtils::parity(0xFFFFFEFF), true);
276 ASSERT_EQ(MUtils::parity(0xFFFFFFEF), true);
277 ASSERT_EQ(MUtils::parity(0xFFFFFFFE), true);
278 ASSERT_EQ(MUtils::parity(0x10101010), false);
279 ASSERT_EQ(MUtils::parity(0x01010101), false);
280 ASSERT_EQ(MUtils::parity(0xC8A2CC96), false);
281 ASSERT_EQ(MUtils::parity(0x504928DD), true );
282 ASSERT_EQ(MUtils::parity(0x38BFB9EC), false);
283 ASSERT_EQ(MUtils::parity(0x73F42695), true );
284 ASSERT_EQ(MUtils::parity(0x9161E326), false);
285 ASSERT_EQ(MUtils::parity(0xB1C93AC2), true );
286 ASSERT_EQ(MUtils::parity(0xCA4B1193), false);
289 //===========================================================================
291 //===========================================================================
293 int main(int argc
, char **argv
)
295 printf("MuldeR's Utilities for Qt v%u.%02u - Regression Test Suite [%s]\n", MUtils::Version::lib_version_major(), MUtils::Version::lib_version_minor(), MUTILS_DEBUG
? "DEBUG" : "RELEASE");
296 printf("Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>. Some rights reserved.\n");
297 printf("Built on %s at %s with %s for Win-%s.\n\n", MUTILS_UTF8(MUtils::Version::lib_build_date().toString(Qt::ISODate
)), MUTILS_UTF8(MUtils::Version::lib_build_time().toString(Qt::ISODate
)), MUtils::Version::compiler_version(), MUtils::Version::compiler_arch());
299 printf("This library is free software; you can redistribute it and/or\n");
300 printf("modify it under the terms of the GNU Lesser General Public\n");
301 printf("License as published by the Free Software Foundation; either\n");
302 printf("version 2.1 of the License, or (at your option) any later version.\n\n");
304 ::testing::InitGoogleTest(&argc
, argv
);
305 return RUN_ALL_TESTS();