Skip to content

Commit

Permalink
Merge pull request #2 from prathameshd30/vector-primitive-exhaustive-…
Browse files Browse the repository at this point in the history
…test

Vector primitive exhaustive test
  • Loading branch information
prathameshd30 authored Jul 12, 2024
2 parents 18d2620 + 7e6dc90 commit dce36bd
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 13 deletions.
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ Initially, I was going to write it in the OOP C Pattern (pointers to functions i

### To Do
- [x] Look into GitHub actions based testing
- [x] Valgrind testing?
- [x] How can I integrate CMake, CTest, GitHub actions and Valgrind?
- [ ] Write primitive as well as pointer-to-structs examples
- [ ] Examples should try to break the framework as much as possible
- [x] Valgrind testing?
- [ ] Use constant pointers, pointers to constants and constant pointers to constant wherever applicable.


### Checklist
- Each function, with pointer params, should check for NULL for each pointer,
Expand Down
19 changes: 10 additions & 9 deletions include/data_structures/vector/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ 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;\
}\
if(size == 0){\
new_obj->data = NULL;\
new_obj->size = 0 ;\
new_obj->size = 0;\
return new_obj;\
}\
new_obj->data = calloc(size,sizeof(TYPE));\
Expand Down Expand Up @@ -173,19 +173,20 @@ bool linear_search_##NAME(NAME* obj, TYPE ref ,bool (*comparator)(TYPE, TYPE), u
}\
return false;\
}\
bool delete_##NAME(NAME* obj, bool (*delete_type)(TYPE)){\
if(!obj){\
bool delete_##NAME(NAME** obj, bool (*delete_type)(TYPE)){\
if(!obj || !*obj){\
return false;\
}\
if(obj->data){\
if((*obj)->data){\
if(delete_type){\
for(uint64_t i = 0; i<obj->size; ++i){\
delete_type(obj->data[i]);\
for(uint64_t i = 0; i<(*obj)->size; ++i){\
delete_type((*obj)->data[i]);\
}\
}\
free(obj->data);\
free((*obj)->data);\
}\
free(obj);\
free(*(obj));\
*obj = NULL;\
return true;\
}\
bool clear_##NAME(NAME* obj, bool (*delete_type)(TYPE), TYPE val){\
Expand Down
75 changes: 73 additions & 2 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 @@ -40,9 +43,77 @@ int main(void){
puts("Empty vector not nulled");
return -1;
}
delete_intVec(iv,NULL);
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 (IMPRACTICAL, Roughly 73700 Petabytes of RAM needed)
// iv = new_intVec(UINT64_MAX);
// if(!iv){
// puts("Vector not created cleanly at size UINT64_MAX");
// return -1;
// }
// 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');
delete_intVec(&iv, NULL);
if(iv!=NULL){
puts("Deleted vector not NULL");
return -1;
}
// Construction from array tested


iv = new_intVec(size);

if(is_empty_intVec(iv)){
Expand Down Expand Up @@ -102,6 +173,6 @@ int main(void){

// More tests required!

delete_intVec(iv,NULL);
delete_intVec(&iv,NULL);
return 0;
}
2 changes: 1 addition & 1 deletion tests/data_structures/vector/vector_struct_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ int main(void){
}
// More tests needed
print_CardVector(cv,print_Card);
delete_CardVector(cv,delete_Card);
delete_CardVector(&cv,delete_Card);
return 0;
}

0 comments on commit dce36bd

Please sign in to comment.