stk_utest_macros.hpp

00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 #ifndef stk_mesh_unit_tests_stk_utest_macros_hpp
00010 #define stk_mesh_unit_tests_stk_utest_macros_hpp
00011 
00012 #ifndef STK_BUILT_IN_SIERRA
00013 #include <STK_config.h>
00014 #endif
00015 
00016 //
00017 //This file is kind of like a unit-test abstraction layer:
00018 //A series of STKUNIT_* macros are defined in terms of either
00019 //cppunit macros, or trilinos/teuchos unit-test macros, depending
00020 //on whether stk_mesh is being built as a sierra product or Trilinos package.
00021 //
00022 
00023 #ifdef HAVE_STK_Trilinos
00024 //If we're building as a Trilinos package, then we'll use the Teuchos unit-test macros.
00025 
00026 #include <Teuchos_UnitTestHarness.hpp>
00027 #include <Teuchos_UnitTestRepository.hpp>
00028 #include <Teuchos_GlobalMPISession.hpp>
00029 
00030 #define STKUNIT_UNIT_TEST(testclass,testmethod) TEUCHOS_UNIT_TEST(testclass,testmethod)
00031 
00032 #define STKUNIT_ASSERT(A) \
00033     {bool success = true; TEUCHOS_TEST_ASSERT(A,std::cout,success); if (!success) throw 1;}
00034 #define STKUNIT_ASSERT_EQUAL(A,B) \
00035     {bool success = true; TEUCHOS_TEST_EQUALITY(B,A,std::cout,success); if (!success) throw 1;}
00036 #define STKUNIT_EXPECT_EQUAL(A,B) STKUNIT_ASSERT_EQUAL(A,B)
00037 #define STKUNIT_ASSERT_THROW(A,B) \
00038     {bool success = true; TEUCHOS_TEST_THROW(A,B,std::cout,success); if (!success) throw 1;}
00039 #define STKUNIT_ASSERT_NO_THROW(A) \
00040     {TEUCHOS_TEST_NOTHROW(A,out,success)}
00041 #define STKUNIT_EXPECT_TRUE(A) \
00042     {bool success = true; TEUCHOS_TEST_ASSERT(A,std::cout,success); if (!success) throw 1;}
00043 #define STKUNIT_EXPECT_FALSE(A) \
00044     {bool success = true; TEUCHOS_TEST_ASSERT(!(A),std::cout,success); if (!success) throw 1;}
00045 #define STKUNIT_ASSERT_TRUE(A) STKUNIT_EXPECT_TRUE(A)
00046 #define STKUNIT_ASSERT_FALSE(A) STKUNIT_EXPECT_FALSE(A)
00047 
00048 
00049 #define STKUNIT_MAIN(argc,argv) \
00050 int main(int argc,char**argv) {\
00051   Teuchos::GlobalMPISession mpiSession(&argc, &argv); \
00052   bool result = Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv); \
00053   std::cout << std::endl ; \
00054   return result; \
00055 }
00056 
00057 
00058 #elif HAVE_STK_CPPUNIT
00059 
00060 //If we're not in Trilinos, we're a sierra product, so we're using cppunit:
00061 
00062 #include <cppunit/TestCase.h>
00063 #include <cppunit/extensions/HelperMacros.h>
00064 #include <cppunit/extensions/TestFactoryRegistry.h>
00065 #include <cppunit/ui/text/TestRunner.h>
00066 
00067 #define STKUNIT_ASSERT(A) CPPUNIT_ASSERT(A)
00068 #define STKUNIT_ASSERT_EQUAL(A,B) CPPUNIT_ASSERT_EQUAL(A,B)
00069 #define STKUNIT_EXPECT_EQUAL(A,B) CPPUNIT_ASSERT_EQUAL(A,B)
00070 #define STKUNIT_ASSERT_THROW(A,B) CPPUNIT_ASSERT_THROW(A,B)
00071 
00072 #define STKUNIT_UNIT_TEST(testclass,testmethod) \
00073 class testclass##_##testmethod : public CppUnit::TestCase { \
00074 private: \
00075   CPPUNIT_TEST_SUITE( testclass##_##testmethod ); \
00076   CPPUNIT_TEST( testmethod ); \
00077   CPPUNIT_TEST_SUITE_END(); \
00078 public: \
00079   testclass##_##testmethod() : CppUnit::TestCase() {} \
00080   void setUp() {} \
00081   void tearDown() {} \
00082   void testmethod(); \
00083 }; \
00084 CPPUNIT_TEST_SUITE_REGISTRATION( testclass##_##testmethod ); \
00085 void testclass##_##testmethod::testmethod() \
00086 
00087 #define STKUNIT_MAIN(argc,argv) \
00088 int main(int argc, char ** argv) \
00089 { \
00090   if ( MPI_SUCCESS != MPI_Init( & argc , & argv ) ) { \
00091     std::cerr << "MPI_Init FAILED" << std::endl ; \
00092     std::abort(); \
00093   } \
00094  \
00095   std::string test_names; \
00096  \
00097   { \
00098     for (int i = 0; i < argc; ++i) { \
00099       if (std::string(argv[i]) == "-test") { \
00100         if (i + 1 < argc) \
00101           test_names = argv[i + 1]; \
00102         else \
00103           std::cout << "Running all tests" << std::endl; \
00104       } \
00105     } \
00106  \
00107     CppUnit::TextUi::TestRunner runner; \
00108     CppUnit::TestFactoryRegistry & registry = \
00109       CppUnit::TestFactoryRegistry::getRegistry(); \
00110     runner.addTest( registry.makeTest() ); \
00111     runner.run(test_names); \
00112   } \
00113  \
00114   MPI_Finalize(); \
00115  \
00116   return 0; \
00117 }
00118 
00119 #else // HAVE_STK_GTEST
00120 
00121 #include <gtest/gtest.h>
00122 
00123 #define STKUNIT_ASSERT(A) ASSERT_TRUE(A)
00124 #define STKUNIT_ASSERT_EQUAL(A,B) ASSERT_EQ(A,B)
00125 #define STKUNIT_EXPECT_EQUAL(A,B) EXPECT_EQ(A,B)
00126 #define STKUNIT_ASSERT_THROW(A,B) ASSERT_THROW(A,B)
00127 #define STKUNIT_ASSERT_NO_THROW(A) ASSERT_NO_THROW(A)
00128 #define STKUNIT_EXPECT_TRUE(A) EXPECT_TRUE(A)
00129 #define STKUNIT_EXPECT_FALSE(A) EXPECT_FALSE(A)
00130 #define STKUNIT_ASSERT_TRUE(A) ASSERT_TRUE(A)
00131 #define STKUNIT_ASSERT_FALSE(A) ASSERT_FALSE(A)
00132 
00133 #define STKUNIT_UNIT_TEST(testclass,testmethod) TEST(testclass,testmethod)
00134 
00135 #define STKUNIT_MAIN(argc,argv) \
00136 int main(int argc, char **argv) { \
00137   if ( MPI_SUCCESS != MPI_Init( & argc , & argv ) ) { \
00138     std::cerr << "MPI_Init FAILED" << std::endl ; \
00139     std::abort(); \
00140   } \
00141   std::cout << "Running main() from gtest_main.cc\n"; \
00142   testing::InitGoogleTest(&argc, argv); \
00143   bool result = RUN_ALL_TESTS(); \
00144   MPI_Finalize(); \
00145   return result; \
00146 }
00147 
00148 
00149 #endif // HAVE_STK_Trilinos
00150 
00151 #endif // stk_mesh_unit_tests_stk_utest_macros_hpp
00152 
Generated on Wed Apr 13 10:05:49 2011 for Sierra Toolkit by  doxygen 1.6.3