1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef UNITTEST_HPP
  26 #define UNITTEST_HPP
  27 
  28 #include <stdlib.h>
  29 #include <stdio.h>
  30 
  31 #define GTEST_DONT_DEFINE_TEST 1
  32 #include "gtest/gtest.h"
  33 
  34 // gtest/gtest.h includes assert.h which will define the assert macro, but hotspot has its
  35 // own standards incompatible assert macro that takes two parameters.
  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