Free Electron
UnitTest.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #define UNIT_START() fe::UnitTest unitTest;
8 #define UNIT_TEST(expression) unitTest(expression,#expression)
9 #define UNIT_TRACK(number) unitTest.track(number);
10 #define UNIT_RETURN() return unitTest.failures();
11 
12 namespace fe
13 {
14 
15 /**************************************************************************//**
16  @brief Counts success and failures in a style similar to assertions
17 
18  @ingroup platform
19 
20  Instantiating the object starts unit testing for the scope.
21 
22  Upon destruction, a report is sent to the global logger.
23 
24  The block contains any number of tests.
25 
26  By convention, a stand-alone unit test executable should return the
27  number of failures and the exit status.
28 
29  @code
30  main()
31  {
32  fe::UnitTest unitTest;
33 
34  unitTest(TRUE,"TRUE is non-zero");
35 
36  unitTest.track(1);
37  return unitTest.failures();
38  }
39  @endcode
40 
41  For convenience a set of macros is provided:
42  @code
43  main()
44  {
45  UNIT_START();
46 
47  UNIT_TEST(TRUE);
48 
49  UNIT_TRACK(1);
50  UNIT_RETURN();
51  }
52  @endcode
53 
54 
55 *//***************************************************************************/
56 class FE_DL_PUBLIC UnitTest
57 {
58  public:
59  UnitTest(void):
60  m_successes(0),
61  m_failures(0),
62  m_attempts(0)
63 
64  {
65  //feRegisterSegvHandler();
66  }
67 virtual ~UnitTest(void)
68  {
69  feLogDirect("\nResults: %d tests ",m_attempts);
70  if(m_failures > 0)
71  {
72  feLogDirect("%d FAILED",m_failures);
73  }
74  else
75  {
76  feLogDirect("ALL PASSED");
77  }
78  feLogDirect("\n\n");
79  }
80  /// @brief Checks the "assertion" and log a message if it fails
81 virtual void operator()(bool success, const char *label="")
82  {
83  if(success)
84  {
85  m_successes++;
86  }
87  else
88  {
89  feLogDirect("UnitTest(%s) failed\n",label);
90  m_failures++;
91  }
92  m_attempts++;
93  }
94 
95  /// @brief Return the number of failures so far
96  int failures(void) { return m_failures; }
97 
98  /** @brief Compare the number of tests run to a reference
99 
100  Returns true if the numbers matched. */
101  int track(int expected)
102  {
103  BWORD equal=true;
104 
105  if(m_attempts!=expected)
106  {
107  feLogDirect("\nWarning: Test count inconsistent!"
108  " Expected %d, but executed %d.",
109  expected,m_attempts);
110  equal=false ;
111  }
112 
113  feLogDirect("\n");
114 
115  return equal;
116  }
117 
118  private:
119  int m_successes;
120  int m_failures;
121  int m_attempts;
122 };
123 
124 } // namespace
kernel
Definition: namespace.dox:3
int track(int expected)
Compare the number of tests run to a reference.
Definition: UnitTest.h:101
virtual void operator()(bool success, const char *label="")
Checks the "assertion" and log a message if it fails.
Definition: UnitTest.h:81
Counts success and failures in a style similar to assertions.
Definition: UnitTest.h:56
int failures(void)
Return the number of failures so far.
Definition: UnitTest.h:96