X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=draft.c;h=e3ae3ad0d5a92a2696c508c9d025d70910444b2e;hb=dbfaf247211cacdff135f0e6e13d14fedd53b4ea;hp=7befa320837138aec11108b09fdd6a122c3c0ac2;hpb=ce72ed9b6fdbf8738aa1bfef8584508ef960252a;p=pdclib.old diff --git a/draft.c b/draft.c index 7befa32..e3ae3ad 100644 --- a/draft.c +++ b/draft.c @@ -35,26 +35,14 @@ void parse_out( const char * spec, va_list ap ); struct status_t { - int base; /* base to which the value shall be converted */ - int flags; /* flags and length modifiers */ - size_t n; /* maximum number of characters to be written */ - size_t i; /* number of characters already written */ - size_t this; /* number of output chars in the current conversion */ - char * s; /* target buffer */ - size_t width; /* width of current field */ - int prec; /* precision of current field */ -}; - -union value_t -{ - int8_t int8; - uint8_t uint8; - int16_t int16; - uint16_t uint16; - int32_t int32; - uint32_t uint32; - int64_t int64; - uint64_t uint64; + int base; /* base to which the value shall be converted */ + int_fast16_t flags; /* flags and length modifiers */ + size_t n; /* maximum number of characters to be written */ + size_t i; /* number of characters already written */ + size_t this; /* number of output chars in the current conversion */ + char * s; /* target buffer */ + size_t width; /* width of current field */ + size_t prec; /* precision of current field */ }; /* x - the character to be delivered @@ -65,11 +53,6 @@ union value_t */ #define DELIVER( x ) do { if ( status->i < status->n ) status->s[status->i] = x; ++(status->i); } while ( 0 ) -/* TODO: Left / right alignment - requires track-keeping of width and printed chars. - "Father function", die für right alignment "tail recursive" gerufen wird, und - "after" für left alignment? Parameter als struct? -*/ - static void int2base( intmax_t value, struct status_t * status ) { ++(status->this); @@ -316,36 +299,6 @@ void parse_out( const char * spec, va_list ap ) } } -/* -static void int2base( int value, int base, struct status_t * status ) - -#define E_minus 1<<0 -#define E_plus 1<<1 -#define E_alt 1<<2 -#define E_space 1<<3 -#define E_zero 1<<4 -#define E_done 1<<5 -#define E_char 1<<6 -#define E_short 1<<7 -#define E_long 1<<8 -#define E_llong 1<<9 -#define E_intmax 1<<10 -#define E_size 1<<11 -#define E_ptrdiff 1<<12 -#define E_double 1<<13 -#define E_lower 1<<14 - - struct status_t -{ - int flags; - size_t n; - size_t i; - char * s; - size_t width; - size_t prec; -}; -*/ - #define TESTCASE( _flags, _n, _width, _prec, _value, _base, _expect ) \ status.flags = _flags | E_term; \ status.n = _n; \ @@ -359,7 +312,7 @@ static void int2base( int value, int base, struct status_t * status ) rc = snprintf( buffer, _n, _expect, _value ); \ if ( ( strcmp( status.s, buffer ) != 0 ) || ( status.i != rc ) ) \ { \ - printf( "Output '%s', RC %d\nExpect '%s', RC %d\n", status.s, status.i, buffer, rc ); \ + printf( "Output '%s', RC %d\nExpect '%s', RC %d\n\n", status.s, status.i, buffer, rc ); \ } \ int main() @@ -369,6 +322,7 @@ int main() int tmp; char * buffer = malloc( 50 ); status.s = malloc( 50 ); +#if 0 TESTCASE( E_plus, 5, 0, 0, 1234, 10, "%+d" ); TESTCASE( E_space, 3, 0, 0, 1234, 10, "% d" ); TESTCASE( E_space, 3, 0, 0, -1234, 10, "% d" ); @@ -391,9 +345,9 @@ int main() TESTCASE( E_space, 6, 6, 0, 1234, 10, "% 6d" ); TESTCASE( E_space, 6, 6, 0, -1234, 10, "% 6d" ); TESTCASE( E_space | E_minus, 6, 6, 0, -1234, 10, "%- 6d" ); - - puts( "--- Serious Tests ---" ); - puts( "- Signed min / max -" ); +#endif + puts( "--- Serious Tests ---\n" ); + puts( "- Signed min / max -\n" ); TESTCASE( E_done, SIZE_MAX, 0, 0, CHAR_MIN, 10, "%hhd" ); TESTCASE( E_done, SIZE_MAX, 0, 0, CHAR_MAX, 10, "%hhd" ); TESTCASE( E_done, SIZE_MAX, 0, 0, 0, 10, "%hhd" ); @@ -409,5 +363,16 @@ int main() TESTCASE( E_done, SIZE_MAX, 0, 0, LLONG_MIN, 10, "%lld" ); TESTCASE( E_done, SIZE_MAX, 0, 0, LLONG_MAX, 10, "%lld" ); TESTCASE( E_done, SIZE_MAX, 0, 0, 0ll, 10, "%lld" ); + puts( "- Unsigned min / max -\n" ); + TESTCASE( E_done, SIZE_MAX, 0, 0, UCHAR_MAX, 10, "%hhu" ); + TESTCASE( E_done, SIZE_MAX, 0, 0, -1, 10, "%hhu" ); + TESTCASE( E_done, SIZE_MAX, 0, 0, USHRT_MAX, 10, "%hu" ); + TESTCASE( E_done, SIZE_MAX, 0, 0, -1, 10, "%hu" ); + TESTCASE( E_done, SIZE_MAX, 0, 0, UINT_MAX, 10, "%u" ); + TESTCASE( E_done, SIZE_MAX, 0, 0, -1, 10, "%u" ); + TESTCASE( E_done, SIZE_MAX, 0, 0, ULONG_MAX, 10, "%lu" ); + TESTCASE( E_done, SIZE_MAX, 0, 0, -1l, 10, "%lu" ); + TESTCASE( E_done, SIZE_MAX, 0, 0, ULLONG_MAX, 10, "%llu" ); + TESTCASE( E_done, SIZE_MAX, 0, 0, -1ll, 10, "%llu" ); return 0; }