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 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