Compare commits
9 Commits
1e536cc3ba
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d3452f523 | ||
|
|
8e41b627bc | ||
|
|
7a54c28c0f | ||
|
|
bd659e64fc | ||
|
|
21ac756fad | ||
|
|
243f04c0ca | ||
| 4cc8cb3d25 | |||
| a9f5b9c3c6 | |||
| 9af9cedd51 |
@@ -7,8 +7,7 @@
|
|||||||
#include "../../common/aliases/aliases.h"
|
#include "../../common/aliases/aliases.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
#define _offset_pointer(PTR, OFFSET) ((void *)((uptr)(PTR) + (OFFSET)))
|
#define _array_header(ARRAY) (ArrayHeader *)(wapp_pointer_offset(ARRAY, (i64)sizeof(ArrayHeader) * -1))
|
||||||
#define _array_header(ARRAY) (ArrayHeader *)(_offset_pointer(ARRAY, (i64)sizeof(ArrayHeader) * -1))
|
|
||||||
|
|
||||||
wapp_persist inline void _array_validate(const GenericArray array, u64 item_size);
|
wapp_persist inline void _array_validate(const GenericArray array, u64 item_size);
|
||||||
|
|
||||||
@@ -55,7 +54,7 @@ void *_array_get(GenericArray array, u64 index, u64 item_size) {
|
|||||||
ArrayHeader *header = _array_header(array);
|
ArrayHeader *header = _array_header(array);
|
||||||
wapp_runtime_assert(index < header->count, "`index` is out of bounds");
|
wapp_runtime_assert(index < header->count, "`index` is out of bounds");
|
||||||
|
|
||||||
return _offset_pointer(array, header->item_size * index);
|
return wapp_pointer_offset(array, header->item_size * index);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _array_set(GenericArray array, u64 index, void *value, u64 item_size) {
|
void _array_set(GenericArray array, u64 index, void *value, u64 item_size) {
|
||||||
@@ -86,7 +85,7 @@ void _array_extend_capped(GenericArray dst, const GenericArray src, u64 item_siz
|
|||||||
u64 remaining_capacity = dst_header->capacity - dst_header->count;
|
u64 remaining_capacity = dst_header->capacity - dst_header->count;
|
||||||
|
|
||||||
u64 copy_count = src_header->count < remaining_capacity ? src_header->count : remaining_capacity;
|
u64 copy_count = src_header->count < remaining_capacity ? src_header->count : remaining_capacity;
|
||||||
void *dst_ptr = _offset_pointer(dst, dst_header->count * dst_header->item_size);
|
void *dst_ptr = wapp_pointer_offset(dst, dst_header->count * dst_header->item_size);
|
||||||
memcpy(dst_ptr, src, copy_count * src_header->item_size);
|
memcpy(dst_ptr, src, copy_count * src_header->item_size);
|
||||||
dst_header->count += copy_count;
|
dst_header->count += copy_count;
|
||||||
}
|
}
|
||||||
@@ -105,7 +104,8 @@ void _array_copy_capped(GenericArray dst, const GenericArray src, u64 item_size)
|
|||||||
dst_header->count = copy_count;
|
dst_header->count = copy_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array, void *value, u64 item_size) {
|
GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array, void *value,
|
||||||
|
ArrayInitFlags flags, u64 item_size) {
|
||||||
wapp_runtime_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL");
|
wapp_runtime_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL");
|
||||||
_array_validate(array, item_size);
|
_array_validate(array, item_size);
|
||||||
|
|
||||||
@@ -114,7 +114,8 @@ GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array,
|
|||||||
ArrayHeader *header = _array_header(array);
|
ArrayHeader *header = _array_header(array);
|
||||||
if (header->count >= header->capacity) {
|
if (header->count >= header->capacity) {
|
||||||
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(header->capacity * 2);
|
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(header->capacity * 2);
|
||||||
output = (GenericArray )_array_alloc_capacity(allocator, new_capacity, header->item_size, false);
|
output = (GenericArray )_array_alloc_capacity(allocator, new_capacity, flags,
|
||||||
|
header->item_size);
|
||||||
if (!output) {
|
if (!output) {
|
||||||
output = array;
|
output = array;
|
||||||
goto RETURN_ARRAY_APPEND_ALLOC;
|
goto RETURN_ARRAY_APPEND_ALLOC;
|
||||||
@@ -124,11 +125,16 @@ GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array,
|
|||||||
|
|
||||||
_array_append_capped(output, value, item_size);
|
_array_append_capped(output, value, item_size);
|
||||||
|
|
||||||
|
if ((flags & ARRAY_INIT_FILLED) == ARRAY_INIT_FILLED) {
|
||||||
|
_array_set_count(output, _array_capacity(output));
|
||||||
|
}
|
||||||
|
|
||||||
RETURN_ARRAY_APPEND_ALLOC:
|
RETURN_ARRAY_APPEND_ALLOC:
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src, u64 item_size) {
|
GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src,
|
||||||
|
ArrayInitFlags flags, u64 item_size) {
|
||||||
wapp_runtime_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL");
|
wapp_runtime_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL");
|
||||||
_array_validate(dst, item_size);
|
_array_validate(dst, item_size);
|
||||||
_array_validate(src, item_size);
|
_array_validate(src, item_size);
|
||||||
@@ -140,7 +146,8 @@ GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, c
|
|||||||
u64 remaining_capacity = dst_header->capacity - dst_header->count;
|
u64 remaining_capacity = dst_header->capacity - dst_header->count;
|
||||||
if (src_header->count >= remaining_capacity) {
|
if (src_header->count >= remaining_capacity) {
|
||||||
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst_header->capacity * 2);
|
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst_header->capacity * 2);
|
||||||
output = (GenericArray )_array_alloc_capacity(allocator, new_capacity, dst_header->item_size, false);
|
output = (GenericArray )_array_alloc_capacity(allocator, new_capacity,
|
||||||
|
flags, dst_header->item_size);
|
||||||
if (!output) {
|
if (!output) {
|
||||||
output = dst;
|
output = dst;
|
||||||
goto RETURN_ARRAY_EXTEND_ALLOC;
|
goto RETURN_ARRAY_EXTEND_ALLOC;
|
||||||
@@ -150,11 +157,16 @@ GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, c
|
|||||||
|
|
||||||
_array_extend_capped(output, src, item_size);
|
_array_extend_capped(output, src, item_size);
|
||||||
|
|
||||||
|
if ((flags & ARRAY_INIT_FILLED) == ARRAY_INIT_FILLED) {
|
||||||
|
_array_set_count(output, _array_capacity(output));
|
||||||
|
}
|
||||||
|
|
||||||
RETURN_ARRAY_EXTEND_ALLOC:
|
RETURN_ARRAY_EXTEND_ALLOC:
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src, u64 item_size) {
|
GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src,
|
||||||
|
ArrayInitFlags flags, u64 item_size) {
|
||||||
wapp_runtime_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL");
|
wapp_runtime_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL");
|
||||||
_array_validate(dst, item_size);
|
_array_validate(dst, item_size);
|
||||||
_array_validate(src, item_size);
|
_array_validate(src, item_size);
|
||||||
@@ -166,7 +178,7 @@ GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, con
|
|||||||
if (src_header->count >= dst_header->capacity) {
|
if (src_header->count >= dst_header->capacity) {
|
||||||
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst_header->capacity * 2);
|
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst_header->capacity * 2);
|
||||||
output = (GenericArray )_array_alloc_capacity(allocator, new_capacity,
|
output = (GenericArray )_array_alloc_capacity(allocator, new_capacity,
|
||||||
src_header->item_size, false);
|
flags, src_header->item_size);
|
||||||
if (!output) {
|
if (!output) {
|
||||||
output = dst;
|
output = dst;
|
||||||
goto RETURN_ARRAY_COPY_ALLOC;
|
goto RETURN_ARRAY_COPY_ALLOC;
|
||||||
@@ -175,6 +187,10 @@ GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, con
|
|||||||
|
|
||||||
_array_copy_capped(output, src, item_size);
|
_array_copy_capped(output, src, item_size);
|
||||||
|
|
||||||
|
if ((flags & ARRAY_INIT_FILLED) == ARRAY_INIT_FILLED) {
|
||||||
|
_array_set_count(output, _array_capacity(output));
|
||||||
|
}
|
||||||
|
|
||||||
RETURN_ARRAY_COPY_ALLOC:
|
RETURN_ARRAY_COPY_ALLOC:
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
@@ -204,28 +220,45 @@ u64 _array_calc_alloc_size(u64 capacity, u64 item_size) {
|
|||||||
return sizeof(ArrayHeader) + item_size * capacity;
|
return sizeof(ArrayHeader) + item_size * capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
GenericArray _array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size,
|
GenericArray _array_alloc_capacity(const Allocator *allocator, u64 capacity, ArrayInitFlags flags,
|
||||||
ArrayInitFlags flags) {
|
u64 item_size) {
|
||||||
wapp_runtime_assert(allocator != NULL, "`allocator` should not be NULL");
|
wapp_runtime_assert(allocator != NULL, "`allocator` should not be NULL");
|
||||||
|
|
||||||
GenericArray output = NULL;
|
GenericArray output = NULL;
|
||||||
|
|
||||||
u64 allocation_size = _array_calc_alloc_size(capacity, item_size);
|
u64 allocation_size = _array_calc_alloc_size(capacity, item_size);
|
||||||
ArrayHeader *header = wapp_mem_allocator_alloc(allocator, allocation_size);
|
void *buffer = wapp_mem_allocator_alloc(allocator, allocation_size);
|
||||||
if (!header) {
|
if (!buffer) {
|
||||||
goto RETURN_ARRAY_ALLOC;
|
goto RETURN_ARRAY_ALLOC;
|
||||||
}
|
}
|
||||||
|
|
||||||
output = (u8 *)(header + 1);
|
output = _array_from_preallocated_buffer(buffer, allocation_size, flags, item_size);
|
||||||
header->magic = WAPP_ARRAY_MAGIC;
|
|
||||||
header->count = flags & ARRAY_INIT_FILLED ? capacity : 0;
|
|
||||||
header->capacity = capacity;
|
|
||||||
header->item_size = item_size;
|
|
||||||
|
|
||||||
RETURN_ARRAY_ALLOC:
|
RETURN_ARRAY_ALLOC:
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
GenericArray _array_from_preallocated_buffer(void *buffer, u64 buffer_size, ArrayInitFlags flags,
|
||||||
|
u64 item_size) {
|
||||||
|
wapp_runtime_assert(buffer != NULL, "`buffer` should not be NULL");
|
||||||
|
|
||||||
|
i64 data_buffer_size = (i64)buffer_size - (i64)(sizeof(ArrayHeader));
|
||||||
|
if (data_buffer_size <= 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 item_capacity = (u64)data_buffer_size / item_size;
|
||||||
|
ArrayHeader *header = (ArrayHeader *)buffer;
|
||||||
|
GenericArray output = (u8 *)(header + 1);
|
||||||
|
header->magic = WAPP_ARRAY_MAGIC;
|
||||||
|
header->count = flags & ARRAY_INIT_FILLED ? item_capacity : 0;
|
||||||
|
header->capacity = item_capacity;
|
||||||
|
header->item_size = item_size;
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
wapp_persist inline void _array_validate(const GenericArray array, u64 item_size) {
|
wapp_persist inline void _array_validate(const GenericArray array, u64 item_size) {
|
||||||
ArrayHeader *header = _array_header(array);
|
ArrayHeader *header = _array_header(array);
|
||||||
wapp_runtime_assert(WAPP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
|
wapp_runtime_assert(WAPP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
|
||||||
|
|||||||
@@ -17,9 +17,6 @@ BEGIN_C_LINKAGE
|
|||||||
#define _calc_array_count(TYPE, ...) wapp_misc_utils_va_args_count(TYPE, __VA_ARGS__)
|
#define _calc_array_count(TYPE, ...) wapp_misc_utils_va_args_count(TYPE, __VA_ARGS__)
|
||||||
#define _calc_array_capacity(TYPE, ...) wapp_misc_utils_u64_round_up_pow2(_calc_array_count(TYPE, __VA_ARGS__) * 2)
|
#define _calc_array_capacity(TYPE, ...) wapp_misc_utils_u64_round_up_pow2(_calc_array_count(TYPE, __VA_ARGS__) * 2)
|
||||||
|
|
||||||
#define wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, FLAGS) \
|
|
||||||
((TYPE *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(TYPE), FLAGS))
|
|
||||||
|
|
||||||
typedef struct Str8 Str8;
|
typedef struct Str8 Str8;
|
||||||
|
|
||||||
// NOTE (Abdelrahman): Typedefs to distinguish arrays from regular pointers
|
// NOTE (Abdelrahman): Typedefs to distinguish arrays from regular pointers
|
||||||
@@ -153,25 +150,33 @@ typedef enum {
|
|||||||
(_array_copy_capped((GenericArray)DST_ARRAY, \
|
(_array_copy_capped((GenericArray)DST_ARRAY, \
|
||||||
(GenericArray)SRC_ARRAY, \
|
(GenericArray)SRC_ARRAY, \
|
||||||
sizeof(TYPE)))
|
sizeof(TYPE)))
|
||||||
#define wapp_array_append_alloc(TYPE, ALLOCATOR_PTR, ARRAY, VALUE_PTR) \
|
#define wapp_array_append_alloc(TYPE, ALLOCATOR_PTR, ARRAY, VALUE_PTR, FLAGS) \
|
||||||
((TYPE *)_array_append_alloc(ALLOCATOR_PTR, \
|
((TYPE *)_array_append_alloc(ALLOCATOR_PTR, \
|
||||||
(GenericArray)ARRAY, \
|
(GenericArray)ARRAY, \
|
||||||
(u8 *)VALUE_PTR, \
|
(u8 *)VALUE_PTR, \
|
||||||
|
FLAGS, \
|
||||||
sizeof(TYPE)))
|
sizeof(TYPE)))
|
||||||
#define wapp_array_extend_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY, SRC_ARRAY) \
|
#define wapp_array_extend_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY, SRC_ARRAY, FLAGS) \
|
||||||
((TYPE *)_array_extend_alloc(ALLOCATOR_PTR, \
|
((TYPE *)_array_extend_alloc(ALLOCATOR_PTR, \
|
||||||
(GenericArray)DST_ARRAY, \
|
(GenericArray)DST_ARRAY, \
|
||||||
(GenericArray)SRC_ARRAY, \
|
(GenericArray)SRC_ARRAY, \
|
||||||
|
FLAGS, \
|
||||||
sizeof(TYPE)))
|
sizeof(TYPE)))
|
||||||
#define wapp_array_copy_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY, SRC_ARRAY) \
|
#define wapp_array_copy_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY, SRC_ARRAY, FLAGS) \
|
||||||
((TYPE *)_array_copy_alloc(ALLOCATOR_PTR, \
|
((TYPE *)_array_copy_alloc(ALLOCATOR_PTR, \
|
||||||
(GenericArray)DST_ARRAY, \
|
(GenericArray)DST_ARRAY, \
|
||||||
(GenericArray)SRC_ARRAY, \
|
(GenericArray)SRC_ARRAY, \
|
||||||
|
FLAGS, \
|
||||||
sizeof(TYPE)))
|
sizeof(TYPE)))
|
||||||
#define wapp_array_clear(TYPE, ARRAY) \
|
#define wapp_array_clear(TYPE, ARRAY) \
|
||||||
(_array_clear((GenericArray)ARRAY, \
|
(_array_clear((GenericArray)ARRAY, \
|
||||||
sizeof(TYPE)))
|
sizeof(TYPE)))
|
||||||
#define wapp_array_calc_alloc_size(TYPE, CAPACITY) _array_calc_alloc_size(CAPACITY, sizeof(TYPE))
|
#define wapp_array_calc_alloc_size(TYPE, CAPACITY) _array_calc_alloc_size(CAPACITY, sizeof(TYPE))
|
||||||
|
#define wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, FLAGS) \
|
||||||
|
((TYPE *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, FLAGS, sizeof(TYPE)))
|
||||||
|
#define wapp_array_from_preallcated_buffer(TYPE, BUFFER, BUFFER_SIZE) \
|
||||||
|
((TYPE *)_array_from_preallcated_buffer(BUFFER, BUFFER_SIZE, sizeof(TYPE)))
|
||||||
|
|
||||||
|
|
||||||
typedef struct header ArrayHeader;
|
typedef struct header ArrayHeader;
|
||||||
struct header {
|
struct header {
|
||||||
@@ -190,14 +195,19 @@ void _array_set(GenericArray array, u64 index, void *value, u64 item_siz
|
|||||||
void _array_append_capped(GenericArray array, void *value, u64 item_size);
|
void _array_append_capped(GenericArray array, void *value, u64 item_size);
|
||||||
void _array_extend_capped(GenericArray dst, const GenericArray src, u64 item_size);
|
void _array_extend_capped(GenericArray dst, const GenericArray src, u64 item_size);
|
||||||
void _array_copy_capped(GenericArray dst, const GenericArray src, u64 item_size);
|
void _array_copy_capped(GenericArray dst, const GenericArray src, u64 item_size);
|
||||||
GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array, void *value, u64 item_size);
|
GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array, void *value,
|
||||||
GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src, u64 item_size);
|
ArrayInitFlags flags, u64 item_size);
|
||||||
GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src, u64 item_size);
|
GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src,
|
||||||
|
ArrayInitFlags flags, u64 item_size);
|
||||||
|
GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src,
|
||||||
|
ArrayInitFlags flags, u64 item_size);
|
||||||
void *_array_pop(GenericArray array, u64 item_size);
|
void *_array_pop(GenericArray array, u64 item_size);
|
||||||
void _array_clear(GenericArray array, u64 item_size);
|
void _array_clear(GenericArray array, u64 item_size);
|
||||||
u64 _array_calc_alloc_size(u64 capacity, u64 item_size);
|
u64 _array_calc_alloc_size(u64 capacity, u64 item_size);
|
||||||
GenericArray _array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size,
|
GenericArray _array_alloc_capacity(const Allocator *allocator, u64 capacity, ArrayInitFlags flags,
|
||||||
ArrayInitFlags flags);
|
u64 item_size);
|
||||||
|
GenericArray _array_from_preallocated_buffer(void *buffer, u64 buffer_size, ArrayInitFlags flags,
|
||||||
|
u64 item_size);
|
||||||
|
|
||||||
#ifdef WAPP_PLATFORM_CPP
|
#ifdef WAPP_PLATFORM_CPP
|
||||||
END_C_LINKAGE
|
END_C_LINKAGE
|
||||||
|
|||||||
85
src/base/queue/queue.c
Normal file
85
src/base/queue/queue.c
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
// vim:fileencoding=utf-8:foldmethod=marker
|
||||||
|
|
||||||
|
#include "queue.h"
|
||||||
|
#include "../array/array.h"
|
||||||
|
#include "../../common/assert/assert.h"
|
||||||
|
#include "../../common/misc/misc_utils.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
void _queue_push(GenericQueue *queue, void *item, u64 item_size) {
|
||||||
|
wapp_debug_assert(queue != NULL, "`queue` should not be NULL");
|
||||||
|
wapp_runtime_assert(item_size == wapp_array_item_size(queue->items), "Invalid type");
|
||||||
|
|
||||||
|
u64 capacity = wapp_array_capacity(queue->items);
|
||||||
|
if (queue->count >= capacity) { return; }
|
||||||
|
|
||||||
|
u64 index = (queue->back)++;
|
||||||
|
_array_set(queue->items, index, item, item_size);
|
||||||
|
++(queue->count);
|
||||||
|
|
||||||
|
if (queue->back >= capacity) {
|
||||||
|
queue->back = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
GenericQueue *_queue_push_alloc(const Allocator *allocator, GenericQueue *queue, void *item, u64 item_size) {
|
||||||
|
wapp_debug_assert(allocator != NULL && queue != NULL && item != NULL,
|
||||||
|
"`allocator`, `queue` and `item` should not be NULL");
|
||||||
|
wapp_runtime_assert(item_size == wapp_array_item_size(queue->items), "Invalid type");
|
||||||
|
|
||||||
|
GenericQueue *output = queue;
|
||||||
|
|
||||||
|
u64 capacity = wapp_array_capacity(queue->items);
|
||||||
|
if (queue->count >= capacity) {
|
||||||
|
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(capacity * 2);
|
||||||
|
u64 array_size = _array_calc_alloc_size(new_capacity, item_size);
|
||||||
|
u64 alloc_size = sizeof(GenericQueue) + array_size;
|
||||||
|
void *buffer = wapp_mem_allocator_alloc(allocator, alloc_size);
|
||||||
|
if (!buffer) {
|
||||||
|
goto RETURN_QUEUE_PUSH_ALLOC;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset((void *)buffer, 0, alloc_size);
|
||||||
|
|
||||||
|
output = (GenericQueue *)buffer;
|
||||||
|
output->items = _array_from_preallocated_buffer((void *)(output + 1), array_size, ARRAY_INIT_FILLED, item_size);
|
||||||
|
|
||||||
|
// NOTE (Abdelrahman): When the queue is full, the front and back indices should
|
||||||
|
// always be the same
|
||||||
|
u64 front_count = capacity - queue->front;
|
||||||
|
u64 back_count = queue->back;
|
||||||
|
void *copy_boundary = (void *)((uptr)(queue->items) + (queue->front * item_size));
|
||||||
|
|
||||||
|
memcpy(output->items, copy_boundary, front_count * item_size);
|
||||||
|
if (back_count > 0) {
|
||||||
|
void *back_copy_dst = (void *)((uptr)(output->items) + (front_count * item_size));
|
||||||
|
memcpy(back_copy_dst, queue->items, back_count * item_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
output->front = 0;
|
||||||
|
output->back = front_count + back_count;
|
||||||
|
output->count = queue->count;
|
||||||
|
}
|
||||||
|
|
||||||
|
_queue_push(output, item, item_size);
|
||||||
|
|
||||||
|
RETURN_QUEUE_PUSH_ALLOC:
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *_queue_pop(GenericQueue *queue, u64 item_size) {
|
||||||
|
wapp_debug_assert(queue != NULL, "`queue` should not be NULL");
|
||||||
|
wapp_runtime_assert(item_size == wapp_array_item_size(queue->items), "Invalid type");
|
||||||
|
|
||||||
|
if (queue->count == 0) { return NULL; }
|
||||||
|
|
||||||
|
u64 index = (queue->front)++;
|
||||||
|
--(queue->count);
|
||||||
|
|
||||||
|
u64 capacity = wapp_array_capacity(queue->items);
|
||||||
|
if (queue->front >= capacity) {
|
||||||
|
queue->front = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _array_get(queue->items, index, item_size);
|
||||||
|
}
|
||||||
100
src/base/queue/queue.h
Normal file
100
src/base/queue/queue.h
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
// vim:fileencoding=utf-8:foldmethod=marker
|
||||||
|
|
||||||
|
#ifndef QUEUE_H
|
||||||
|
#define QUEUE_H
|
||||||
|
|
||||||
|
#include "../array/array.h"
|
||||||
|
#include "../mem/allocator/mem_allocator.h"
|
||||||
|
#include "../../common/aliases/aliases.h"
|
||||||
|
#include "../../common/platform/platform.h"
|
||||||
|
|
||||||
|
#ifdef WAPP_PLATFORM_CPP
|
||||||
|
BEGIN_C_LINKAGE
|
||||||
|
#endif // !WAPP_PLATFORM_CPP
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
GenericArray items;
|
||||||
|
u64 front;
|
||||||
|
u64 back;
|
||||||
|
u64 count;
|
||||||
|
} GenericQueue;
|
||||||
|
|
||||||
|
// NOTE (Abdelrahman): GenericQueue typedefs for readability
|
||||||
|
typedef GenericQueue VoidPtrQueue;
|
||||||
|
typedef GenericQueue C8Queue;
|
||||||
|
typedef GenericQueue C16Queue;
|
||||||
|
typedef GenericQueue C32Queue;
|
||||||
|
typedef GenericQueue U8Queue;
|
||||||
|
typedef GenericQueue U16Queue;
|
||||||
|
typedef GenericQueue U32Queue;
|
||||||
|
typedef GenericQueue U64Queue;
|
||||||
|
typedef GenericQueue B8Queue;
|
||||||
|
typedef GenericQueue I8Queue;
|
||||||
|
typedef GenericQueue I16Queue;
|
||||||
|
typedef GenericQueue I32Queue;
|
||||||
|
typedef GenericQueue I64Queue;
|
||||||
|
typedef GenericQueue F32Queue;
|
||||||
|
typedef GenericQueue F64Queue;
|
||||||
|
typedef GenericQueue F128Queue;
|
||||||
|
typedef GenericQueue UptrQueue;
|
||||||
|
typedef GenericQueue IptrQueue;
|
||||||
|
typedef GenericQueue Str8Queue;
|
||||||
|
|
||||||
|
#ifdef WAPP_PLATFORM_CPP
|
||||||
|
#define wapp_queue(TYPE, CAPACITY) ([&]() { \
|
||||||
|
wapp_persist GenericArray arr = wapp_array_with_capacity(TYPE, CAPACITY, ARRAY_INIT_FILLED); \
|
||||||
|
wapp_persist GenericQueue queue = { \
|
||||||
|
arr, \
|
||||||
|
0, \
|
||||||
|
0, \
|
||||||
|
0, \
|
||||||
|
}; \
|
||||||
|
\
|
||||||
|
return queue; \
|
||||||
|
}())
|
||||||
|
#define wapp_queue_alloc(TYPE, ALLOCATOR_PTR, CAPACITY) ([&]() { \
|
||||||
|
wapp_persist GenericQueue queue = { \
|
||||||
|
wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, ARRAY_INIT_FILLED) \
|
||||||
|
0, \
|
||||||
|
0, \
|
||||||
|
0, \
|
||||||
|
}; \
|
||||||
|
\
|
||||||
|
return queue; \
|
||||||
|
}())
|
||||||
|
#else
|
||||||
|
#define wapp_queue(TYPE, CAPACITY) ((GenericQueue){ \
|
||||||
|
.items = wapp_array_with_capacity(TYPE, CAPACITY, ARRAY_INIT_FILLED), \
|
||||||
|
.front = 0, \
|
||||||
|
.back = 0, \
|
||||||
|
.count = 0, \
|
||||||
|
})
|
||||||
|
#define wapp_queue_alloc(TYPE, ALLOCATOR_PTR, CAPACITY) ((GenericQueue){ \
|
||||||
|
.items = wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, ARRAY_INIT_FILLED), \
|
||||||
|
.front = 0, \
|
||||||
|
.back = 0, \
|
||||||
|
.count = 0, \
|
||||||
|
})
|
||||||
|
#endif // !WAPP_PLATFORM_CPP
|
||||||
|
|
||||||
|
#define wapp_queue_capacity(QUEUE_PTR) (wapp_array_capacity((QUEUE_PTR)->items))
|
||||||
|
#define wapp_queue_item_size(QUEUE_PTR) (wapp_array_item_size((QUEUE_PTR)->items))
|
||||||
|
#define wapp_queue_push(TYPE, QUEUE_PTR, VALUE_PTR) ( \
|
||||||
|
_queue_push(QUEUE_PTR, VALUE_PTR, sizeof(TYPE)) \
|
||||||
|
)
|
||||||
|
#define wapp_queue_push_alloc(TYPE, ALLOCATOR_PTR, QUEUE_PTR, VALUE_PTR) ( \
|
||||||
|
_queue_push_alloc(ALLOCATOR_PTR, QUEUE_PTR, VALUE_PTR, sizeof(TYPE)) \
|
||||||
|
)
|
||||||
|
#define wapp_queue_pop(TYPE, QUEUE_PTR) ( \
|
||||||
|
(TYPE *)_queue_pop(QUEUE_PTR, sizeof(TYPE)) \
|
||||||
|
)
|
||||||
|
|
||||||
|
void _queue_push(GenericQueue *queue, void *item, u64 item_size);
|
||||||
|
GenericQueue *_queue_push_alloc(const Allocator *allocator, GenericQueue *queue, void *item, u64 item_size);
|
||||||
|
void *_queue_pop(GenericQueue *queue, u64 item_size);
|
||||||
|
|
||||||
|
#ifdef WAPP_PLATFORM_CPP
|
||||||
|
END_C_LINKAGE
|
||||||
|
#endif // !WAPP_PLATFORM_CPP
|
||||||
|
|
||||||
|
#endif // !QUEUE_H
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "wapp_base.h"
|
#include "wapp_base.h"
|
||||||
#include "array/array.c"
|
#include "array/array.c"
|
||||||
#include "dbl_list/dbl_list.c"
|
#include "dbl_list/dbl_list.c"
|
||||||
|
#include "queue/queue.c"
|
||||||
#include "mem/allocator/mem_allocator.c"
|
#include "mem/allocator/mem_allocator.c"
|
||||||
#include "mem/utils/mem_utils.c"
|
#include "mem/utils/mem_utils.c"
|
||||||
#include "strings/str8/str8.c"
|
#include "strings/str8/str8.c"
|
||||||
|
|||||||
@@ -3,8 +3,9 @@
|
|||||||
#ifndef WAPP_BASE_H
|
#ifndef WAPP_BASE_H
|
||||||
#define WAPP_BASE_H
|
#define WAPP_BASE_H
|
||||||
|
|
||||||
#include "dbl_list/dbl_list.h"
|
|
||||||
#include "array/array.h"
|
#include "array/array.h"
|
||||||
|
#include "dbl_list/dbl_list.h"
|
||||||
|
#include "queue/queue.h"
|
||||||
#include "mem/allocator/mem_allocator.h"
|
#include "mem/allocator/mem_allocator.h"
|
||||||
#include "mem/utils/mem_utils.h"
|
#include "mem/utils/mem_utils.h"
|
||||||
#include "strings/str8/str8.h"
|
#include "strings/str8/str8.h"
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ BEGIN_C_LINKAGE
|
|||||||
)
|
)
|
||||||
|
|
||||||
#define wapp_is_power_of_two(NUM) ((NUM & (NUM - 1)) == 0)
|
#define wapp_is_power_of_two(NUM) ((NUM & (NUM - 1)) == 0)
|
||||||
|
#define wapp_pointer_offset(PTR, OFFSET) ((void *)((uptr)(PTR) + (OFFSET)))
|
||||||
|
|
||||||
#ifdef WAPP_PLATFORM_CPP
|
#ifdef WAPP_PLATFORM_CPP
|
||||||
END_C_LINKAGE
|
END_C_LINKAGE
|
||||||
|
|||||||
@@ -10,6 +10,12 @@
|
|||||||
#include "../../../common/aliases/aliases.h"
|
#include "../../../common/aliases/aliases.h"
|
||||||
#include "../../../base/array/array.h"
|
#include "../../../base/array/array.h"
|
||||||
#include "../../../base/strings/str8/str8.h"
|
#include "../../../base/strings/str8/str8.h"
|
||||||
|
|
||||||
|
#ifdef WAPP_PLATFORM_APPLE
|
||||||
|
#define _FILE_OFFSET_BITS 64
|
||||||
|
#define lseek64 lseek
|
||||||
|
#endif // !WAPP_PLATFORM_APPLE
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|||||||
@@ -166,7 +166,7 @@ TestFuncResult test_i32_array_append_alloc(void) {
|
|||||||
I32Array array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8);
|
I32Array array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8);
|
||||||
I32Array array2 = wapp_array(i32, 1, 2);
|
I32Array array2 = wapp_array(i32, 1, 2);
|
||||||
|
|
||||||
I32Array arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &((i32){10}));
|
I32Array arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &((i32){10}), ARRAY_INIT_NONE);
|
||||||
result = arr_ptr == array1;
|
result = arr_ptr == array1;
|
||||||
|
|
||||||
u64 count = 4;
|
u64 count = 4;
|
||||||
@@ -174,7 +174,7 @@ TestFuncResult test_i32_array_append_alloc(void) {
|
|||||||
b8 running = true;
|
b8 running = true;
|
||||||
while (running) {
|
while (running) {
|
||||||
i32 num = (i32)index;
|
i32 num = (i32)index;
|
||||||
arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num);
|
arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num, ARRAY_INIT_NONE);
|
||||||
|
|
||||||
++index;
|
++index;
|
||||||
running = index < count;
|
running = index < count;
|
||||||
@@ -194,10 +194,10 @@ TestFuncResult test_i32_array_extend_alloc(void) {
|
|||||||
I32Array array2 = wapp_array(i32, 1, 2);
|
I32Array array2 = wapp_array(i32, 1, 2);
|
||||||
I32Array array3 = wapp_array(i32, 1, 2, 3, 4);
|
I32Array array3 = wapp_array(i32, 1, 2, 3, 4);
|
||||||
|
|
||||||
I32Array arr_ptr = wapp_array_extend_alloc(i32, &allocator, array1, array3);
|
I32Array arr_ptr = wapp_array_extend_alloc(i32, &allocator, array1, array3, ARRAY_INIT_NONE);
|
||||||
result = arr_ptr == array1;
|
result = arr_ptr == array1;
|
||||||
|
|
||||||
arr_ptr = wapp_array_extend_alloc(i32, &allocator, array2, array3);
|
arr_ptr = wapp_array_extend_alloc(i32, &allocator, array2, array3, ARRAY_INIT_NONE);
|
||||||
result = result && arr_ptr != array2;
|
result = result && arr_ptr != array2;
|
||||||
|
|
||||||
wapp_mem_arena_allocator_destroy(&allocator);
|
wapp_mem_arena_allocator_destroy(&allocator);
|
||||||
@@ -215,7 +215,7 @@ TestFuncResult test_i32_array_copy_alloc(void) {
|
|||||||
I32Array array = NULL;
|
I32Array array = NULL;
|
||||||
|
|
||||||
u64 expected_count = 5;
|
u64 expected_count = 5;
|
||||||
array = wapp_array_copy_alloc(i32, &allocator, dst1, src);
|
array = wapp_array_copy_alloc(i32, &allocator, dst1, src, ARRAY_INIT_NONE);
|
||||||
result = wapp_array_count(array) == expected_count && array == dst1;
|
result = wapp_array_count(array) == expected_count && array == dst1;
|
||||||
|
|
||||||
u64 index = 0;
|
u64 index = 0;
|
||||||
@@ -228,7 +228,7 @@ TestFuncResult test_i32_array_copy_alloc(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
expected_count = 5;
|
expected_count = 5;
|
||||||
array = wapp_array_copy_alloc(i32, &allocator, dst2, src);
|
array = wapp_array_copy_alloc(i32, &allocator, dst2, src, ARRAY_INIT_NONE);
|
||||||
result = result && wapp_array_count(array) == expected_count && array != dst2;
|
result = result && wapp_array_count(array) == expected_count && array != dst2;
|
||||||
|
|
||||||
index = 0;
|
index = 0;
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ TestFuncResult test_i32_array_append_alloc(void) {
|
|||||||
I32Array array2 = wapp_array(i32, 1, 2);
|
I32Array array2 = wapp_array(i32, 1, 2);
|
||||||
|
|
||||||
i32 num = 10;
|
i32 num = 10;
|
||||||
I32Array arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &num);
|
I32Array arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &num, ARRAY_INIT_NONE);
|
||||||
result = arr_ptr == array1;
|
result = arr_ptr == array1;
|
||||||
|
|
||||||
u64 count = 4;
|
u64 count = 4;
|
||||||
@@ -177,7 +177,7 @@ TestFuncResult test_i32_array_append_alloc(void) {
|
|||||||
b8 running = true;
|
b8 running = true;
|
||||||
while (running) {
|
while (running) {
|
||||||
num = (i32)index;
|
num = (i32)index;
|
||||||
arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num);
|
arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num, ARRAY_INIT_NONE);
|
||||||
|
|
||||||
++index;
|
++index;
|
||||||
running = index < count;
|
running = index < count;
|
||||||
@@ -197,10 +197,10 @@ TestFuncResult test_i32_array_extend_alloc(void) {
|
|||||||
I32Array array2 = wapp_array(i32, 1, 2);
|
I32Array array2 = wapp_array(i32, 1, 2);
|
||||||
I32Array array3 = wapp_array(i32, 1, 2, 3, 4);
|
I32Array array3 = wapp_array(i32, 1, 2, 3, 4);
|
||||||
|
|
||||||
I32Array array = wapp_array_extend_alloc(i32, &allocator, array1, array3);
|
I32Array array = wapp_array_extend_alloc(i32, &allocator, array1, array3, ARRAY_INIT_NONE);
|
||||||
result = array == array1;
|
result = array == array1;
|
||||||
|
|
||||||
array = wapp_array_extend_alloc(i32, &allocator, array2, array3);
|
array = wapp_array_extend_alloc(i32, &allocator, array2, array3, ARRAY_INIT_NONE);
|
||||||
result = result && array != array2;
|
result = result && array != array2;
|
||||||
|
|
||||||
wapp_mem_arena_allocator_destroy(&allocator);
|
wapp_mem_arena_allocator_destroy(&allocator);
|
||||||
@@ -218,7 +218,7 @@ TestFuncResult test_i32_array_copy_alloc(void) {
|
|||||||
I32Array array = nullptr;
|
I32Array array = nullptr;
|
||||||
|
|
||||||
u64 expected_count = 5;
|
u64 expected_count = 5;
|
||||||
array = wapp_array_copy_alloc(i32, &allocator, dst1, src);
|
array = wapp_array_copy_alloc(i32, &allocator, dst1, src, ARRAY_INIT_NONE);
|
||||||
result = wapp_array_count(array) == expected_count && array == dst1;
|
result = wapp_array_count(array) == expected_count && array == dst1;
|
||||||
|
|
||||||
u64 index = 0;
|
u64 index = 0;
|
||||||
@@ -231,7 +231,7 @@ TestFuncResult test_i32_array_copy_alloc(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
expected_count = 5;
|
expected_count = 5;
|
||||||
array = wapp_array_copy_alloc(i32, &allocator, dst2, src);
|
array = wapp_array_copy_alloc(i32, &allocator, dst2, src, ARRAY_INIT_NONE);
|
||||||
result = result && wapp_array_count(array) == expected_count && array != dst2;
|
result = result && wapp_array_count(array) == expected_count && array != dst2;
|
||||||
|
|
||||||
index = 0;
|
index = 0;
|
||||||
|
|||||||
98
tests/queue/test_queue.c
Normal file
98
tests/queue/test_queue.c
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
// vim:fileencoding=utf-8:foldmethod=marker
|
||||||
|
|
||||||
|
#include "wapp.h"
|
||||||
|
#include "test_queue.h"
|
||||||
|
|
||||||
|
#define CAPACITY 8
|
||||||
|
|
||||||
|
TestFuncResult test_queue_push(void) {
|
||||||
|
I32Queue queue = wapp_queue(i32, CAPACITY);
|
||||||
|
|
||||||
|
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||||
|
i32 item = (i32)i;
|
||||||
|
wapp_queue_push(i32, &queue, &item);
|
||||||
|
}
|
||||||
|
|
||||||
|
b8 result = true;
|
||||||
|
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||||
|
i32 *value = ((i32 *)(queue.items)) + i;
|
||||||
|
result = result && value != NULL && *value == (i32)i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return wapp_tester_result(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TestFuncResult test_queue_push_alloc(void) {
|
||||||
|
Allocator arena = wapp_mem_arena_allocator_init(MiB(64));
|
||||||
|
I32Queue queue = wapp_queue(i32, CAPACITY);
|
||||||
|
|
||||||
|
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||||
|
i32 item = (i32)i;
|
||||||
|
wapp_queue_push(i32, &queue, &item);
|
||||||
|
}
|
||||||
|
|
||||||
|
b8 result = true;
|
||||||
|
i32 item = 8;
|
||||||
|
u64 new_capacity = CAPACITY * 2;
|
||||||
|
I32Queue *new_queue = wapp_queue_push_alloc(i32, &arena, &queue, &item);
|
||||||
|
if (new_queue && new_queue != &queue) {
|
||||||
|
queue = *new_queue;
|
||||||
|
}
|
||||||
|
u64 capacity = wapp_queue_capacity(&queue);
|
||||||
|
result = result && capacity == new_capacity;
|
||||||
|
|
||||||
|
for (u64 i = 0; i < 2; ++i) {
|
||||||
|
wapp_queue_pop(i32, &queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 remaining = wapp_queue_capacity(&queue) - queue.count;
|
||||||
|
for (u64 i = 0; i < remaining; ++i) {
|
||||||
|
item = remaining + i;
|
||||||
|
wapp_queue_push(i32, &queue, &item);
|
||||||
|
}
|
||||||
|
|
||||||
|
++item;
|
||||||
|
new_queue = wapp_queue_push_alloc(i32, &arena, &queue, &item);
|
||||||
|
if (new_queue && new_queue != &queue) {
|
||||||
|
queue = *new_queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = result && wapp_queue_capacity(&queue) == new_capacity * 2;
|
||||||
|
|
||||||
|
i32 *arr = (i32 *)queue.items;
|
||||||
|
for (u64 i = 0; i < queue.count; ++i) {
|
||||||
|
// NOTE (Abdelrahman): First queue value is currently 2
|
||||||
|
result = result && arr[i] == (i32)(2 + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
wapp_mem_arena_allocator_destroy(&arena);
|
||||||
|
|
||||||
|
return wapp_tester_result(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TestFuncResult test_queue_pop(void) {
|
||||||
|
I32Queue queue = wapp_queue(i32, CAPACITY);
|
||||||
|
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||||
|
i32 item = (i32)i;
|
||||||
|
wapp_queue_push(i32, &queue, &item);
|
||||||
|
}
|
||||||
|
|
||||||
|
b8 result = true;
|
||||||
|
u64 half_count = queue.count / 2;
|
||||||
|
for (u64 i = 0; i < half_count; ++i) {
|
||||||
|
i32 *value = wapp_queue_pop(i32, &queue);
|
||||||
|
result = result && value != NULL && *value == (i32)i;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (u64 i = 0; i < half_count; ++i) {
|
||||||
|
i32 item = (i32)i + CAPACITY;
|
||||||
|
wapp_queue_push(i32, &queue, &item);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||||
|
i32 *value = wapp_queue_pop(i32, &queue);
|
||||||
|
result = result && value != NULL && *value == (i32)half_count + (i32)i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return wapp_tester_result(result);
|
||||||
|
}
|
||||||
96
tests/queue/test_queue.cc
Normal file
96
tests/queue/test_queue.cc
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
#include "wapp.h"
|
||||||
|
#include "test_queue.h"
|
||||||
|
|
||||||
|
#define CAPACITY 8
|
||||||
|
|
||||||
|
TestFuncResult test_queue_push(void) {
|
||||||
|
I32Queue queue = wapp_queue(i32, CAPACITY);
|
||||||
|
|
||||||
|
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||||
|
i32 item = (i32)i;
|
||||||
|
wapp_queue_push(i32, &queue, &item);
|
||||||
|
}
|
||||||
|
|
||||||
|
b8 result = true;
|
||||||
|
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||||
|
i32 *value = ((i32 *)(queue.items)) + i;
|
||||||
|
result = result && value != NULL && *value == (i32)i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return wapp_tester_result(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TestFuncResult test_queue_push_alloc(void) {
|
||||||
|
Allocator arena = wapp_mem_arena_allocator_init(MiB(64));
|
||||||
|
I32Queue queue = wapp_queue(i32, CAPACITY);
|
||||||
|
|
||||||
|
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||||
|
i32 item = (i32)i;
|
||||||
|
wapp_queue_push(i32, &queue, &item);
|
||||||
|
}
|
||||||
|
|
||||||
|
b8 result = true;
|
||||||
|
i32 item = 8;
|
||||||
|
u64 new_capacity = CAPACITY * 2;
|
||||||
|
I32Queue *new_queue = wapp_queue_push_alloc(i32, &arena, &queue, &item);
|
||||||
|
if (new_queue && new_queue != &queue) {
|
||||||
|
queue = *new_queue;
|
||||||
|
}
|
||||||
|
u64 capacity = wapp_queue_capacity(&queue);
|
||||||
|
result = result && capacity == new_capacity;
|
||||||
|
|
||||||
|
for (u64 i = 0; i < 2; ++i) {
|
||||||
|
wapp_queue_pop(i32, &queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 remaining = wapp_queue_capacity(&queue) - queue.count;
|
||||||
|
for (u64 i = 0; i < remaining; ++i) {
|
||||||
|
item = remaining + i;
|
||||||
|
wapp_queue_push(i32, &queue, &item);
|
||||||
|
}
|
||||||
|
|
||||||
|
++item;
|
||||||
|
new_queue = wapp_queue_push_alloc(i32, &arena, &queue, &item);
|
||||||
|
if (new_queue && new_queue != &queue) {
|
||||||
|
queue = *new_queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = result && wapp_queue_capacity(&queue) == new_capacity * 2;
|
||||||
|
|
||||||
|
i32 *arr = (i32 *)queue.items;
|
||||||
|
for (u64 i = 0; i < queue.count; ++i) {
|
||||||
|
// NOTE (Abdelrahman): First queue value is currently 2
|
||||||
|
result = result && arr[i] == (i32)(2 + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
wapp_mem_arena_allocator_destroy(&arena);
|
||||||
|
|
||||||
|
return wapp_tester_result(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TestFuncResult test_queue_pop(void) {
|
||||||
|
I32Queue queue = wapp_queue(i32, CAPACITY);
|
||||||
|
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||||
|
i32 item = (i32)i;
|
||||||
|
wapp_queue_push(i32, &queue, &item);
|
||||||
|
}
|
||||||
|
|
||||||
|
b8 result = true;
|
||||||
|
u64 half_count = queue.count / 2;
|
||||||
|
for (u64 i = 0; i < half_count; ++i) {
|
||||||
|
i32 *value = wapp_queue_pop(i32, &queue);
|
||||||
|
result = result && value != NULL && *value == (i32)i;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (u64 i = 0; i < half_count; ++i) {
|
||||||
|
i32 item = (i32)i + CAPACITY;
|
||||||
|
wapp_queue_push(i32, &queue, &item);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||||
|
i32 *value = wapp_queue_pop(i32, &queue);
|
||||||
|
result = result && value != NULL && *value == (i32)half_count + (i32)i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return wapp_tester_result(result);
|
||||||
|
}
|
||||||
12
tests/queue/test_queue.h
Normal file
12
tests/queue/test_queue.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
// vim:fileencoding=utf-8:foldmethod=marker
|
||||||
|
|
||||||
|
#ifndef TEST_QUEUE_H
|
||||||
|
#define TEST_QUEUE_H
|
||||||
|
|
||||||
|
#include "wapp.h"
|
||||||
|
|
||||||
|
TestFuncResult test_queue_push(void);
|
||||||
|
TestFuncResult test_queue_push_alloc(void);
|
||||||
|
TestFuncResult test_queue_pop(void);
|
||||||
|
|
||||||
|
#endif // !TEST_QUEUE_H
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "test_arena.h"
|
#include "test_arena.h"
|
||||||
#include "test_str8_array.h"
|
#include "test_str8_array.h"
|
||||||
#include "test_i32_array.h"
|
#include "test_i32_array.h"
|
||||||
|
#include "test_queue.h"
|
||||||
#include "test_cpath.h"
|
#include "test_cpath.h"
|
||||||
#include "test_file.h"
|
#include "test_file.h"
|
||||||
#include "test_shell_commander.h"
|
#include "test_shell_commander.h"
|
||||||
@@ -43,6 +44,9 @@ int main(void) {
|
|||||||
test_i32_array_copy_alloc,
|
test_i32_array_copy_alloc,
|
||||||
test_i32_array_pop,
|
test_i32_array_pop,
|
||||||
test_i32_array_clear,
|
test_i32_array_clear,
|
||||||
|
test_queue_push,
|
||||||
|
test_queue_push_alloc,
|
||||||
|
test_queue_pop,
|
||||||
test_str8_lit,
|
test_str8_lit,
|
||||||
test_str8_lit_ro,
|
test_str8_lit_ro,
|
||||||
test_str8_buf,
|
test_str8_buf,
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "test_arena.h"
|
#include "test_arena.h"
|
||||||
#include "test_str8_array.h"
|
#include "test_str8_array.h"
|
||||||
#include "test_i32_array.h"
|
#include "test_i32_array.h"
|
||||||
|
#include "test_queue.h"
|
||||||
#include "test_cpath.h"
|
#include "test_cpath.h"
|
||||||
#include "test_file.h"
|
#include "test_file.h"
|
||||||
#include "test_shell_commander.h"
|
#include "test_shell_commander.h"
|
||||||
@@ -43,6 +44,9 @@ int main(void) {
|
|||||||
test_i32_array_copy_alloc,
|
test_i32_array_copy_alloc,
|
||||||
test_i32_array_pop,
|
test_i32_array_pop,
|
||||||
test_i32_array_clear,
|
test_i32_array_clear,
|
||||||
|
test_queue_push,
|
||||||
|
test_queue_push_alloc,
|
||||||
|
test_queue_pop,
|
||||||
test_str8_lit,
|
test_str8_lit,
|
||||||
test_str8_lit_ro,
|
test_str8_lit_ro,
|
||||||
test_str8_buf,
|
test_str8_buf,
|
||||||
|
|||||||
Reference in New Issue
Block a user