testing working, and fixed memory leak in replace with substring function

This commit is contained in:
herbglitch 2024-08-31 06:09:33 -06:00
parent 4c0c5d377d
commit 5de968688c
7 changed files with 131 additions and 75 deletions

39
tests/std/lexer.c Normal file
View file

@ -0,0 +1,39 @@
#include "../test.h"
#include "arc/std/lexer.h"
ARC_TEST(Lexer_Char_Match){
ARC_Lexer *lexer;
ARC_Lexer_Create(&lexer);
ARC_Lexer_InitBasicTokenRules(lexer);
ARC_String *simple;
ARC_String_CreateWithStrlen(&simple, "::{}!/.");
ARC_Lexer_LexString(lexer, &simple);
ARC_LexerToken token;
token = ARC_Lexer_GetToken(lexer, 0);
ARC_CHECK(token.rule == ARC_LEXER_TOKEN_COLON_ID);
token = ARC_Lexer_GetToken(lexer, 1);
ARC_CHECK(token.rule == ARC_LEXER_TOKEN_COLON_ID);
token = ARC_Lexer_GetToken(lexer, 2);
ARC_CHECK(token.rule == ARC_LEXER_TOKEN_LEFT_CURLY_BRACE_ID);
token = ARC_Lexer_GetToken(lexer, 3);
ARC_CHECK(token.rule == ARC_LEXER_TOKEN_RIGHT_CURLY_BRACE_ID);
token = ARC_Lexer_GetToken(lexer, 4);
ARC_CHECK(token.rule == ARC_LEXER_TOKEN_BANG_ID);
token = ARC_Lexer_GetToken(lexer, 5);
ARC_CHECK(token.rule == ARC_LEXER_TOKEN_FORWARD_SLASH_ID);
token = ARC_Lexer_GetToken(lexer, 6);
ARC_CHECK(token.rule == ARC_LEXER_TOKEN_PERIOD_ID);
ARC_Lexer_Destroy(lexer);
}

159
tests/std/vector.c Normal file
View file

