2019-06-09 21:45:44 -05:00
// 28 may 2019
# include "test.h"
2020-01-26 19:06:12 -06:00
# include "thread.h"
# include "../common/testhooks.h"
2019-06-09 21:45:44 -05:00
2020-01-26 19:02:03 -06:00
// Do not put any test cases in this file; they will not be run.
2020-05-11 19:54:20 -05:00
// Notes on these functions:
2020-05-11 20:03:31 -05:00
// - Try to wrap them as tightly around the specific calls being tested as possible, to avoid accidentally catching something else. Ideally, only one libui function call should be made between a begin/end pair, even indirectly.
2020-05-11 19:54:20 -05:00
// - I don't know if these are thread-safe yet (TODO potentially make them so so this first part can be made tighter).
2019-06-09 21:45:44 -05:00
struct checkProgrammerErrorParams {
bool caught ;
char * msgGot ;
2020-05-09 01:33:18 -05:00
const char * msgWant ; // NULL if should not be caught
2019-06-09 21:45:44 -05:00
} ;
2020-02-23 17:04:23 -06:00
static char * ourStrdup ( const char * s )
2020-01-26 19:02:03 -06:00
{
char * t ;
size_t n ;
n = ( strlen ( s ) + 1 ) * sizeof ( char ) ;
t = ( char * ) malloc ( n ) ;
if ( t = = NULL )
2020-02-23 17:04:23 -06:00
TestFatalf ( " memory exhausted in ourStrdup() copying %s " , s ) ;
2020-01-26 19:02:03 -06:00
memcpy ( t , s , n ) ;
return t ;
}
2019-06-09 21:45:44 -05:00
static void handleProgrammerError ( const char * msg , void * data )
{
struct checkProgrammerErrorParams * p = ( struct checkProgrammerErrorParams * ) data ;
p - > caught = true ;
if ( strcmp ( msg , p - > msgWant ) ! = 0 )
2020-02-23 17:04:23 -06:00
p - > msgGot = ourStrdup ( msg ) ;
2019-06-09 21:45:44 -05:00
}
2020-02-23 17:04:23 -06:00
#if 0
// TODO
2019-06-09 21:45:44 -05:00
static void checkProgrammerErrorThreadProc ( void * data )
{
struct checkProgrammerErrorParams * p = ( struct checkProgrammerErrorParams * ) data ;
2019-06-15 18:53:50 -05:00
( * ( p - > f ) ) ( ) ;
2019-06-09 21:45:44 -05:00
}
2020-01-26 19:02:03 -06:00
static void checkProgrammerErrorSubtestImpl ( struct checkProgrammerErrorParams * p )
2019-06-09 21:45:44 -05:00
{
if ( p - > inThread ) {
threadThread * thread ;
threadSysError err ;
err = threadNewThread ( checkProgrammerErrorThreadProc , p , & thread ) ;
if ( err ! = 0 )
2020-02-09 12:37:45 -06:00
// TODO these should only fatal out of the subtest (see TODO below)
2020-01-26 19:02:03 -06:00
TestFatalfFull ( p - > file , p - > line , " error creating thread: " threadSysErrorFmt , threadSysErrorFmtArg ( err ) ) ;
2019-06-09 21:45:44 -05:00
err = threadThreadWaitAndFree ( thread ) ;
if ( err ! = 0 )
2020-01-26 19:02:03 -06:00
TestFatalfFull ( p - > file , p - > line , " error waiting for thread to finish: " threadSysErrorFmt , threadSysErrorFmtArg ( err ) ) ;
2019-06-09 21:45:44 -05:00
}
}
2020-02-23 17:04:23 -06:00
# endif
void * beginCheckProgrammerError ( const char * want )
{
struct checkProgrammerErrorParams * p ;
p = ( struct checkProgrammerErrorParams * ) malloc ( sizeof ( struct checkProgrammerErrorParams ) ) ;
if ( p = = NULL )
TestFatalf ( " memory exhausted allocating beginCheckProgrammerError() context " ) ;
memset ( p , 0 , sizeof ( struct checkProgrammerErrorParams ) ) ;
p - > msgWant = want ;
uiprivTestHookReportProgrammerError ( handleProgrammerError , p ) ;
return p ;
}
2019-06-09 21:45:44 -05:00
2020-05-09 01:33:18 -05:00
static void checkWantProgrammerError ( const char * file , long line , struct checkProgrammerErrorParams * p )
2019-06-09 21:45:44 -05:00
{
2020-02-23 17:04:23 -06:00
if ( ! p - > caught )
TestErrorfFull ( file , line , " did not throw a programmer error; should have " ) ;
if ( p - > msgGot ! = NULL ) {
TestErrorfFull ( file , line , " message doesn't match expected string: " diff ( " %s " ) ,
p - > msgGot , p - > msgWant ) ;
free ( p - > msgGot ) ;
2019-06-15 18:53:50 -05:00
}
2020-05-09 01:33:18 -05:00
}
static void checkWantNoProgrammerError ( const char * file , long line , struct checkProgrammerErrorParams * p )
{
if ( p - > caught )
TestErrorfFull ( file , line , " threw a programmer error; should not have " ) ;
if ( p - > msgGot ! = NULL ) {
TestErrorfFull ( file , line , " got unexpected programmer error message when none was expected: %s " , p - > msgGot ) ;
free ( p - > msgGot ) ;
}
}
void endCheckProgrammerErrorFull ( const char * file , long line , void * context )
{
struct checkProgrammerErrorParams * p = ( struct checkProgrammerErrorParams * ) context ;
if ( p - > msgWant ! = NULL )
checkWantProgrammerError ( file , line , p ) ;
else
checkWantNoProgrammerError ( file , line , p ) ;
2020-02-23 17:04:23 -06:00
free ( p ) ;
2020-01-26 19:02:03 -06:00
uiprivTestHookReportProgrammerError ( NULL , NULL ) ;
2019-06-09 21:45:44 -05:00
}