Run bringup and takedown for every test.
[beanstalkd.git] / cut.c
blobec75ead8f00c19e0b213ae8fc343178ee3cd14c9
1 /*
2 * libcut.inc
3 * CUT 2.1
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.
10 * $log$
13 #include <string.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <stdarg.h>
17 #include "cut.h"
19 #ifndef BOOL /* Just in case -- helps in portability */
20 #define BOOL int
21 #endif
23 #ifndef FALSE
24 #define FALSE (0)
25 #endif
27 #ifndef TRUE
28 #define TRUE 1
29 #endif
31 typedef struct NameStackItem NameStackItem;
32 typedef struct NameStackItem *NameStack;
34 struct NameStackItem
36 NameStackItem * next;
37 char * name;
38 CUTTakedownFunction *takedown;
41 static int breakpoint = 0;
42 static int count = 0;
43 static BOOL test_hit_error = FALSE;
44 static NameStack nameStack;
46 static void traceback( void );
47 static void cut_exit( void );
49 /* I/O Functions */
51 static void print_string( char *string )
53 printf( "%s", string );
54 fflush( stdout );
57 static void print_string_as_error( char *filename, int lineNumber, char *string )
59 printf( "%s(%d): %s", filename, lineNumber, string );
60 fflush( stdout );
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 )
70 printf( "%d", i );
71 fflush( stdout );
74 static void print_integer_in_field( int i, int width )
76 printf( "%*d", width, i );
77 fflush( stdout );
80 static void new_line( void )
82 printf( "\n" );
83 fflush( stdout );
86 static void print_character( char ch )
88 printf( "%c", ch );
89 fflush( stdout );
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 )
106 return *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 )
121 NameStackItem *item;
123 item = (NameStackItem *)( malloc( sizeof( NameStackItem ) ) );
124 if( item != NULL )
126 item -> next = stack_topOf( stack );
127 item -> name = name;
128 item -> takedown = tdfunc;
130 *stack = item;
134 static void stack_drop( NameStack *stack )
136 NameStackItem *oldItem;
138 if( stack_isNotEmpty( stack ) )
140 oldItem = stack_topOf( stack );
141 *stack = oldItem -> next;
143 free( oldItem );
147 /* CUT Initialization and Takedown Functions */
149 void cut_init( int brkpoint )
151 breakpoint = brkpoint;
152 count = 0;
153 test_hit_error = FALSE;
154 nameStack = NULL;
156 if( brkpoint >= 0 )
158 print_string( "Breakpoint at test " );
159 print_integer( brkpoint );
160 new_line();
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 )
173 if( !leftover )
174 return;
176 print_integer_in_field( base, position );
177 while( --leftover )
178 dot();
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;
193 else
194 lastGroupLocation -= 14;
196 print_group( lastGroupLocation, countsOnLastLine-10, 10);
198 else
200 print_group(
201 lastGroupLocation,
202 countsOnLastLine - dotsLeftOver,
203 dotsLeftOver
208 void cut_break_formatting( void ) // DEPRECATED: Do not use in future software
210 new_line();
213 void cut_resume_formatting( void )
215 new_line();
216 print_recap( count );
219 void cut_interject( const char *comment, ... )
221 va_list marker;
222 va_start(marker,comment);
224 cut_break_formatting();
225 vprintf(comment,marker);
226 cut_resume_formatting();
228 va_end(marker);
231 /* Test Progress Accounting functions */
233 void __cut_mark_point( char *filename, int lineNumber )
235 if( ( count % 10 ) == 0 )
237 if( ( count % 50 ) == 0 )
238 new_line();
240 print_integer_in_field( count, 5 );
242 else
243 dot();
245 count++;
246 if( count == breakpoint )
248 print_string_as_error( filename, lineNumber, "Breakpoint hit" );
249 new_line();
250 traceback();
251 cut_exit();
255 void __cut_assert_equals( // DEPRECATED: Do not use in future software
256 char *filename,
257 int lineNumber,
258 char *message,
259 char *expression,
260 BOOL success,
261 int expected
264 __cut_mark_point( filename, lineNumber );
266 if( success != FALSE )
267 return;
269 cut_break_formatting();
270 print_string_as_error( filename, lineNumber, message );
271 new_line();
272 print_string_as_error( filename, lineNumber, "Failed expression: " );
273 print_string( expression );
274 new_line();
275 print_string_as_error( filename, lineNumber, "Actual value: " );
276 print_integer_as_expected( expected );
277 new_line();
279 test_hit_error = TRUE;
280 cut_resume_formatting();
284 void __cut_assert(
285 char *filename,
286 int lineNumber,
287 char *message,
288 char *expression,
289 BOOL success
292 __cut_mark_point( filename, lineNumber );
294 if( success != FALSE )
295 return;
297 cut_break_formatting();
298 print_string_as_error( filename, lineNumber, message );
299 new_line();
300 print_string_as_error( filename, lineNumber, "Failed expression: " );
301 print_string( expression );
302 new_line();
304 test_hit_error = TRUE;
305 cut_resume_formatting();
309 /* Test Delineation and Teardown Support Functions */
311 static void traceback()
313 if( stack_isNotEmpty( &nameStack ) )
314 print_string( "Traceback" );
315 else
316 print_string( "(No traceback available.)" );
318 while( stack_isNotEmpty( &nameStack ) )
320 print_string( ": " );
321 print_string( stack_topOf( &nameStack ) -> name );
323 if( stack_topOf( &nameStack ) -> takedown != NULL )
325 print_string( "(taking down)" );
326 stack_topOf( &nameStack ) -> takedown();
329 stack_drop( &nameStack );
331 if( stack_isNotEmpty( &nameStack ) )
332 space();
335 new_line();
338 void cut_start( char *name, CUTTakedownFunction *takedownFunction )
340 stack_push( &nameStack, name, takedownFunction );
343 int __cut_check_errors( char *filename, int lineNumber )
345 if( test_hit_error || stack_isEmpty( &nameStack ) )
347 cut_break_formatting();
348 if( stack_isEmpty( &nameStack ) )
349 print_string_as_error( filename, lineNumber, "Missing cut_start(); no traceback possible." );
350 else
351 traceback();
353 cut_exit();
354 return 0;
355 } else return 1;
358 void __cut_end( char *filename, int lineNumber, char *closingFrame )
360 if( test_hit_error || stack_isEmpty( &nameStack ) )
362 cut_break_formatting();
363 if( stack_isEmpty( &nameStack ) )
364 print_string_as_error( filename, lineNumber, "Missing cut_start(); no traceback possible." );
365 else
366 traceback();
368 cut_exit();
370 else
372 if( strcmp( stack_topOf( &nameStack ) -> name, closingFrame ) == 0 )
373 stack_drop( &nameStack );
374 else
376 print_string_as_error( filename, lineNumber, "Mismatched cut_end()." );
377 traceback();
378 cut_exit();