/*############################################################################## HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ############################################################################## */ #ifdef _USE_CPPUNIT #include "unittests.hpp" #include "jstats.h" #include "jregexp.hpp" #include "jfile.hpp" #include "deftype.hpp" #include "rmtfile.hpp" /* * This is the main unittest driver for HPCC. From here, * all unit tests, be they internal or external (API), * will run. * * All internal unit tests, written on the same source * files as the implementation they're testing, can be * dynamically linked via the helper class below. * * All external unit tests (API tests, test-driven * development, interface documentation and general * usability tests) should be implemented as source * files within the same directory as this file, and * statically linked together. * * CPPUnit will automatically recognise and run them all. */ void usage() { printf("\n" "Usage:\n" " unittests \n" "\n" "Options:\n" " -a --all Include all tests, including timing and stress tests\n" " -d --load path Dynamically load a library/all libraries in a directory.\n" " By default, the HPCCSystems lib directory is loaded.\n" " -e --exact Match subsequent test names exactly\n" " -h --help Display this help text\n" " -l --list List matching tests but do not execute them\n" " -x --exclude Exclude subsequent test names\n" "\n"); } bool matchName(const char *name, const StringArray &patterns) { ForEachItemIn(idx, patterns) { bool match; const char *pattern = patterns.item(idx); if (strchr(pattern, '*')) { match = WildMatch(name, pattern, true); } else match = streq(name, pattern); if (match) return true; } return false; } LoadedObject *loadDll(const char *thisDll) { try { DBGLOG("Loading %s", thisDll); return new LoadedObject(thisDll); } catch (IException *E) { E->Release(); } catch (...) { } return NULL; } void loadDlls(IArray &objects, const char * libDirectory) { const char * mask = "*" SharedObjectExtension; Owned libDir = createIFile(libDirectory); Owned libFiles = libDir->directoryFiles(mask,false,false); ForEach(*libFiles) { const char *thisDll = libFiles->query().queryFilename(); if (!strstr(thisDll, "javaembed")) // Bit of a hack, but loading this if java not present terminates... if (!strstr(thisDll, "py2embed")) // These two clash, so ... if (!strstr(thisDll, "py3embed")) // ... best to load neither... { LoadedObject *loaded = loadDll(thisDll); if (loaded) objects.append(*loaded); } } } int main(int argc, char* argv[]) { InitModuleObjects(); StringArray includeNames; StringArray excludeNames; StringArray loadLocations; bool wildMatch = true; bool exclude = false; bool includeAll = false; bool verbose = false; bool list = false; bool useDefaultLocations = true; for (int argNo = 1; argNo < argc; argNo++) { const char *arg = argv[argNo]; if (arg[0]=='-') { if (streq(arg, "-x") || streq(arg, "--exclude")) exclude = true; else if (streq(arg, "-v") || streq(arg, "--verbose")) verbose = true; else if (streq(arg, "-e") || streq(arg, "--exact")) wildMatch = false; else if (streq(arg, "-a") || streq(arg, "--all")) includeAll = true; else if (streq(arg, "-l") || streq(arg, "--list")) list = true; else if (streq(arg, "-d") || streq(arg, "--load")) { useDefaultLocations = false; argNo++; if (argNosetMessageFields(MSGFIELD_time); else removeLog(); if (!includeNames.length()) includeNames.append("*"); if (!includeAll) { excludeNames.append("*stress*"); excludeNames.append("*timing*"); } if (useDefaultLocations) { // Default library location depends on the executable location... StringBuffer dir; splitFilename(argv[0], &dir, &dir, NULL, NULL); dir.replaceString(PATHSEPSTR "bin" PATHSEPSTR, PATHSEPSTR "lib" PATHSEPSTR); if (verbose) DBGLOG("Adding default library location %s", dir.str()); loadLocations.append(dir); #ifdef _DEBUG dir.replaceString(PATHSEPSTR "lib" PATHSEPSTR, PATHSEPSTR "libs" PATHSEPSTR); loadLocations.append(dir); if (verbose) DBGLOG("Adding default library location %s", dir.str()); #endif } IArray objects; ForEachItemIn(idx, loadLocations) { const char *location = loadLocations.item(idx); Owned file = createIFile(location); switch (file->isDirectory()) { case notFound: if (verbose && !useDefaultLocations) DBGLOG("Specified library location %s not found", location); break; case foundYes: loadDlls(objects, location); break; case foundNo: LoadedObject *loaded = loadDll(location); if (loaded) objects.append(*loaded); break; } } bool wasSuccessful = false; { // New scope as we need the TestRunner to be destroyed before unloading the dlls... CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry(); CppUnit::TextUi::TestRunner runner; CppUnit::Test *all = registry.makeTest(); int numTests = all->getChildTestCount(); for (int i = 0; i < numTests; i++) { CppUnit::Test *sub = all->getChildTestAt(i); std::string name = sub->getName(); if (matchName(name.c_str(), includeNames)) { if (matchName(name.c_str(), excludeNames)) { if (verbose) DBGLOG("Excluding test %s", name.c_str()); } else if (list) printf("%s\n", name.c_str()); else { if (verbose) DBGLOG("Including test %s", name.c_str()); runner.addTest(sub); } } } wasSuccessful = list || runner.run( "", false ); } releaseAtoms(); ClearTypeCache(); // Clear this cache before the file hooks are unloaded removeFileHooks(); objects.kill(); ExitModuleObjects(); return wasSuccessful; } //MORE: This can't be included in jlib because of the dll dependency class InternalStatisticsTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE( InternalStatisticsTest ); CPPUNIT_TEST(testMappings); CPPUNIT_TEST_SUITE_END(); void testMappings() { try { verifyStatisticFunctions(); } catch (IException * e) { StringBuffer msg; fprintf(stderr, "Failure: %s", e->errorMessage(msg).str()); e->Release(); ASSERT(false); } } }; CPPUNIT_TEST_SUITE_REGISTRATION( InternalStatisticsTest ); CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( InternalStatisticsTest, "StatisticsTest" ); class PtreeThreadingTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE( PtreeThreadingTest ); CPPUNIT_TEST(testContention); CPPUNIT_TEST_SUITE_END(); void testContention() { _testContention(ipt_lowmem); _testContention(ipt_fast); } void _testContention(byte flags) { class casyncfor: public CAsyncFor { volatile int v; void donothing() { v++; } byte flags = ipt_none; int mode = 0; int iterations = 0; const char *desc = nullptr; public: casyncfor(const char *_desc, byte _flags, int _mode, int _iter) : flags(_flags), mode(_mode), iterations(_iter), desc(_desc) { }; double For(unsigned num, unsigned maxatonce, double overhead = 0.0) { unsigned start = msTick(); CAsyncFor::For(num, maxatonce); unsigned elapsed = msTick()-start; double looptime = (elapsed * 1.0) / (iterations*num); if (mode < 3) DBGLOG("%s (%s) test completed in %d ms (%f ms/iter)", desc, flags & ipt_fast ? "fast" : "lowmem", elapsed, looptime-overhead); return looptime; } void Do(unsigned i) { for (unsigned i = 0; i < iterations; i++) { Owned p = mode >= 3 ? nullptr : createPTreeFromXMLString( "" " " " 1" " 1" " 1" " 1" " 0" " 1" " hthor" " " " " " " " " " " " " "" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " 1" " " " dgABCAEAGBAAAAB7IGludGVnZXI4IHYgfTsK " " 1" " " " AQAAAAAAAAA= " " " " " " completed" " " " " " " " " " " " " " " " 1" " 1" " " " AQAAAAAAAAA= " " " " " " " " community_6.0.0-trunk0Debug[heads/cass-wu-part3-0-g10b954-dirty]" " " " " " " " 1" " " " b25lAAEIAQAYAAAAAA== " " 1" " " " AQAAAAAAAAA= " " " " " " " " " " " " " " " " " " " " " " " " " "" , flags); switch(mode) { case 1: case 3: for (int j = 0; j < 100000; j++) donothing(); break; case 2: case 4: for (int j = 0; j < 1000000; j++) donothing(); break; } } } } max("maxContention",flags,0,1000), some("someContention",flags,1,200), min("minContention",flags,2,200), csome("control some",flags,3,200), cmin("control min",flags,4,200), seq("single",flags,0,1000); max.For(8,8); some.For(8,8,csome.For(8,8)); min.For(8,8,cmin.For(8,8)); seq.For(8,1); } }; CPPUNIT_TEST_SUITE_REGISTRATION( PtreeThreadingTest ); CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( PtreeThreadingTest, "PtreeThreadingTest" ); //MORE: This can't be included in jlib because of the dll dependency class StringBufferTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE( StringBufferTest ); CPPUNIT_TEST(testReplace); CPPUNIT_TEST_SUITE_END(); void testReplace() { StringBuffer r ("1 bb c"); r.replaceString(" ", "x"); ASSERT(streq(r, "1xbbxc")); } }; CPPUNIT_TEST_SUITE_REGISTRATION( StringBufferTest ); CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringBufferTest, "StringBufferTest" ); #endif // _USE_CPPUNIT