Loading...
Searching...
No Matches
vector.c
Go to the documentation of this file.
#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);
}
bool.h
ARC_False
#define ARC_False
Definition bool.h:12
ARC_True
#define ARC_True
Definition bool.h:11
ARC_Bool
#define ARC_Bool
Definition bool.h:10
errno.h
arc_errno
int32_t arc_errno
Definition errno.c:5
ARC_Vector_GetSize
uint32_t ARC_Vector_GetSize(ARC_Vector *vector)
gets the current size of an ARC_Vector as an unsigned 32 bit integer
Definition vector.c:149
ARC_Vector_Get
void * ARC_Vector_Get(ARC_Vector *vector, uint32_t index)
gets an item from an ARC_Vector at a position index
Definition vector.c:153
ARC_Vector_Add
void ARC_Vector_Add(ARC_Vector *vector, void *data)
adds an item to an ARC_Vector
Definition vector.c:70
ARC_Vector_Remove
void ARC_Vector_Remove(ARC_Vector *vector, void *data)
removes an item from a matching item in an ARC_Vector
Definition vector.c:97
ARC_Vector_Destroy
void ARC_Vector_Destroy(ARC_Vector *vector)
destroys an ARC_Vector
Definition vector.c:54
ARC_Vector_RemoveIndex
void ARC_Vector_RemoveIndex(ARC_Vector *vector, uint32_t index)
removes an item from an ARC_Vector at an index
Definition vector.c:110
ARC_Vector_Create
void ARC_Vector_Create(ARC_Vector **vector, ARC_Vector_CompareDataFn *compareDataFn, ARC_Vector_DestroyDataFn *destroyDataFn)
creates an ARC_Vector which is an "expandable" array
Definition vector.c:31
ARC_Vector
a private dynamic array type
Definition vector.c:12
test.h
ARC_CHECK
#define ARC_CHECK(TEST)
Definition test.h:33
ARC_TEST
#define ARC_TEST(MESSAGE)
Definition test.h:31
TEST_Vector_CompareDataFn
ARC_Bool TEST_Vector_CompareDataFn(void *dataA, void *dataB)
Definition vector.c:8
TEST_Vector_DestroyDataFn
void TEST_Vector_DestroyDataFn(void *data)
Definition vector.c:17
vector.h
ARC_Vector_CompareDataFn
ARC_Bool(* ARC_Vector_CompareDataFn)(void *dataA, void *dataB)
a callback that allows the user to define a way to check the data stored in a vector for a match
Definition vector.h:24