[0d10090] | 1 | #include <stdio.h> |
---|
[309be81] | 2 | #include <stdlib.h> |
---|
| 3 | #include "bench.h" |
---|
[87c5f40] | 4 | #include "c-pair.h" |
---|
[309be81] | 5 | #include "c-stack.h" |
---|
[0d10090] | 6 | #include "c-print.h" |
---|
[309be81] | 7 | |
---|
[87c5f40] | 8 | _Bool* new_bool( _Bool b ) { |
---|
[3fb7f5e] | 9 | _Bool* q = malloc(sizeof(_Bool)); /***/ |
---|
[87c5f40] | 10 | *q = b; |
---|
| 11 | return q; |
---|
| 12 | } |
---|
| 13 | |
---|
| 14 | char* new_char( char c ) { |
---|
[3fb7f5e] | 15 | char* q = malloc(sizeof(char)); /***/ |
---|
[87c5f40] | 16 | *q = c; |
---|
| 17 | return q; |
---|
| 18 | } |
---|
| 19 | |
---|
| 20 | int* new_int( int i ) { |
---|
[3fb7f5e] | 21 | int* q = malloc(sizeof(int)); /***/ |
---|
[87c5f40] | 22 | *q = i; |
---|
[122aecd] | 23 | return q; |
---|
| 24 | } |
---|
| 25 | |
---|
[3fb7f5e] | 26 | void* copy_bool( const void* p ) { return new_bool( *(const _Bool*)p ); } /***/ |
---|
| 27 | void* copy_char( const void* p ) { return new_char( *(const char*)p ); } /***/ |
---|
| 28 | void* copy_int( const void* p ) { return new_int( *(const int*)p ); } /***/ |
---|
| 29 | void* copy_pair_bool_char( const void* p ) { return copy_pair( p, copy_bool, copy_char ); } /***/ |
---|
| 30 | void free_pair_bool_char( void* p ) { free_pair( p, free, free ); } /***/ |
---|
[87c5f40] | 31 | |
---|
[a381b46] | 32 | int cmp_bool( const void* a, const void* b ) { /***/ |
---|
| 33 | return *(const _Bool*)a == *(const _Bool*)b ? 0 : *(const _Bool*)a < *(const _Bool*)b ? -1 : 1; |
---|
[87c5f40] | 34 | } |
---|
| 35 | |
---|
[c87cd93] | 36 | int cmp_char( const void* a, const void* b ) { /***/ |
---|
| 37 | return *(const char*)a == *(const char*)b ? 0 : *(const char*)a < *(const char*)b ? -1 : 1; |
---|
[87c5f40] | 38 | } |
---|
| 39 | |
---|
[309be81] | 40 | int main(int argc, char** argv) { |
---|
[6a8ac0b] | 41 | FILE * out = fopen("/dev/null", "w"); |
---|
[c87cd93] | 42 | int maxi = 0, vali = 42; |
---|
| 43 | struct stack si = new_stack(), ti; |
---|
[3fb7f5e] | 44 | |
---|
[c87cd93] | 45 | REPEAT_TIMED( "push_int", N, push_stack( &si, new_int( vali ) ); ) |
---|
| 46 | TIMED( "copy_int", copy_stack( &ti, &si, copy_int ); /***/ ) |
---|
| 47 | TIMED( "clear_int", clear_stack( &si, free ); /***/ ) |
---|
| 48 | REPEAT_TIMED( "pop_int", N, |
---|
| 49 | int* xi = pop_stack( &ti ); |
---|
| 50 | if ( *xi > maxi ) { maxi = *xi; } |
---|
| 51 | free(xi); ) |
---|
| 52 | REPEAT_TIMED( "print_int", N/2, print( out, "dsds", vali, ":", vali, "\n" ); /***/ ) |
---|
[87c5f40] | 53 | |
---|
[c87cd93] | 54 | struct pair * maxp = new_pair( new_bool(0), new_char('\0') ), |
---|
| 55 | * valp = new_pair( new_bool(1), new_char('a') ); |
---|
| 56 | struct stack sp = new_stack(), tp; |
---|
| 57 | |
---|
| 58 | REPEAT_TIMED( "push_pair", N, push_stack( &sp, copy_pair_bool_char( valp ) ); ) |
---|
| 59 | TIMED( "copy_pair", copy_stack( &tp, &sp, copy_pair_bool_char ); /***/ ) |
---|
| 60 | TIMED( "clear_pair", clear_stack( &sp, free_pair_bool_char ); /***/ ) |
---|
| 61 | REPEAT_TIMED( "pop_pair", N, |
---|
| 62 | struct pair * xp = pop_stack( &tp ); |
---|
| 63 | if ( cmp_pair( xp, maxp, cmp_bool, cmp_char /***/ ) > 0 ) { |
---|
| 64 | free_pair_bool_char( maxp ); /***/ |
---|
| 65 | maxp = xp; |
---|
[87c5f40] | 66 | } else { |
---|
[c87cd93] | 67 | free_pair_bool_char( xp ); /***/ |
---|
[a381b46] | 68 | } ) |
---|
[c87cd93] | 69 | REPEAT_TIMED( "print_pair", N/2, print( out, "pbcspbcs", *valp, ":", *valp, "\n" ); /***/ ) |
---|
| 70 | free_pair_bool_char( maxp ); /***/ |
---|
| 71 | free_pair_bool_char( valp ); /***/ |
---|
[0d10090] | 72 | fclose(out); |
---|
[309be81] | 73 | } |
---|