[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 ); } /***/ |
---|
[87c5f40] | 27 | |
---|
[3fb7f5e] | 28 | void* copy_char( const void* p ) { return new_char( *(const char*)p ); } /***/ |
---|
[87c5f40] | 29 | |
---|
[3fb7f5e] | 30 | void* copy_int( const void* p ) { return new_int( *(const int*)p ); } /***/ |
---|
[87c5f40] | 31 | |
---|
[3fb7f5e] | 32 | void* copy_pair_bool_char( const void* p ) { return copy_pair( p, copy_bool, copy_char ); } /***/ |
---|
[87c5f40] | 33 | |
---|
[3fb7f5e] | 34 | void free_pair_bool_char( void* p ) { free_pair( p, free, free ); } /***/ |
---|
[87c5f40] | 35 | |
---|
| 36 | int cmp_bool( const void* a, const void* b ) { |
---|
[3fb7f5e] | 37 | return *(const _Bool*)a == *(const _Bool*)b ? 0 : *(const _Bool*)a < *(const _Bool*)b ? -1 : 1; /***/ |
---|
[87c5f40] | 38 | } |
---|
| 39 | |
---|
| 40 | int cmp_char( const void* a, const void* b ) { |
---|
[3fb7f5e] | 41 | return *(const char*)a == *(const char*)b ? 0 : *(const char*)a < *(const char*)b ? -1 : 1; /***/ |
---|
[87c5f40] | 42 | } |
---|
| 43 | |
---|
[309be81] | 44 | int main(int argc, char** argv) { |
---|
[3fb7f5e] | 45 | FILE* out = fopen("c-out.txt", "w"); |
---|
[309be81] | 46 | srand(20171025); |
---|
| 47 | |
---|
| 48 | struct stack s = new_stack(); |
---|
| 49 | REPEAT_TIMED( "push_int", |
---|
[87c5f40] | 50 | push_stack(&s, new_int( rand() )); |
---|
[309be81] | 51 | ) |
---|
| 52 | |
---|
[122aecd] | 53 | struct stack t; |
---|
| 54 | TIMED( "copy_int", |
---|
[3fb7f5e] | 55 | copy_stack(&t, &s, copy_int); /***/ |
---|
[122aecd] | 56 | ) |
---|
| 57 | |
---|
| 58 | TIMED( "clear_int", |
---|
[3fb7f5e] | 59 | clear_stack(&s, free); /***/ |
---|
[122aecd] | 60 | ) |
---|
| 61 | |
---|
[87c5f40] | 62 | int max = 0; |
---|
[122aecd] | 63 | REPEAT_TIMED( "pop_int", |
---|
[3fb7f5e] | 64 | int* x = pop_stack(&t); /***/ |
---|
[87c5f40] | 65 | if ( *x > max ) { max = *x; } |
---|
[122aecd] | 66 | free(x); |
---|
| 67 | ) |
---|
[3fb7f5e] | 68 | print( out, "d", max, "\n" ); /***/ |
---|
| 69 | |
---|
| 70 | REPEAT_N_TIMED( "print_int", N/2, |
---|
| 71 | print( out, "dsds", rand(), ":", rand(), "\n" ); /***/ |
---|
| 72 | ) |
---|
[87c5f40] | 73 | |
---|
| 74 | struct stack s2 = new_stack(); |
---|
| 75 | REPEAT_TIMED( "push_bool_char", |
---|
| 76 | push_stack(&s2, new_pair( new_bool( rand() & 0x1 ), new_char( rand() & 0x7F ) )); |
---|
| 77 | ) |
---|
| 78 | |
---|
| 79 | struct stack t2; |
---|
| 80 | TIMED( "copy_bool_char", |
---|
[3fb7f5e] | 81 | copy_stack(&t2, &s2, copy_pair_bool_char); /***/ |
---|
[87c5f40] | 82 | ) |
---|
| 83 | |
---|
| 84 | TIMED( "clear_bool_char", |
---|
[3fb7f5e] | 85 | clear_stack(&s2, free_pair_bool_char); /***/ |
---|
[87c5f40] | 86 | ) |
---|
| 87 | |
---|
| 88 | struct pair* max2 = new_pair( new_bool(0), new_char('\0') ); |
---|
| 89 | REPEAT_TIMED( "pop_bool_char", |
---|
[3fb7f5e] | 90 | struct pair* x = pop_stack(&t2); /***/ |
---|
| 91 | if ( cmp_pair( x, max2, cmp_bool, cmp_char ) > 0 ) { /***/ |
---|
| 92 | free_pair_bool_char( max2 ); /***/ |
---|
[87c5f40] | 93 | max2 = x; |
---|
| 94 | } else { |
---|
[3fb7f5e] | 95 | free_pair_bool_char( x ); /***/ |
---|
[87c5f40] | 96 | } |
---|
| 97 | ) |
---|
[3fb7f5e] | 98 | print( out, "pbc", *max2, "\n" ); /***/ |
---|
| 99 | free_pair_bool_char( max2 ); /***/ |
---|
| 100 | |
---|
| 101 | REPEAT_N_TIMED( "print_pair", N/2, |
---|
| 102 | struct pair p1 = ((struct pair){ new_bool(rand() & 0x1), new_char(rand() & 0x7F) }); /***/ |
---|
| 103 | struct pair p2 = ((struct pair){ new_bool(rand() & 0x1), new_char(rand() & 0x7F) }); /***/ |
---|
| 104 | print( out, "pbcspbcs", p1, ":", p2, "\n" ); /***/ |
---|
| 105 | free(p1.first); free(p1.second); /***/ |
---|
| 106 | free(p2.first); free(p2.second); /***/ |
---|
[0d10090] | 107 | ) |
---|
[3fb7f5e] | 108 | |
---|
[0d10090] | 109 | fclose(out); |
---|
[309be81] | 110 | } |
---|