< prev index next >

test/native/unittest.hpp

Print this page
rev 11753 : 8159817: Convert FreeRegionList_test to GTest
Reviewed-by: duke


  36 // The workaround is to undef assert and then re-define it. The re-definition
  37 // must unfortunately be copied since debug.hpp might already have been
  38 // included and a second include wouldn't work due to the header guards in debug.hpp.
  39 #ifdef assert
  40   #undef assert
  41   #ifdef vmassert
  42     #define assert(p, ...) vmassert(p, __VA_ARGS__)
  43   #endif
  44 #endif
  45 
  46 #define CONCAT(a, b) a ## b
  47 
  48 #define TEST(category, name) GTEST_TEST(category, CONCAT(name, _test))
  49 
  50 #define TEST_VM(category, name) GTEST_TEST(category, CONCAT(name, _test_vm))
  51 
  52 #define TEST_VM_F(test_fixture, name)                               \
  53   GTEST_TEST_(test_fixture, name ## _test_vm, test_fixture,         \
  54               ::testing::internal::GetTypeId<test_fixture>())
  55 
  56 #define TEST_OTHER_VM(category, name)                               \





  57   static void test_  ## category ## _ ## name ## _();               \
  58                                                                     \
  59   static void child_ ## category ## _ ## name ## _() {              \
  60     ::testing::GTEST_FLAG(throw_on_failure) = true;                 \
  61     test_ ## category ## _ ## name ## _();                          \
  62     fprintf(stderr, "OKIDOKI");                                     \
  63     exit(0);                                                        \
  64   }                                                                 \



  65                                                                     \
  66   TEST(category, CONCAT(name, _other_vm)) {                         \
  67     ASSERT_EXIT(child_ ## category ## _ ## name ## _(),             \
  68                 ::testing::ExitedWithCode(0),                       \
  69                 ".*OKIDOKI.*");                                     \
  70   }                                                                 \
  71                                                                     \
  72   void test_ ## category ## _ ## name ## _()
  73 
  74 #ifdef ASSERT
  75 #define TEST_VM_ASSERT(category, name)                              \
  76   static void test_  ## category ## _ ## name ## _();               \
  77                                                                     \
  78   static void child_ ## category ## _ ## name ## _() {              \
  79     ::testing::GTEST_FLAG(throw_on_failure) = true;                 \
  80     test_ ## category ## _ ## name ## _();                          \
  81     exit(0);                                                        \


























  82   }                                                                 \
  83                                                                     \






  84   TEST(category, CONCAT(name, _vm_assert)) {                        \
  85     ASSERT_EXIT(child_ ## category ## _ ## name ## _(),             \
  86                 ::testing::ExitedWithCode(1),                       \
  87                 "assert failed");                                   \
  88   }                                                                 \
  89                                                                     \
  90   void test_ ## category ## _ ## name ## _()
  91 #else
  92 #define TEST_VM_ASSERT(...)                                         \
  93     TEST_VM_ASSERT is only available in debug builds
  94 #endif
  95 
  96 #ifdef ASSERT
  97 #define TEST_VM_ASSERT_MSG(category, name, msg)                     \
  98   static void test_  ## category ## _ ## name ## _();               \
  99                                                                     \
 100   static void child_ ## category ## _ ## name ## _() {              \
 101     ::testing::GTEST_FLAG(throw_on_failure) = true;                 \
 102     test_ ## category ## _ ## name ## _();                          \
 103     exit(0);                                                        \
 104   }                                                                 \
 105                                                                     \
 106   TEST(category, CONCAT(name, _vm_assert)) {                        \
 107     ASSERT_EXIT(child_ ## category ## _ ## name ## _(),             \
 108                 ::testing::ExitedWithCode(1),                       \
 109                 "assert failed: " msg);                             \
 110   }                                                                 \
 111                                                                     \
 112   void test_ ## category ## _ ## name ## _()
 113 #else
 114 #define TEST_VM_ASSERT_MSG(...)                                     \
 115     TEST_VM_ASSERT_MSG is only available in debug builds
 116 #endif
 117 
 118 #endif // UNITTEST_HPP


  36 // The workaround is to undef assert and then re-define it. The re-definition
  37 // must unfortunately be copied since debug.hpp might already have been
  38 // included and a second include wouldn't work due to the header guards in debug.hpp.
  39 #ifdef assert
  40   #undef assert
  41   #ifdef vmassert
  42     #define assert(p, ...) vmassert(p, __VA_ARGS__)
  43   #endif
  44 #endif
  45 
  46 #define CONCAT(a, b) a ## b
  47 
  48 #define TEST(category, name) GTEST_TEST(category, CONCAT(name, _test))
  49 
  50 #define TEST_VM(category, name) GTEST_TEST(category, CONCAT(name, _test_vm))
  51 
  52 #define TEST_VM_F(test_fixture, name)                               \
  53   GTEST_TEST_(test_fixture, name ## _test_vm, test_fixture,         \
  54               ::testing::internal::GetTypeId<test_fixture>())
  55 
  56 #define str(s) #s
  57 
  58 #define TEST_SUCCESS(category, name)                                \
  59   str(category) ":" str(name) ":" "OKIDOKI"
  60 
  61 #define CHILD_VM(category, name)                                    \
  62   static void test_  ## category ## _ ## name ## _();               \
  63                                                                     \
  64   static void child_ ## category ## _ ## name ## _() {              \
  65     ::testing::GTEST_FLAG(throw_on_failure) = true;                 \
  66     test_ ## category ## _ ## name ## _();                          \
  67     fprintf(stderr, "%s", TEST_SUCCESS(category, name));            \
  68     exit(0);                                                        \
  69   }                                                                 
  70 
  71 #define TEST_OTHER_VM(category, name)                               \
  72   CHILD_VM(category, name)                                          \
  73                                                                     \
  74   TEST(category, CONCAT(name, _other_vm)) {                         \
  75     ASSERT_EXIT(child_ ## category ## _ ## name ## _(),             \
  76                 ::testing::ExitedWithCode(0),                       \
  77                 ".*" TEST_SUCCESS(category, name) ".*");            \
  78   }                                                                 \
  79                                                                     \
  80   void test_ ## category ## _ ## name ## _()
  81 
  82 #define TEST_OTHER_VM_WITH_FLAGS(category, name, flags)             \


  83                                                                     \
  84   CHILD_VM(category, name)                                          \
  85                                                                     \
  86   TEST(category, CONCAT(name, _other_vm_with_flags)) {              \
  87     const char *old_flags = getenv("_JAVA_OPTIONS");                \
  88     const char *safe_old_flags =                                    \
  89         (old_flags == NULL) ? "" : old_flags;                       \
  90     const char *safe_new_flags = (flags == NULL) ? "" : flags;      \
  91     const size_t new_options_len = strlen("_JAVA_OPTIONS=")         \
  92         + strlen(safe_new_flags) + 1;                               \
  93                                                                     \
  94     char *new_options = (char*) malloc(new_options_len);            \
  95     jio_snprintf(new_options, new_options_len, "_JAVA_OPTIONS=%s",  \
  96              safe_new_flags);                                       \
  97     ASSERT_EQ(putenv(new_options), 0)                               \
  98         << "Unable to setup new flags: " << new_options             \
  99         << "; errno = " << errno;                                   \
 100                                                                     \
 101     ASSERT_EXIT(child_ ## category ## _ ## name ## _(),             \
 102                 ::testing::ExitedWithCode(0),                       \
 103                 ".*" TEST_SUCCESS(category, name) ".*");            \
 104                                                                     \
 105     const size_t stored_options_len = strlen("_JAVA_OPTIONS=")      \
 106             + strlen(safe_old_flags) + 1;                           \
 107     char *stored_options = (char*) malloc (stored_options_len);     \
 108     jio_snprintf(stored_options, stored_options_len,                \
 109             "_JAVA_OPTIONS=%s", safe_old_flags);                    \
 110                                                                     \
 111     ASSERT_EQ(putenv(stored_options), 0)                            \
 112         << "Unable to restore flags: " << stored_options            \
 113         << "; errno = " << errno;                                   \
 114   }                                                                 \
 115                                                                     \
 116   void test_ ## category ## _ ## name ## _()
 117 
 118 #ifdef ASSERT
 119 #define TEST_VM_ASSERT(category, name)                              \
 120   CHILD_VM(category, name)                                          \
 121                                                                     \
 122   TEST(category, CONCAT(name, _vm_assert)) {                        \
 123     ASSERT_EXIT(child_ ## category ## _ ## name ## _(),             \
 124                 ::testing::ExitedWithCode(1),                       \
 125                 "assert failed");                                   \
 126   }                                                                 \
 127                                                                     \
 128   void test_ ## category ## _ ## name ## _()
 129 #else
 130 #define TEST_VM_ASSERT(...)                                         \
 131     TEST_VM_ASSERT is only available in debug builds
 132 #endif
 133 
 134 #ifdef ASSERT
 135 #define TEST_VM_ASSERT_MSG(category, name, msg)                     \
 136   CHILD_VM(category, name)                                          \






 137                                                                     \
 138   TEST(category, CONCAT(name, _vm_assert)) {                        \
 139     ASSERT_EXIT(child_ ## category ## _ ## name ## _(),             \
 140                 ::testing::ExitedWithCode(1),                       \
 141                 "assert failed: " msg);                             \
 142   }                                                                 \
 143                                                                     \
 144   void test_ ## category ## _ ## name ## _()
 145 #else
 146 #define TEST_VM_ASSERT_MSG(...)                                     \
 147     TEST_VM_ASSERT_MSG is only available in debug builds
 148 #endif
 149 
 150 #endif // UNITTEST_HPP
< prev index next >