| 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 | #include <stdio.h> | ||
| 22 | |||
| 23 | /* queue_int */ | ||
| 24 | #define QUEUE_T int | ||
| 25 | #define QUEUE_SIZE 10 | ||
| 26 | #include "support/queue_template.h" | ||
| 27 | |||
| 28 | |||
| 29 | typedef struct my_struct_t { | ||
| 30 | char a; | ||
| 31 | double d; | ||
| 32 | int i; | ||
| 33 | } my_struct_t; | ||
| 34 | |||
| 35 | /* queue_my_struct_t */ | ||
| 36 | #define QUEUE_T my_struct_t | ||
| 37 | #define QUEUE_KEY_T char | ||
| 38 | #define QUEUE_SIZE 10 | ||
| 39 | #include "support/queue_template.h" | ||
| 40 | |||
| 41 | |||
| 42 | 1 | int main(int argc, char *argv[]) { | |
| 43 | |||
| 44 | /* int */ | ||
| 45 | { | ||
| 46 | queue_int queue; | ||
| 47 | 1 | queue_int_init(&queue); | |
| 48 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_int_size(&queue) == 0 ); |
| 49 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_int_empty(&queue) == true ); |
| 50 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_int_has_space(&queue) == true ); |
| 51 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_int_front(&queue) == NULL ); |
| 52 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_int_back(&queue) == NULL ); |
| 53 | |||
| 54 | /* fill and clear */ | ||
| 55 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
|
10 | for (int i = 0; i < 9; ++i) { |
| 56 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
|
9 | assert( queue_int_has_space(&queue) == true ); |
| 57 | 9 | queue_int_enqueue(&queue, i); | |
| 58 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
|
9 | assert( queue_int_empty(&queue) == false ); |
| 59 | } | ||
| 60 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_int_has_space(&queue) == false ); |
| 61 | 1 | queue_int_clear(&queue); | |
| 62 | |||
| 63 | /* enqueue 42 */ | ||
| 64 | 1 | queue_int_enqueue(&queue, 42); | |
| 65 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_int_size(&queue) == 1 ); |
| 66 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( *(int*)queue_int_front(&queue) == 42 ); |
| 67 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( *(int*)queue_int_back(&queue) == 42 ); |
| 68 | |||
| 69 | /* enqueue 50 */ | ||
| 70 | 1 | queue_int_enqueue(&queue, 50); | |
| 71 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_int_size(&queue) == 2 ); |
| 72 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( *(int*)queue_int_front(&queue) == 42 ); |
| 73 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( *(int*)queue_int_back(&queue) == 50 ); |
| 74 | |||
| 75 | /* enqueue 60 */ | ||
| 76 | 1 | queue_int_enqueue(&queue, 60); | |
| 77 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_int_size(&queue) == 3 ); |
| 78 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( *(int*)queue_int_front(&queue) == 42 ); |
| 79 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( *(int*)queue_int_back(&queue) == 60 ); |
| 80 | |||
| 81 | /* dequeue 42 */ | ||
| 82 | int item; | ||
| 83 | 1 | queue_int_dequeue(&queue, &item); | |
| 84 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( item == 42 ); |
| 85 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_int_size(&queue) == 2 ); |
| 86 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( *(int*)queue_int_front(&queue) == 50 ); |
| 87 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( *(int*)queue_int_back(&queue) == 60 ); |
| 88 | |||
| 89 | /* loop in linear queue */ | ||
| 90 | 1 | queue_int_enqueue(&queue, 70); | |
| 91 | 1 | queue_int_enqueue(&queue, 80); | |
| 92 | 1 | queue_int_enqueue(&queue, 90); | |
| 93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( queue.next > queue.front ); |
| 94 | 1 | size_t num_iters = 0; | |
| 95 | 1 | for (int *it = queue_int_front(&queue); | |
| 96 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | it != NULL; |
| 97 | 5 | it = queue_int_next(&queue, it)) { | |
| 98 | 5 | printf("item: %d\n", *it); | |
| 99 | 5 | ++num_iters; | |
| 100 | } | ||
| 101 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( num_iters == queue_int_size(&queue) ); |
| 102 | |||
| 103 | /* remove in linear queue */ | ||
| 104 | 1 | queue_int_remove(&queue, 80); | |
| 105 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( *(int*)queue_int_front(&queue) == 50 ); |
| 106 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( *(int*)queue_int_back(&queue) == 90 ); |
| 107 | 1 | num_iters = 0; | |
| 108 | 1 | for (int *it = queue_int_front(&queue); | |
| 109 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | it != NULL; |
| 110 | 4 | it = queue_int_next(&queue, it)) { | |
| 111 | 4 | printf("item: %d\n", *it); | |
| 112 | 4 | ++num_iters; | |
| 113 | } | ||
| 114 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( num_iters == queue_int_size(&queue) ); |
| 115 | |||
| 116 | /* loop in circular queue */ | ||
| 117 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | for (int i=0; i<5; ++i) { |
| 118 | 5 | queue_int_enqueue(&queue, i); | |
| 119 | 5 | queue_int_dequeue(&queue, &item); | |
| 120 | } | ||
| 121 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( queue.next < queue.front ); |
| 122 | 1 | num_iters = 0; | |
| 123 | 1 | for (int *it = queue_int_front(&queue); | |
| 124 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | it != NULL; |
| 125 | 4 | it = queue_int_next(&queue, it)) { | |
| 126 | 4 | printf("item: %d\n", *it); | |
| 127 | 4 | ++num_iters; | |
| 128 | } | ||
| 129 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( num_iters == queue_int_size(&queue) ); |
| 130 | |||
| 131 | /* remove in circular queue */ | ||
| 132 | 1 | queue_int_remove(&queue, 2); | |
| 133 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( *(int*)queue_int_front(&queue) == 1 ); |
| 134 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( *(int*)queue_int_back(&queue) == 4 ); |
| 135 | 1 | num_iters = 0; | |
| 136 | 1 | for (int *it = queue_int_front(&queue); | |
| 137 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | it != NULL; |
| 138 | 3 | it = queue_int_next(&queue, it)) { | |
| 139 | 3 | printf("item: %d\n", *it); | |
| 140 | 3 | ++num_iters; | |
| 141 | } | ||
| 142 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( num_iters == queue_int_size(&queue) ); |
| 143 | |||
| 144 | /* remove in consecutive positions */ | ||
| 145 | 1 | queue_int_enqueue(&queue, 4); | |
| 146 | 1 | queue_int_enqueue(&queue, 4); | |
| 147 | 1 | queue_int_enqueue(&queue, 4); | |
| 148 | 1 | queue_int_enqueue(&queue, 4); | |
| 149 | 1 | queue_int_enqueue(&queue, 5); | |
| 150 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_int_size(&queue) == 8 ); |
| 151 | 1 | queue_int_remove(&queue, 4); | |
| 152 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_int_size(&queue) == 3 ); |
| 153 | |||
| 154 | /* replace integer, should be no-op */ | ||
| 155 | 1 | queue_int_replace(&queue, 5); | |
| 156 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( *(int*)queue_int_back(&queue) == 5 ); |
| 157 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_int_size(&queue) == 3 ); |
| 158 | |||
| 159 | 1 | queue_int_clear(&queue); | |
| 160 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_int_empty(&queue) ); |
| 161 | } | ||
| 162 | |||
| 163 | /* my_struct_t */ | ||
| 164 | { | ||
| 165 | queue_my_struct_t queue; | ||
| 166 | 1 | queue_my_struct_t_init(&queue); | |
| 167 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_my_struct_t_empty(&queue) ); |
| 168 | |||
| 169 | const my_struct_t struct1 = { .a = 'a', .d = 1.0, .i = 5 }; | ||
| 170 | const my_struct_t struct2 = { .a = 'A', .d = 3.0, .i = 8 }; | ||
| 171 | |||
| 172 | 1 | queue_my_struct_t_enqueue(&queue, struct1); | |
| 173 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_my_struct_t_size(&queue) == 1 ); |
| 174 | 1 | queue_my_struct_t_enqueue(&queue, struct2); | |
| 175 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_my_struct_t_size(&queue) == 2 ); |
| 176 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_my_struct_t_empty(&queue) == false ); |
| 177 | |||
| 178 | 1 | my_struct_t *front = queue_my_struct_t_front(&queue); | |
| 179 | 1 | my_struct_t *back = queue_my_struct_t_back(&queue); | |
| 180 |
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( front->a == 'a' |
| 181 | && front->d - 1.0 < 1e-9 | ||
| 182 | && front->i == 5 ); | ||
| 183 |
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( back->a == 'A' |
| 184 | && back->d - 3.0 < 1e-9 | ||
| 185 | && back->i == 8 ); | ||
| 186 | |||
| 187 | /* replace */ | ||
| 188 | const my_struct_t struct3 = { .a = 'A', .d = 4.0, .i = 9 }; | ||
| 189 | 1 | queue_my_struct_t_replace(&queue, struct3); | |
| 190 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_my_struct_t_size(&queue) == 2 ); |
| 191 | 1 | back = queue_my_struct_t_back(&queue); | |
| 192 |
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( back->a == 'A' |
| 193 | && back->d - 4.0 < 1e-9 | ||
| 194 | && back->i == 9 ); | ||
| 195 | |||
| 196 | 1 | const my_struct_t struct4 = { .a = 'b', .d = 0.0, .i = 0 }; | |
| 197 | const my_struct_t struct5 = { .a = 'c', .d = 1.0, .i = 0 }; | ||
| 198 | const my_struct_t struct6 = { .a = 'd', .d = 2.0, .i = 0 }; | ||
| 199 | 1 | queue_my_struct_t_enqueue(&queue, struct4); | |
| 200 | 1 | queue_my_struct_t_enqueue(&queue, struct5); | |
| 201 | 1 | queue_my_struct_t_enqueue(&queue, struct6); | |
| 202 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_my_struct_t_size(&queue) == 5 ); |
| 203 | |||
| 204 | /* delete front element */ | ||
| 205 | 1 | queue_my_struct_t_delete(&queue, queue_my_struct_t_front(&queue)); | |
| 206 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_my_struct_t_size(&queue) == 4 ); |
| 207 | 1 | front = queue_my_struct_t_front(&queue); | |
| 208 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( front->a == 'A' ); |
| 209 | |||
| 210 | /* delete middle element */ | ||
| 211 | 1 | for (my_struct_t *it = queue_my_struct_t_front(&queue); | |
| 212 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | it != NULL; |
| 213 | 3 | it = queue_my_struct_t_next(&queue, it)) { | |
| 214 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (it->a == 'c') { |
| 215 | 1 | queue_my_struct_t_delete(&queue, it); | |
| 216 | } | ||
| 217 | } | ||
| 218 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_my_struct_t_size(&queue) == 3 ); |
| 219 | |||
| 220 | /* delete last element */ | ||
| 221 | 1 | queue_my_struct_t_delete(&queue, queue_my_struct_t_back(&queue)); | |
| 222 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_my_struct_t_size(&queue) == 2 ); |
| 223 | 1 | back = queue_my_struct_t_back(&queue); | |
| 224 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( back->a == 'b' ); |
| 225 | |||
| 226 | 1 | for (my_struct_t *it = queue_my_struct_t_front(&queue); | |
| 227 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | it != NULL; |
| 228 | 2 | it = queue_my_struct_t_next(&queue, it)) { | |
| 229 | 2 | printf("item: %c\n", it->a); | |
| 230 | } | ||
| 231 | |||
| 232 | /* delete middle element in circular buffer */ | ||
| 233 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | for (int i = 0; i < 5; ++i) { |
| 234 | 5 | my_struct_t tmp = { .a = 'z' };; | |
| 235 | 5 | queue_my_struct_t_dequeue(&queue, &tmp); | |
| 236 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | assert( tmp.a != 'z' ); |
| 237 | 5 | ++tmp.a; | |
| 238 | 5 | queue_my_struct_t_enqueue(&queue, tmp); | |
| 239 | 5 | ++tmp.a; | |
| 240 | 5 | queue_my_struct_t_enqueue(&queue, tmp); | |
| 241 | } | ||
| 242 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_my_struct_t_size(&queue) == 7 ); |
| 243 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | assert( queue.front == 6 && queue.next == 3 ); |
| 244 | /* no-op */ | ||
| 245 | 1 | queue_my_struct_t_delete(&queue, queue.items + 3); | |
| 246 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_my_struct_t_size(&queue) == 7 ); |
| 247 | 1 | queue_my_struct_t_delete(&queue, queue.items + 5); | |
| 248 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_my_struct_t_size(&queue) == 7 ); |
| 249 | /* delete */ | ||
| 250 | 1 | queue_my_struct_t_delete(&queue, queue.items); | |
| 251 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_my_struct_t_size(&queue) == 6 ); |
| 252 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | assert( queue.front == 6 && queue.next == 2 ); |
| 253 | |||
| 254 | 1 | queue_my_struct_t_clear(&queue); | |
| 255 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( queue_my_struct_t_empty(&queue) ); |
| 256 | } | ||
| 257 | |||
| 258 | 1 | return 0; | |
| 259 | } | ||
| 260 |