Rest regular stack_resize as well

This commit is contained in:
Imbus 2025-08-23 21:13:50 +02:00
parent 12590533a7
commit ad0246a4f8

18
stack.c
View file

@ -196,12 +196,11 @@ int main(void) {
} }
{ {
uint8_t buf[32];
Stack st; Stack st;
if (stack_init_raw(&st, (uint8_t *)buf, 32, sizeof(int)) != Ok) { if (stack_init(&st, 8, sizeof(int)) != Ok) {
printf("Error: stack_init_raw did not init properly..."); printf("Error: stack_init_raw did not init properly...");
return 1; return 1;
} };
for (int i = 1; stack_push(&st, &i) == Ok; i++); for (int i = 1; stack_push(&st, &i) == Ok; i++);
@ -210,10 +209,12 @@ int main(void) {
assert(stack_length(&st) == 8); assert(stack_length(&st) == 8);
assert(!stack_empty(&st)); assert(!stack_empty(&st));
/* Make sure to not call stack_destroy or free here, since buf[] is static */ uint8_t buf2[64];
uint8_t *old_buf = stack_resize_raw(&st, buf2, 64);
uint8_t buf2[64]; assert(old_buf);
stack_resize_raw(&st, buf2, 64); if (old_buf)
free(old_buf);
assert(stack_capacity(&st) == 16); assert(stack_capacity(&st) == 16);
assert(stack_length(&st) == 8); assert(stack_length(&st) == 8);
@ -223,10 +224,11 @@ int main(void) {
int r; int r;
for (int i = 8; i >= 0; i--) { for (int i = 8; i >= 0; i--) {
if (stack_pop(&st, &r) != Ok) if (stack_pop(&st, &r) != Ok)
return 1; break;
printf("%d == %d\n", i, r);
assert(r == i); assert(r == i);
} }
} }
printf("All tests passed!\n");
} }