wibble  0.1.28
test-runner.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 #include <wibble/sys/macros.h>
3 
4 #include <unistd.h>
5 #ifdef POSIX
6 #include <wibble/sys/pipe.h>
7 #endif
8 #include <cstdio>
9 
10 #define RUN(x, y) x().y()
11 
12 struct RunTest {
13  const char *name;
14  void (*run)();
15 };
16 
17 struct RunSuite {
18  const char *name;
20  int testCount;
21 
22  int findTest( std::string name ) {
23  for ( int i = 0; i < testCount; ++i )
24  if ( tests[i].name == name )
25  return i;
26  return -1;
27  }
28 
29 };
30 
31 struct RunFeedback {
32  virtual void status( std::string l ) = 0;
33  virtual void waitForAck() = 0;
34 };
35 
36 #ifdef POSIX
37 struct RunAll {
38  RunSuite *suites;
39  int suiteCount;
40  RunFeedback *feedback;
41 
42  RunSuite *findSuite( std::string name ) {
43  for ( int i = 0; i < suiteCount; ++i )
44  if ( suites[i].name == name )
45  return suites + i;
46  return 0;
47  }
48 
49  void runSuite( RunSuite &s, int fromTest, int suite, int suiteCount )
50  {
51  feedback->status( wibble::str::fmtf(
52  "s/s: (%d/%d) %s", suite + 1, suiteCount, s.name ) );
53  for ( int i = fromTest; i < s.testCount; ++i ) {
54  feedback->status( wibble::str::fmtf(
55  "t/s: (%d/%d) %s", i, s.testCount, s.tests[i].name ) );
56  feedback->waitForAck();
57  s.tests[i].run();
58  feedback->status( std::string( "t/d: " ) + s.tests[i].name );
59  feedback->waitForAck();
60  // exit( 0 ); // TODO make this optional; safety vs
61  // performance tradeoff
62  }
63  feedback->status( std::string( "s/d: " ) + s.name );
64  }
65 
66  void runTest( RunSuite &s, int test )
67  {
68  feedback->status( std::string( "s/s: (1/1) " ) + s.name );
69  feedback->status( std::string( "t/s: (1/1) " ) + s.tests[test].name );
70  feedback->waitForAck();
71  s.tests[test].run();
72  feedback->status( std::string( "t/d: " ) + s.tests[test].name );
73  feedback->waitForAck();
74  feedback->status( std::string( "s/d: " ) + s.name );
75  }
76 
77  void runFrom( int suite, int test )
78  {
79  for ( int i = suite; i < suiteCount; ++i ) {
80  assert( suite <= suiteCount );
81  runSuite( suites[i], test, i, suiteCount );
82  test = 0;
83  }
84  }
85 };
86 #endif