1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER
#include <cstdlib>
#include <gtest/gtest.h>
#include <Kokkos_Core.hpp>
#include "KokkosExecutionEnvironmentNeverInitializedFixture.hpp"
namespace {
using ScopeGuard_DeathTest = KokkosExecutionEnvironmentNeverInitialized;
/**
* Test to create a scope guard normally.
*/
TEST_F(ScopeGuard_DeathTest, create) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
// run it in a different process so side effects are not kept
EXPECT_EXIT(
{
{
Kokkos::ScopeGuard guard{};
if (!Kokkos::is_initialized()) std::exit(EXIT_FAILURE);
if (Kokkos::is_finalized()) std::exit(EXIT_FAILURE);
}
if (Kokkos::is_initialized()) std::exit(EXIT_FAILURE);
if (!Kokkos::is_finalized()) std::exit(EXIT_FAILURE);
std::exit(EXIT_SUCCESS);
},
testing::ExitedWithCode(EXIT_SUCCESS), "");
}
/**
* Test to create a scope guard with an argument.
*/
TEST_F(ScopeGuard_DeathTest, create_argument) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
// run it in a different process so side effects are not kept
EXPECT_EXIT(
{
{
Kokkos::InitializationSettings settings{};
Kokkos::ScopeGuard guard{settings};
}
std::exit(EXIT_SUCCESS);
},
testing::ExitedWithCode(EXIT_SUCCESS), "");
}
/**
* Test to create another scope guard when one has been created.
*/
TEST_F(ScopeGuard_DeathTest, create_while_initialize) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
EXPECT_DEATH(
{
Kokkos::ScopeGuard guard1{};
// create a second scope guard while there is one already existing
Kokkos::ScopeGuard guard2{};
},
"Creating a ScopeGuard while Kokkos is initialized");
}
/**
* Test to create a scope guard when initialization has been done manually.
*/
TEST_F(ScopeGuard_DeathTest, create_after_initialize) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
EXPECT_DEATH(
{
Kokkos::initialize();
// create a scope guard after manual initialization
Kokkos::ScopeGuard guard{};
},
"Creating a ScopeGuard while Kokkos is initialized");
}
/**
* Test to create another scope guard when one has been destroyed.
*/
TEST_F(ScopeGuard_DeathTest, create_after_finalize) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
EXPECT_DEATH(
{
{ Kokkos::ScopeGuard guard1{}; }
// create a second scope guard while the first one has been destroyed
// already
Kokkos::ScopeGuard guard2{};
},
"Creating a ScopeGuard after Kokkos was finalized");
}
/**
* Test to destroy a scope guard when finalization has been done manually.
*/
TEST_F(ScopeGuard_DeathTest, destroy_after_finalize) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
EXPECT_DEATH(
{
// create a scope guard and finalize it manually
Kokkos::ScopeGuard guard{};
Kokkos::finalize();
},
"Destroying a ScopeGuard after Kokkos was finalized");
}
/**
* Static tests
*/
// Test scope guard is not copyable.
static_assert(!std::is_copy_assignable_v<Kokkos::ScopeGuard>);
static_assert(!std::is_copy_constructible_v<Kokkos::ScopeGuard>);
// Test scope guard is not movable.
static_assert(!std::is_move_assignable_v<Kokkos::ScopeGuard>);
static_assert(!std::is_move_constructible_v<Kokkos::ScopeGuard>);
} // namespace
|