Ocean
Loading...
Searching...
No Matches
TestScopedObject.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_TEST_TESTBASE_TEST_SCOPED_OBJECT_H
9#define META_OCEAN_TEST_TESTBASE_TEST_SCOPED_OBJECT_H
10
13
14#include "ocean/base/Lock.h"
16
17namespace Ocean
18{
19
20namespace Test
21{
22
23namespace TestBase
24{
25
26/**
27 * This class implement a test for the ScopedObject class.
28 * @ingroup testbase
29 */
30class OCEAN_TEST_BASE_EXPORT TestScopedObject
31{
32 protected:
33
34 class Manager : public Singleton<Manager>
35 {
36 friend class Singleton<Manager>;
37
38 protected:
39
40 using IdCounterMap = std::unordered_map<uint64_t, uint64_t>;
41
42 public:
43
44 /**
45 * Returns a thread-safe unique id.
46 * @return The unique id
47 */
48 uint64_t uniqueId();
49
50 /**
51 * Returns whether the manager hold at least one object associated with a specific id.
52 * @param id The id to check
53 * @return True, if so
54 */
55 bool hasObject(const uint64_t id) const;
56
57 /**
58 * Returns the number of objects associated with a specific id.
59 * @param id The id to check
60 * @return The number of object
61 */
62 uint64_t numberObjects(const uint64_t id) const;
63
64 /**
65 * Adds an object with specific id.
66 * @param id The id of the object
67 */
68 void addObject(const uint64_t id);
69
70 /**
71 * Removes an object with a specific id.
72 * @param id The id of the object
73 * @return True, if succeeded
74 */
75 bool removeObject(const uint64_t id);
76
77 protected:
78
79 /**
80 * Default constructor.
81 */
82 Manager() = default;
83
84 protected:
85
86 /// The counter for unique ids.
87 uint64_t uniqueIdCounter_ = 0ull;
88
89 /// The map mapping ids to counters.
91
92 /// The manager's lock.
93 mutable Lock lock_;
94 };
95
96 /**
97 * This class implements an object.
98 */
99 class Object
100 {
101 public:
102
103 /**
104 * Default constructor.
105 */
106 Object() = default;
107
108 /**
109 * Creates a new object with given id.
110 */
111 explicit inline Object(const uint64_t id);
112
113 /**
114 * Returns the id of the object.
115 * @return The object's id
116 */
117 inline uint64_t id() const;
118
119 protected:
120
121 /// The id of the object.
122 uint64_t id_ = 0ull;
123 };
124
125 /**
126 * Definition of a vector holding pointers to objects.
127 */
128 using ObjectPointers = std::vector<Object*>;
129
130 /**
131 * Definition of an unordered map mapping ids to counters.
132 */
133 using CounterMap = std::unordered_map<int32_t, size_t>;
134
135 public:
136
137 /**
138 * Tests the ScopedObject class.
139 * @param testDuration Number of seconds for each test, with range (0, infinity)
140 * @param selector The test selector
141 * @return True, if succeeded
142 */
143 static bool test(const double testDuration, const TestSelector& selector = TestSelector());
144
145 /**
146 * Tests the ScopedObject with runtime release function.
147 * @param testDuration Number of seconds for each test, with range (0, infinity)
148 * @return True, if succeeded
149 */
150 static bool testRuntime(const double testDuration);
151
152 /**
153 * Tests the ScopedObject with compile time release function.
154 * @param testDuration Number of seconds for each test, with range (0, infinity)
155 * @return True, if succeeded
156 */
157 static bool testCompileTime(const double testDuration);
158
159 protected:
160
161 /**
162 * Creates a new object.
163 * @param id The id of the object
164 * @return The new object
165 */
166 static Object* createObject(const uint64_t id);
167
168 /**
169 * Releases an object.
170 * @param object The object to be released, must be valid
171 */
172 static void releaseObject(Object* object);
173
174 /**
175 * Increases the counter of the counter map.
176 * @param id The id of the counter to increase.
177 * @return True, if succeeded
178 */
179 static bool increaseCounter(const uint32_t id);
180
181 /**
182 * Returns a static map mapping ids to counters.
183 */
185};
186
187inline TestScopedObject::Object::Object(const uint64_t id) :
188 id_(id)
189{
190 // nothing to do here
191}
192
193inline uint64_t TestScopedObject::Object::id() const
194{
195 return id_;
196}
197
198}
199
200}
201
202}
203
204#endif // META_OCEAN_TEST_TESTBASE_TEST_SCOPED_OBJECT_H
This class implements a recursive lock object.
Definition Lock.h:31
This template class is the base class for all singleton objects.
Definition Singleton.h:71
Definition TestScopedObject.h:35
bool hasObject(const uint64_t id) const
Returns whether the manager hold at least one object associated with a specific id.
void addObject(const uint64_t id)
Adds an object with specific id.
IdCounterMap idCounterMap_
The map mapping ids to counters.
Definition TestScopedObject.h:90
std::unordered_map< uint64_t, uint64_t > IdCounterMap
Definition TestScopedObject.h:40
bool removeObject(const uint64_t id)
Removes an object with a specific id.
uint64_t uniqueId()
Returns a thread-safe unique id.
uint64_t numberObjects(const uint64_t id) const
Returns the number of objects associated with a specific id.
Lock lock_
The manager's lock.
Definition TestScopedObject.h:93
This class implements an object.
Definition TestScopedObject.h:100
uint64_t id() const
Returns the id of the object.
Definition TestScopedObject.h:193
This class implement a test for the ScopedObject class.
Definition TestScopedObject.h:31
static bool testCompileTime(const double testDuration)
Tests the ScopedObject with compile time release function.
static void releaseObject(Object *object)
Releases an object.
std::vector< Object * > ObjectPointers
Definition of a vector holding pointers to objects.
Definition TestScopedObject.h:128
static bool test(const double testDuration, const TestSelector &selector=TestSelector())
Tests the ScopedObject class.
static Object * createObject(const uint64_t id)
Creates a new object.
std::unordered_map< int32_t, size_t > CounterMap
Definition of an unordered map mapping ids to counters.
Definition TestScopedObject.h:133
static bool increaseCounter(const uint32_t id)
Increases the counter of the counter map.
static bool testRuntime(const double testDuration)
Tests the ScopedObject with runtime release function.
static CounterMap & counterMap()
Returns a static map mapping ids to counters.
This class implements a test selector that parses test function strings and determines which tests sh...
Definition TestSelector.h:51
The namespace covering the entire Ocean framework.
Definition Accessor.h:15