Skip to content

Commit

Permalink
Prepped for #160, fixed a stack-shrink bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratstail91 committed Dec 17, 2024
1 parent a28053d commit 3e17916
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 54 deletions.
20 changes: 0 additions & 20 deletions source/toy_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,6 @@ TOY_API Toy_Array* Toy_resizeArray(Toy_Array* array, unsigned int capacity);
#define TOY_ARRAY_EXPANSION_RATE 2
#endif

//quick allocate
#ifndef TOY_ARRAY_ALLOCATE
#define TOY_ARRAY_ALLOCATE() Toy_resizeArray(NULL, TOY_ARRAY_INITIAL_CAPACITY)
#endif

//quick free
#ifndef TOY_ARRAY_FREE
#define TOY_ARRAY_FREE(array) (array = Toy_resizeArray(array, 0))
#endif

//one line to expand the array
#ifndef TOY_ARRAY_EXPAND
#define TOY_ARRAY_EXPAND(array) (array = (array != NULL && (array)->count + 1 > (array)->capacity ? Toy_resizeArray(array, (array)->capacity * TOY_ARRAY_EXPANSION_RATE) : array))
#endif

//quick push back
#ifndef TOY_ARRAY_PUSHBACK
#define TOY_ARRAY_PUSHBACK(array, value) (TOY_ARRAY_EXPAND(array), (array)->data[(array)->count++] = (value))
#endif

