g4tools  5.4.0
utest
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_utest
5 #define tools_utest
6 
7 #include "args"
8 #include <cstdio> //for FILE*
9 
10 namespace tools {
11 
12 class test {
13 public:
14  typedef bool(*func)(std::ostream&,bool);
15  typedef bool(*func2)(std::ostream&,const args&);
16  typedef bool(*func3)(std::ostream&,int,char**);
17 
18  typedef bool(*cfunc)(FILE*,bool);
19 public:
20  test():m_do_it(false),m_func(0),m_func2(0),m_func3(0),m_cfunc(0){}
21  test(bool a_do_it,func a_func):m_do_it(a_do_it),m_func(a_func),m_func2(0),m_func3(0),m_cfunc(0){}
22  test(bool a_do_it,func2 a_func):m_do_it(a_do_it),m_func(0),m_func2(a_func),m_func3(0),m_cfunc(0){}
23  test(bool a_do_it,func3 a_func):m_do_it(a_do_it),m_func(0),m_func2(0),m_func3(a_func),m_cfunc(0){}
24  test(bool a_do_it,cfunc a_func):m_do_it(a_do_it),m_func(0),m_func2(0),m_func3(0),m_cfunc(a_func){}
25  virtual ~test(){}
26 public:
27  test(const test& a_from)
28  :m_do_it(a_from.m_do_it)
29  ,m_func(a_from.m_func),m_func2(a_from.m_func2),m_func3(a_from.m_func3),m_cfunc(a_from.m_cfunc){}
30  test& operator=(const test& a_from) {
31  m_do_it = a_from.m_do_it;
32  m_func = a_from.m_func;
33  m_func2 = a_from.m_func2;
34  m_func3 = a_from.m_func3;
35  m_cfunc = a_from.m_cfunc;
36  return *this;
37  }
38 public:
39  bool m_do_it;
44 };
45 
46 class utest : public std::vector< std::pair<std::string,test> > {
47  typedef std::pair<std::string,test> named_test;
48  typedef std::vector<named_test> parent;
49 public:
50  utest(std::ostream& a_out):m_out(a_out){}
51  virtual~ utest(){}
52 public:
53  utest(const utest& a_from):parent(a_from),m_out(a_from.m_out){}
54  utest& operator=(const utest& a_from){ parent::operator=(a_from);return *this;}
55 public:
56  void add(const std::string& a_name,test::func a_func) {
57  (*this).push_back(named_test(a_name,test(false,a_func)));
58  }
59  void add(const std::string& a_name,test::func2 a_func) {
60  (*this).push_back(named_test(a_name,test(false,a_func)));
61  }
62  void add(const std::string& a_name,test::func3 a_func) {
63  (*this).push_back(named_test(a_name,test(false,a_func)));
64  }
65  void add(const std::string& a_name,test::cfunc a_func) {
66  (*this).push_back(named_test(a_name,test(false,a_func)));
67  }
68 
69  bool exec(const args& a_args,bool a_verbose,int a_argc=0,char** a_argv=0) {
70  {tools_vforit(named_test,*this,it) (*it).second.m_do_it = false;}
71 
72  {tools_vforcit(args::arg,a_args.get_args(),it) {
73  const std::string& key = (*it).first;
74  bool found = false;
75  tools_vforit(named_test,*this,itn) {
76  const std::string& name = (*itn).first;
77  if(key==std::string("-"+name)) {found=true;break;}
78  if(key==std::string("-no_"+name)) {found=true;break;} //valid arg, but not a test.
79  }
80  if(!found) {
81  m_out << "WARNING : arg " << sout(key) << " is not a test." << std::endl;
82  }
83  }}
84 
85  bool some = false;
86  {tools_vforit(named_test,*this,it) {
87  const std::string& name = (*it).first;
88  if(a_args.is_arg("-"+name)) {
89  (*it).second.m_do_it = true;
90  some = true;
91  }
92  }}
93 
94  if(!some) { //do all.
95  tools_vforit(named_test,*this,it) (*it).second.m_do_it = true;
96  }
97 
98  {tools_vforit(named_test,*this,it) {
99  const std::string& name = (*it).first;
100  if(a_args.is_arg("-no_"+name)) {
101  (*it).second.m_do_it = false;
102  }
103  }}
104 
105  bool status = true;
106 
107  {tools_vforcit(named_test,*this,it) {
108  const std::string& name = (*it).first;
109  if((*it).second.m_do_it && (*it).second.m_func) {
110  if(a_verbose) m_out << "test_" << name << " ..." << std::endl;
111  if(!(*it).second.m_func(m_out,a_verbose)) {
112  m_out << "test_" << name << " failed." << std::endl;
113  status = false;
114  }
115  if(a_verbose) m_out << "test_" << name << " end." << std::endl;
116  }
117  }}
118 
119  {tools_vforcit(named_test,*this,it) {
120  const std::string& name = (*it).first;
121  if((*it).second.m_do_it && (*it).second.m_func2) {
122  if(a_verbose) m_out << "test_" << name << " ..." << std::endl;
123  if(!(*it).second.m_func2(m_out,a_args)) {
124  m_out << "test_" << name << " failed." << std::endl;
125  status = false;
126  }
127  if(a_verbose) m_out << "test_" << name << " end." << std::endl;
128  }
129  }}
130 
131  {tools_vforcit(named_test,*this,it) {
132  const std::string& name = (*it).first;
133  if((*it).second.m_do_it && (*it).second.m_func3) {
134  if(a_verbose) m_out << "test_" << name << " ..." << std::endl;
135  if(!(*it).second.m_func3(m_out,a_argc,a_argv)) {
136  m_out << "test_" << name << " failed." << std::endl;
137  status = false;
138  }
139  if(a_verbose) m_out << "test_" << name << " end." << std::endl;
140  }
141  }}
142 
143  {tools_vforcit(named_test,*this,it) {
144  const std::string& name = (*it).first;
145  if((*it).second.m_do_it && (*it).second.m_cfunc) {
146  if(a_verbose) m_out << "test_" << name << " ..." << std::endl;
147  if(!(*it).second.m_cfunc(stdout,a_verbose)) {
148  m_out << "test_" << name << " failed." << std::endl;
149  status = false;
150  }
151  if(a_verbose) m_out << "test_" << name << " end." << std::endl;
152  }
153  }}
154 
155  tools_vforit(named_test,*this,it) (*it).second.m_do_it = false;
156 
157  return status;
158  }
159 
160  void list() {tools_vforit(named_test,*this,it) m_out << (*it).first << std::endl;}
161 protected:
162  std::ostream& m_out;
163 };
164 
165 }
166 
167 #endif
tools::args::is_arg
bool is_arg(const std::string &a_string) const
Definition: args:92
tools::utest::utest
utest(const utest &a_from)
Definition: utest:53
tools::test::~test
virtual ~test()
Definition: utest:25
tools::test::test
test(bool a_do_it, func2 a_func)
Definition: utest:22
tools::test::m_func3
func3 m_func3
Definition: utest:42
tools::args::get_args
const std::vector< arg > & get_args() const
Definition: args:89
tools::test::test
test(bool a_do_it, func a_func)
Definition: utest:21
tools::test::cfunc
bool(* cfunc)(FILE *, bool)
Definition: utest:18
tools::utest::list
void list()
Definition: utest:160
tools::utest::add
void add(const std::string &a_name, test::func a_func)
Definition: utest:56
tools::utest::operator=
utest & operator=(const utest &a_from)
Definition: utest:54
tools::test::m_func2
func2 m_func2
Definition: utest:41
tools::test::func3
bool(* func3)(std::ostream &, int, char **)
Definition: utest:16
tools::args::arg
std::pair< std::string, std::string > arg
Definition: args:30
tools::test::func2
bool(* func2)(std::ostream &, const args &)
Definition: utest:15
tools::test::test
test()
Definition: utest:20
tools::args
Definition: args:24
tools::test::m_func
func m_func
Definition: utest:40
tools::sout
Definition: sout:17
tools::utest
Definition: utest:46
tools_vforit
#define tools_vforit(a__T, a__v, a__it)
Definition: forit:13
tools::utest::add
void add(const std::string &a_name, test::cfunc a_func)
Definition: utest:65
tools::test
Definition: utest:12
tools::utest::exec
bool exec(const args &a_args, bool a_verbose, int a_argc=0, char **a_argv=0)
Definition: utest:69
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::utest::~ utest
virtual ~ utest()
Definition: utest:51
args
tools::utest::add
void add(const std::string &a_name, test::func2 a_func)
Definition: utest:59
tools::test::m_do_it
bool m_do_it
Definition: utest:39
tools::file::found
bool found(const std::string &a_file, const std::string &a_what, std::vector< std::string > &a_found)
Definition: file:507
tools::test::func
bool(* func)(std::ostream &, bool)
Definition: utest:14
tools::utest::m_out
std::ostream & m_out
Definition: utest:162
tools::test::operator=
test & operator=(const test &a_from)
Definition: utest:30
tools::test::test
test(bool a_do_it, func3 a_func)
Definition: utest:23
tools_vforcit
#define tools_vforcit(a__T, a__v, a__it)
Definition: forit:7
tools::test::test
test(bool a_do_it, cfunc a_func)
Definition: utest:24
tools::utest::add
void add(const std::string &a_name, test::func3 a_func)
Definition: utest:62
tools::test::m_cfunc
cfunc m_cfunc
Definition: utest:43
tools::test::test
test(const test &a_from)
Definition: utest:27
tools::utest::utest
utest(std::ostream &a_out)
Definition: utest:50