Skip to content

Commit

Permalink
Provide better isolation for tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
kr committed Dec 15, 2008
1 parent 5f00729 commit 9b63f7a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 231 deletions.
256 changes: 38 additions & 218 deletions cut.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,9 @@
#define TRUE 1
#endif

typedef struct NameStackItem NameStackItem;
typedef struct NameStackItem *NameStack;

struct NameStackItem
{
NameStackItem * next;
char * name;
CUTTakedownFunction *takedown;
};

static int breakpoint = 0;
static int count = 0;
static BOOL test_hit_error = FALSE;
static NameStack nameStack;

static void traceback( void );
static void cut_exit( void );
static int any_problems = 0;

/* I/O Functions */

Expand All @@ -60,11 +46,6 @@ static void print_string_as_error( char *filename, int lineNumber, char *string
fflush( stdout );
}

static void print_integer_as_expected( int i )
{
printf( "(signed) %d (unsigned) %u (hex) 0x%08X", i, i, i );
}

static void print_integer( int i )
{
printf( "%d", i );
Expand Down Expand Up @@ -99,59 +80,12 @@ static void space( void )
print_character( ' ' );
}

/* Name Stack Functions */

static NameStackItem *stack_topOf( NameStack *stack )
{
return *stack;
}

static BOOL stack_isEmpty( NameStack *stack )
{
return stack_topOf( stack ) == NULL;
}

static BOOL stack_isNotEmpty( NameStack *stack )
{
return !( stack_isEmpty( stack ) );
}

static void stack_push( NameStack *stack, char *name, CUTTakedownFunction *tdfunc )
{
NameStackItem *item;

item = (NameStackItem *)( malloc( sizeof( NameStackItem ) ) );
if( item != NULL )
{
item -> next = stack_topOf( stack );
item -> name = name;
item -> takedown = tdfunc;

*stack = item;
}
}

static void stack_drop( NameStack *stack )
{
NameStackItem *oldItem;

if( stack_isNotEmpty( stack ) )
{
oldItem = stack_topOf( stack );
*stack = oldItem -> next;

free( oldItem );
}
}

/* CUT Initialization and Takedown Functions */

