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