Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ahSingleton_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
46#include <gtest/gtest.h>
47
48#include <ahSingleton.H>
49
50#include <atomic>
51#include <thread>
52#include <type_traits>
53#include <vector>
54
55namespace
56{
57class MacroSingletonMutable
58{
59 Make_Singleton(MacroSingletonMutable)
60
61private:
62 MacroSingletonMutable() = default;
63
64public:
65 int value = 0;
66};
67
68class MacroSingletonCtorCount
69{
70 Make_Singleton(MacroSingletonCtorCount)
71
72private:
73 MacroSingletonCtorCount() { ++ctor_count; }
74
75public:
76 static inline std::atomic<int> ctor_count{0};
77};
78} // namespace
79
80TEST(AhSingleton, SingletonGetInstanceReturnsSameObject)
81{
84 EXPECT_EQ(&a, &b);
85}
86
87TEST(AhSingleton, MakeSingletonReturnsSameObject)
88{
89 auto & a = MacroSingletonMutable::get_instance();
90 auto & b = MacroSingletonMutable::get_instance();
91 EXPECT_EQ(&a, &b);
92}
93
94TEST(AhSingleton, MakeSingletonReturnsNonConstReferenceAndIsMutable)
95{
96 auto & a = MacroSingletonMutable::get_instance();
97 a.value = 123;
98 EXPECT_EQ(MacroSingletonMutable::get_instance().value, 123);
99}
100
101TEST(AhSingleton, CopyAndMoveAreDisabled)
102{
103 static_assert(!std::is_copy_constructible_v<Singleton>);
104 static_assert(!std::is_copy_assignable_v<Singleton>);
105 static_assert(!std::is_move_constructible_v<Singleton>);
106 static_assert(!std::is_move_assignable_v<Singleton>);
107
108 static_assert(!std::is_copy_constructible_v<MacroSingletonMutable>);
109 static_assert(!std::is_copy_assignable_v<MacroSingletonMutable>);
110 static_assert(!std::is_move_constructible_v<MacroSingletonMutable>);
111 static_assert(!std::is_move_assignable_v<MacroSingletonMutable>);
112}
113
114TEST(AhSingleton, GetInstanceIsNoexcept)
115{
116 // Verify that get_instance() is noexcept for both Singleton class and macro
117 static_assert(noexcept(Singleton::get_instance()));
118 static_assert(noexcept(MacroSingletonMutable::get_instance()));
119}
120
121TEST(AhSingleton, ThreadSafeInitializationMeyersSingleton)
122{
123 MacroSingletonCtorCount::ctor_count.store(0);
124
125 constexpr int kThreads = 16;
126 constexpr int kIters = 5000;
127
128 std::vector<std::thread> threads;
129 threads.reserve(kThreads);
130
131 std::atomic<void *> first_addr{nullptr};
132 std::atomic<bool> mismatch{false};
133
134 for (int t = 0; t < kThreads; ++t)
135 {
136 threads.emplace_back([&]() {
137 for (int i = 0; i < kIters; ++i)
138 {
139 auto * addr = (void *) &MacroSingletonCtorCount::get_instance();
140 void * expected = nullptr;
141 if (first_addr.compare_exchange_strong(expected, addr))
142 continue;
143
144 if (first_addr.load() != addr)
145 mismatch.store(true);
146 }
147 });
148 }
149
150 for (auto & th : threads)
151 th.join();
152
153 EXPECT_FALSE(mismatch.load());
154 EXPECT_EQ(MacroSingletonCtorCount::ctor_count.load(), 1);
155}
156
157int main(int argc, char ** argv)
158{
159 ::testing::InitGoogleTest(&argc, argv);
160 return RUN_ALL_TESTS();
161}
Minimal, header-only singleton helpers using the Meyers singleton pattern.
#define Make_Singleton(name)
Macro to inject Meyers singleton functionality into a class.
int main()
A minimal singleton class implementation.
static Singleton & get_instance() noexcept
Returns the unique instance of the Singleton.
#define TEST(name)