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 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 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 validation has succeeded.
110 */
111 Validation() = default;
112
113 /**
114 * Creates a new validation object associated with a random generator, by default the validation 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 virtual ~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 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 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 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 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 virtual bool succeeded() const;
359
360 /**
361 * Returns whether this validation has succeeded so far.
362 * This function should not be used as a final validation check, but rather as an intermediate check to determine whether expensive or additional tests can be skipped due to an earlier failure.
363 * Use succeeded() as the final validation step.
364 * @return True if no failures have occurred; false if any validation has failed.
365 * @see succeeded()
366 */
367 [[nodiscard]] inline bool succeededSoFar() const;
368
369 /**
370 * Returns a string containing the random generator's initial seed, if any
371 * @return The string with initial seed test, empty if no random generator is associated with this validation object
372 */
373 inline std::string randomGeneratorOutput() const;
374
375 protected:
376
377 /**
378 * Disabled copy constructor.
379 */
380 Validation(const Validation&) = delete;
381
382 /**
383 * Sets the succeeded state to false.
384 */
385 inline void setSucceededFalse();
386
387 protected:
388
389 /// True, if the validation has succeeded; False, if the validation has failed.
390 bool succeeded_ = true;
391
392 /// Optional random generator object which will be used during validation.
394
395#ifdef OCEAN_DEBUG
396 /// True, if the success state of this validation has been checked.
397 mutable bool succeededChecked_ = false;
398#endif // OCEAN_DEBUG
399};
400
401#ifndef OCEAN_EXPECT_TRUE
402 #define OCEAN_EXPECT_TRUE(validation, ...) validation.expectTrue(__VA_ARGS__, __FILE__, __LINE__)
403#endif
404
405#ifndef OCEAN_EXPECT_FALSE
406 #define OCEAN_EXPECT_FALSE(validation, ...) validation.expectFalse(__VA_ARGS__, __FILE__, __LINE__)
407#endif
408
409#ifndef OCEAN_EXPECT_EQUAL
410 #define OCEAN_EXPECT_EQUAL(validation, ...) validation.expectEqual(__VA_ARGS__, __FILE__, __LINE__)
411#endif
412
413#ifndef OCEAN_EXPECT_NOT_EQUAL
414 #define OCEAN_EXPECT_NOT_EQUAL(validation, ...) validation.expectNotEqual(__VA_ARGS__, __FILE__, __LINE__)
415#endif
416
417#ifndef OCEAN_EXPECT_LESS
418 #define OCEAN_EXPECT_LESS(validation, ...) validation.expectLess(__VA_ARGS__, __FILE__, __LINE__)
419#endif
420
421#ifndef OCEAN_EXPECT_LESS_EQUAL
422 #define OCEAN_EXPECT_LESS_EQUAL(validation, ...) validation.expectLessEqual(__VA_ARGS__, __FILE__, __LINE__)
423#endif
424
425#ifndef OCEAN_EXPECT_GREATER
426 #define OCEAN_EXPECT_GREATER(validation, ...) validation.expectGreater(__VA_ARGS__, __FILE__, __LINE__)
427#endif
428
429#ifndef OCEAN_EXPECT_GREATER_EQUAL
430 #define OCEAN_EXPECT_GREATER_EQUAL(validation, ...) validation.expectGreaterEqual(__VA_ARGS__, __FILE__, __LINE__)
431#endif
432
433#ifndef OCEAN_EXPECT_INSIDE_RANGE
434 #define OCEAN_EXPECT_INSIDE_RANGE(validation, ...) validation.expectInsideRange(__VA_ARGS__, __FILE__, __LINE__)
435#endif
436
437#ifndef OCEAN_SET_FAILED
438 #define OCEAN_SET_FAILED(validation) validation.setFailed(__FILE__, __LINE__);
439#endif
440
441inline Validation::Validation(RandomGenerator& randomGenerator) :
442 randomGenerator_(&randomGenerator)
443{
444 // nothing to do here
445}
446
448{
449#ifdef OCEAN_DEBUG
450 ocean_assert(succeededChecked_ && "The validation has not been checked for success");
451#endif
452}
453
454inline void Validation::expectTrue(const bool value)
455{
456 if (!value)
457 {
459
460 Log::debug() << "Validation::expectTrue() failed at unknown location" << randomGeneratorOutput();
461 }
462}
463
464inline void Validation::expectTrue(const bool value, const char* file, const int line)
465{
466 if (!value)
467 {
469
470 ocean_assert(file != nullptr);
471 if (file != nullptr)
472 {
473#ifdef OCEAN_USE_GTEST
474 std::cerr << "\nValidation::expectTrue() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
475#else
476 Log::error() << "Validation::expectTrue() failed in '" << file << "', in line " << line << randomGeneratorOutput();
477#endif
478 }
479 }
480}
481
482inline void Validation::expectFalse(const bool value)
483{
484 if (value)
485 {
487
488 Log::debug() << "Validation::expectFalse() failed at unknown location" << randomGeneratorOutput();
489 }
490}
491
492inline void Validation::expectFalse(const bool value, const char* file, const int line)
493{
494 if (value)
495 {
497
498 ocean_assert(file != nullptr);
499 if (file != nullptr)
500 {
501#ifdef OCEAN_USE_GTEST
502 std::cerr << "\nValidation::expectFalse() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
503#else
504 Log::error() << "Validation::expectFalse() failed in '" << file << "', in line " << line << randomGeneratorOutput();
505#endif
506 }
507 }
508}
509
510template <typename T>
511inline void Validation::expectEqual(const T& value0, const T& value1)
512{
513 if (value0 != value1)
514 {
516
517 if constexpr (Log::isSupported<T>())
518 {
519 Log::debug() << "Validation::expectEqual(" << value0 << ", " << value1 <<") failed at unknown location" << randomGeneratorOutput();
520 }
521 else
522 {
523 Log::debug() << "Validation::expectEqual() failed at unknown location" << randomGeneratorOutput();
524 }
525 }
526}
527
528template <typename T>
529inline void Validation::expectEqual(const T& value0, const T& value1, const char* file, const int line)
530{
531 if (value0 != value1)
532 {
534
535 ocean_assert(file != nullptr);
536 if (file != nullptr)
537 {
538#ifdef OCEAN_USE_GTEST
539 if constexpr (Log::isSupported<T, std::ostream>())
540 {
541 std::cerr << "\nValidation::expectEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
542 }
543 else
544 {
545 std::cerr << "\nValidation::expectEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
546 }
547#else
548 if constexpr (Log::isSupported<T>())
549 {
550 Log::error() << "Validation::expectEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput();
551 }
552 else
553 {
554 Log::error() << "Validation::expectEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput();
555 }
556#endif // OCEAN_USE_GTEST
557 }
558 }
559}
560
561template <typename T>
562inline void Validation::expectNotEqual(const T& value0, const T& value1)
563{
564 if (value0 == value1)
565 {
567
568 if constexpr (Log::isSupported<T>())
569 {
570 Log::debug() << "Validation::expectNotEqual(" << value0 << ", " << value1 <<") failed at unknown location" << randomGeneratorOutput();
571 }
572 else
573 {
574 Log::debug() << "Validation::expectNotEqual() failed at unknown location" << randomGeneratorOutput();
575 }
576 }
577}
578
579template <typename T>
580inline void Validation::expectNotEqual(const T& value0, const T& value1, const char* file, const int line)
581{
582 if (value0 == value1)
583 {
585
586 ocean_assert(file != nullptr);
587 if (file != nullptr)
588 {
589#ifdef OCEAN_USE_GTEST
590 if constexpr (Log::isSupported<T, std::ostream>())
591 {
592 std::cerr << "\nValidation::expectNotEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
593 }
594 else
595 {
596 std::cerr << "\nValidation::expectNotEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
597 }
598#else
599 if constexpr (Log::isSupported<T>())
600 {
601 Log::error() << "Validation::expectNotEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput();
602 }
603 else
604 {
605 Log::error() << "Validation::expectNotEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput();
606 }
607#endif // OCEAN_USE_GTEST
608 }
609 }
610}
611
612template <typename T>
613inline void Validation::expectLess(const T& value0, const T& value1)
614{
615 if (!(value0 < value1))
616 {
618
619 if constexpr (Log::isSupported<T>())
620 {
621 Log::debug() << "Validation::expectLess(" << value0 << ", " << value1 <<") failed at unknown location" << randomGeneratorOutput();
622 }
623 else
624 {
625 Log::debug() << "Validation::expectLess() failed at unknown location" << randomGeneratorOutput();
626 }
627 }
628}
629
630template <typename T>
631inline void Validation::expectLess(const T& value0, const T& value1, const char* file, const int line)
632{
633 if (!(value0 < value1))
634 {
636
637 ocean_assert(file != nullptr);
638 if (file != nullptr)
639 {
640#ifdef OCEAN_USE_GTEST
641 if constexpr (Log::isSupported<T, std::ostream>())
642 {
643 std::cerr << "\nValidation::expectLess(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
644 }
645 else
646 {
647 std::cerr << "\nValidation::expectLess() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
648 }
649#else
650 if constexpr (Log::isSupported<T>())
651 {
652 Log::error() << "Validation::expectLess(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput();
653 }
654 else
655 {
656 Log::error() << "Validation::expectLess() failed in '" << file << "', in line " << line << randomGeneratorOutput();
657 }
658#endif // OCEAN_USE_GTEST
659 }
660 }
661}
662
663template <typename T>
664inline void Validation::expectLessEqual(const T& value0, const T& value1)
665{
666 if (!(value0 <= value1))
667 {
669
670 if constexpr (Log::isSupported<T>())
671 {
672 Log::debug() << "Validation::expectLessEqual(" << value0 << ", " << value1 <<") failed at unknown location" << randomGeneratorOutput();
673 }
674 else
675 {
676 Log::debug() << "Validation::expectLessEqual() failed at unknown location" << randomGeneratorOutput();
677 }
678 }
679}
680
681template <typename T>
682inline void Validation::expectLessEqual(const T& value0, const T& value1, const char* file, const int line)
683{
684 if (!(value0 <= value1))
685 {
687
688 ocean_assert(file != nullptr);
689 if (file != nullptr)
690 {
691#ifdef OCEAN_USE_GTEST
692 if constexpr (Log::isSupported<T, std::ostream>())
693 {
694 std::cerr << "\nValidation::expectLessEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
695 }
696 else
697 {
698 std::cerr << "\nValidation::expectLessEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
699 }
700#else
701 if constexpr (Log::isSupported<T>())
702 {
703 Log::error() << "Validation::expectLessEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput();
704 }
705 else
706 {
707 Log::error() << "Validation::expectLessEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput();
708 }
709#endif // OCEAN_USE_GTEST
710 }
711 }
712}
713
714template <typename T>
715inline void Validation::expectGreater(const T& value0, const T& value1)
716{
717 if (!(value0 > value1))
718 {
720
721 if constexpr (Log::isSupported<T>())
722 {
723 Log::debug() << "Validation::expectGreater(" << value0 << ", " << value1 <<") failed at unknown location" << randomGeneratorOutput();
724 }
725 else
726 {
727 Log::debug() << "Validation::expectGreater() failed at unknown location" << randomGeneratorOutput();
728 }
729 }
730}
731
732template <typename T>
733inline void Validation::expectGreater(const T& value0, const T& value1, const char* file, const int line)
734{
735 if (!(value0 > value1))
736 {
738
739 ocean_assert(file != nullptr);
740 if (file != nullptr)
741 {
742#ifdef OCEAN_USE_GTEST
743 if constexpr (Log::isSupported<T, std::ostream>())
744 {
745 std::cerr << "\nValidation::expectGreater(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
746 }
747 else
748 {
749 std::cerr << "\nValidation::expectGreater() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
750 }
751#else
752 if constexpr (Log::isSupported<T>())
753 {
754 Log::error() << "Validation::expectGreater(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput();
755 }
756 else
757 {
758 Log::error() << "Validation::expectGreater() failed in '" << file << "', in line " << line << randomGeneratorOutput();
759 }
760#endif // OCEAN_USE_GTEST
761 }
762 }
763}
764
765template <typename T>
766inline void Validation::expectGreaterEqual(const T& value0, const T& value1)
767{
768 if (!(value0 >= value1))
769 {
771
772 if constexpr (Log::isSupported<T>())
773 {
774 Log::debug() << "Validation::expectGreaterEqual(" << value0 << ", " << value1 <<") failed at unknown location" << randomGeneratorOutput();
775 }
776 else
777 {
778 Log::debug() << "Validation::expectGreaterEqual() failed at unknown location" << randomGeneratorOutput();
779 }
780 }
781}
782
783template <typename T>
784inline void Validation::expectGreaterEqual(const T& value0, const T& value1, const char* file, const int line)
785{
786 if (!(value0 >= value1))
787 {
789
790 ocean_assert(file != nullptr);
791 if (file != nullptr)
792 {
793#ifdef OCEAN_USE_GTEST
794 if constexpr (Log::isSupported<T, std::ostream>())
795 {
796 std::cerr << "\nValidation::expectGreaterEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
797 }
798 else
799 {
800 std::cerr << "\nValidation::expectGreaterEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
801 }
802#else
803 if constexpr (Log::isSupported<T>())
804 {
805 Log::error() << "Validation::expectGreaterEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput();
806 }
807 else
808 {
809 Log::error() << "Validation::expectGreaterEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput();
810 }
811#endif // OCEAN_USE_GTEST
812 }
813 }
814}
815
816template <typename T>
817inline void Validation::expectInsideRange(const T& lower, const T& value, const T& upper)
818{
819 if (!(lower <= value && value <= upper))
820 {
822
823 if constexpr (Log::isSupported<T>())
824 {
825 Log::debug() << "Validation::expectInsideRange(" << lower << ", " << value << ", " << upper << ") failed at unknown location" << randomGeneratorOutput();
826 }
827 else
828 {
829 Log::debug() << "Validation::expectInsideRange() failed at unknown location" << randomGeneratorOutput();
830 }
831 }
832}
833
834template <typename T>
835inline void Validation::expectInsideRange(const T& lower, const T& value, const T& upper, const char* file, const int line)
836{
837 if (!(lower <= value && value <= upper))
838 {
840
841 ocean_assert(file != nullptr);
842 if (file != nullptr)
843 {
844#ifdef OCEAN_USE_GTEST
845 if constexpr (Log::isSupported<T, std::ostream>())
846 {
847 std::cerr << "\nValidation::expectInsideRange(" << lower << ", " << value << ", " << upper <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
848 }
849 else
850 {
851 std::cerr << "\nValidation::expectInsideRange() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
852 }
853#else
854 if constexpr (Log::isSupported<T>())
855 {
856 Log::error() << "Validation::expectInsideRange(" << lower << ", " << value << ", " << upper << ") failed in '" << file << "', in line " << line << randomGeneratorOutput();
857 }
858 else
859 {
860 Log::error() << "Validation::expectInsideRange() failed in '" << file << "', in line " << line << randomGeneratorOutput();
861 }
862#endif // OCEAN_USE_GTEST
863 }
864 }
865}
866
868{
870
871 Log::debug() << "Validation::setFailed() at unknown location" << randomGeneratorOutput();
872}
873
874inline void Validation::setFailed(const char* file, const int line)
875{
877
878 ocean_assert(file != nullptr);
879 if (file != nullptr)
880 {
881#ifdef OCEAN_USE_GTEST
882 std::cerr << "\nValidation::setFailed() in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
883#else
884 Log::error() << "Validation::setFailed() in '" << file << "', in line " << line << randomGeneratorOutput();
885#endif
886 }
887}
888
889inline bool Validation::succeeded() const
890{
891#ifdef OCEAN_DEBUG
892 succeededChecked_ = true;
893#endif
894
895 return succeeded_;
896}
897
898inline bool Validation::succeededSoFar() const
899{
900 return succeeded_;
901}
902
903inline std::string Validation::randomGeneratorOutput() const
904{
905 if (randomGenerator_ != nullptr)
906 {
907 return ", with random generator initial seed '" + String::toAString(randomGenerator_->initialSeed()) + "'";
908 }
909
910 return std::string();
911}
912
914{
915 succeeded_ = false;
916}
917
918inline std::ostream& operator<<(std::ostream& stream, const Validation& validation)
919{
920 if (validation.succeededSoFar())
921 {
922 stream << "succeeded.";
923 }
924 else
925 {
926 stream << "FAILED!";
927 }
928
929 return stream;
930}
931
932template <bool tActive>
933MessageObject<tActive>& operator<<(MessageObject<tActive>& messageObject, const Validation& validation)
934{
935 if (validation.succeededSoFar())
936 {
937 messageObject << "succeeded.";
938 }
939 else
940 {
941 messageObject << "FAILED!";
942 }
943
944 return messageObject;
945}
946
947template <bool tActive>
948MessageObject<tActive>& operator<<(MessageObject<tActive>&& messageObject, const Validation& validation)
949{
950 if (validation.succeededSoFar())
951 {
952 messageObject << "succeeded.";
953 }
954 else
955 {
956 messageObject << "FAILED!";
957 }
958
959 return messageObject;
960}
961
962}
963
964}
965
966#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:397
void expectFalse(const bool value)
Informs this validation object that a value is expected to be False.
Definition Validation.h:482
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:817
bool succeededSoFar() const
Returns whether this validation has succeeded so far.
Definition Validation.h:898
virtual ~Validation()
Destructs this validation object.
Definition Validation.h:447
RandomGenerator * randomGenerator_
Optional random generator object which will be used during validation.
Definition Validation.h:393
virtual bool succeeded() const
Returns if this validation has succeeded.
Definition Validation.h:889
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:766
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:664
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:562
void setSucceededFalse()
Sets the succeeded state to false.
Definition Validation.h:913
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:511
Validation(const Validation &)=delete
Disabled copy constructor.
Validation()=default
Default constructor, by default the validation has succeeded.
void expectTrue(const bool value)
Informs this validation object that a value is expected to be True.
Definition Validation.h:454
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:613
std::string randomGeneratorOutput() const
Returns a string containing the random generator's initial seed, if any.
Definition Validation.h:903
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:715
void setFailed()
Explicitly sets the validation to be failed.
Definition Validation.h:867
bool succeeded_
True, if the validation has succeeded; False, if the validation has failed.
Definition Validation.h:390
std::ostream & operator<<(std::ostream &stream, const TestResult &testResult)
Writes a test result to a stream.
Definition TestResult.h:200
The namespace covering the entire Ocean framework.
Definition Accessor.h:15