5 * Copyright (c) 2001-2002 Samuel A. Falvo II, William D. Tanksley
6 * See CUT-LICENSE.TXT for details.
8 * Based on WDT's 'TestAssert' package.
19 #ifndef BOOL /* Just in case -- helps in portability */
31 typedef struct NameStackItem NameStackItem
;
32 typedef struct NameStackItem
*NameStack
;
38 CUTTakedownFunction
*takedown
;
41 static int breakpoint
= 0;
43 static BOOL test_hit_error
= FALSE
;
44 static NameStack nameStack
;
46 static void traceback( void );
47 static void cut_exit( void );
51 static void print_string( char *string
)
53 printf( "%s", string
);
57 static void print_string_as_error( char *filename
, int lineNumber
, char *string
)
59 printf( "%s(%d): %s", filename
, lineNumber
, string
);
63 static void print_integer_as_expected( int i
)
65 printf( "(signed) %d (unsigned) %u (hex) 0x%08X", i
, i
, i
);
68 static void print_integer( int i
)
74 static void print_integer_in_field( int i
, int width
)
76 printf( "%*d", width
, i
);
80 static void new_line( void )
86 static void print_character( char ch
)
92 static void dot( void )
94 print_character( '.' );
97 static void space( void )
99 print_character( ' ' );
102 /* Name Stack Functions */
104 static NameStackItem
*stack_topOf( NameStack
*stack
)
109 static BOOL
stack_isEmpty( NameStack
*stack
)
111 return stack_topOf( stack
) == NULL
;
114 static BOOL
stack_isNotEmpty( NameStack
*stack
)
116 return !( stack_isEmpty( stack
) );
119 static void stack_push( NameStack
*stack
, char *name
, CUTTakedownFunction
*tdfunc
)
123 item
= (NameStackItem
*)( malloc( sizeof( NameStackItem
) ) );
126 item
-> next
= stack_topOf( stack
);
128 item
-> takedown
= tdfunc
;
134 static void stack_drop( NameStack
*stack
)
136 NameStackItem
*oldItem
;
138 if( stack_isNotEmpty( stack
) )
140 oldItem
= stack_topOf( stack
);
141 *stack
= oldItem
-> next
;
147 /* CUT Initialization and Takedown Functions */
149 void cut_init( int brkpoint
)
151 breakpoint
= brkpoint
;
153 test_hit_error
= FALSE
;
158 print_string( "Breakpoint at test " );
159 print_integer( brkpoint
);
164 void cut_exit( void )
166 exit( test_hit_error
!= FALSE
);
169 /* User Interface functions */
171 static void print_group( int position
, int base
, int leftover
)
176 print_integer_in_field( base
, position
);
181 static void print_recap( int count
)
183 int countsOnLastLine
= count
% 50;
184 int groupsOnLastLine
= countsOnLastLine
/ 10;
185 int dotsLeftOver
= countsOnLastLine
% 10;
186 int lastGroupLocation
=
187 countsOnLastLine
- dotsLeftOver
+ ( 4 * groupsOnLastLine
) + 5;
189 if( dotsLeftOver
== 0 )
191 if( countsOnLastLine
== 0 )
192 lastGroupLocation
= 61;
194 lastGroupLocation
-= 14;
196 print_group( lastGroupLocation
, countsOnLastLine
-10, 10);
202 countsOnLastLine
- dotsLeftOver
,
208 void cut_break_formatting( void ) // DEPRECATED: Do not use in future software
213 void cut_resume_formatting( void )
216 print_recap( count
);
219 void cut_interject( const char *comment
, ... )
222 va_start(marker
,comment
);
224 cut_break_formatting();
225 vprintf(comment
,marker
);
226 cut_resume_formatting();
231 /* Test Progress Accounting functions */
233 void __cut_mark_point( char *filename
, int lineNumber
)
235 if( ( count
% 10 ) == 0 )
237 if( ( count
% 50 ) == 0 )
240 print_integer_in_field( count
, 5 );
246 if( count
== breakpoint
)
248 print_string_as_error( filename
, lineNumber
, "Breakpoint hit" );
264 __cut_mark_point( filename
, lineNumber
);
266 if( success
!= FALSE
)
269 cut_break_formatting();
270 print_string_as_error( filename
, lineNumber
, message
);
272 print_string_as_error( filename
, lineNumber
, "Failed expression: " );
273 print_string( expression
);
276 test_hit_error
= TRUE
;
277 cut_resume_formatting();
281 /* Test Delineation and Teardown Support Functions */
283 static void traceback()
285 if( stack_isNotEmpty( &nameStack
) )
286 print_string( "Traceback" );
288 print_string( "(No traceback available.)" );
290 while( stack_isNotEmpty( &nameStack
) )
292 print_string( ": " );
293 print_string( stack_topOf( &nameStack
) -> name
);
295 if( stack_topOf( &nameStack
) -> takedown
!= NULL
)
297 print_string( "(taking down)" );
298 stack_topOf( &nameStack
) -> takedown();
301 stack_drop( &nameStack
);
303 if( stack_isNotEmpty( &nameStack
) )
310 void cut_start( char *name
, CUTTakedownFunction
*takedownFunction
)
312 stack_push( &nameStack
, name
, takedownFunction
);
315 int __cut_check_errors( char *filename
, int lineNumber
)
317 if( test_hit_error
|| stack_isEmpty( &nameStack
) )
319 cut_break_formatting();
320 if( stack_isEmpty( &nameStack
) )
321 print_string_as_error( filename
, lineNumber
, "Missing cut_start(); no traceback possible." );
330 void __cut_end( char *filename
, int lineNumber
, char *closingFrame
)
332 if( test_hit_error
|| stack_isEmpty( &nameStack
) )
334 cut_break_formatting();
335 if( stack_isEmpty( &nameStack
) )
336 print_string_as_error( filename
, lineNumber
, "Missing cut_start(); no traceback possible." );
344 if( strcmp( stack_topOf( &nameStack
) -> name
, closingFrame
) == 0 )
345 stack_drop( &nameStack
);
348 print_string_as_error( filename
, lineNumber
, "Mismatched cut_end()." );
356 __cut_run(char *group_name
, fn bringup
, fn takedown
, char *test_name
, fn test
,
357 char *filename
, int lineno
)
359 cut_start(group_name
, takedown
);
361 __cut_check_errors(filename
, lineno
);
362 cut_start(test_name
, 0);
364 __cut_end(filename
, lineno
, test_name
);
365 __cut_end(filename
, lineno
, group_name
);