Skip to content

Commit

Permalink
tested vector constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
prathameshd30 committed Jul 12, 2024
1 parent 18d2620 commit beb1bce
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.exe
.vscode/
.vs/
out/

list.txt

Expand Down
2 changes: 1 addition & 1 deletion include/data_structures/vector/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct NAME{\
TYPE* data;\
uint64_t size;\
};\
NAME* new_##NAME(uint64_t size){\
NAME* new_##NAME(uint64_t size){ /*Size is set only after allocation of vector->data memory, need someone to clarify if this is advised*/ \
NAME* new_obj = malloc(sizeof(NAME));\
if(!new_obj){\
return NULL;\
Expand Down
63 changes: 63 additions & 0 deletions tests/data_structures/vector/vector_primitive_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "vector.h"
#include "bubble_sort.h"

#define ARRAY_SIZE 10

DEFINE_VECTOR(intVec,int)
DEFINE_BUBBLE_SORT_ARR(intArr,int)

Expand All @@ -27,6 +29,7 @@ bool int_is_equal(int a, int b){
}

int main(void){
// Testing of 0 sized constructor
intVec* iv = new_intVec(0);
if(!iv){
puts("Failed to create int_vector");
Expand All @@ -41,8 +44,68 @@ int main(void){
return -1;
}
delete_intVec(iv,NULL);
if(iv!=NULL){
puts("Deleted integer vector still points to memory");
return -1;
}

// Testing of +ve sized constructor
const uint64_t size = 10;
iv = new_intVec(size);
if(iv->size != size){
puts("Vector size incorrectly set");
return -1;
}
if(iv->data == NULL){
puts("Vector data is NULL even though size is not 0");
return -1;
}
delete_intVec(iv, NULL);
if(iv!=NULL){
puts("Deleted vector of +ve size is not NULL");
return -1;
}

// Test at uint64_t max
iv = new_intVec(UINT64_MAX);
if(iv->size!=UINT64_MAX){
puts("Incorrect vector size set at UINT64_MAX");
return -1;
}
if(iv->data==NULL){
puts("Vector data is NULL at size UINT64_MAX");
return -1;
}
delete_intVec(iv,NULL);
if(iv!=NULL){
puts("Vector is not null after being freed at UINT64_MAX");
return -1;
}

/* Negative size testing is not possible as parameter passed to the constructor
is in itself an unsigned quantity. Hence, a negative number would just be
converted to a (rather large) positive integer */

/* It was done this way to maximise the largest size supported by the vector,
though, in doing so, it transfers a burden onto the programmer to check
whatever size is passed in to the constructor, is +ve*/


int arr[ARRAY_SIZE] = {0};
for(uint64_t i = 0; i<sizeof(arr)/sizeof(arr[0]); ++i){
arr[i] = i;
}
iv = construct_from_arr_intVec(arr,sizeof(arr)/sizeof(arr[0]));
if(iv->size != sizeof(arr)/sizeof(arr[0])){
puts("Vector size incorrectly set");
return -1;
}
print_intVec(iv,print_int);
putchar('\n');

// Construction from array tested


iv = new_intVec(size);

if(is_empty_intVec(iv)){
Expand Down

0 comments on commit beb1bce

Please sign in to comment.