//TODO: array.getLength()
//TODO: array.pushFront(x)
//TODO: array.pushBack(x)
Expand Down
8 changes: 4 additions & 4 deletions source/toy_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void Toy_freeStack(Toy_Stack* stack) {

void Toy_pushStack(Toy_Stack** stackHandle, Toy_Value value) {
//don't go overboard
if ((*stackHandle)->count >= TOY_STACK_OVERFLOW) {
if ((*stackHandle)->count >= TOY_STACK_OVERFLOW_THRESHOLD) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Stack overflow\n" TOY_CC_RESET);
exit(-1);
}
Expand Down Expand Up @@ -72,9 +72,9 @@ Toy_Value Toy_popStack(Toy_Stack** stackHandle) {
}

//shrink if possible
if ((*stackHandle)->count > TOY_STACK_INITIAL_CAPACITY && (*stackHandle)->count < (*stackHandle)->capacity * TOY_STACK_CONTRACTION_THRESHOLD) {
(*stackHandle)->capacity /= 2;
unsigned int newCapacity = (*stackHandle)->capacity;
if ((*stackHandle)->capacity > TOY_STACK_INITIAL_CAPACITY && (*stackHandle)->count <= (*stackHandle)->capacity / TOY_STACK_CONTRACTION_THRESHOLD) {
(*stackHandle)->capacity /= TOY_STACK_CONTRACTION_THRESHOLD;
unsigned int newCapacity = (*stackHandle)->capacity; //cache for the msg

(*stackHandle) = realloc((*stackHandle), (*stackHandle)->capacity * sizeof(Toy_Value) + sizeof(Toy_Stack));

Expand Down
6 changes: 3 additions & 3 deletions source/toy_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ TOY_API Toy_Value Toy_popStack(Toy_Stack** stackHandle);
#endif

#ifndef TOY_STACK_CONTRACTION_THRESHOLD
#define TOY_STACK_CONTRACTION_THRESHOLD (1 / 4)
#define TOY_STACK_CONTRACTION_THRESHOLD 4 // this integer means 'shrink when count drops below one-forth of capacity'
#endif

//prevent an infinite expansion, limited to 1MB
#ifndef TOY_STACK_OVERFLOW
#define TOY_STACK_OVERFLOW (1024 * 1024 / sizeof(Toy_Value))
#ifndef TOY_STACK_OVERFLOW_THRESHOLD
#define TOY_STACK_OVERFLOW_THRESHOLD (1024 * 1024 / sizeof(Toy_Value) - sizeof(Toy_Stack))
#endif
2 changes: 1 addition & 1 deletion source/toy_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void Toy_insertTable(Toy_Table** tableHandle, Toy_Value key, Toy_Value value) {
}

//expand the capacity
if ((*tableHandle)->count > (*tableHandle)->capacity * TOY_TABLE_EXPANSION_THRESHOLD) {
if ((*tableHandle)->count >= (*tableHandle)->capacity * TOY_TABLE_EXPANSION_THRESHOLD) {
(*tableHandle) = Toy_private_adjustTableCapacity((*tableHandle), (*tableHandle)->capacity * TOY_TABLE_EXPANSION_RATE);
}

Expand Down
4 changes: 2 additions & 2 deletions source/toy_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ TOY_API Toy_Table* Toy_private_adjustTableCapacity(Toy_Table* oldTable, unsigned
#define TOY_TABLE_EXPANSION_RATE 2
#endif

//expand when the contents passes a certain percentage of the capacity
//expand when the contents passes a certain percentage (80%) of the capacity
#ifndef TOY_TABLE_EXPANSION_THRESHOLD
#define TOY_TABLE_EXPANSION_THRESHOLD 0.8
#define TOY_TABLE_EXPANSION_THRESHOLD 0.8f
#endif
2 changes: 1 addition & 1 deletion source/toy_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void Toy_freeValue(Toy_Value value) {
Toy_freeValue(ptr->data[i]);
}

TOY_ARRAY_FREE(ptr);
Toy_resizeArray(ptr, 0);
break;
}

Expand Down
16 changes: 8 additions & 8 deletions tests/cases/test_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@
int test_array(void) {
//test allocation and free
{
Toy_Array* array = TOY_ARRAY_ALLOCATE();
TOY_ARRAY_FREE(array);
Toy_Array* array = Toy_resizeArray(NULL, TOY_ARRAY_INITIAL_CAPACITY);
array = Toy_resizeArray(array, 0);
}

//test initial data
{
Toy_Array* array = TOY_ARRAY_ALLOCATE();
Toy_Array* array = Toy_resizeArray(NULL, TOY_ARRAY_INITIAL_CAPACITY);

//check you can access the memory
array->data[1] = TOY_VALUE_FROM_INTEGER(42);

TOY_ARRAY_FREE(array);
array = Toy_resizeArray(array, 0);
}

//test multiple arrays (no overlaps or conflicts)
{
Toy_Array* array1 = TOY_ARRAY_ALLOCATE();
Toy_Array* array2 = TOY_ARRAY_ALLOCATE();
Toy_Array* array1 = Toy_resizeArray(NULL, TOY_ARRAY_INITIAL_CAPACITY);
Toy_Array* array2 = Toy_resizeArray(NULL, TOY_ARRAY_INITIAL_CAPACITY);

array1->data[1] = TOY_VALUE_FROM_INTEGER(42);
array2->data[1] = TOY_VALUE_FROM_INTEGER(42);

TOY_ARRAY_FREE(array1);
TOY_ARRAY_FREE(array2);
Toy_resizeArray(array1, 0);
Toy_resizeArray(array2, 0);
}

return 0;
Expand Down
23 changes: 20 additions & 3 deletions tests/cases/test_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ int test_stack_basics(void) {
}

int test_stack_stress(void) {
//stress the stack's contents
//stress the stack
{
Toy_Stack* stack = Toy_allocateStack();

//allocate 500 values
//push 500 values
for (int i = 0; i < 500; i++) {
Toy_pushStack(&stack, TOY_VALUE_FROM_INTEGER(i));
}
Expand All @@ -98,11 +98,28 @@ int test_stack_stress(void) {
stack->capacity != 512 ||
stack->count != 500)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to stress the Toy_Stack\n" TOY_CC_RESET);
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to stress push the Toy_Stack\n" TOY_CC_RESET);
Toy_freeStack(stack);
return -1;
}

//pop each value
while(stack->count > 0) {
Toy_popStack(&stack); //ignore the results
}

//check if it worked
if (
stack == NULL ||
stack->capacity != TOY_STACK_INITIAL_CAPACITY || //reset to initial capacity
stack->count != 0)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to stress pop the Toy_Stack\n" TOY_CC_RESET);
Toy_freeStack(stack);
return -1;
}

//cleanup
Toy_freeStack(stack);
}

Expand Down
28 changes: 16 additions & 12 deletions tests/cases/test_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include <stdlib.h>
#include <string.h>

//util macros
#define TOY_ARRAY_EXPAND(array) (array = (array != NULL && (array)->count + 1 > (array)->capacity ? Toy_resizeArray(array, (array)->capacity * TOY_ARRAY_EXPANSION_RATE) : array))
#define TOY_ARRAY_PUSHBACK(array, value) (TOY_ARRAY_EXPAND(array), (array)->data[(array)->count++] = (value))

int test_value_creation(void) {
//test for the correct size
{
Expand Down Expand Up @@ -68,7 +72,7 @@ int test_value_creation(void) {
//test creating arrays
{
//setup
Toy_Array* array = TOY_ARRAY_ALLOCATE();
Toy_Array* array = Toy_resizeArray(NULL, TOY_ARRAY_INITIAL_CAPACITY);
TOY_ARRAY_PUSHBACK(array, TOY_VALUE_FROM_INTEGER(42));
TOY_ARRAY_PUSHBACK(array, TOY_VALUE_FROM_INTEGER(69));
TOY_ARRAY_PUSHBACK(array, TOY_VALUE_FROM_INTEGER(8891));
Expand All @@ -84,12 +88,12 @@ int test_value_creation(void) {
)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: 'array' value failed\n" TOY_CC_RESET);
TOY_ARRAY_FREE(array);
Toy_resizeArray(array, 0);
return -1;
}

//cleanup
TOY_ARRAY_FREE(array);
Toy_resizeArray(array, 0);
}

return 0;
Expand Down Expand Up @@ -139,7 +143,7 @@ int test_value_copying(void) {
//test copy arrays
{
//setup
Toy_Array* array = TOY_ARRAY_ALLOCATE();
Toy_Array* array = Toy_resizeArray(NULL, TOY_ARRAY_INITIAL_CAPACITY);
TOY_ARRAY_PUSHBACK(array, TOY_VALUE_FROM_INTEGER(42));
TOY_ARRAY_PUSHBACK(array, TOY_VALUE_FROM_INTEGER(69));
TOY_ARRAY_PUSHBACK(array, TOY_VALUE_FROM_INTEGER(8891));
Expand Down Expand Up @@ -186,7 +190,7 @@ int test_value_hashing(void) {
//skip float
Toy_Value s = TOY_VALUE_FROM_STRING(Toy_createString(&bucket, "Hello world"));

Toy_Array* array = TOY_ARRAY_ALLOCATE();
Toy_Array* array = Toy_resizeArray(NULL, TOY_ARRAY_INITIAL_CAPACITY);
TOY_ARRAY_PUSHBACK(array, TOY_VALUE_FROM_INTEGER(42));
TOY_ARRAY_PUSHBACK(array, TOY_VALUE_FROM_INTEGER(69));
TOY_ARRAY_PUSHBACK(array, TOY_VALUE_FROM_INTEGER(8891));
Expand All @@ -202,13 +206,13 @@ int test_value_hashing(void) {
)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: Unexpected hash of a value\n" TOY_CC_RESET);
TOY_ARRAY_FREE(array);
Toy_resizeArray(array, 0);
Toy_freeBucket(&bucket);
return -1;
}

//cleanup
TOY_ARRAY_FREE(array);
Toy_resizeArray(array, 0);
Toy_freeBucket(&bucket);
}

Expand Down Expand Up @@ -259,14 +263,14 @@ int test_value_equality(void) {
//again with arrays
{
//setup
Toy_Array* array1 = TOY_ARRAY_ALLOCATE();
Toy_Array* array1 = Toy_resizeArray(NULL, TOY_ARRAY_INITIAL_CAPACITY);
TOY_ARRAY_PUSHBACK(array1, TOY_VALUE_FROM_INTEGER(42));
TOY_ARRAY_PUSHBACK(array1, TOY_VALUE_FROM_INTEGER(69));
TOY_ARRAY_PUSHBACK(array1, TOY_VALUE_FROM_INTEGER(8891));

Toy_Value value1 = TOY_VALUE_FROM_ARRAY(array1);

Toy_Array* array2 = TOY_ARRAY_ALLOCATE();
Toy_Array* array2 = Toy_resizeArray(NULL, TOY_ARRAY_INITIAL_CAPACITY);
TOY_ARRAY_PUSHBACK(array2, TOY_VALUE_FROM_INTEGER(42));
TOY_ARRAY_PUSHBACK(array2, TOY_VALUE_FROM_INTEGER(69));
TOY_ARRAY_PUSHBACK(array2, TOY_VALUE_FROM_INTEGER(8891));
Expand Down Expand Up @@ -512,7 +516,7 @@ int test_value_stringify(void) {
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_SMALL);

//setup
Toy_Array* array = TOY_ARRAY_ALLOCATE();
Toy_Array* array = Toy_resizeArray(NULL, TOY_ARRAY_INITIAL_CAPACITY);
TOY_ARRAY_PUSHBACK(array, TOY_VALUE_FROM_INTEGER(42));
TOY_ARRAY_PUSHBACK(array, TOY_VALUE_FROM_INTEGER(69));
TOY_ARRAY_PUSHBACK(array, TOY_VALUE_FROM_INTEGER(8891));
Expand All @@ -525,14 +529,14 @@ int test_value_stringify(void) {
{
fprintf(stderr, TOY_CC_ERROR "ERROR: stringify array '[42,69,8891]' failed\n" TOY_CC_RESET);
free(buffer);
TOY_ARRAY_FREE(array);
array = Toy_resizeArray(array, 0);
Toy_freeBucket(&bucket);
return -1;
}

//cleanup
free(buffer);
TOY_ARRAY_FREE(array);
Toy_resizeArray(array, 0);
Toy_freeBucket(&bucket);
}

Expand Down

0 comments on commit 3e17916

Please sign in to comment.