@ -0,0 +1,159 @@
#include "../test.h"
#include "arc/std/bool.h"
#include "arc/std/errno.h"
#include "arc/std/vector.h"
#include <stdint.h>
#include <stdlib.h>
ARC_Bool TEST_Vector_CompareDataFn(void *dataA, void *dataB){
if(*(int32_t *)dataA == *(int32_t *)dataB){
return ARC_True;
}
return ARC_False;
}
//TODO: more tests with destroy data fn added
void TEST_Vector_DestroyDataFn(void *data){
free((int32_t *)data);
}
ARC_TEST(Vector_Add_RemoveIndex_Get){
ARC_Vector *vector;
ARC_Vector_Create(&vector, NULL, NULL);
int32_t val0 = 0;
int32_t val1 = 1;
int32_t val2 = 2;
int32_t val3 = 3;
int32_t val4 = 4;
ARC_Vector_Add(vector, &val0);
ARC_Vector_Add(vector, &val1);
ARC_Vector_Add(vector, &val2);
ARC_Vector_Add(vector, &val3);
ARC_Vector_Add(vector, &val4);
ARC_CHECK(0 == *(int32_t *)ARC_Vector_Get(vector, 0));
ARC_CHECK(1 == *(int32_t *)ARC_Vector_Get(vector, 1));
ARC_CHECK(2 == *(int32_t *)ARC_Vector_Get(vector, 2));
ARC_CHECK(3 == *(int32_t *)ARC_Vector_Get(vector, 3));
ARC_CHECK(4 == *(int32_t *)ARC_Vector_Get(vector, 4));
ARC_Vector_RemoveIndex(vector, 0);
ARC_CHECK(1 == *(int32_t *)ARC_Vector_Get(vector, 0));
ARC_CHECK(2 == *(int32_t *)ARC_Vector_Get(vector, 1));
ARC_CHECK(3 == *(int32_t *)ARC_Vector_Get(vector, 2));
ARC_CHECK(4 == *(int32_t *)ARC_Vector_Get(vector, 3));
ARC_Vector_RemoveIndex(vector, 3);
ARC_CHECK(1 == *(int32_t *)ARC_Vector_Get(vector, 0));
ARC_CHECK(2 == *(int32_t *)ARC_Vector_Get(vector, 1));
ARC_CHECK(3 == *(int32_t *)ARC_Vector_Get(vector, 2));
ARC_Vector_RemoveIndex(vector, 1);
ARC_CHECK(1 == *(int32_t *)ARC_Vector_Get(vector, 0));
ARC_CHECK(3 == *(int32_t *)ARC_Vector_Get(vector, 1));
ARC_Vector_RemoveIndex(vector, 1);
ARC_CHECK(1 == *(int32_t *)ARC_Vector_Get(vector, 0));
ARC_Vector_RemoveIndex(vector, 0);
ARC_Vector_Destroy(vector);
}
ARC_TEST(Vector_Add_Remove_Get){
ARC_Vector *vector;
ARC_Vector_CompareDataFn testCompareDataFn = TEST_Vector_CompareDataFn;
ARC_Vector_Create(&vector, &testCompareDataFn, NULL);
int32_t val0 = 0;
int32_t val1 = 1;
int32_t val2 = 2;
int32_t val3 = 3;
int32_t val4 = 4;
ARC_Vector_Add(vector, &val0);
ARC_Vector_Add(vector, &val1);
ARC_Vector_Add(vector, &val2);
ARC_Vector_Add(vector, &val3);
ARC_Vector_Add(vector, &val4);
ARC_CHECK(0 == *(int32_t *)ARC_Vector_Get(vector, 0));
ARC_CHECK(1 == *(int32_t *)ARC_Vector_Get(vector, 1));
ARC_CHECK(2 == *(int32_t *)ARC_Vector_Get(vector, 2));
ARC_CHECK(3 == *(int32_t *)ARC_Vector_Get(vector, 3));
ARC_CHECK(4 == *(int32_t *)ARC_Vector_Get(vector, 4));
ARC_Vector_Remove(vector, &val0);
ARC_CHECK(1 == *(int32_t *)ARC_Vector_Get(vector, 0));
ARC_CHECK(2 == *(int32_t *)ARC_Vector_Get(vector, 1));
ARC_CHECK(3 == *(int32_t *)ARC_Vector_Get(vector, 2));
ARC_CHECK(4 == *(int32_t *)ARC_Vector_Get(vector, 3));
ARC_Vector_Remove(vector, &val4);
ARC_CHECK(1 == *(int32_t *)ARC_Vector_Get(vector, 0));
ARC_CHECK(2 == *(int32_t *)ARC_Vector_Get(vector, 1));
ARC_CHECK(3 == *(int32_t *)ARC_Vector_Get(vector, 2));
ARC_Vector_Remove(vector, &val2);
ARC_CHECK(1 == *(int32_t *)ARC_Vector_Get(vector, 0));
ARC_CHECK(3 == *(int32_t *)ARC_Vector_Get(vector, 1));
ARC_Vector_Remove(vector, &val1);
ARC_CHECK(3 == *(int32_t *)ARC_Vector_Get(vector, 0));
ARC_Vector_Remove(vector, &val3);
ARC_Vector_Destroy(vector);
}
ARC_TEST(Vector_Add_RemoveIndex_GetSize){
ARC_Vector *vector;
ARC_Vector_Create(&vector, NULL, NULL);
int32_t val0 = 0;
int32_t val1 = 1;
int32_t val2 = 2;
int32_t val3 = 3;
int32_t val4 = 4;
ARC_Vector_Add(vector, &val0);
ARC_Vector_Add(vector, &val1);
ARC_Vector_Add(vector, &val2);
ARC_Vector_Add(vector, &val3);
ARC_Vector_Add(vector, &val4);
ARC_CHECK(5 == ARC_Vector_GetSize(vector));
ARC_Vector_RemoveIndex(vector, 0);
ARC_CHECK(4 == ARC_Vector_GetSize(vector));
ARC_Vector_RemoveIndex(vector, 0);
ARC_CHECK(3 == ARC_Vector_GetSize(vector));
ARC_Vector_RemoveIndex(vector, 0);
ARC_CHECK(2 == ARC_Vector_GetSize(vector));
ARC_Vector_RemoveIndex(vector, 0);
ARC_CHECK(1 == ARC_Vector_GetSize(vector));
ARC_Vector_RemoveIndex(vector, 0);
ARC_CHECK(0 == ARC_Vector_GetSize(vector));
ARC_Vector_Destroy(vector);
}
ARC_TEST(Vector_Add_RemoveIndex_Get_Try_Out_Of_Bounds){
ARC_Vector *vector;
ARC_Vector_Create(&vector, NULL, NULL);
int32_t val0 = 0;
ARC_Vector_Add(vector, &val0);
ARC_CHECK(NULL == ARC_Vector_Get(vector, 1));
arc_errno = 0;
ARC_Vector_RemoveIndex(vector, 0);
ARC_Vector_Destroy(vector);
}