Sierra Toolkit Version of the Day
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 #else
00015 #define HAVE_MPI
00016 #include <mpi.h>
00017 #endif
00018 
00019 //
00020 //This file is kind of like a unit-test abstraction layer:
00021 //A series of STKUNIT_* macros are defined in terms of either
00022 //gtest macros, or trilinos/teuchos unit-test macros, depending
00023 //on whether stk_mesh is being built as a sierra product or Trilinos package.
00024 //
00025 #ifdef HAVE_MPI
00026 #define RUN_TEST_REDUCE(error) \
00027   int tmp_error = error; \
00028   int reduce_result = MPI_Allreduce ( &tmp_error, &error, 1 /*count*/, \
00029                                       MPI_INT, MPI_MAX, MPI_COMM_WORLD ); \
00030   if (reduce_result != MPI_SUCCESS) { \
00031     std::cerr << "MPI_Allreduce FAILED" << std::endl; \
00032     error = true; \
00033   } \
00034   if ( !error ) \
00035     std::cout << "STKUNIT_ALL_PASS" << std::endl;
00036 #else
00037 #define RUN_TEST_REDUCE(error) \
00038   if (error != 0) { \
00039     std::cerr << "Test FAILED" << std::endl; \
00040     error = true; \
00041   } \
00042   if ( !error ) \
00043     std::cout << "STKUNIT_ALL_PASS" << std::endl;
00044 #endif
00045 
00046 
00047 #ifdef HAVE_STK_Trilinos
00048 //If we're building as a Trilinos package, then we'll use the Teuchos unit-test macros.
00049 
00050 #include <Teuchos_UnitTestHarness.hpp>
00051 #include <Teuchos_UnitTestRepository.hpp>
00052 #include <Teuchos_GlobalMPISession.hpp>
00053 
00054 #define STKUNIT_UNIT_TEST(testclass,testmethod) TEUCHOS_UNIT_TEST(testclass,testmethod)
00055 
00056 #define STKUNIT_ASSERT(A) \
00057     {bool success = true; TEUCHOS_TEST_ASSERT(A,std::cout,success); if (!success) throw 1;}
00058 #define STKUNIT_ASSERT_EQUAL(A,B) \
00059     {bool success = true; TEUCHOS_TEST_EQUALITY(B,A,std::cout,success); if (!success) throw 1;}
00060 #define STKUNIT_EXPECT_EQUAL(A,B) STKUNIT_ASSERT_EQUAL(A,B)
00061 #define STKUNIT_ASSERT_EQ(A,B) STKUNIT_ASSERT_EQUAL(A,B)
00062 #define STKUNIT_ASSERT_NE(A,B) \
00063     {bool success = true; TEUCHOS_TEST_INEQUALITY(B,A,std::cout,success); if (!success) throw 1;}
00064 #define STKUNIT_ASSERT_LE(A,B) \
00065     {bool success = true; TEUCHOS_TEST_COMPARE(A,<=,B,std::cout,success); if (!success) throw 1;}
00066 #define STKUNIT_ASSERT_LT(A,B) \
00067     {bool success = true; TEUCHOS_TEST_COMPARE(A,<,B,std::cout,success); if (!success) throw 1;}
00068 #define STKUNIT_ASSERT_GE(A,B) \
00069     {bool success = true; TEUCHOS_TEST_COMPARE(A,>=,B,std::cout,success); if (!success) throw 1;}
00070 #define STKUNIT_ASSERT_GT(A,B) \
00071     {bool success = true; TEUCHOS_TEST_COMPARE(A,>,B,std::cout,success); if (!success) throw 1;}
00072 #define STKUNIT_EXPECT_EQ(A,B) STKUNIT_ASSERT_EQ(A,B)
00073 #define STKUNIT_EXPECT_NE(A,B) STKUNIT_ASSERT_NE(A,B)
00074 #define STKUNIT_EXPECT_LE(A,B) STKUNIT_ASSERT_LE(A,B)
00075 #define STKUNIT_EXPECT_LT(A,B) STKUNIT_ASSERT_LT(A,B)
00076 #define STKUNIT_EXPECT_GE(A,B) STKUNIT_ASSERT_GE(A,B)
00077 #define STKUNIT_EXPECT_GT(A,B) STKUNIT_ASSERT_GT(A,B)
00078 #define STKUNIT_ASSERT_STREQ(A,B) STKUNIT_ASSERT_EQ(A,B)
00079 #define STKUNIT_ASSERT_STRNE(A,B) STKUNIT_ASSERT_NE(A,B)
00080 #define STKUNIT_ASSERT_STRCASEEQ(A,B) STKUNIT_ASSERT_STREQ(toupper(A),toupper(B))
00081 #define STKUNIT_ASSERT_STRCASENE(A,B) STKUNIT_ASSERT_STRNE(toupper(A),toupper(B))
00082 #define STKUNIT_EXPECT_STREQ(A,B) STKUNIT_EXPECT_EQ(A,B)
00083 #define STKUNIT_EXPECT_STRNE(A,B) STKUNIT_EXPECT_NE(A,B)
00084 #define STKUNIT_EXPECT_STRCASEEQ(A,B) STKUNIT_EXPECT_STREQ(toupper(A),toupper(B))
00085 #define STKUNIT_EXPECT_STRCASENE(A,B) STKUNIT_EXPECT_STRNE(toupper(A),toupper(B))
00086 #define STKUNIT_ASSERT_THROW(A,B) \
00087     {bool success = true; TEUCHOS_TEST_THROW(A,B,std::cout,success); if (!success) throw 1;}
00088 #define STKUNIT_ASSERT_NO_THROW(A) \
00089     {TEUCHOS_TEST_NOTHROW(A,out,success)}
00090 #define STKUNIT_EXPECT_TRUE(A) \
00091     {bool success = true; TEUCHOS_TEST_ASSERT(A,std::cout,success); if (!success) throw 1;}
00092 #define STKUNIT_EXPECT_FALSE(A) \
00093     {bool success = true; TEUCHOS_TEST_ASSERT(!(A),std::cout,success); if (!success) throw 1;}
00094 #define STKUNIT_ASSERT_TRUE(A) STKUNIT_EXPECT_TRUE(A)
00095 #define STKUNIT_ASSERT_FALSE(A) STKUNIT_EXPECT_FALSE(A)
00096 #define STKUNIT_ASSERT_DOUBLE_EQ(A,B) \
00097     {bool success = true; TEUCHOS_TEST_EQUALITY(B,A,std::cout,success); if (!success) throw 1;}
00098 #define STKUNIT_EXPECT_DOUBLE_EQ(A,B) STKUNIT_ASSERT_DOUBLE_EQ(A,B)
00099 #define STKUNIT_ASSERT_NEAR(A,B, tol) \
00100     {bool success = true; TEUCHOS_TEST_FLOATING_EQUALITY(B,A,tol,std::cout,success); if (!success) throw 1;}
00101 #define STKUNIT_EXPECT_NEAR(A,B, tol) STKUNIT_ASSERT_NEAR(A, B, tol)
00102 
00103 #define STKUNIT_MAIN(argc,argv) \
00104 int* STKUNIT_ARGC;                                                 \
00105 char** STKUNIT_ARGV;                                               \
00106 int main(int argc,char**argv) {\
00107 STKUNIT_ARGC = &argc; \
00108 STKUNIT_ARGV = argv; \
00109   Teuchos::GlobalMPISession mpiSession(&argc, &argv); \
00110   int error = Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv); \
00111   RUN_TEST_REDUCE(error); \
00112   return error; \
00113 }
00114 
00115 #else // HAVE_STK_GTEST
00116 
00117 #include <gtest/gtest.h>
00118 
00119 #define STKUNIT_ASSERT(A) ASSERT_TRUE(A)
00120 #define STKUNIT_ASSERT_EQUAL(A,B) ASSERT_EQ(A,B)
00121 #define STKUNIT_EXPECT_EQUAL(A,B) EXPECT_EQ(A,B)
00122 #define STKUNIT_ASSERT_EQ(A,B) ASSERT_EQ(A,B)
00123 #define STKUNIT_ASSERT_NE(A,B) ASSERT_NE(A,B)
00124 #define STKUNIT_ASSERT_LE(A,B) ASSERT_LE(A,B)
00125 #define STKUNIT_ASSERT_LT(A,B) ASSERT_LT(A,B)
00126 #define STKUNIT_ASSERT_GE(A,B) ASSERT_GE(A,B)
00127 #define STKUNIT_ASSERT_GT(A,B) ASSERT_GT(A,B)
00128 #define STKUNIT_EXPECT_EQ(A,B) EXPECT_EQ(A,B)
00129 #define STKUNIT_EXPECT_NE(A,B) EXPECT_NE(A,B)
00130 #define STKUNIT_EXPECT_LE(A,B) EXPECT_LE(A,B)
00131 #define STKUNIT_EXPECT_LT(A,B) EXPECT_LT(A,B)
00132 #define STKUNIT_EXPECT_GE(A,B) EXPECT_GE(A,B)
00133 #define STKUNIT_EXPECT_GT(A,B) EXPECT_GT(A,B)
00134 #define STKUNIT_ASSERT_STREQ(A,B) ASSERT_STREQ(A,B)
00135 #define STKUNIT_ASSERT_STRNE(A,B) ASSERT_STRNE(A,B)
00136 #define STKUNIT_ASSERT_STRCASEEQ(A,B) ASSERT_STRCASEEQ(A,B)
00137 #define STKUNIT_ASSERT_STRCASENE(A,B) ASSERT_STRCASENE(A,B)
00138 #define STKUNIT_EXPECT_STREQ(A,B) EXPECT_STREQ(A,B)
00139 #define STKUNIT_EXPECT_STRNE(A,B) EXPECT_STRNE(A,B)
00140 #define STKUNIT_EXPECT_STRCASEEQ(A,B) EXPECT_STRCASEEQ(A,B)
00141 #define STKUNIT_EXPECT_STRCASENE(A,B) EXPECT_STRCASENE(A,B)
00142 #define STKUNIT_ASSERT_THROW(A,B) ASSERT_THROW(A,B)
00143 #define STKUNIT_ASSERT_NO_THROW(A) ASSERT_NO_THROW(A)
00144 #define STKUNIT_EXPECT_TRUE(A) EXPECT_TRUE(A)
00145 #define STKUNIT_EXPECT_FALSE(A) EXPECT_FALSE(A)
00146 #define STKUNIT_ASSERT_TRUE(A) ASSERT_TRUE(A)
00147 #define STKUNIT_ASSERT_FALSE(A) ASSERT_FALSE(A)
00148 #define STKUNIT_ASSERT_DOUBLE_EQ(A,B) ASSERT_DOUBLE_EQ(A,B)
00149 #define STKUNIT_EXPECT_DOUBLE_EQ(A,B) EXPECT_DOUBLE_EQ(A,B)
00150 #define STKUNIT_ASSERT_NEAR(A,B,tol) ASSERT_NEAR(A,B,tol)
00151 #define STKUNIT_EXPECT_NEAR(A,B,tol) EXPECT_NEAR(A,B,tol)
00152 
00153 #define STKUNIT_UNIT_TEST(testclass,testmethod) TEST(testclass,testmethod)
00154 
00155 #define STKUNIT_MAIN(argc,argv)                                    \
00156 int* STKUNIT_ARGC;                                                 \
00157 char** STKUNIT_ARGV;                                               \
00158 int main(int argc, char **argv) {                                  \
00159   if ( MPI_SUCCESS != MPI_Init( & argc , & argv ) ) {              \
00160     std::cerr << "MPI_Init FAILED" << std::endl ;                  \
00161     std::abort();                                                  \
00162   }                                                                \
00163   STKUNIT_ARGC = &argc;                                            \
00164   STKUNIT_ARGV = argv;                                             \
00165   std::cout << "Running main() from gtest_main.cc\n";              \
00166   testing::InitGoogleTest(&argc, argv);                            \
00167   int error = RUN_ALL_TESTS();                                     \
00168   RUN_TEST_REDUCE(error);                                          \
00169   MPI_Finalize();                                                  \
00170   return error;                                                    \
00171 }
00172 
00173 #define STKUNIT_WITH_TRACING_MAIN(argc, argv)                      \
00174 int* STKUNIT_ARGC;                                                 \
00175 char** STKUNIT_ARGV;                                               \
00176 int main(int argc, char **argv) {                                  \
00177   use_case::UseCaseEnvironment use_case_environment(&argc, &argv); \
00178   std::cout << "Running main() from gtest_main.cc\n";              \
00179   testing::InitGoogleTest(&argc, argv);                            \
00180   STKUNIT_ARGC = &argc;                                            \
00181   STKUNIT_ARGV = argv;                                             \
00182   int error = RUN_ALL_TESTS();                                     \
00183   RUN_TEST_REDUCE(error);                                          \
00184   return error;                                                    \
00185 }
00186 
00187 #define STKUNIT_WITH_SIERRA_MAIN(argc,argv,prod)    \
00188 int main(int argc, char **argv) { \
00189   sierra::Env::set_input_file_required(false); \
00190   testing::InitGoogleTest(&argc, argv); \
00191   sierra::Env::Startup startup__(&argc, &argv, sierra::prod::get_product_name(), __DATE__ " " __TIME__); \
00192   int error = RUN_ALL_TESTS();                                          \
00193   RUN_TEST_REDUCE(error);                                          \
00194   return error; \
00195 }
00196 
00197 #endif // HAVE_STK_Trilinos
00198 
00199 #endif // stk_mesh_unit_tests_stk_utest_macros_hpp
00200 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines