HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
regTest.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_TF_REG_TEST_H
8 #define PXR_BASE_TF_REG_TEST_H
9 
10 /// \file tf/regTest.h
11 /// \ingroup group_tf_Internal
12 /// Support for simple regression tests.
13 
14 #include "pxr/pxr.h"
15 
16 #include "pxr/base/tf/api.h"
17 #include "pxr/base/tf/singleton.h"
18 #include "pxr/base/tf/hash.h"
19 #include "pxr/base/tf/hashmap.h"
20 #include <string>
21 
23 
24 /// \class TfRegTest
25 /// \ingroup group_tf_Internal
26 ///
27 /// \c TfRegTest is a singleton class, which is used to register functions
28 /// with either type \c bool \c (*)(int, char*[]), or functions returning type
29 /// \c bool and taking no arguments.
30 ///
31 /// Here is how \c TfRegTest is used to create tests in separate files, which
32 /// are then compiled into a single standalone executable (and not part of any
33 /// library):
34 ///
35 /// // file: main.cpp
36 /// \include test/main.cpp
37 ///
38 /// // file: hammer.cpp
39 /// \code
40 /// #include "pxr/base/tf/regTest.h"
41 ///
42 /// static bool
43 /// Test_PtHammer()
44 /// {
45 /// bool success;
46 /// ...
47 /// return success;
48 /// }
49 /// TF_ADD_REGTEST(PtHammer);
50 /// \endcode
51 ///
52 /// \code
53 /// // file: drill.cpp
54 /// static bool
55 /// Test_PtDrill(int argc, char *argv[])
56 /// {
57 /// bool success;
58 /// ...
59 /// return success;
60 /// }
61 /// TF_ADD_REGTEST(PtDrill);
62 /// \endcode
63 ///
64 /// When \c main.cpp, \c drill.cpp and \c hammer.cpp are compiled into
65 /// an executable, then a test can be run by invoking the executable
66 /// with a first argument of either \p PtHammer or \p PtDrill.
67 /// Since \p PtHammer is a function without arguments, supplying additional
68 /// arguments is an error; but \p PtDrill takes arguments, so additional
69 /// command-line arguments specified are passed to the function.
70 /// (Most library test functions shouldn't need any arguments.)
71 ///
72 class TfRegTest {
73 public:
74  /// Run a single regression test function, returning 0 if the function
75  /// succeeded and 1 otherwise.
76  ///
77  /// This function is intended to be called as follows:
78  /// \code
79  /// int main(int argc, char *argv[]) {
80  /// return TfRegTest::Main(argc, argv);
81  /// }
82  /// \endcode
83  ///
84  /// The first argument is the name of the test to be run. If the
85  /// registered test function run takes no arguments, then no arguments
86  /// other than the test name should be supplied. Otherwise, the \c Main()
87  /// passes \c argc-1 and \c argv+1 to the test function, and the test
88  /// function is responsible for argument checking.
89  static int Main(int argc, char *argv[]) {
90  return GetInstance()._Main(argc, argv);
91  }
92 
93  TF_API
94  static TfRegTest& GetInstance();
95 
96  /// Type of a function with no arguments.
97  typedef bool (*RegFunc)();
98 
99  /// Type of a function with arguments.
100  ///
101  /// When \c Main(argc,argv) is requested to run a function of type
102  /// \c RegFuncWithArgs, it invokes the function with arguments \c argc-1
103  /// and \c argv+1.
104  typedef bool (*RegFuncWithArgs)(int argc, char *argv[]);
105 
106  TF_API
107  bool Register(const char* name, RegFunc);
108  TF_API
109  bool Register(const char* name, RegFuncWithArgs);
110 
111 private:
112  friend class TfSingleton<TfRegTest>;
113  TF_API
114  int _Main(int argc, char *argv[]);
115 
116  void _PrintTestNames();
117 
120  _Hash _functionTable;
121  _HashWithArgs _functionTableWithArgs;
122 };
123 
125 
126 /// Adds the function Test_\p name, under name \p name, as a runnable
127 /// regression test. Test_\p name must be of type \c RegFunc or
128 /// \c RegFuncWithArgs.
129 ///
130 /// \ingroup group_tf_Internal
131 /// \hideinitializer
132 #define TF_ADD_REGTEST(name) \
133  bool Tf_RegTst##name = TfRegTest::GetInstance().Register(#name, Test_##name)
134 
136 
137 #endif
#define TF_API
Definition: api.h:23
TF_API_TEMPLATE_CLASS(TfSingleton< TfRegTest >)
OutGridT const XformOp bool bool
bool(* RegFunc)()
Type of a function with no arguments.
Definition: regTest.h:97
static TF_API TfRegTest & GetInstance()
GLuint const GLchar * name
Definition: glcorearb.h:786
TF_API bool Register(const char *name, RegFunc)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
static int Main(int argc, char *argv[])
Definition: regTest.h:89
bool(* RegFuncWithArgs)(int argc, char *argv[])
Definition: regTest.h:104