archeus/tests/std/chemical.c

109 lines
3.8 KiB
C
Raw Normal View History

2024-12-23 00:16:32 -07:00
#include "../test.h"
#include "arc/std/errno.h"
#include "arc/std/chemical.h"
#include "arc/std/parser.h"
#include "arc/std/parser/helpers.h"
#include <stdlib.h>
static const char *testType = "int32";
void TEST_ChemicalType_CopyInt32Fn(void **type, ARC_ParserTagToken *parsedData, ARC_Chemical *chemical){
//go into the <numberSign> tag
ARC_ParserTagToken *childTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(parsedData->tagTokens, 0);
if(childTagToken->id != ARC_CHEMICAL_NUMBER_SIGN){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR("TEST_ChemicalType_CopyInt32Fn(type, parsedData, chemical), parsed data was not a <numberSign>");
*type = NULL;
return;
}
//check if the first tag is a minus sign and create a string starting with that if it is
childTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(childTagToken->tagTokens, 0);
ARC_String *int32String;
ARC_String_Create(&int32String, NULL, 0);
if(childTagToken->id == ARC_CHEMICAL_MINUS){
ARC_String_AppendCStringWithStrlen(&int32String, "-");
}
ARC_ParserData_HelperRecurseStringAdd(&int32String, parsedData);
//set the max character size 2,147,483,647 (10 characters) or -2,147,483,648 (11 characters)
uint32_t maxInt32Size = 10;
if(int32String->data[0] == '-'){
maxInt32Size++;
}
//if the string is bigger than the possible size return NULL and error
if(int32String->length > maxInt32Size){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("TEST_ChemicalType_CopyInt32Fn(type, parsedData, chemical), size \"%s\" was bigger or smaller than the max 2,147,483,647 or min -2,147,483,648", int32String->data);
*type = NULL;
ARC_String_Destroy(int32String);
return;
}
if(int32String->length == maxInt32Size){
char maxint32CStr[10] = "2147483647";
//offset starting index and last number if there is a negative
uint8_t stringIndex = 0;
if(int32String->data[0] == '-'){
stringIndex++;
maxint32CStr[9]++;
}
for(uint8_t index = 0; index < 10; index++, stringIndex++){
if(int32String->data[stringIndex] > maxint32CStr[index]){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("TEST_ChemicalType_CopyInt32Fn(type, parsedData, chemical), size \"%s\" was bigger or smaller than the max 2,147,483,647 or min -2,147,483,648", int32String->data);
*type = NULL;
ARC_String_Destroy(int32String);
return;
}
}
}
//copy the int32_t
*type = malloc(sizeof(int32_t));
*(int32_t *)(*type) = (int32_t)ARC_String_ToInt64_t(int32String);
//cleanup
ARC_String_Destroy(int32String);
}
void TEST_ChemicalType_DestroyInt32Fn(void *type){
free((int32_t *)type);
}
ARC_TEST(Chemical_BasicTest){
2025-01-29 17:31:18 -07:00
ARC_Chemical *chemical;
ARC_Chemical_Create(&chemical);
2025-01-29 17:31:18 -07:00
ARC_CHECK(arc_errno == 0);
ARC_ChemicalType int32Type = {
TEST_ChemicalType_CopyInt32Fn,
TEST_ChemicalType_DestroyInt32Fn
};
ARC_Chemical_RegisterTypeWithCStr(chemical, testType, int32Type);
2025-03-08 02:43:16 -07:00
char *tempCString = "tests/res/std/chemical/first.chemical";
ARC_String *tempString;
ARC_String_CreateWithStrlen(&tempString, tempCString);
2025-03-08 02:43:16 -07:00
ARC_Chemical_LoadFromFile(chemical, tempString);
int32_t testVal = *(int32_t *)ARC_Chemical_GetWithCStr(chemical, "test::test");
ARC_CHECK(testVal == 5);
testVal = *(int32_t *)ARC_Chemical_GetWithCStr(chemical, "test::test1");
ARC_CHECK(testVal == -7);
ARC_Chemical_UnloadFromFile(chemical, tempString);
void *nullVal = ARC_Chemical_GetWithCStr(chemical, "test::test");
ARC_CHECK(nullVal == NULL);
//cleanup
ARC_String_Destroy(tempString);
2025-01-29 17:31:18 -07:00
ARC_Chemical_Destroy(chemical);
}