unittests.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #ifdef _USE_CPPUNIT
  15. #include "platform.h"
  16. #include "jlib.hpp"
  17. #include "jlog.hpp"
  18. #include "jmisc.hpp"
  19. #include <cppunit/extensions/TestFactoryRegistry.h>
  20. #include <cppunit/ui/text/TestRunner.h>
  21. #include <cppunit/extensions/HelperMacros.h>
  22. #define ASSERT(a) { if (!(a)) CPPUNIT_ASSERT(a); }
  23. /*
  24. * This is the main unittest driver for HPCC. From here,
  25. * all unit tests, be they internal or external (API),
  26. * will run.
  27. *
  28. * All internal unit tests, written on the same source
  29. * files as the implementation they're testing, can be
  30. * dynamically linked via the helper class below.
  31. *
  32. * All external unit tests (API tests, test-driven
  33. * development, interface documentation and general
  34. * usability tests) should be implemented as source
  35. * files within the same directory as this file, and
  36. * statically linked together.
  37. *
  38. * CPPUnit will automatically recognise and run them all.
  39. */
  40. /*
  41. * Helper class to unload libraries at the end
  42. * and make sure the SharedObject gets deleted
  43. * correctly.
  44. *
  45. * This is important to run valgrind tests and not
  46. * having to care about which memory leaks are "good"
  47. * and which are not.
  48. */
  49. class LoadedObject : public IInterface, CInterface {
  50. SharedObject *so;
  51. public:
  52. IMPLEMENT_IINTERFACE;
  53. LoadedObject(const char * name)
  54. {
  55. so = new SharedObject;
  56. so->load(name, true);
  57. }
  58. ~LoadedObject()
  59. {
  60. so->unload();
  61. delete so;
  62. }
  63. };
  64. int main(int argc, char* argv[])
  65. {
  66. InitModuleObjects();
  67. // These are the internal unit tests covered by other modules and libraries
  68. Array objects;
  69. objects.append(*(new LoadedObject ("jhtree")));
  70. objects.append(*(new LoadedObject ("roxiemem")));
  71. objects.append(*(new LoadedObject ("thorhelper")));
  72. queryStderrLogMsgHandler()->setMessageFields(MSGFIELD_time);
  73. CppUnit::TextUi::TestRunner runner;
  74. if (argc==1)
  75. {
  76. CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
  77. runner.addTest( registry.makeTest() );
  78. }
  79. else
  80. {
  81. for (int name = 1; name < argc; name++)
  82. {
  83. CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry(argv[name]);
  84. runner.addTest( registry.makeTest() );
  85. }
  86. }
  87. bool wasSucessful = runner.run( "", false );
  88. ExitModuleObjects();
  89. releaseAtoms();
  90. return wasSucessful;
  91. }
  92. #endif // _USE_CPPUNIT