void cut_init( int brkpoint )
{
breakpoint = brkpoint;
count = 0;
test_hit_error = FALSE;
nameStack = NULL;

if( brkpoint >= 0 )
{
Expand All @@ -163,74 +97,13 @@ void cut_init( int brkpoint )

void cut_exit( void )
{
exit( test_hit_error != FALSE );
}

/* User Interface functions */

static void print_group( int position, int base, int leftover )
{
if( !leftover )
return;

print_integer_in_field( base, position );
while( --leftover )
dot();
}

static void print_recap( int count )
{
int countsOnLastLine = count % 50;
int groupsOnLastLine = countsOnLastLine / 10;
int dotsLeftOver = countsOnLastLine % 10;
int lastGroupLocation =
countsOnLastLine - dotsLeftOver + ( 4 * groupsOnLastLine ) + 5;

if( dotsLeftOver == 0 )
{
if( countsOnLastLine == 0 )
lastGroupLocation = 61;
else
lastGroupLocation -= 14;

print_group( lastGroupLocation, countsOnLastLine-10, 10);
}
else
{
print_group(
lastGroupLocation,
countsOnLastLine - dotsLeftOver,
dotsLeftOver
);
}
}

void cut_break_formatting( void ) // DEPRECATED: Do not use in future software
{
new_line();
}

void cut_resume_formatting( void )
{
new_line();
print_recap( count );
}

void cut_interject( const char *comment, ... )
{
va_list marker;
va_start(marker,comment);

cut_break_formatting();
vprintf(comment,marker);
cut_resume_formatting();

va_end(marker);
exit(any_problems);
}

/* Test Progress Accounting functions */

void __cut_mark_point( char *filename, int lineNumber )
static void
cut_mark_point(char out, char *filename, int lineNumber )
{
if( ( count % 10 ) == 0 )
{
Expand All @@ -240,14 +113,13 @@ void __cut_mark_point( char *filename, int lineNumber )
print_integer_in_field( count, 5 );
}
else
dot();
print_character(out);

count++;
if( count == breakpoint )
{
print_string_as_error( filename, lineNumber, "Breakpoint hit" );
new_line();
traceback();
cut_exit();
}
}
Expand All @@ -261,107 +133,55 @@ void __cut_assert(
BOOL success
)
{
__cut_mark_point( filename, lineNumber );

if( success != FALSE )
return;
if (success) return;

cut_break_formatting();
new_line();
print_string_as_error( filename, lineNumber, message );
new_line();
print_string_as_error( filename, lineNumber, "Failed expression: " );
print_string( expression );
new_line();

test_hit_error = TRUE;
cut_resume_formatting();
exit(-1);
}


/* Test Delineation and Teardown Support Functions */

static void traceback()
{
if( stack_isNotEmpty( &nameStack ) )
print_string( "Traceback" );
else
print_string( "(No traceback available.)" );

while( stack_isNotEmpty( &nameStack ) )
{
print_string( ": " );
print_string( stack_topOf( &nameStack ) -> name );

if( stack_topOf( &nameStack ) -> takedown != NULL )
{
print_string( "(taking down)" );
stack_topOf( &nameStack ) -> takedown();
}

stack_drop( &nameStack );

if( stack_isNotEmpty( &nameStack ) )
space();
}

new_line();
}

void cut_start( char *name, CUTTakedownFunction *takedownFunction )
static void
die(const char *msg)
{
stack_push( &nameStack, name, takedownFunction );
perror(msg);
exit(3);
}

int __cut_check_errors( char *filename, int lineNumber )
void
__cut_run(char *group_name, cut_fn bringup, cut_fn takedown, char *test_name,
cut_fn test, char *filename, int lineno)
{
if( test_hit_error || stack_isEmpty( &nameStack ) )
{
cut_break_formatting();
if( stack_isEmpty( &nameStack ) )
print_string_as_error( filename, lineNumber, "Missing cut_start(); no traceback possible." );
else
traceback();

cut_exit();
return 0;
} else return 1;
}
pid_t pid;
int status;

void __cut_end( char *filename, int lineNumber, char *closingFrame )
{
if( test_hit_error || stack_isEmpty( &nameStack ) )
{
cut_break_formatting();
if( stack_isEmpty( &nameStack ) )
print_string_as_error( filename, lineNumber, "Missing cut_start(); no traceback possible." );
else
traceback();

cut_exit();
}
else
{
if( strcmp( stack_topOf( &nameStack ) -> name, closingFrame ) == 0 )
stack_drop( &nameStack );
else
{
print_string_as_error( filename, lineNumber, "Mismatched cut_end()." );
traceback();
cut_exit();
if (pid = fork()) {
if (pid < 0) die("\nfork()");
wait(&status);

if (!status) {
/* success */
cut_mark_point('.', filename, lineno );
} else if (status == 65280) {
/* failure */
cut_mark_point('F', filename, lineno );
any_problems = 1;
} else {
/* error */
cut_mark_point('E', filename, lineno );
any_problems = 1;
}
} else {
bringup();
test();
takedown();
exit(0);
}
}
}

void
__cut_run(char *group_name, fn bringup, fn takedown, char *test_name, fn test,
char *filename, int lineno)
{
cut_start(group_name, takedown);
bringup();
__cut_check_errors(filename, lineno);
cut_start(test_name, 0);
test();
__cut_end(filename, lineno, test_name);
__cut_end(filename, lineno, group_name);
takedown();
}
16 changes: 5 additions & 11 deletions cut.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@
#ifndef CUT_CUT_H_INCLUDED
#define CUT_CUT_H_INCLUDED

typedef void CUTTakedownFunction( void );
typedef void(*fn)(void);
typedef void(*cut_fn)(void);

void cut_start ( char *, CUTTakedownFunction * );
void cut_init ( int breakpoint );
void cut_break_formatting ( void );
void cut_resume_formatting( void );
void cut_interject( const char *, ... );
void cut_init(int);
void cut_exit(void);

#define cut_run(G, T) __cut_run("group-" #G, \
__CUT_BRINGUP__ ## G, \
Expand All @@ -45,7 +41,6 @@ void cut_interject( const char *, ... );
__FILE__, \
__LINE__);

#define cut_mark_point() __cut_mark_point(__FILE__,__LINE__)
#define ASSERT(X,msg) __cut_assert(__FILE__,__LINE__,msg,#X,X)

#define STATIC_ASSERT(X) extern bool __static_ASSERT_at_line_##__LINE__##__[ (0!=(X))*2-1 ];
Expand All @@ -56,9 +51,8 @@ void cut_interject( const char *, ... );
* macros instead.
*/

void __cut_run(char *, fn, fn, char *, fn, char *, int);
void __cut_mark_point ( char *, int );
void __cut_assert ( char *, int, char *, char *, int );
void __cut_run(char *, cut_fn, cut_fn, char *, cut_fn, char *, int);
void __cut_assert( char *, int, char *, char *, int );

#endif

4 changes: 2 additions & 2 deletions cutgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ void EmitUnitTesterBody()
}

BlankLine();
Emit( " cut_break_formatting();" );
Emit( " printf(\"Done.\\n\");" );
Emit( " printf(\"\\nDone.\\n\");" );
Emit( " cut_exit();\n" );
Emit( " return 0;\n}\n" );
}

Expand Down

0 comments on commit 9b63f7a

Please sign in to comment.