Skip to content

Commit e903f8a

Browse files
committed
fix: not adjusting head after realloc
- Big bug - Tests passing - Added test for push_ex
1 parent 0973dd7 commit e903f8a

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

source/utils/queue.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void queue_init_data(struct queue* qu, size_t capacity)
2626
qu->data = new_data;
2727
qu->capacity = capacity;
2828
qu->free_capacity = capacity - qu->size;
29+
qu->head = qu->data + qu->size;
2930
}
3031

3132
void queue_expand_capacity(struct queue* qu, size_t size)
@@ -34,11 +35,11 @@ void queue_expand_capacity(struct queue* qu, size_t size)
3435
if (size ==0)
3536
return;
3637

37-
if (qu->size + size < qu->free_capacity)
38+
if (qu->size + size < (qu->free_capacity * QUEUE_FREE_CAP_BUFFER))
3839
return;
3940

4041
size_t new_cap = qu->capacity * 2, new_free_cap = new_cap - qu->size;
41-
while (qu->size + size >= new_free_cap) {
42+
while (qu->size + size >= (new_free_cap * QUEUE_FREE_CAP_BUFFER)) {
4243
new_cap *= 2;
4344
new_free_cap = new_cap - qu->size;
4445
}
@@ -71,8 +72,6 @@ void queue_init(struct queue** qu, size_t capacity)
7172
(*qu)->size = 0;
7273
(*qu)->data = NULL;
7374
queue_init_data(*qu, capacity);
74-
75-
(*qu)->head = (*qu)->data;
7675
}
7776

7877
/**
@@ -116,7 +115,7 @@ void queue_push(struct queue* qu, char* data, size_t size)
116115
return;
117116
}
118117

119-
if (qu->size + size >= qu->free_capacity) {
118+
if (qu->size + size >= (qu->free_capacity * QUEUE_FREE_CAP_BUFFER)) {
120119
queue_expand_capacity(qu, size);
121120
log_info("queue_push: expanding current capacity to %d\n", qu->capacity);
122121
}
@@ -146,7 +145,7 @@ void queue_push_ex(struct queue* qu, size_t size)
146145
return;
147146
}
148147

149-
if (qu->size + size >= qu->free_capacity) {
148+
if (qu->size + size >= (qu->free_capacity * QUEUE_FREE_CAP_BUFFER)) {
150149
queue_expand_capacity(qu, size);
151150
log_info("queue_push: expanding current capacity to %d\n", qu->capacity);
152151
}

source/utils/queue.h

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
extern "C" {
66
#endif
77

8+
#define QUEUE_FREE_CAP_BUFFER 0.5 // Percentage
9+
810
struct queue {
911
char* data;
1012
size_t capacity;

test/queue/queue-test.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,19 @@ TEST_CASE("Queue Edge Cases")
9090

9191
// As it should over push the capacity, size should remain the same.
9292
REQUIRE(qu->size == 9);
93-
REQUIRE(qu->capacity == 20);
93+
REQUIRE(qu->capacity == 40);
94+
}
95+
96+
SECTION("Push Ex Over Capacity")
97+
{
98+
size_t s = 4;
99+
for (size_t k = 0; k < 18; k++) {
100+
memcpy(qu->head, const_cast<char*>("world"), 5);
101+
queue_push_ex(qu, 5);
102+
103+
// As it should over push the capacity, size should remain the same.
104+
REQUIRE(qu->size == s + ((k + 1) * 5));
105+
}
94106
}
95107

96108
SECTION("Pop Empty Queue")

0 commit comments

Comments
 (0)