HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_TestManager.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_TestManager.h (C++)
7  *
8  * COMMENTS: This class provides a test harness framework.
9  *
10  * USAGE:
11  * 1. Create a main() method for your test application like so:
12  * int main(int argc, char *argv[])
13  * {
14  * return UT_TestManager::get().run(argc, argv);
15  * }
16  * The return code will be the number of test failures.
17  * To obtain the command line usage, use "testapp -h".
18  *
19  * 2. For each test case, use one of the TEST_REGISTER*() macros for
20  * registering a test function. The function should return true if the
21  * test passed.
22  */
23 
24 #ifndef __UT_TESTMANAGER_H__
25 #define __UT_TESTMANAGER_H__
26 
27 #include "UT_API.h"
28 #include "UT_Functor.h"
29 #include "UT_Main.h"
30 #include "UT_NonCopyable.h"
31 #include "UT_StopWatch.h"
32 #include "UT_WorkArgs.h"
33 #include "UT_WorkBuffer.h"
34 #include <SYS/SYS_Types.h>
35 
37 {
38 public:
40  virtual ~UT_TestManager() { }
41 
43 
44  /// Access the global test manager
45  static UT_TestManager & get();
46 
47  /// Determine if the test was interactively invoked
48  virtual bool isInteractive() const = 0;
49 
50  /// Determine if the tests are being run for performance
51  virtual bool isPerformance() const = 0;
52 
53  /// Determine the number of threads requested
54  virtual int numThreads() const = 0;
55 
56  /// Called by main() to run the tests. Returns the number of test failures.
57  virtual int run(int argc, char *argv[]) = 0;
58 
59  /// Called by the TEST_REGISTER*() macros to automatically add a new test
60  /// case
61  virtual void addTest(const char *name,
62  UT_Functor<bool> &callback) = 0;
63 
64  /// Allow for logging of intermediate test results. If logging is turned
65  /// on in the test manager, the test name, status and run time will be
66  /// saved.
67  virtual void logResult(const char *name, bool success,
68  fpreal64 run_time) = 0;
69 
70  /// Get the arguments passed into this program.
71  virtual const UT_WorkArgs& getArgs() const = 0;
72 };
73 
74 /// @brief Status maintainer used to run a test.
75 ///
76 /// This class simplifies the book-keeping when running a test. The unit test
77 /// will automatically log information (i.e. performance, memory) if the
78 /// UT_TestManager is so configured.
79 ///
80 /// Example 1: @code
81 /// static bool
82 /// unitTest(int argument)
83 /// {
84 /// UT_TestUnit status("UnitTest: %d", argument);
85 ///
86 /// if (!doTest(...))
87 /// return status.fail("Test failed: %s", some_string);
88 ///
89 /// return status.ok();
90 /// }
91 /// @endcode
92 ///
93 /// Example 2: @code
94 /// static bool
95 /// unitTest(int argument)
96 /// {
97 /// UT_TestUnit unit(UT_TestUnit::OK, "UnitTest: %d", argument);
98 ///
99 /// if (!doTest1(...))
100 /// unit.fail("Test 1 failed: %s", some_string);
101 /// if (!doTest2(...))
102 /// unit.fail("Test 2 failed: %s", some_string);
103 ///
104 /// return unit.status();
105 /// }
106 /// @endcode
108 {
109 public:
110  enum InitEnum { OK, FAIL };
111 
112  UT_TestUnit(const char *format, ...) SYS_PRINTF_CHECK_ATTRIBUTE(2, 3);
113  UT_TestUnit(InitEnum init_enum, const char *format, ...)
115  ~UT_TestUnit();
116 
117  /// Print out the message given by a format. This includes the test name,
118  /// and other information. No newline should be included in the format.
119  ///
120  /// The function @b always returns @b false.
121  bool fail(const char *format=NULL, ...)
122  SYS_PRINTF_CHECK_ATTRIBUTE(2, 3);
123 
124  /// Print out the message given by a format. This includes the test name,
125  /// and other information. No newline should be included in the format.
126  ///
127  /// The function @b always returns @b true.
128  bool ok(const char *format=NULL, ...)
129  SYS_PRINTF_CHECK_ATTRIBUTE(2, 3);
130 
131  /// Restart the timer. If format is non-NULL, then it will print the time
132  /// as well from when the timer last started. Note that the timer is
133  /// automatically started in the constructor.
134  void restartTimer(const char *format=NULL, ...)
135  SYS_PRINTF_CHECK_ATTRIBUTE(2, 3);
136 
137  /// Enable printing of success/failure when this unit test is destroyed.
138  void setAlwaysPrint(bool f) { myAlwaysPrint = f; }
139 
140  /// Query the status of the test
141  bool status() const { return myStatus; }
142  bool getStatus() const { return myStatus; }
143  bool setStatus(bool b) { myStatus = b; return myStatus; }
144 private:
145  UT_WorkBuffer myName; // Test-name
146  UT_Timer myTimer;
147  bool myStatus;
148  bool myPrint;
149  bool myAlwaysPrint;
150 };
151 
152 /// Use this define to log information into the result table
153 #define TEST_LOGRESULT(NAME, SUCCESS, TIME) \
154  UT_TestManager::get().logResult(NAME, SUCCESS, TIME)
155 
156 /// Use this macro after your test function is declared. It will register
157 /// your method with the test manager. Your method should return true if the
158 /// test succeeded, false otherwise. The name is the label that will be used
159 /// for invoking this specific test.
160 #define TEST_REGISTER_FN(name, method) \
161  class method##Registrar \
162  { \
163  public: \
164  method##Registrar() \
165  { \
166  UT_Functor<bool> callback(&method); \
167  UT_TestManager::get().addTest(name, callback); \
168  } \
169  }; \
170  static method##Registrar the##method##Registrar;
171 
172 /// Use this macro after your test class is declared. It will create a static
173 /// instance of your class that then registers your 'bool classname::method()'
174 /// with the test manager. Your method should return true if the test
175 /// succeeded, false otherwise. The name is the label that will be used for
176 /// invoking this specific test.
177 #define TEST_REGISTER(name, classname, method) \
178  class classname##method##Registrar \
179  { \
180  public: \
181  classname##method##Registrar() \
182  { \
183  UT_Functor<bool> callback(&myTest, \
184  &classname::method); \
185  UT_TestManager::get().addTest(name, callback); \
186  } \
187  private: \
188  classname myTest; \
189  }; \
190  static classname##method##Registrar \
191  the##classname##method##Registrar;
192 
193 #define TEST_UNIT_MAIN() \
194  int theMain(int argc, char *argv[]) \
195  { \
196  /*Ensure we reset the task scheduler to avoid hangning on exit in \
197  * Windows.*/ \
198  UT_Thread::resetNumProcessors(); \
199  return UT_TestManager::get().run(argc, argv); \
200  } \
201  UT_MAIN(theMain);
202 
203 #endif // __UT_TESTMANAGER_H__
Status maintainer used to run a test.
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:623
#define UT_API
Definition: UT_API.h:14
double fpreal64
Definition: SYS_Types.h:201
GLfloat f
Definition: glcorearb.h:1926
#define SYS_PRINTF_CHECK_ATTRIBUTE(string_index, first_to_check)
Definition: SYS_Types.h:447
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
virtual ~UT_TestManager()
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
auto get(const UT_ARTIterator< T > &it) -> decltype(it.key())
Definition: UT_ARTMap.h:1073
GLuint const GLchar * name
Definition: glcorearb.h:786
bool status() const
Query the status of the test.
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
bool getStatus() const
bool setStatus(bool b)