Reworked generic structures, read more

The following structures are now more independant:

- Toy_Array
- Toy_Stack
- Toy_Bucket
- Toy_String

I reworked a lot of the memory allocation, so now there are more direct
calls to malloc() or realloc(), rather than relying on the macros from
toy_memory.h.

I've also split toy_memory into proper array and bucket files, because
it makes more sense this way, rather than having them both jammed into
one file. This means the eventual hashtable structure can also stand on
its own.

Toy_Array is a new wrapper around raw array pointers, and all of the
structures have their metadata embedded into their allocated memory now,
using variable length array members.

A lot of 'capacity' and 'count' variables were changed to 'size_t'
types, but this doesn't seem to be a problem anywhere.

If the workflow fails, then I'll leave it for tonight - I'm too tired,
and I don't want to overdo myself.
This commit is contained in:
2024-10-01 20:24:52 +10:00
parent 53b0fc158c
commit 7b453bc35f
34 changed files with 566 additions and 576 deletions

View File

@@ -3,40 +3,40 @@
#include <stdio.h>
int test_stack_with_init() {
//init and free the stack
int test_stack_basics() {
//allocate and free the stack
{
Toy_Stack stack;
Toy_initStack(&stack);
Toy_Stack* stack = Toy_allocateStack();
//check if it worked
if (
stack.ptr != NULL ||
stack.capacity != 0 ||
stack.count != 0)
stack == NULL ||
stack->capacity != 64 ||
stack->count != 0)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to init Toy_Stack\n" TOY_CC_RESET);
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to allocate Toy_Stack\n" TOY_CC_RESET);
Toy_freeStack(stack);
return -1;
}
Toy_freeStack(&stack);
Toy_freeStack(stack);
}
//push, peek and pop stack
{
Toy_Stack stack;
Toy_initStack(&stack);
Toy_Stack* stack = Toy_allocateStack();
//check if it worked (push)
Toy_pushStack(&stack, TOY_VALUE_TO_INTEGER(42));
Toy_pushStack(&stack, TOY_VALUE_TO_INTEGER(69));
Toy_pushStack(&stack, TOY_VALUE_TO_INTEGER(420));
if (
stack.ptr == NULL ||
stack.capacity != 64 ||
stack.count != 3)
stack == NULL ||
stack->capacity != 64 ||
stack->count != 3)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to push Toy_Stack\n" TOY_CC_RESET);
Toy_freeStack(stack);
return -1;
}
@@ -47,19 +47,21 @@ int test_stack_with_init() {
TOY_VALUE_AS_INTEGER(top1) != 420)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to peek Toy_Stack\n" TOY_CC_RESET);
Toy_freeStack(stack);
return -1;
}
//check if it worked (pop)
Toy_Value top2 = Toy_popStack(&stack);
if (
stack.ptr == NULL ||
stack.capacity != 64 ||
stack.count != 2 ||
stack == NULL ||
stack->capacity != 64 ||
stack->count != 2 ||
TOY_VALUE_IS_INTEGER(top2) != true ||
TOY_VALUE_AS_INTEGER(top2) != 420)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to pop Toy_Stack\n" TOY_CC_RESET);
Toy_freeStack(stack);
return -1;
}
@@ -70,86 +72,38 @@ int test_stack_with_init() {
TOY_VALUE_AS_INTEGER(top3) != 69)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to pop then peek Toy_Stack\n" TOY_CC_RESET);
Toy_freeStack(stack);
return -1;
}
Toy_freeStack(&stack);
Toy_freeStack(stack);
}
return 0;
}
int test_stack_with_preallocate() {
//preallocate and free the stack
int test_stack_stress() {
//stress the stack's contents
{
Toy_Stack stack;
Toy_preallocateStack(&stack);
Toy_Stack* stack = Toy_allocateStack();
//allocate 500 values
for (int i = 0; i < 500; i++) {
Toy_pushStack(&stack, TOY_VALUE_TO_INTEGER(i));
}
//check if it worked
if (
stack.ptr == NULL ||
stack.capacity != 64 ||
stack.count != 0)
stack == NULL ||
stack->capacity != 512 ||
stack->count != 500)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to preallocate Toy_Stack\n" TOY_CC_RESET);
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to stress the Toy_Stack\n" TOY_CC_RESET);
Toy_freeStack(stack);
return -1;
}
Toy_freeStack(&stack);
}
//push, peek and pop stack
{
Toy_Stack stack;
Toy_initStack(&stack);
//check if it worked (push)
Toy_pushStack(&stack, TOY_VALUE_TO_INTEGER(42));
Toy_pushStack(&stack, TOY_VALUE_TO_INTEGER(69));
Toy_pushStack(&stack, TOY_VALUE_TO_INTEGER(420));
if (
stack.ptr == NULL ||
stack.capacity != 64 ||
stack.count != 3)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to push Toy_Stack\n" TOY_CC_RESET);
return -1;
}
//check if it worked (peek)
Toy_Value top1 = Toy_peekStack(&stack);
if (
TOY_VALUE_IS_INTEGER(top1) != true ||
TOY_VALUE_AS_INTEGER(top1) != 420)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to peek Toy_Stack\n" TOY_CC_RESET);
return -1;
}
//check if it worked (pop)
Toy_Value top2 = Toy_popStack(&stack);
if (
stack.ptr == NULL ||
stack.capacity != 64 ||
stack.count != 2 ||
TOY_VALUE_IS_INTEGER(top2) != true ||
TOY_VALUE_AS_INTEGER(top2) != 420)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to pop Toy_Stack\n" TOY_CC_RESET);
return -1;
}
//check if it worked (post-pop peek)
Toy_Value top3 = Toy_peekStack(&stack);
if (
TOY_VALUE_IS_INTEGER(top3) != true ||
TOY_VALUE_AS_INTEGER(top3) != 69)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to pop then peek Toy_Stack\n" TOY_CC_RESET);
return -1;
}
Toy_freeStack(&stack);
Toy_freeStack(stack);
}
return 0;
@@ -160,7 +114,7 @@ int main() {
int total = 0, res = 0;
{
res = test_stack_with_init();
res = test_stack_basics();
if (res == 0) {
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
}
@@ -168,7 +122,7 @@ int main() {
}
{
res = test_stack_with_preallocate();
res = test_stack_stress();
if (res == 0) {
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
}