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 
29 #include "UT_Functor.h"
30 #include "UT_StopWatch.h"
31 #include "UT_WorkBuffer.h"
32 #include <SYS/SYS_Types.h>
33 
35 {
36 public:
37  virtual ~UT_TestManager() { }
38 
39  /// Access the global test manager
40  static UT_TestManager & get();
41 
42  /// Determine if the test was interactively invoked
43  virtual bool isInteractive() const = 0;
44 
45  /// Determine if the tests are being run for performance
46  virtual bool isPerformance() const = 0;
47 
48  /// Determine the number of threads requested
49  virtual int numThreads() const = 0;
50 
51  /// Called by main() to run the tests. Returns the number of test failures.
52  virtual int run(int argc, char *argv[]) = 0;
53 
54  /// Called by the TEST_REGISTER*() macros to automatically add a new test
55  /// case
56  virtual void addTest(const char *name,
57  UT_Functor<bool> &callback) = 0;
58 
59  /// Allow for logging of intermediate test results. If logging is turned
60  /// on in the test manager, the test name, status and run time will be
61  /// saved.
62  virtual void logResult(const char *name, bool success,
63  fpreal64 run_time) = 0;
64 };
65 
66 /// @brief Status maintainer used to run a test.
67 ///
68 /// This class simplifies the book-keeping when running a test. The unit test
69 /// will automatically log information (i.e. performance, memory) if the
70 /// UT_TestManager is so configured.
71 ///
72 /// Example: @code
73 /// static bool
74 /// unitTest(int argument)
75 /// {
76 /// UT_TestUnit status("UnitTest: %d", argument);
77 ///
78 /// if (!doTest(...))
79 /// return status.fail("Test failed: %s", some_string);
80 ///
81 /// return status.ok();
82 /// }
83 /// @endcode
85 {
86 public:
87  UT_TestUnit(const char *format, ...) SYS_PRINTF_CHECK_ATTRIBUTE(2, 3);
88  ~UT_TestUnit();
89 
90  /// Print out the message given by a format. This includes the test name,
91  /// and other information. No newline should be included in the format.
92  ///
93  /// The function @b always returns @b false.
94  bool fail(const char *format=NULL, ...)
96 
97  /// Print out the message given by a format. This includes the test name,
98  /// and other information. No newline should be included in the format.
99  ///
100  /// The function @b always returns @b true.
101  bool ok(const char *format=NULL, ...)
103 
104  /// Restart the timer. If format is non-NULL, then it will print the time
105  /// as well from when the timer last started. Note that the timer is
106  /// automatically started in the constructor.
107  void restartTimer(const char *format=NULL, ...)
109 
110  /// Enable printing of success/failure when this unit test is destroyed.
111  void setAlwaysPrint(bool f) { myAlwaysPrint = f; }
112 
113  /// Query the status of the test
114  bool getStatus() const { return myStatus; }
115  bool setStatus(bool b) { myStatus = b; return myStatus; }
116 private:
117  UT_WorkBuffer myName; // Test-name
118  UT_Timer myTimer;
119  bool myStatus;
120  bool myPrint;
121  bool myAlwaysPrint;
122 };
123 
124 /// Use this define to log information into the result table
125 #define TEST_LOGRESULT(NAME, SUCCESS, TIME) \
126  UT_TestManager::get().logResult(NAME, SUCCESS, TIME)
127 
128 /// Use this macro after your test function is declared. It will register
129 /// your method with the test manager. Your method should return true if the
130 /// test succeeded, false otherwise. The name is the label that will be used
131 /// for invoking this specific test.
132 #define TEST_REGISTER_FN(name, method) \
133  class method##Registrar \
134  { \
135  public: \
136  method##Registrar() \
137  { \
138  UT_Functor<bool> callback(&method); \
139  UT_TestManager::get().addTest(name, callback); \
140  } \
141  }; \
142  static method##Registrar the##method##Registrar;
143 
144 /// Use this macro after your test class is declared. It will create a static
145 /// instance of your class that then registers your 'bool classname::method()'
146 /// with the test manager. Your method should return true if the test
147 /// succeeded, false otherwise. The name is the label that will be used for
148 /// invoking this specific test.
149 #define TEST_REGISTER(name, classname, method) \
150  class classname##method##Registrar \
151  { \
152  public: \
153  classname##method##Registrar() \
154  { \
155  UT_Functor<bool> callback(&myTest, \
156  &classname::method); \
157  UT_TestManager::get().addTest(name, callback); \
158  } \
159  private: \
160  classname myTest; \
161  }; \
162  static classname##method##Registrar \
163  the##classname##method##Registrar;
164 
165 #endif // __UT_TESTMANAGER_H__
bool bool void void setAlwaysPrint(bool f)
Enable printing of success/failure when this unit test is destroyed.
Status maintainer used to run a test.
#define UT_API
Definition: UT_API.h:13
GLfloat f
Definition: glcorearb.h:1925
double fpreal64
Definition: SYS_Types.h:192
#define SYS_PRINTF_CHECK_ATTRIBUTE(string_index, first_to_check)
Definition: SYS_Types.h:426
virtual ~UT_TestManager()
GLuint const GLchar * name
Definition: glcorearb.h:785
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
bool getStatus() const
Query the status of the test.
bool setStatus(bool b)
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:107