diff options
| author | Thomas Schmucker <ts@its1.de> | 2022-04-10 10:28:39 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2022-04-10 10:28:39 +0200 |
| commit | 424e1f2a3eb59b97462c5e15ef13b80c66f4ba1b (patch) | |
| tree | a25ff97be8d3f0f8630831add02e29960ec9da29 /src | |
| parent | 2ce316a28503bcad9f08210b453a369d55dc8482 (diff) | |
| download | data-structures-424e1f2a3eb59b97462c5e15ef13b80c66f4ba1b.tar.gz data-structures-424e1f2a3eb59b97462c5e15ef13b80c66f4ba1b.tar.bz2 data-structures-424e1f2a3eb59b97462c5e15ef13b80c66f4ba1b.zip | |
feat: added tests
Diffstat (limited to 'src')
| -rw-r--r-- | src/stack-array.c | 106 |
1 files changed, 74 insertions, 32 deletions
diff --git a/src/stack-array.c b/src/stack-array.c index 5f4ba41..a36de7b 100644 --- a/src/stack-array.c +++ b/src/stack-array.c | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | #include <assert.h> | ||
| 1 | #include <stdbool.h> | 2 | #include <stdbool.h> |
| 2 | #include <stdio.h> | 3 | #include <stdio.h> |
| 3 | #include <stdlib.h> | 4 | #include <stdlib.h> |
| @@ -23,37 +24,53 @@ stack_init(struct stack *stack) | |||
| 23 | } | 24 | } |
| 24 | /* -->8-- */ | 25 | /* -->8-- */ |
| 25 | 26 | ||
| 26 | /* --8<-- stack2_push */ | 27 | /* --8<-- stack2_grow_if_needed */ |
| 27 | bool | 28 | static bool |
| 28 | stack_push(struct stack *stack, T data) | 29 | stack_grow_if_needed(struct stack *stack) |
| 29 | { | 30 | { |
| 30 | if ( stack->array == NULL ) { | 31 | if ( stack->p == stack->sz ) { |
| 31 | if ( (stack->array = malloc(10 * sizeof *stack->array)) != NULL ) { | 32 | if ( stack->array == NULL ) { |
| 32 | stack->sz = 10; | 33 | static const size_t initial_size = 10; |
| 33 | stack->p = 0; | 34 | |
| 35 | if ( (stack->array = reallocarray(NULL, initial_size, sizeof stack->array[0])) != NULL ) { | ||
| 36 | stack->sz = initial_size; | ||
| 37 | stack->p = 0; | ||
| 38 | } | ||
| 39 | else { | ||
| 40 | ERROR("out of memory"); | ||
| 41 | return false; | ||
| 42 | } | ||
| 34 | } | 43 | } |
| 35 | else { | 44 | else { |
| 36 | ERROR("out of memory"); | 45 | const size_t new_size = stack->sz * 3 / 2; |
| 37 | return false; | 46 | |
| 47 | T *new_array; | ||
| 48 | |||
| 49 | if ( (new_array = reallocarray(stack->array, new_size, sizeof stack->array[0])) != NULL ) { | ||
| 50 | stack->array = new_array; | ||
| 51 | stack->sz = new_size; | ||
| 52 | } | ||
| 53 | else { | ||
| 54 | ERROR("out of memory"); | ||
| 55 | return false; | ||
| 56 | } | ||
| 38 | } | 57 | } |
| 39 | } | 58 | } |
| 59 | return true; | ||
| 60 | } | ||
| 61 | /* -->8-- */ | ||
| 40 | 62 | ||
| 41 | if ( stack->p == stack->sz ) { | 63 | /* --8<-- stack2_push */ |
| 42 | size_t new_sz; | 64 | bool |
| 43 | T * new_array; | 65 | stack_push(struct stack *stack, T data) |
| 66 | { | ||
| 67 | bool success = stack_grow_if_needed(stack); | ||
| 44 | 68 | ||
| 45 | if ( (new_array = realloc(stack->array, (new_sz = (stack->sz * 3 / 2)) * sizeof *stack->array)) != NULL ) { | 69 | if ( success ) { |
| 46 | stack->array = new_array; | 70 | stack->array[stack->p++] = data; |
| 47 | stack->sz = new_sz; | ||
| 48 | } | ||
| 49 | else { | ||
| 50 | ERROR("out of memory"); | ||
| 51 | return false; | ||
| 52 | } | ||
| 53 | } | 71 | } |
| 54 | 72 | ||
| 55 | stack->array[stack->p++] = data; | 73 | return success; |
| 56 | return true; | ||
| 57 | } | 74 | } |
| 58 | /* -->8-- */ | 75 | /* -->8-- */ |
| 59 | 76 | ||
| @@ -90,26 +107,51 @@ stack_free(struct stack *stack) | |||
| 90 | } | 107 | } |
| 91 | /* -->8-- */ | 108 | /* -->8-- */ |
| 92 | 109 | ||
| 93 | int | 110 | void |
| 94 | main() | 111 | test_stack(void) |
| 95 | { | 112 | { |
| 96 | struct stack stack[1]; | 113 | struct stack stack[1]; |
| 97 | 114 | ||
| 98 | stack_init(stack); | 115 | stack_init(stack); |
| 99 | 116 | ||
| 100 | for ( int i = 0; i != 20; ++i ) | 117 | assert(stack_empty(stack) == true); |
| 118 | |||
| 119 | for ( int i = 0; i != 5; ++i ) | ||
| 101 | stack_push(stack, i); | 120 | stack_push(stack, i); |
| 102 | 121 | ||
| 103 | while ( !stack_empty(stack) ) { | 122 | assert(stack_empty(stack) == false); |
| 104 | int i; | ||
| 105 | 123 | ||
| 106 | if ( stack_pop(stack, &i) ) | 124 | T i; |
| 107 | printf("%d\n", i); | 125 | assert(stack_pop(stack, &i) == true); |
| 108 | else | 126 | assert(i == 4); |
| 109 | ERROR("this shouldn't happen!"); | 127 | assert(stack_pop(stack, &i) == true); |
| 110 | } | 128 | assert(i == 3); |
| 129 | assert(stack_pop(stack, &i) == true); | ||
| 130 | assert(i == 2); | ||
| 131 | assert(stack_pop(stack, &i) == true); | ||
| 132 | assert(i == 1); | ||
| 133 | assert(stack_pop(stack, &i) == true); | ||
| 134 | assert(i == 0); | ||
| 135 | |||
| 136 | assert(stack_pop(stack, &i) == false); | ||
| 137 | |||
| 138 | assert(stack_empty(stack) == true); | ||
| 139 | |||
| 140 | assert(stack[0].sz == 10); | ||
| 141 | assert(stack[0].p == 0); | ||
| 142 | |||
| 143 | for ( int i = 0; i != 23; ++i ) | ||
| 144 | stack_push(stack, i); | ||
| 145 | |||
| 146 | assert(stack[0].sz == 33); | ||
| 147 | assert(stack[0].p == 23); | ||
| 111 | 148 | ||
| 112 | stack_free(stack); | 149 | stack_free(stack); |
| 150 | } | ||
| 113 | 151 | ||
| 152 | int | ||
| 153 | main() | ||
| 154 | { | ||
| 155 | test_stack(); | ||
| 114 | return EXIT_SUCCESS; | 156 | return EXIT_SUCCESS; |
| 115 | } | 157 | } |
