vec/tests/04_vec.c
2024-11-15 16:52:59 -06:00

67 lines
1.6 KiB
C

#define DEBUG
#include "../include/except.h"
#include "../include/mem.h"
#include "../include/vec.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
printf("\n\n");
fprintf(stdout,"Testing vec init");
Vec v;
vec_init(&v);
TEST(v.items != NULL);
TEST(v.cap == VEC_INIT_SZ);
TEST(v.len == 0);
fprintf(stdout,"Testing vec push\n");
char *str = "Hello";
char *str2 = "World";
char *str3 = "Goodbye";
int p1 = vec_push(&v, (void *)str);
TEST(p1 == 1);
TEST(v.len == 1);
int p2 = vec_push(&v, (void *)str2);
TEST(p2 == 2);
TEST(v.len == 2);
TEST(v.cap == 2);
int p3 = vec_push(&v,str3);
TEST(p3 == 3);
TEST(v.len == 3);
TEST(v.cap == 4);
fprintf(stdout,"Testing vec pop\n");
char *str4 = (char *)vec_pop(&v);
TEST(strcmp(str4,str3) == 0);
TEST(v.len == 2);
TEST(v.cap == 4);
fprintf(stdout,"Testing vec read\n");
char *ptr = (char *)vec_read(&v,1);
TEST(strcmp(str2,ptr) == 0);
TEST(v.len == 2);
TEST(v.cap == 4);
fprintf(stdout,"Testing vec_set\n");
vec_set(&v,1,(void *)ptr);
TEST(strcmp( (char *)v.items[1],(char *)ptr ) == 0);
TEST(v.len == 2);
TEST(v.cap == 4);
fprintf(stdout,"Testing vec delete resizing vec to 100\n");
vec_realloc(&v,100);
TEST(v.cap == 100);
fprintf(stdout,"Testing vec delete, should resize vec after deletion to half size.\n");
vec_delete(&v,1);
TEST(v.len == 1);
TEST(v.cap == 100/2);
fprintf(stdout,"Testing vec_free\n");
vec_free(&v);
TEST(v.items == NULL);
return EXIT_SUCCESS;
}