| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************/ | ||
| 2 | /* Copyright 2009-2024 Barcelona Supercomputing Center */ | ||
| 3 | /* */ | ||
| 4 | /* This file is part of the DLB library. */ | ||
| 5 | /* */ | ||
| 6 | /* DLB is free software: you can redistribute it and/or modify */ | ||
| 7 | /* it under the terms of the GNU Lesser General Public License as published by */ | ||
| 8 | /* the Free Software Foundation, either version 3 of the License, or */ | ||
| 9 | /* (at your option) any later version. */ | ||
| 10 | /* */ | ||
| 11 | /* DLB is distributed in the hope that it will be useful, */ | ||
| 12 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | ||
| 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | ||
| 14 | /* GNU Lesser General Public License for more details. */ | ||
| 15 | /* */ | ||
| 16 | /* You should have received a copy of the GNU Lesser General Public License */ | ||
| 17 | /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */ | ||
| 18 | /*********************************************************************************/ | ||
| 19 | |||
| 20 | #include <assert.h> | ||
| 21 | |||
| 22 | /* array_int */ | ||
| 23 | #define ARRAY_T int | ||
| 24 | #include "support/array_template.h" | ||
| 25 | |||
| 26 | |||
| 27 | typedef struct my_struct_t { | ||
| 28 | char a; | ||
| 29 | double d; | ||
| 30 | int i; | ||
| 31 | } my_struct_t; | ||
| 32 | |||
| 33 | /* array_my_struct_t */ | ||
| 34 | #define ARRAY_T my_struct_t | ||
| 35 | #define ARRAY_KEY_T char | ||
| 36 | #include "support/array_template.h" | ||
| 37 | |||
| 38 | |||
| 39 | 1 | int main(int argc, char *argv[]) { | |
| 40 | |||
| 41 | enum { ARRAY_CAPACITY = 10 }; | ||
| 42 | |||
| 43 | /* int */ | ||
| 44 | { | ||
| 45 | array_int array; | ||
| 46 | 1 | array_int_init(&array, ARRAY_CAPACITY); | |
| 47 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( array.count == 0 ); |
| 48 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( array.items != NULL ); |
| 49 | |||
| 50 | 1 | array_int_push(&array, 42); | |
| 51 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( array.count == 1 ); |
| 52 | 1 | array_int_push(&array, 42); | |
| 53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( array.count == 2 ); |
| 54 | 1 | array_int_push(&array, 0); | |
| 55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( array.count == 3 ); |
| 56 | |||
| 57 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( array_int_get(&array, 0) == 42 ); |
| 58 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( array_int_get(&array, 1) == 42 ); |
| 59 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( array_int_get(&array, 2) == 0 ); |
| 60 | |||
| 61 | 1 | array_int_clear(&array); | |
| 62 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( array.count == 0 ); |
| 63 | 1 | array_int_destroy(&array); | |
| 64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( array.items == NULL ); |
| 65 | } | ||
| 66 | |||
| 67 | /* my_struct_t */ | ||
| 68 | { | ||
| 69 | array_my_struct_t array; | ||
| 70 | 1 | array_my_struct_t_init(&array, ARRAY_CAPACITY); | |
| 71 | |||
| 72 | const my_struct_t struct1 = { .a = 'a', .d = 1.0, .i = 5 }; | ||
| 73 | const my_struct_t struct2 = { .a = 'A', .d = 3.0, .i = 8 }; | ||
| 74 | |||
| 75 | 1 | array_my_struct_t_push(&array, struct1); | |
| 76 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( array.count == 1 ); |
| 77 | 1 | array_my_struct_t_push(&array, struct2); | |
| 78 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( array.count == 2 ); |
| 79 | |||
| 80 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | assert( array.items[0].a == 'a' |
| 81 | && array.items[0].d - 1.0 < 1e-9 | ||
| 82 | && array.items[0].i == 5 ); | ||
| 83 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | assert( array.items[1].a == 'A' |
| 84 | && array.items[1].d - 3.0 < 1e-9 | ||
| 85 | && array.items[1].i == 8 ); | ||
| 86 | |||
| 87 | 1 | my_struct_t item = { .a = 'b', .d = 1.0, .i = 3 }; | |
| 88 | 1 | array_my_struct_t_push(&array, item); | |
| 89 | 1 | item.a = 'r'; | |
| 90 | 1 | array_my_struct_t_push(&array, item); | |
| 91 | 1 | item.a = 'd'; | |
| 92 | 1 | array_my_struct_t_push(&array, item); | |
| 93 | 1 | item.a = 'C'; | |
| 94 | 1 | array_my_struct_t_push(&array, item); | |
| 95 | 1 | array_my_struct_t_sort(&array); | |
| 96 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
|
7 | for (size_t i = 0; i<array.count; ++i) { |
| 97 | 6 | fprintf(stderr, "item %lu: {%c, %f, %d}\n", i, | |
| 98 | 6 | array.items[i].a, | |
| 99 | 6 | array.items[i].d, | |
| 100 | 6 | array.items[i].i); | |
| 101 | } | ||
| 102 | |||
| 103 | 1 | array_my_struct_t_clear(&array); | |
| 104 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( array.count == 0 ); |
| 105 | 1 | array_my_struct_t_destroy(&array); | |
| 106 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( array.items == NULL ); |
| 107 | } | ||
| 108 | |||
| 109 | 1 | return 0; | |
| 110 | } | ||
| 111 |