libept
test-runner.h
Go to the documentation of this file.
00001 #include <unistd.h>
00002 
00003 #define RUN(x, y) x().y()
00004 
00005 struct RunTest {
00006     const char *name;
00007     void (*run)();
00008 };
00009 
00010 struct RunSuite {
00011     const char *name;
00012     RunTest *tests;
00013     int testCount;
00014 };
00015 
00016 struct RunAll {
00017     RunSuite *suites;
00018     int suiteCount;
00019     FILE *status, *confirm;
00020 
00021     RunSuite *findSuite( std::string name ) {
00022         for ( int i = 0; i < suiteCount; ++i )
00023             if ( suites[i].name == name )
00024                 return suites + i;
00025         return 0;
00026     }
00027 
00028     void waitForAck() {
00029         size_t n = 0; char *line = 0;
00030         size_t read = getline( &line, &n, confirm );
00031         assert_eq( read, 4 );
00032         assert_eq( std::string( "ack\n" ), line );
00033         free( line );
00034     }
00035 
00036     void runSuite( RunSuite &s, int fromTest, int suite, int suiteCount )
00037     {
00038         fprintf( status, "s/s: (%d/%d) %s\n", suite + 1, suiteCount, s.name );
00039         for ( int i = fromTest; i < s.testCount; ++i ) {
00040             fprintf( status, "t/s: (%d/%d) %s\n", i, s.testCount,
00041                      s.tests[i].name );
00042             fflush( status );
00043             waitForAck();
00044             s.tests[i].run();
00045             fprintf( status, "t/d: %s\n", s.tests[i].name );
00046             fflush( status );
00047             waitForAck();
00048             // exit( 0 ); // TODO make this optional; safety vs
00049                        // performance tradeoff
00050         }
00051         fprintf( status, "s/d: %s\n", s.name );
00052     }
00053 
00054     void runFrom( int suite, int test )
00055     {
00056         for ( int i = suite; i < suiteCount; ++i ) {
00057             assert( suite <= suiteCount );
00058             runSuite( suites[i], test, i, suiteCount );
00059             test = 0;
00060         }
00061     }
00062 };
00063