Remove unused deprecated function.
[beanstalkd.git] / cut.c
blob5b2ba677606a23868e1c5fc3fb0cc1e814241e30
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();
256 void __cut_assert(
257 char *filename,
258 int lineNumber,
259 char *message,
260 char *expression,
261 BOOL success
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();
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" );
287 else
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 ) )
304 space();
307 new_line();
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." );
322 else
323 traceback();
325 cut_exit();
326 return 0;
327 } else return 1;
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." );
337 else
338 traceback();
340 cut_exit();
342 else
344 if( strcmp( stack_topOf( &nameStack ) -> name, closingFrame ) == 0 )
345 stack_drop( &nameStack );
346 else
348 print_string_as_error( filename, lineNumber, "Mismatched cut_end()." );
349 traceback();
350 cut_exit();
355 void
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);
360 bringup();
361 __cut_check_errors(filename, lineno);
362 cut_start(test_name, 0);
363 test();
364 __cut_end(filename, lineno, test_name);
365 __cut_end(filename, lineno, group_name);
366 takedown();