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