Ocean
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 
15 namespace Ocean
16 {
17 
18 namespace 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 less than another value.
189  * In case 'value0 < value1' is false, the 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 expectLess(const T& value0, const T& value1);
197 
198  /**
199  * Informs this validation object that a value is expected to be less than another value.
200  * In case 'value0 < value1' is false, the 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 expectLess(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 or equal to 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 expectLessEqual(const T& value0, const T& value1);
222 
223  /**
224  * Informs this validation object that a value is expected to be less than or equal to 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 expectLessEqual(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 greater than 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 expectGreater(const T& value0, const T& value1);
247 
248  /**
249  * Informs this validation object that a value is expected to be greater than 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 expectGreater(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 or equal to 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 expectGreaterEqual(const T& value0, const T& value1);
272 
273  /**
274  * Informs this validation object that a value is expected to be greater than or equal to 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 expectGreaterEqual(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 inside a range.
289  * In case 'lower <= value && value <= upper' is false, the validation object will not succeed.
290  * @param lower The lower bound of the range (inclusive)
291  * @param value The value to be checked
292  * @param upper The upper bound of the range (inclusive)
293  * @see succeeded().
294  * @tparam T The data type of the value
295  */
296  template <typename T>
297  inline void expectInsideRange(const T& lower, const T& value, const T& upper);
298 
299  /**
300  * Informs this validation object that a value is expected to be inside a range.
301  * In case 'lower <= value && value <= upper' is false, the validation object will not succeed.
302  * This function will also write a message to the error log.
303  * @param lower The lower bound of the range (inclusive)
304  * @param value The value to be checked
305  * @param upper The upper bound of the range (inclusive)
306  * @param file The source file in which the function call happens, e.g., __FILE__, must be valid
307  * @param line The line in the source file in which the function call happens, e.g., __LINE__, must be valid
308  * @see succeeded().
309  * @tparam T The data type of the value
310  */
311  template <typename T>
312  inline void expectInsideRange(const T& lower, const T& value, const T& upper, const char* file, const int line);
313 
314  /**
315  * Explicitly sets the validation to be failed.
316  * @see succeeded().
317  */
318  inline void setFailed();
319 
320  /**
321  * Explicitly sets the validation to be failed.
322  * This function will also write a message to the error log.
323  * @param file The source file in which the function call happens, e.g., __FILE__, must be valid
324  * @param line The line in the source file in which the function call happens, e.g., __LINE__, must be valid
325  * @see succeeded().
326  */
327  inline void setFailed(const char* file, const int line);
328 
329  /**
330  * Returns if this validation has succeeded.
331  * @return True, if so; False, if the validation has failed
332  */
333  [[nodiscard]] inline bool succeeded() const;
334 
335  /**
336  * Returns a string containing the random generator's initial seed, if any
337  * @return The string with initial seed test, empty if no random generator is associated with this validation object
338  */
339  inline std::string randomGeneratorOutput() const;
340 
341  protected:
342 
343  /**
344  * Disabled copy constructor.
345  */
346  Validation(const Validation&) = delete;
347 
348  /**
349  * Sets the succeeded state to false.
350  */
351  inline void setSucceededFalse();
352 
353  protected:
354 
355  /// True, if the validation has succeeded; False, if the validation has failed.
356  bool succeeded_ = true;
357 
358  /// Optional random generator object which will be used during validation.
360 
361 #ifdef OCEAN_DEBUG
362  /// True, if the success state of this validation has been checked.
363  mutable bool succeededChecked_ = false;
364 #endif // OCEAN_DEBUG
365 };
366 
367 #ifndef OCEAN_EXPECT_TRUE
368  #define OCEAN_EXPECT_TRUE(validation, value) validation.expectTrue(value, __FILE__, __LINE__)
369 #endif
370 
371 #ifndef OCEAN_EXPECT_FALSE
372  #define OCEAN_EXPECT_FALSE(validation, value) validation.expectFalse(value, __FILE__, __LINE__)
373 #endif
374 
375 #ifndef OCEAN_EXPECT_EQUAL
376  #define OCEAN_EXPECT_EQUAL(validation, value0, value1) validation.expectEqual(value0, value1, __FILE__, __LINE__)
377 #endif
378 
379 #ifndef OCEAN_EXPECT_LESS
380  #define OCEAN_EXPECT_LESS(validation, value0, value1) validation.expectLess(value0, value1, __FILE__, __LINE__)
381 #endif
382 
383 #ifndef OCEAN_EXPECT_LESS_EQUAL
384  #define OCEAN_EXPECT_LESS_EQUAL(validation, value0, value1) validation.expectLessEqual(value0, value1, __FILE__, __LINE__)
385 #endif
386 
387 #ifndef OCEAN_EXPECT_GREATER
388  #define OCEAN_EXPECT_GREATER(validation, value0, value1) validation.expectGreater(value0, value1, __FILE__, __LINE__)
389 #endif
390 
391 #ifndef OCEAN_EXPECT_GREATER_EQUAL
392  #define OCEAN_EXPECT_GREATER_EQUAL(validation, value0, value1) validation.expectGreaterEqual(value0, value1, __FILE__, __LINE__)
393 #endif
394 
395 #ifndef OCEAN_EXPECT_INSIDE_RANGE
396  #define OCEAN_EXPECT_INSIDE_RANGE(validation, lower, value, upper) validation.expectInsideRange(lower, value, upper, __FILE__, __LINE__)
397 #endif
398 
399 #ifndef OCEAN_SET_FAILED
400  #define OCEAN_SET_FAILED(validation) validation.setFailed(__FILE__, __LINE__);
401 #endif
402 
403 inline Validation::Validation(RandomGenerator& randomGenerator) :
404  randomGenerator_(&randomGenerator)
405 {
406  // nothing to do here
407 }
408 
410 {
411 #ifdef OCEAN_DEBUG
412  ocean_assert(succeededChecked_ && "The validation has not been check for success");
413 #endif
414 }
415 
416 inline void Validation::expectTrue(const bool value)
417 {
418  if (!value)
419  {
421 
422  Log::debug() << "Validation::expectTrue() failed at unknown location" << randomGeneratorOutput();
423  }
424 }
425 
426 inline void Validation::expectTrue(const bool value, const char* file, const int line)
427 {
428  if (!value)
429  {
431 
432  ocean_assert(file != nullptr);
433  if (file != nullptr)
434  {
435 #ifdef OCEAN_USE_GTEST
436  std::cerr << "\nValidation::expectTrue() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
437 #else
438  Log::error() << "Validation::expectTrue() failed in '" << file << "', in line " << line << randomGeneratorOutput();
439 #endif
440  }
441  }
442 }
443 
444 inline void Validation::expectFalse(const bool value)
445 {
446  if (value)
447  {
449 
450  Log::debug() << "Validation::expectFalse() failed at unknown location" << randomGeneratorOutput();
451  }
452 }
453 
454 inline void Validation::expectFalse(const bool value, const char* file, const int line)
455 {
456  if (value)
457  {
459 
460  ocean_assert(file != nullptr);
461  if (file != nullptr)
462  {
463 #ifdef OCEAN_USE_GTEST
464  std::cerr << "\nValidation::expectFalse() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
465 #else
466  Log::error() << "Validation::expectFalse() failed in '" << file << "', in line " << line << randomGeneratorOutput();
467 #endif
468  }
469  }
470 }
471 
472 template <typename T>
473 inline void Validation::expectEqual(const T& value0, const T& value1)
474 {
475  if (value0 != value1)
476  {
478 
479  if constexpr (Log::isSupported<T>())
480  {
481  Log::debug() << "Validation::expectEqual(" << value0 << ", " << value1 <<") failed at unknown location" << randomGeneratorOutput();
482  }
483  else
484  {
485  Log::debug() << "Validation::expectEqual() failed at unknown location" << randomGeneratorOutput();
486  }
487  }
488 }
489 
490 template <typename T>
491 inline void Validation::expectEqual(const T& value0, const T& value1, const char* file, const int line)
492 {
493  if (value0 != value1)
494  {
496 
497  ocean_assert(file != nullptr);
498  if (file != nullptr)
499  {
500 #ifdef OCEAN_USE_GTEST
501  if constexpr (Log::isSupported<T, std::ostream>())
502  {
503  std::cerr << "\nValidation::expectEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
504  }
505  else
506  {
507  std::cerr << "\nValidation::expectEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
508  }
509 #else
510  if constexpr (Log::isSupported<T>())
511  {
512  Log::error() << "Validation::expectEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput();
513  }
514  else
515  {
516  Log::error() << "Validation::expectEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput();
517  }
518 #endif // OCEAN_USE_GTEST
519  }
520  }
521 }
522 
523 template <typename T>
524 inline void Validation::expectLess(const T& value0, const T& value1)
525 {
526  if (!(value0 < value1))
527  {
529 
530  if constexpr (Log::isSupported<T>())
531  {
532  Log::debug() << "Validation::expectLess(" << value0 << ", " << value1 <<") failed at unknown location" << randomGeneratorOutput();
533  }
534  else
535  {
536  Log::debug() << "Validation::expectLess() failed at unknown location" << randomGeneratorOutput();
537  }
538  }
539 }
540 
541 template <typename T>
542 inline void Validation::expectLess(const T& value0, const T& value1, const char* file, const int line)
543 {
544  if (!(value0 < value1))
545  {
547 
548  ocean_assert(file != nullptr);
549  if (file != nullptr)
550  {
551 #ifdef OCEAN_USE_GTEST
552  if constexpr (Log::isSupported<T, std::ostream>())
553  {
554  std::cerr << "\nValidation::expectLess(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
555  }
556  else
557  {
558  std::cerr << "\nValidation::expectLess() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
559  }
560 #else
561  if constexpr (Log::isSupported<T>())
562  {
563  Log::error() << "Validation::expectLess(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput();
564  }
565  else
566  {
567  Log::error() << "Validation::expectLess() failed in '" << file << "', in line " << line << randomGeneratorOutput();
568  }
569 #endif // OCEAN_USE_GTEST
570  }
571  }
572 }
573 
574 template <typename T>
575 inline void Validation::expectLessEqual(const T& value0, const T& value1)
576 {
577  if (!(value0 <= value1))
578  {
580 
581  if constexpr (Log::isSupported<T>())
582  {
583  Log::debug() << "Validation::expectLessEqual(" << value0 << ", " << value1 <<") failed at unknown location" << randomGeneratorOutput();
584  }
585  else
586  {
587  Log::debug() << "Validation::expectLessEqual() failed at unknown location" << randomGeneratorOutput();
588  }
589  }
590 }
591 
592 template <typename T>
593 inline void Validation::expectLessEqual(const T& value0, const T& value1, const char* file, const int line)
594 {
595  if (!(value0 <= value1))
596  {
598 
599  ocean_assert(file != nullptr);
600  if (file != nullptr)
601  {
602 #ifdef OCEAN_USE_GTEST
603  if constexpr (Log::isSupported<T, std::ostream>())
604  {
605  std::cerr << "\nValidation::expectLessEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
606  }
607  else
608  {
609  std::cerr << "\nValidation::expectLessEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
610  }
611 #else
612  if constexpr (Log::isSupported<T>())
613  {
614  Log::error() << "Validation::expectLessEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput();
615  }
616  else
617  {
618  Log::error() << "Validation::expectLessEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput();
619  }
620 #endif // OCEAN_USE_GTEST
621  }
622  }
623 }
624 
625 template <typename T>
626 inline void Validation::expectGreater(const T& value0, const T& value1)
627 {
628  if (!(value0 > value1))
629  {
631 
632  if constexpr (Log::isSupported<T>())
633  {
634  Log::debug() << "Validation::expectGreater(" << value0 << ", " << value1 <<") failed at unknown location" << randomGeneratorOutput();
635  }
636  else
637  {
638  Log::debug() << "Validation::expectGreater() failed at unknown location" << randomGeneratorOutput();
639  }
640  }
641 }
642 
643 template <typename T>
644 inline void Validation::expectGreater(const T& value0, const T& value1, const char* file, const int line)
645 {
646  if (!(value0 > value1))
647  {
649 
650  ocean_assert(file != nullptr);
651  if (file != nullptr)
652  {
653 #ifdef OCEAN_USE_GTEST
654  if constexpr (Log::isSupported<T, std::ostream>())
655  {
656  std::cerr << "\nValidation::expectGreater(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
657  }
658  else
659  {
660  std::cerr << "\nValidation::expectGreater() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
661  }
662 #else
663  if constexpr (Log::isSupported<T>())
664  {
665  Log::error() << "Validation::expectGreater(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput();
666  }
667  else
668  {
669  Log::error() << "Validation::expectGreater() failed in '" << file << "', in line " << line << randomGeneratorOutput();
670  }
671 #endif // OCEAN_USE_GTEST
672  }
673  }
674 }
675 
676 template <typename T>
677 inline void Validation::expectGreaterEqual(const T& value0, const T& value1)
678 {
679  if (!(value0 >= value1))
680  {
682 
683  if constexpr (Log::isSupported<T>())
684  {
685  Log::debug() << "Validation::expectGreaterEqual(" << value0 << ", " << value1 <<") failed at unknown location" << randomGeneratorOutput();
686  }
687  else
688  {
689  Log::debug() << "Validation::expectGreaterEqual() failed at unknown location" << randomGeneratorOutput();
690  }
691  }
692 }
693 
694 template <typename T>
695 inline void Validation::expectGreaterEqual(const T& value0, const T& value1, const char* file, const int line)
696 {
697  if (!(value0 >= value1))
698  {
700 
701  ocean_assert(file != nullptr);
702  if (file != nullptr)
703  {
704 #ifdef OCEAN_USE_GTEST
705  if constexpr (Log::isSupported<T, std::ostream>())
706  {
707  std::cerr << "\nValidation::expectGreaterEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
708  }
709  else
710  {
711  std::cerr << "\nValidation::expectGreaterEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
712  }
713 #else
714  if constexpr (Log::isSupported<T>())
715  {
716  Log::error() << "Validation::expectGreaterEqual(" << value0 << ", " << value1 <<") failed in '" << file << "', in line " << line << randomGeneratorOutput();
717  }
718  else
719  {
720  Log::error() << "Validation::expectGreaterEqual() failed in '" << file << "', in line " << line << randomGeneratorOutput();
721  }
722 #endif // OCEAN_USE_GTEST
723  }
724  }
725 }
726 
727 template <typename T>
728 inline void Validation::expectInsideRange(const T& lower, const T& value, const T& upper)
729 {
730  if (!(lower <= value && value <= upper))
731  {
733 
734  if constexpr (Log::isSupported<T>())
735  {
736  Log::debug() << "Validation::expectInsideRange(" << lower << ", " << value << ", " << upper << ") failed at unknown location" << randomGeneratorOutput();
737  }
738  else
739  {
740  Log::debug() << "Validation::expectInsideRange() failed at unknown location" << randomGeneratorOutput();
741  }
742  }
743 }
744 
745 template <typename T>
746 inline void Validation::expectInsideRange(const T& lower, const T& value, const T& upper, const char* file, const int line)
747 {
748  if (!(lower <= value && value <= upper))
749  {
751 
752  ocean_assert(file != nullptr);
753  if (file != nullptr)
754  {
755 #ifdef OCEAN_USE_GTEST
756  if constexpr (Log::isSupported<T, std::ostream>())
757  {
758  std::cerr << "\nValidation::expectInsideRange(" << lower << ", " << value << ", " << upper <<") failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
759  }
760  else
761  {
762  std::cerr << "\nValidation::expectInsideRange() failed in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
763  }
764 #else
765  if constexpr (Log::isSupported<T>())
766  {
767  Log::error() << "Validation::expectInsideRange(" << lower << ", " << value << ", " << upper << ") failed in '" << file << "', in line " << line << randomGeneratorOutput();
768  }
769  else
770  {
771  Log::error() << "Validation::expectInsideRange() failed in '" << file << "', in line " << line << randomGeneratorOutput();
772  }
773 #endif // OCEAN_USE_GTEST
774  }
775  }
776 }
777 
779 {
781 
782  Log::debug() << "Validation::setFailed() at unknown location" << randomGeneratorOutput();
783 }
784 
785 inline void Validation::setFailed(const char* file, const int line)
786 {
788 
789  ocean_assert(file != nullptr);
790  if (file != nullptr)
791  {
792 #ifdef OCEAN_USE_GTEST
793  std::cerr << "\nValidation::setFailed() in '" << file << "', in line " << line << randomGeneratorOutput() << "\n" << std::endl;
794 #else
795  Log::error() << "Validation::setFailed() in '" << file << "', in line " << line << randomGeneratorOutput();
796 #endif
797  }
798 }
799 
800 inline bool Validation::succeeded() const
801 {
802 #ifdef OCEAN_DEBUG
803  succeededChecked_ = true;
804 #endif
805 
806  return succeeded_;
807 }
808 
809 inline std::string Validation::randomGeneratorOutput() const
810 {
811  if (randomGenerator_ != nullptr)
812  {
813  return ", with random generator initial seed '" + String::toAString(randomGenerator_->initialSeed()) + "'";
814  }
815 
816  return std::string();
817 }
818 
820 {
821  succeeded_ = false;
822 }
823 
824 inline std::ostream& operator<<(std::ostream& stream, const Validation& validation)
825 {
826  if (validation.succeeded())
827  {
828  stream << "succeeded.";
829  }
830  else
831  {
832  stream << "FAILED!";
833  }
834 
835  return stream;
836 }
837 
838 template <bool tActive>
839 MessageObject<tActive>& operator<<(MessageObject<tActive>& messageObject, const Validation& validation)
840 {
841  if (validation.succeeded())
842  {
843  messageObject << "succeeded.";
844  }
845  else
846  {
847  messageObject << "FAILED!";
848  }
849 
850  return messageObject;
851 }
852 
853 template <bool tActive>
854 MessageObject<tActive>& operator<<(MessageObject<tActive>&& messageObject, const Validation& validation)
855 {
856  if (validation.succeeded())
857  {
858  messageObject << "succeeded.";
859  }
860  else
861  {
862  messageObject << "FAILED!";
863  }
864 
865  return messageObject;
866 }
867 
868 }
869 
870 }
871 
872 #endif // META_OCEAN_TEST_VALIDATION_H
static MessageObject error()
Returns the message for error messages.
Definition: Messenger.h:1074
static DebugMessageObject debug()
Returns the message for debug messages.
Definition: Messenger.h:1059
Messenger object, one object for each message.
Definition: Messenger.h:427
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:178
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:363
void expectFalse(const bool value)
Informs this validation object that a value is expected to be False.
Definition: Validation.h:444
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:728
~Validation()
Destructs this validation object.
Definition: Validation.h:409
RandomGenerator * randomGenerator_
Optional random generator object which will be used during validation.
Definition: Validation.h:359
bool succeeded() const
Returns if this validation has succeeded.
Definition: Validation.h:800
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:677
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:575
void setSucceededFalse()
Sets the succeeded state to false.
Definition: Validation.h:819
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:473
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:416
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:524
std::string randomGeneratorOutput() const
Returns a string containing the random generator's initial seed, if any.
Definition: Validation.h:809
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:626
void setFailed()
Explicitly sets the validation to be failed.
Definition: Validation.h:778
bool succeeded_
True, if the validation has succeeded; False, if the validation has failed.
Definition: Validation.h:356
std::ostream & operator<<(std::ostream &stream, const Validation &validation)
Definition: Validation.h:824
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15