Ocean
Loading...
Searching...
No Matches
TestValue.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_VALUE_H
9#define META_OCEAN_TEST_TESTBASE_TEST_VALUE_H
10
12
14#include "ocean/base/Value.h"
15
16namespace Ocean
17{
18
19namespace Test
20{
21
22namespace TestBase
23{
24
25/**
26 * This class implements a Value test.
27 * @ingroup testbase
28 */
29class OCEAN_TEST_BASE_EXPORT TestValue
30{
31 public:
32
33 /**
34 * Invokes all Value tests.
35 * @param testDuration Number of seconds for each test, with range (0, infinity)
36 * @return True, if succeeded
37 */
38 static bool test(const double testDuration);
39
40 /**
41 * Tests the constructor function.
42 * @param testDuration Number of seconds for each test, with range (0, infinity)
43 * @return True, if succeeded
44 */
45 static bool testConstructor(const double testDuration);
46
47 /**
48 * Tests the comparison operators.
49 * @param testDuration Number of seconds for each test, with range (0, infinity)
50 * @return True, if succeeded
51 */
52 static bool testComparison(const double testDuration);
53
54 /**
55 * Tests the copy function.
56 * @param testDuration Number of seconds for each test, with range (0, infinity)
57 * @return True, if succeeded
58 */
59 static bool testCopy(const double testDuration);
60
61 /**
62 * Tests the move function.
63 * @param testDuration Number of seconds for each test, with range (0, infinity)
64 * @return True, if succeeded
65 */
66 static bool testMove(const double testDuration);
67
68 /**
69 * Tests the read/read to/from buffer functions.
70 * @param testDuration Number of seconds for each test, with range (0, infinity)
71 * @return True, if succeeded
72 */
73 static bool testReadWrite(const double testDuration);
74
75 protected:
76
77 /**
78 * Returns a random valid Value object.
79 * @param randomGenerator The random generator to be used
80 * @return The random value object
81 */
82 static Value createRandomValue(RandomGenerator& randomGenerator);
83
84 /**
85 * Verifies that the given value object has a specific type and value.
86 * @param object The value object to check
87 * @param value The value the object must have
88 * @return True, if so
89 * @tparam T The data type of the value object
90 */
91 template <typename T>
92 static bool verifyValue(const Value& object, const T& value);
93};
94
95template <>
96inline bool TestValue::verifyValue(const Value& object, const bool& value)
97{
98 if (!object.isBool() || object.boolValue() != value)
99 {
100 return false;
101 }
102
103 if (!object || object.isNull())
104 {
105 return false;
106 }
107
108 return true;
109}
110
111template <>
112inline bool TestValue::verifyValue(const Value& object, const int32_t& value)
113{
114 if (!object.isInt() || object.intValue() != value)
115 {
116 return false;
117 }
118
119 if (!object || object.isNull())
120 {
121 return false;
122 }
123
124 return true;
125}
126
127template <>
128inline bool TestValue::verifyValue(const Value& object, const int64_t& value)
129{
130 if (!object.isInt64() || object.int64Value() != value)
131 {
132 return false;
133 }
134
135 if (!object || object.isNull())
136 {
137 return false;
138 }
139
140 return true;
141}
142
143template <>
144inline bool TestValue::verifyValue(const Value& object, const float& value)
145{
146 if (!object.isFloat() || object.floatValue() != value)
147 {
148 return false;
149 }
150
151 if (!object || object.isNull())
152 {
153 return false;
154 }
155
156 return true;
157}
158
159template <>
160inline bool TestValue::verifyValue(const Value& object, const double& value)
161{
162 if (!object.isFloat64() || object.float64Value() != value)
163 {
164 return false;
165 }
166
167 if (!object || object.isNull())
168 {
169 return false;
170 }
171
172 return true;
173}
174
175template <>
176inline bool TestValue::verifyValue(const Value& object, const std::string& value)
177{
178 if (!object.isString() || object.stringValue() != value)
179 {
180 return false;
181 }
182
183 if (!object || object.isNull())
184 {
185 return false;
186 }
187
188 return true;
189}
190
191template <>
192inline bool TestValue::verifyValue(const Value& object, const std::vector<uint8_t>& value)
193{
194 if (!object.isBuffer())
195 {
196 return false;
197 }
198
199 size_t size = 0;
200 const void* data = object.bufferValue(size);
201
202 if (size == value.size())
203 {
204 if (size >= 1 && memcmp(value.data(), data, size) != 0)
205 {
206 return false;
207 }
208 }
209 else
210 {
211 return false;
212 }
213
214 if (!object || object.isNull())
215 {
216 return false;
217 }
218
219 return true;
220}
221
222template <typename T>
223bool TestValue::verifyValue(const Value& /*object*/, const T& /*value*/)
224{
225 ocean_assert(false && "Missing implementation!");
226 return false;
227}
228
229}
230
231}
232
233}
234
235#endif // META_OCEAN_TEST_TESTBASE_TEST_VALUE_H
This class implements a generator for random numbers.
Definition RandomGenerator.h:42
This class implements a Value test.
Definition TestValue.h:30
static bool testCopy(const double testDuration)
Tests the copy function.
static bool test(const double testDuration)
Invokes all Value tests.
static bool verifyValue(const Value &object, const T &value)
Verifies that the given value object has a specific type and value.
Definition TestValue.h:223
static bool testComparison(const double testDuration)
Tests the comparison operators.
static bool testReadWrite(const double testDuration)
Tests the read/read to/from buffer functions.
static bool testMove(const double testDuration)
Tests the move function.
static Value createRandomValue(RandomGenerator &randomGenerator)
Returns a random valid Value object.
static bool testConstructor(const double testDuration)
Tests the constructor function.
This class implements a type independent value.
Definition Value.h:23
The namespace covering the entire Ocean framework.
Definition Accessor.h:15