Ocean
Loading...
Searching...
No Matches
Validation.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_VALIDATION_H
9#define META_OCEAN_TEST_VALIDATION_H
10
11#include "ocean/test/Test.h"
12
14
15namespace Ocean
16{
17
18namespace Test
19{
20
21/**
22 * This class implements a helper class to validate tests.
23 *
24 * The following example shows how to use Validation and calling the functions directly.<br>
25 * In case of an error, no further information will be provided:
26 * @code
27 * bool testFunction()
28 * {
29 * Log::info() << "Running a test ...";
30 *
31 * Validation validation;
32 *
33 * validation.expectTrue(4 + 4 == 8);
34 *
35 * validation.expectFalse(4 + 4 == 7);
36 *
37 * validation.expectEqual(4 + 4, 8);
38 *
39 * if (4 + 4 == 7)
40 * {
41 * validation.setFailed();
42 * }
43 *
44 * Log::info() << "Validation: " << validation;
45 *
46 * return validation.succeeded();
47 * }
48 * @endcode
49 *
50 * The following example shows how to use Validation and calling the functions directly but also providing __FILE__ and __LINE__ macro parameters.<br>
51 * In case of an error, additional in information about the location will be provided:
52 * @code
53 * bool testFunction()
54 * {
55 * Log::info() << "Running a test ...";
56 *
57 * Validation validation;
58 *
59 * validation.expectTrue(4 + 4 == 8, __FILE__, __LINE__);
60 *
61 * validation.expectFalse(4 + 4 == 7, __FILE__, __LINE__);
62 *
63 * validation.expectEqual(4 + 4, 8, __FILE__, __LINE__);
64 *
65 * if (4 + 4 == 7)
66 * {
67 * validation.setFailed(__FILE__, __LINE__);
68 * }
69 *
70 * Log::info() << "Validation: " << validation;
71 *
72 * return validation.succeeded();
73 * }
74 * @endcode
75 *
76 * The following example shows how to use Validation while not calling the object's functions directly but using the corresponding macros.<br>
77 * In case of an error, additional in information about the location will be provided:
78 * @code
79 * bool testFunction()
80 * {
81 * Log::info() << "Running a test ...";
82 *
83 * Validation validation;
84 *
85 * OCEAN_EXPECT_TRUE(validation, 4 + 4 == 8);
86 *
87 * OCEAN_EXPECT_FALSE(validation, 4 + 4 == 7);
88 *
89 * OCEAN_EXPECT_EQUAL(validation, 4 + 4, 8);
90 *
91 * if (4 + 4 == 7)
92 * {
93 * OCEAN_SET_FAILED(validation);
94 * }
95 *
96 * Log::info() << "Validation: " << validation;
97 *
98 * return validation.succeeded();
99 * }
100 * @endcode
101 * @see PrecisionValidation.
102 * @ingroup test
103 */
105{
106 public:
107
108 /**
109 * Default constructor, by default the verified has succeeded.
110 */
111 Validation() = default;
112
113 /**
114 * Creates a new validation object associated with a random generator, by default the verified has succeeded.
115 * @param randomGenerator The random generator which will be used during verification
116 */
117 explicit inline Validation(RandomGenerator& randomGenerator);
118
119 /**
120 * Destructs this validation object.
121 */
122 inline ~Validation();
123
124 /**
125 * Informs this validation object that a value is expected to be True.
126 * In case the value is False, this validation object will not succeed.
127 * @param value The value to be expected True
128 * @see succeeded().
129 */
130 inline void expectTrue(const bool value);
131
132 /**
133 * Informs this validation object that a value is expected to be True.
134 * In case the value is False, this validation object will not succeed.<br>
135 * This function will also write a message to the error log.
136 * @param value The value to be expected True
137 * @param file The source file in which the function call happens, e.g., __FILE__, must be valid
138 * @param line The line in the source file in which the function call happens, e.g., __LINE__, must be valid
139 * @see succeeded().
140 */
141 inline void expectTrue(const bool value, const char* file, const int line);
142
143 /**
144 * Informs this validation object that a value is expected to be False.
145 * In case the value is True, this validation object will not succeed.
146 * @param value The value to be expected False
147 * @see succeeded().
148 */
149 inline void expectFalse(const bool value);
150
151 /**
152 * Informs this validation object that a value is expected to be False.
153 * In case the value is True, this validation object will not succeed.<br>
154 * This function will also write a message to the error log.
155 * @param value The value to be expected False
156 * @param file The source file in which the function call happens, e.g., __FILE__, must be valid
157 * @param line The line in the source file in which the function call happens, e.g., __LINE__, must be valid
158 * @see succeeded().
159 */
160 inline void expectFalse(const bool value, const char* file, const int line);
161
162 /**
163 * Informs this validation object that a value is expected to be equal to another value.
164 * In case the both values are not identical, this validation object will not succeed.
165 * @param value0 The first value to compare
166 * @param value1 The second value to compare
167 * @see succeeded().
168 * @tparam T The data type of both values
169 */
170 template <typename T>
171 inline void expectEqual(const T& value0, const T& value1);
172
173 /**
174 * Informs this validation object that a value is expected to be equal to another value.
175 * In case the both values are not identical, this validation object will not succeed.<br>
176 * This function will also write a message to the error log.
177 * @param value0 The first value to compare
178 * @param value1 The second value to compare
179 * @param file The source file in which the function call happens, e.g., __FILE__, must be valid
180 * @param line The line in the source file in which the function call happens, e.g., __LINE__, must be valid
181 * @see succeeded().
182 * @tparam T The data type of both values
183 */
184 template <typename T>
185 inline void expectEqual(const T& value0, const T& value1, const char* file, const int line);
186
187 /**
188 * Informs this validation object that a value is expected to be not equal to another value.
189 * In case the both values are equal, this validation object will not succeed.
190 * @param value0 The first value to compare
191 * @param value1 The second value to compare
192 * @see succeeded().
193 * @tparam T The data type of both values
194 */
195 template <typename T>
196 inline void expectNotEqual(const T& value0, const T& value1);
197
198 /**
199 * Informs this validation object that a value is expected to be not equal to another value.
200 * In case the both values are equal, this validation object will not succeed.<br>
201 * This function will also write a message to the error log.
202 * @param value0 The first value to compare
203 * @param value1 The second value to compare
204 * @param file The source file in which the function call happens, e.g., __FILE__, must be valid
205 * @param line The line in the source file in which the function call happens, e.g., __LINE__, must be valid
206 * @see succeeded().
207 * @tparam T The data type of both values
208 */
209 template <typename T>
210 inline void expectNotEqual(const T& value0, const T& value1, const char* file, const int line);
211
212 /**
213 * Informs this validation object that a value is expected to be less than another value.
214 * In case 'value0 < value1' is false, the validation object will not succeed.
215 * @param value0 The first value to compare
216 * @param value1 The second value to compare
217 * @see succeeded().
218 * @tparam T The data type of both values
219 */
220 template <typename T>
221 inline void expectLess(const T& value0, const T& value1);
222
223 /**
224 * Informs this validation object that a value is expected to be less than another value.
225 * In case 'value0 < value1' is false, the validation object will not succeed.<br>
226 * This function will also write a message to the error log.
227 * @param value0 The first value to compare
228 * @param value1 The second value to compare
229 * @param file The source file in which the function call happens, e.g., __FILE__, must be valid
230 * @param line The line in the source file in which the function call happens, e.g., __LINE__, must be valid
231 * @see succeeded().
232 * @tparam T The data type of both values
233 */
234 template <typename T>
235 inline void expectLess(const T& value0, const T& value1, const char* file, const int line);
236
237 /**
238 * Informs this validation object that a value is expected to be less than or equal to another value.
239 * In case 'value0 <= value1' is false, the validation object will not succeed.
240 * @param value0 The first value to compare
241 * @param value1 The second value to compare
242 * @see succeeded().
243 * @tparam T The data type of both values
244 */
245 template <typename T>
246 inline void expectLessEqual(const T& value0, const T& value1);
247
248 /**
249 * Informs this validation object that a value is expected to be less than or equal to another value.
250 * In case 'value0 <= value1' is false, the validation object will not succeed.<br>
251 * This function will also write a message to the error log.
252 * @param value0 The first value to compare
253 * @param value1 The second value to compare
254 * @param file The source file in which the function call happens, e.g., __FILE__, must be valid
255 * @param line The line in the source file in which the function call happens, e.g., __LINE__, must be valid
256 * @see succeeded().
257 * @tparam T The data type of both values
258 */
259 template <typename T>
260 inline void expectLessEqual(const T& value0, const T& value1, const char* file, const int line);
261
262 /**
263 * Informs this validation object that a value is expected to be greater than another value.
264 * In case 'value0 > value1' is false, the validation object will not succeed.
265 * @param value0 The first value to compare
266 * @param value1 The second value to compare
267 * @see succeeded().
268 * @tparam T The data type of both values
269 */
270 template <typename T>
271 inline void expectGreater(const T& value0, const T& value1);
272
273 /**
274 * Informs this validation object that a value is expected to be greater than another value.
275 * In case 'value0 > value1' is false, the validation object will not succeed.<br>
276 * This function will also write a message to the error log.
277 * @param value0 The first value to compare
278 * @param value1 The second value to compare
279 * @param file The source file in which the function call happens, e.g., __FILE__, must be valid
280 * @param line The line in the source file in which the function call happens, e.g., __LINE__, must be valid
281 * @see succeeded().
282 * @tparam T The data type of both values
283 */
284 template <typename T>
285 inline void expectGreater(const T& value0, const T& value1, const char* file, const int line);
286
287 /**
288 * Informs this validation object that a value is expected to be greater than or equal to another value.
289 * In case 'value0 >= value1' is false, the validation object will not succeed.
290 * @param value0 The first value to compare
291 * @param value1 The second value to compare
292 * @see succeeded().
293 * @tparam T The data type of both values
294 */
295 template <typename T>
296 inline void expectGreaterEqual(const T& value0, const T& value1);
297
298 /**
299 * Informs this validation object that a value is expected to be greater than or equal to another value.
300 * In case 'value0 >= value1' is false, the validation object will not succeed.<br>
301 * This function will also write a message to the error log.
302 * @param value0 The first value to compare
303 * @param value1 The second value to compare
304 * @param file The source file in which the function call happens, e.g., __FILE__, must be valid
305 * @param line The line in the source file in which the function call happens, e.g., __LINE__, must be valid
306 * @see succeeded().
307 * @tparam T The data type of both values
308 */
309 template <typename T>
310 inline void expectGreaterEqual(const T& value0, const T& value1, const char* file, const int line);
311
312 /**
313 * Informs this validation object that a value is expected to be inside a range.
314 * In case 'lower <= value && value <= upper' is false, the validation object will not succeed.
315 * @param lower The lower bound of the range (inclusive)
316 * @param value The value to be checked
317 * @param upper The upper bound of the range (inclusive)
318 * @see succeeded().
319 * @tparam T The data type of the value
320 */
321 template <typename T>
322 inline void expectInsideRange(const T& lower, const T& value, const T& upper);
323
324 /**
325 * Informs this validation object that a value is expected to be inside a range.
326 * In case 'lower <= value && value <= upper' is false, the validation object will not succeed.
327 * This function will also write a message to the error log.
328 * @param lower The lower bound of the range (inclusive)
329 * @param value The value to be checked
330 * @param upper The upper bound of the range (inclusive)
331 * @param file The source file in which the function call happens, e.g., __FILE__, must be valid
332 * @param line The line in the source file in which the function call happens, e.g., __LINE__, must be valid
333 * @see succeeded().
334 * @tparam T The data type of the value
335 */
336 template <typename T>
337 inline void expectInsideRange(const T& lower, const T& value, const T& upper, const char* file, const int line);
338
339 /**
340 * Explicitly sets the validation to be failed.
341 * @see succeeded().
342 */
343 inline void setFailed();
344
345 /**
346 * Explicitly sets the validation to be failed.
347 * This function will also write a message to the error log.
348 * @param file The source file in which the function call happens, e.g., __FILE__, must be valid
349 * @param line The line in the source file in which the function call happens, e.g., __LINE__, must be valid
350 * @see succeeded().
351 */
352 inline void setFailed(const char* file, const int line);
353
354 /**
355 * Returns if this validation has succeeded.
356 * @return True, if so; False, if the validation has failed
357 */
358 [[nodiscard]] inline bool succeeded() const;
359
360 /**
361 * Returns a string containing the random generator's initial seed, if any
362 * @return The string with initial seed test, empty if no random generator is associated with this validation object
363 */
364 inline std::string randomGeneratorOutput() const;
365
366 protected:
367
368 /**
369 * Disabled copy constructor.
370 */
371 Validation(const Validation&) = delete;
372
373 /**
374 * Sets the succeeded state to false.
375 */
376 inline void setSucceededFalse();
377
378 protected:
379
380 /// True, if the validation has succeeded; False, if the validation has failed.
381 bool succeeded_ = true;
382
383 /// Optional random generator object which will be used during validation.
385
386#ifdef OCEAN_DEBUG
387 /// True, if the success state of this validation has been checked.
388 mutable bool succeededChecked_ = false;
389#endif // OCEAN_DEBUG
390};
391
392#ifndef OCEAN_EXPECT_TRUE
393 #define OCEAN_EXPECT_TRUE(validation, ...) validation.expectTrue(__VA_ARGS__, __FILE__, __LINE__)
394#endif
395
396#ifndef OCEAN_EXPECT_FALSE
397 #define OCEAN_EXPECT_FALSE(validation, ...) validation.expectFalse(__VA_ARGS__, __FILE__, __LINE__)
398#endif
399
400#ifndef OCEAN_EXPECT_EQUAL
401 #define OCEAN_EXPECT_EQUAL(validation, ...) validation.expectEqual(__VA_ARGS__, __FILE__, __LINE__)
402#endif
403
404#ifndef OCEAN_EXPECT_NOT_EQUAL
405 #define OCEAN_EXPECT_NOT_EQUAL(validation, ...) validation.expectNotEqual(__VA_ARGS__, __FILE__, __LINE__)
406#endif
407
408#ifndef OCEAN_EXPECT_LESS
409 #define OCEAN_EXPECT_LESS(validation, ...) validation.expectLess(__VA_ARGS__, __FILE__, __LINE__)
410#endif
411
412#ifndef OCEAN_EXPECT_LESS_EQUAL
413 #define OCEAN_EXPECT_LESS_EQUAL(validation, ...) validation.expectLessEqual(__VA_ARGS__, __FILE__, __LINE__)
414#endif
415
416#ifndef OCEAN_EXPECT_GREATER
417 #define OCEAN_EXPECT_GREATER(validation, ...) validation.expectGreater(__VA_ARGS__, __FILE__, __LINE__)
418#endif
419
420#ifndef OCEAN_EXPECT_GREATER_EQUAL
421 #define OCEAN_EXPECT_GREATER_EQUAL(validation, ...) validation.expectGreaterEqual(__VA_ARGS__, __FILE__, __LINE__)
422#endif
423
424#ifndef OCEAN_EXPECT_INSIDE_RANGE
425 #define OCEAN_EXPECT_INSIDE_RANGE(validation, ...) validation.expectInsideRange(__VA_ARGS__, __FILE__, __LINE__)
426#endif
427
428#ifndef OCEAN_SET_FAILED
429 #define OCEAN_SET_FAILED(validation) validation.setFailed(__FILE__, __LINE__);
430#endif
431
432inline Validation::Validation(RandomGenerator& randomGenerator) :
433 randomGenerator_(&randomGenerator)
434{
435 // nothing to do here
436}
437
439{
440#ifdef OCEAN_DEBUG
441 ocean_assert(succeededChecked_ && "The validation has not been check for success");
442#endif
443}
444
445inline void Validation::expectTrue(const bool value)
446{
447 if (!value)
448 {
450
451 Log::debug() << "Validation::expectTrue() failed at unknown location" << randomGeneratorOutput();
452 }
453}
454
455inline void Validation::expectTrue(const bool value, const char* file, const int line)
456{
457 if (!value)
458 {
460
461 ocean_assert(file != nullptr);
462 if (file != nullptr)
463 {
464#ifdef OCEAN_USE_GTEST
465 std::cerr << "\nValidation::expectTrue() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
466#else
467 Log::error() << "Validation::expectTrue() failed in '" << file << "', in line " << line << randomGeneratorOutput();
468#endif
469 }
470 }
471}
472
473inline void Validation::expectFalse(const bool value)
474{
475 if (value)
476 {
478
479 Log::debug() << "Validation::expectFalse() failed at unknown location" << randomGeneratorOutput();
480 }
481}
482
483inline void Validation::expectFalse(const bool value, const char* file, const int line)
484{
485 if (value)
486 {
488
489 ocean_assert(file != nullptr);
490 if (file != nullptr)
491 {
492#ifdef OCEAN_USE_GTEST
493 std::cerr << "\nValidation::expectFalse() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
494#else
495 Log::error() << "Validation::expectFalse() failed in '" << file << "', in line " << line << randomGeneratorOutput();
496#endif
497 }
498 }
499}
500
501template <typename T>
502inline void Validation::expectEqual(const T& value0, const T& value1)
503{
504 if (value0 != value1)
505 {
507
508 if constexpr (Log::isSupported<T>())
509 {
510 Log::debug() << "Validation::expectEqual(" << value0 << ", " << value1 <<") failed at unknown location" << randomGeneratorOutput();
511 }
512 else
513 {
514 Log::debug() << "Validation::expectEqual() failed at unknown location" << randomGeneratorOutput();
515 }
516 }
517}
518
519template <typename T>
520inline void Validation::expectEqual(const T& value0, const T& value1, const char* file, const int line)
521{
522 if (value0 != value1)
523 {
525
526 ocean_assert(file != nullptr);
527 if (file != nullptr)
528 {
529#ifdef OCEAN_USE_GTEST
530 if constexpr (Log::isSupported<T, std::ostream>())
531 {
532 std::cerr << "\nValidation::expectEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
533 }
534 else
535 {
536 std::cerr << "\nValidation::expectEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
537 }
538#else
539 if constexpr (Log::isSupported<T>())
540 {
541 Log::error() << "Validation::expectEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput();
542 }
543 else
544 {
545 Log::error() << "Validation::expectEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput();
546 }
547#endif // OCEAN_USE_GTEST
548 }
549 }
550}
551
552template <typename T>
553inline void Validation::expectNotEqual(const T& value0, const T& value1)
554{
555 if (value0 == value1)
556 {
558
559 if constexpr (Log::isSupported<T>())
560 {
561 Log::debug() << "Validation::expectNotEqual(" << value0 << ", " << value1 <<") failed at unknown location" << randomGeneratorOutput();
562 }
563 else
564 {
565 Log::debug() << "Validation::expectNotEqual() failed at unknown location" << randomGeneratorOutput();
566 }
567 }
568}
569
570template <typename T>
571inline void Validation::expectNotEqual(const T& value0, const T& value1, const char* file, const int line)
572{
573 if (value0 == value1)
574 {
576
577 ocean_assert(file != nullptr);
578 if (file != nullptr)
579 {
580#ifdef OCEAN_USE_GTEST
581 if constexpr (Log::isSupported<T, std::ostream>())
582 {
583 std::cerr << "\nValidation::expectNotEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
584 }
585 else
586 {
587 std::cerr << "\nValidation::expectNotEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
588 }
589#else
590 if constexpr (Log::isSupported<T>())
591 {
592 Log::error() << "Validation::expectNotEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput();
593 }
594 else
595 {
596 Log::error() << "Validation::expectNotEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput();
597 }
598#endif // OCEAN_USE_GTEST
599 }
600 }
601}
602
603template <typename T>
604inline void Validation::expectLess(const T& value0, const T& value1)
605{
606 if (!(value0 < value1))
607 {
609
610 if constexpr (Log::isSupported<T>())
611 {
612 Log::debug() << "Validation::expectLess(" << value0 << ", " << value1 <<") failed at unknown location" << randomGeneratorOutput();
613 }
614 else
615 {
616 Log::debug() << "Validation::expectLess() failed at unknown location" << randomGeneratorOutput();
617 }
618 }
619}
620
621template <typename T>
622inline void Validation::expectLess(const T& value0, const T& value1, const char* file, const int line)
623{
624 if (!(value0 < value1))
625 {
627
628 ocean_assert(file != nullptr);
629 if (file != nullptr)
630 {
631#ifdef OCEAN_USE_GTEST
632 if constexpr (Log::isSupported<T, std::ostream>())
633 {
634 std::cerr << "\nValidation::expectLess(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
635 }
636 else
637 {
638 std::cerr << "\nValidation::expectLess() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
639 }
640#else
641 if constexpr (Log::isSupported<T>())
642 {
643 Log::error() << "Validation::expectLess(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput();
644 }
645 else
646 {
647 Log::error() << "Validation::expectLess() failed in '" << file << "', in line " << line << randomGeneratorOutput();
648 }
649#endif // OCEAN_USE_GTEST
650 }
651 }
652}
653
654template <typename T>
655inline void Validation::expectLessEqual(const T& value0, const T& value1)
656{
657 if (!(value0 <= value1))
658 {
660
661 if constexpr (Log::isSupported<T>())
662 {
663 Log::debug() << "Validation::expectLessEqual(" << value0 << ", " << value1 <<") failed at unknown location" << randomGeneratorOutput();
664 }
665 else
666 {
667 Log::debug() << "Validation::expectLessEqual() failed at unknown location" << randomGeneratorOutput();
668 }
669 }
670}
671
672template <typename T>
673inline void Validation::expectLessEqual(const T& value0, const T& value1, const char* file, const int line)
674{
675 if (!(value0 <= value1))
676 {
678
679 ocean_assert(file != nullptr);
680 if (file != nullptr)
681 {
682#ifdef OCEAN_USE_GTEST
683 if constexpr (Log::isSupported<T, std::ostream>())
684 {
685 std::cerr << "\nValidation::expectLessEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
686 }
687 else
688 {
689 std::cerr << "\nValidation::expectLessEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
690 }
691#else
692 if constexpr (Log::isSupported<T>())
693 {
694 Log::error() << "Validation::expectLessEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput();
695 }
696 else
697 {
698 Log::error() << "Validation::expectLessEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput();
699 }
700#endif // OCEAN_USE_GTEST
701 }
702 }
703}
704
705template <typename T>
706inline void Validation::expectGreater(const T& value0, const T& value1)
707{
708 if (!(value0 > value1))
709 {
711
712 if constexpr (Log::isSupported<T>())
713 {
714 Log::debug() << "Validation::expectGreater(" << value0 << ", " << value1 <<") failed at unknown location" << randomGeneratorOutput();
715 }
716 else
717 {
718 Log::debug() << "Validation::expectGreater() failed at unknown location" << randomGeneratorOutput();
719 }
720 }
721}
722
723template <typename T>
724inline void Validation::expectGreater(const T& value0, const T& value1, const char* file, const int line)
725{
726 if (!(value0 > value1))
727 {
729
730 ocean_assert(file != nullptr);
731 if (file != nullptr)
732 {
733#ifdef OCEAN_USE_GTEST
734 if constexpr (Log::isSupported<T, std::ostream>())
735 {
736 std::cerr << "\nValidation::expectGreater(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
737 }
738 else
739 {
740 std::cerr << "\nValidation::expectGreater() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
741 }
742#else
743 if constexpr (Log::isSupported<T>())
744 {
745 Log::error() << "Validation::expectGreater(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput();
746 }
747 else
748 {
749 Log::error() << "Validation::expectGreater() failed in '" << file << "', in line " << line << randomGeneratorOutput();
750 }
751#endif // OCEAN_USE_GTEST
752 }
753 }
754}
755
756template <typename T>
757inline void Validation::expectGreaterEqual(const T& value0, const T& value1)
758{
759 if (!(value0 >= value1))
760 {
762
763 if constexpr (Log::isSupported<T>())
764 {
765 Log::debug() << "Validation::expectGreaterEqual(" << value0 << ", " << value1 <<") failed at unknown location" << randomGeneratorOutput();
766 }
767 else
768 {
769 Log::debug() << "Validation::expectGreaterEqual() failed at unknown location" << randomGeneratorOutput();
770 }
771 }
772}
773
774template <typename T>
775inline void Validation::expectGreaterEqual(const T& value0, const T& value1, const char* file, const int line)
776{
777 if (!(value0 >= value1))
778 {
780
781 ocean_assert(file != nullptr);
782 if (file != nullptr)
783 {
784#ifdef OCEAN_USE_GTEST
785 if constexpr (Log::isSupported<T, std::ostream>())
786 {
787 std::cerr << "\nValidation::expectGreaterEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
788 }
789 else
790 {
791 std::cerr << "\nValidation::expectGreaterEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
792 }
793#else
794 if constexpr (Log::isSupported<T>())
795 {
796 Log::error() << "Validation::expectGreaterEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput();
797 }
798 else
799 {
800 Log::error() << "Validation::expectGreaterEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput();
801 }
802#endif // OCEAN_USE_GTEST
803 }
804 }
805}
806
807template <typename T>
808inline void Validation::expectInsideRange(const T& lower, const T& value, const T& upper)
809{
810 if (!(lower <= value && value <= upper))
811 {
813
814 if constexpr (Log::isSupported<T>())
815 {
816 Log::debug() << "Validation::expectInsideRange(" << lower << ", " << value << ", " << upper << ") failed at unknown location" << randomGeneratorOutput();
817 }
818 else
819 {
820 Log::debug() << "Validation::expectInsideRange() failed at unknown location" << randomGeneratorOutput();
821 }
822 }
823}
824
825template <typename T>
826inline void Validation::expectInsideRange(const T& lower, const T& value, const T& upper, const char* file, const int line)
827{
828 if (!(lower <= value && value <= upper))
829 {
831
832 ocean_assert(file != nullptr);
833 if (file != nullptr)
834 {
835#ifdef OCEAN_USE_GTEST
836 if constexpr (Log::isSupported<T, std::ostream>())
837 {
838 std::cerr << "\nValidation::expectInsideRange(" << lower << ", " << value << ", " << upper <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
839 }
840 else
841 {
842 std::cerr << "\nValidation::expectInsideRange() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
843 }
844#else
845 if constexpr (Log::isSupported<T>())
846 {
847 Log::error() << "Validation::expectInsideRange(" << lower << ", " << value << ", " << upper << ") failed in '" << file << "', in line " << line << randomGeneratorOutput();
848 }
849 else
850 {
851 Log::error() << "Validation::expectInsideRange() failed in '" << file << "', in line " << line << randomGeneratorOutput();
852 }
853#endif // OCEAN_USE_GTEST
854 }
855 }
856}
857
859{
861
862 Log::debug() << "Validation::setFailed() at unknown location" << randomGeneratorOutput();
863}
864
865inline void Validation::setFailed(const char* file, const int line)
866{
868
869 ocean_assert(file != nullptr);
870 if (file != nullptr)
871 {
872#ifdef OCEAN_USE_GTEST
873 std::cerr << "\nValidation::setFailed() in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
874#else
875 Log::error() << "Validation::setFailed() in '" << file << "', in line " << line << randomGeneratorOutput();
876#endif
877 }
878}
879
880inline bool Validation::succeeded() const
881{
882#ifdef OCEAN_DEBUG
883 succeededChecked_ = true;
884#endif
885
886 return succeeded_;
887}
888
889inline std::string Validation::randomGeneratorOutput() const
890{
891 if (randomGenerator_ != nullptr)
892 {
893 return ", with random generator initial seed '" + String::toAString(randomGenerator_->initialSeed()) + "'";
894 }
895
896 return std::string();
897}
898
900{
901 succeeded_ = false;
902}
903
904inline std::ostream& operator<<(std::ostream& stream, const Validation& validation)
905{
906 if (validation.succeeded())
907 {
908 stream << "succeeded.";
909 }
910 else
911 {
912 stream << "FAILED!";
913 }
914
915 return stream;
916}
917
918template <bool tActive>
919MessageObject<tActive>& operator<<(MessageObject<tActive>& messageObject, const Validation& validation)
920{
921 if (validation.succeeded())
922 {
923 messageObject << "succeeded.";
924 }
925 else
926 {
927 messageObject << "FAILED!";
928 }
929
930 return messageObject;
931}
932
933template <bool tActive>
934MessageObject<tActive>& operator<<(MessageObject<tActive>&& messageObject, const Validation& validation)
935{
936 if (validation.succeeded())
937 {
938 messageObject << "succeeded.";
939 }
940 else
941 {
942 messageObject << "FAILED!";
943 }
944
945 return messageObject;
946}
947
948}
949
950}
951
952#endif // META_OCEAN_TEST_VALIDATION_H
static MessageObject error()
Returns the message for error messages.
Definition Messenger.h:1095
static DebugMessageObject debug()
Returns the message for debug messages.
Definition Messenger.h:1080
Messenger object, one object for each message.
Definition Messenger.h:448
This class implements a generator for random numbers.
Definition RandomGenerator.h:42
unsigned int initialSeed() const
Returns the initial seed value which was used to initialize this random generator.
Definition RandomGenerator.h:192
static std::string toAString(const char value)
Converts a value to a string with 8bit character.
This class implements a helper class to validate tests.
Definition Validation.h:105
bool succeededChecked_
True, if the success state of this validation has been checked.
Definition Validation.h:388
void expectFalse(const bool value)
Informs this validation object that a value is expected to be False.
Definition Validation.h:473
void expectInsideRange(const T &lower, const T &value, const T &upper)
Informs this validation object that a value is expected to be inside a range.
Definition Validation.h:808
~Validation()
Destructs this validation object.
Definition Validation.h:438
RandomGenerator * randomGenerator_
Optional random generator object which will be used during validation.
Definition Validation.h:384
bool succeeded() const
Returns if this validation has succeeded.
Definition Validation.h:880
void expectGreaterEqual(const T &value0, const T &value1)
Informs this validation object that a value is expected to be greater than or equal to another value.
Definition Validation.h:757
void expectLessEqual(const T &value0, const T &value1)
Informs this validation object that a value is expected to be less than or equal to another value.
Definition Validation.h:655
void expectNotEqual(const T &value0, const T &value1)
Informs this validation object that a value is expected to be not equal to another value.
Definition Validation.h:553
void setSucceededFalse()
Sets the succeeded state to false.
Definition Validation.h:899
void expectEqual(const T &value0, const T &value1)
Informs this validation object that a value is expected to be equal to another value.
Definition Validation.h:502
Validation(const Validation &)=delete
Disabled copy constructor.
Validation()=default
Default constructor, by default the verified has succeeded.
void expectTrue(const bool value)
Informs this validation object that a value is expected to be True.
Definition Validation.h:445
void expectLess(const T &value0, const T &value1)
Informs this validation object that a value is expected to be less than another value.
Definition Validation.h:604
std::string randomGeneratorOutput() const
Returns a string containing the random generator's initial seed, if any.
Definition Validation.h:889
void expectGreater(const T &value0, const T &value1)
Informs this validation object that a value is expected to be greater than another value.
Definition Validation.h:706
void setFailed()
Explicitly sets the validation to be failed.
Definition Validation.h:858
bool succeeded_
True, if the validation has succeeded; False, if the validation has failed.
Definition Validation.h:381
std::ostream & operator<<(std::ostream &stream, const Validation &validation)
Definition Validation.h:904
The namespace covering the entire Ocean framework.
Definition Accessor.h:15