blob: 8f9d4a48c7a694967dfe540c5a5e4d4da26e87e8 [file] [log] [blame]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/no_destructor.h"
#include <string>
#include <utility>
#include "base/logging.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
struct CheckOnDestroy {
~CheckOnDestroy() { CHECK(false); }
};
TEST(NoDestructorTest, SkipsDestructors) {
NoDestructor<CheckOnDestroy> destructor_should_not_run;
}
struct CopyOnly {
CopyOnly() = default;
CopyOnly(const CopyOnly&) = default;
CopyOnly& operator=(const CopyOnly&) = default;
CopyOnly(CopyOnly&&) = delete;
CopyOnly& operator=(CopyOnly&&) = delete;
};
struct MoveOnly {
MoveOnly() = default;
MoveOnly(const MoveOnly&) = delete;
MoveOnly& operator=(const MoveOnly&) = delete;
MoveOnly(MoveOnly&&) = default;
MoveOnly& operator=(MoveOnly&&) = default;
};
struct ForwardingTestStruct {
ForwardingTestStruct(const CopyOnly&, MoveOnly&&) {}
};
TEST(NoDestructorTest, ForwardsArguments) {
CopyOnly copy_only;
MoveOnly move_only;
static NoDestructor<ForwardingTestStruct> test_forwarding(
copy_only, std::move(move_only));
}
TEST(NoDestructorTest, Accessors) {
static NoDestructor<std::string> awesome("awesome");
EXPECT_EQ("awesome", *awesome);
EXPECT_EQ(0, awesome->compare("awesome"));
EXPECT_EQ(0, awesome.get()->compare("awesome"));
}
// Passing initializer list to a NoDestructor like in this test
// is ambiguous in GCC.
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84849
#if !defined(COMPILER_GCC) && !defined(__clang__)
TEST(NoDestructorTest, InitializerList) {
static NoDestructor<std::vector<std::string>> vector({"a", "b", "c"});
}
#endif
} // namespace
} // namespace base