OpendTect  6.6
testprog.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*+
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: A.H. Bril
8  Date: Dec 2013
9 ________________________________________________________________________
10 
11  Single include getting many of the tools you need for test programs.
12 
13  The macro mInitTestProg() will take care of:
14  1) Initialisation of program args
15  2) A file-scope variable 'bool quiet_': whether progress info is required
16  3) A command line parser 'CommandLineParser& clParser()'
17 
18  When the file is included in a lib source, #define __test_lib_source__. To use
19  the macros and inline functions, you'll have to make a variable 'quiet_'
20  (e.g. as class member).
21 
22 -*/
23 
24 #include "commandlineparser.h"
25 #include "genc.h"
26 #include "keystrs.h"
27 #include "debug.h"
28 #include "ptrman.h"
29 #include "od_ostream.h"
30 #include "odruncontext.h"
31 
32 #ifndef __test_lib_source__
33 
34 # ifdef __win__
35 # include "winmain.h"
36 # endif
37 
38 // Kept for ABI, do not use
39 static mUsedVar bool quiet = true;
41 //
42 // Use this in stand-alone test
43 # define mTestMainFnName testMain
44 
45 int testMain(int,char**);
46 
47 # ifndef mMainIsDefined
48 # define mMainIsDefined
49 int main(int argc, char** argv)
50 {
52  ExitProgram( testMain(argc,argv) );
53 }
54 # endif
55 
56 static bool quiet_ mUnusedVar = true;
58 
59 static inline CommandLineParser& clParser()
60 {
61  return *the_testprog_parser_;
62 }
63 
64 
65 # define mRunSubTest( nm ) \
66  tstStream() << "\n\n\n->" << #nm << " subtest\n\n"; \
67  status = test_main_##nm( argc, argv ); \
68  if ( status != 0 ) \
69  return status
70 
71 # define mTestProgInits() \
72  od_init_test_program( argc, argv ); \
73  the_testprog_parser_ = new CommandLineParser; \
74  quiet_ = clParser().hasKey( sKey::Quiet() ); \
75  CommandLineParser& parser mUnusedVar = *the_testprog_parser_; \
76  quiet = quiet_;
77 
78 # define mInitCompositeTestProg(mod) \
79  mTestProgInits(); \
80  tstStream() << "** '" << #mod << "' composite test\n\n"; \
81  int status
82 
83 #define mExitTestProg( var )
84 
85 # define mInitTestProg() mTestProgInits()
86 # define mInitBatchTestProg() \
87  int argc = GetArgC(); char** argv = GetArgV(); \
88  mInitTestProg()
89 
90 #endif // ifndef __test_lib_source__
91 
92 
93 static inline mUnusedVar od_ostream& tstStream( bool err=false )
94 {
95  if ( !quiet_ || err )
96  {
97  if ( err )
98  od_ostream::logStream() << "[FAIL] ";
99  return od_ostream::logStream();
100  }
101  return od_ostream::nullStream();
102 }
103 
105 {
106  return tstStream( false );
107 }
108 
110 {
111  return tstStream( true );
112 }
113 
114 inline bool handleTestResult( bool isok, const char* desc, const char* emsg=0 )
115 {
116  if ( isok )
117  logStream() << "[OK] " << desc << od_endl;
118  else
119  {
120  if ( !emsg )
121  emsg = "<no details>";
122  errStream() << desc << ": " << emsg << od_endl;
123  }
124 
125  return isok;
126 }
127 
128 #define mRunStandardTest( test, desc ) \
129  { const bool testres = test; \
130  if ( !handleTestResult(testres,desc) ) return false; }
131 
132 #define mRunStandardTestWithError( test, desc, err ) \
133  { const bool testres = test ; \
134  if ( !handleTestResult(testres,desc,err) ) return false; }
testMain
int testMain(int, char **)
ExitProgram
int ExitProgram(int ret)
quiet
static mUsedVar bool quiet
Definition: testprog.h:39
keystrs.h
the_testprog_parser_
static PtrMan< CommandLineParser > the_testprog_parser_
Definition: testprog.h:57
od_ostream.h
od_endl
od_ostream & od_endl(od_ostream &strm)
Definition: od_ostream.h:119
od_ostream::nullStream
static od_ostream & nullStream()
genc.h
errStream
static od_ostream & errStream()
Definition: testprog.h:109
commandlineparser.h
handleTestResult
bool handleTestResult(bool isok, const char *desc, const char *emsg=0)
Definition: testprog.h:114
CommandLineParser
Definition: commandlineparser.h:53
OD::TestProgCtxt
@ TestProgCtxt
Definition: odruncontext.h:25
main
int main(int argc, char **argv)
Definition: testprog.h:49
clParser
static CommandLineParser & clParser()
Definition: testprog.h:59
ptrman.h
odruncontext.h
PtrMan
Definition: ptrman.h:121
mUnusedVar
#define mUnusedVar
Definition: plfdefs.h:210
quiet_
static bool quiet_
Definition: testprog.h:56
theparser
static mUsedVar PtrMan< CommandLineParser > theparser
Definition: testprog.h:40
od_ostream::logStream
static od_ostream & logStream()
used by ErrMsg and UsrMsg
OD::SetRunContext
void SetRunContext(RunCtxt)
tstStream
static od_ostream & tstStream(bool err=false)
Definition: testprog.h:93
od_ostream
OD class for stream write common access to the user log file, or std::cout in other than od_main.
Definition: od_ostream.h:26
logStream
static od_ostream & logStream()
Definition: testprog.h:104
debug.h
winmain.h

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B.V. 1995-2021