mirror of
https://github.com/bkthomps/Containers.git
synced 2025-12-05 15:15:49 +00:00
Before this bug was fixed, it would result in a segmentation fault due to an off-by-one error in the trim function in the deque file. This off by one error was due to the fact that the end index points to the index after the last allocated block. However, in the trim function, the end index was treated as the last allocated block. Thus, in the case that the last block was completely full, the trim function would function as if there were another block after that block with one item in it. Thus, this block would be copied from without the memory being allocated.
63 lines
1.6 KiB
C
63 lines
1.6 KiB
C
#include "test.h"
|
|
#include "../src/queue.h"
|
|
|
|
void test_queue(void)
|
|
{
|
|
int val[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
|
queue me = queue_init(sizeof(int));
|
|
assert(me);
|
|
assert(queue_size(me) == 0);
|
|
assert(queue_is_empty(me));
|
|
for (int i = 0; i < 10; i++) {
|
|
queue_push(me, &val[i]);
|
|
int get = 0;
|
|
assert(queue_back(&get, me));
|
|
assert(get == val[i]);
|
|
int stuff = 0;
|
|
assert(queue_front(&stuff, me));
|
|
assert(stuff == 1);
|
|
}
|
|
assert(queue_size(me) == 10);
|
|
assert(!queue_is_empty(me));
|
|
int get_arr[10] = {0};
|
|
queue_copy_to_array(get_arr, me);
|
|
for (int i = 0; i < 10; i++) {
|
|
assert(get_arr[i] == i + 1);
|
|
}
|
|
for (int i = 0; i < 9; i++) {
|
|
int get = 0;
|
|
assert(queue_pop(&get, me));
|
|
assert(get == i + 1);
|
|
}
|
|
queue_trim(me);
|
|
assert(queue_size(me) == 1);
|
|
queue_clear(me);
|
|
assert(queue_size(me) == 0);
|
|
int get = 0;
|
|
assert(!queue_pop(&get, me));
|
|
assert(!queue_front(&get, me));
|
|
assert(!queue_back(&get, me));
|
|
me = queue_destroy(me);
|
|
assert(!me);
|
|
me = queue_init(sizeof(int));
|
|
assert(me);
|
|
for (int i = 123; i < 123456; i++) {
|
|
queue_push(me, &i);
|
|
}
|
|
const int old_size = queue_size(me);
|
|
int pop_count = 0;
|
|
while (!queue_is_empty(me)) {
|
|
queue_pop(&get, me);
|
|
pop_count++;
|
|
}
|
|
assert(pop_count == old_size);
|
|
queue_destroy(me);
|
|
// Testing automatic trim
|
|
me = queue_init(sizeof(int));
|
|
for (int i = 0; i < 100; i++) {
|
|
queue_push(me, &i);
|
|
queue_pop(&get, me);
|
|
}
|
|
queue_destroy(me);
|
|
}
|