I'm doing a circular buffer / array in C using Cygwin, using unity testing along with each function and i'm encountering some bizarre things happening.
- First unity test was to ensure that the struct was initiated properly. It passed.
- Second unity test was to see if an element could be added and then deleted. It passed.
- Third unity test is adding 2 elements and then removing them after one another. It failed.
For some reason, and i've gone over this multiple times, it doesn't even remove the first value anymore. Given the fact that the buffer never switches the pointer for the head, it would seem likely that if it can remove 1 element as seen in the first test. It should surely be able to remove at least 1 element from the 2nd test.
The code for the 2 functions are given below and the unity test functions are just simple assertions to compare integers with those found in elements.
My money is on removeHead at this point, but is there an obvious mistake somewhere?
int addElement(struct circularBuffer* bufferPtr, int value){
if(bufferPtr.tail == SIZE-1 && bufferPtr.head = 0 ){ //Q full
printf("OVERFLOW");
return INT_MIN;
}
if(bufferPtr.head != 0 && bufferPtr.tail == SIZE-1){ //Q not full
bufferPtr.tail = 0;
bufferPtr->tail = value;
} else if(bufferPtr.tail+1 != bufferPtr.head){
bufferPtr.tail = bufferPtr.tail + 1;
bufferPtr->tail = value;
}
return value;
}
int removeHead(struct circularBuffer* bufferPtr){
if(bufferPtr.head == -1 || bufferPtr.head > bufferPtr.tail){
printf("UNDERFLOW");
return INT_MIN;
}else{
int val = bufferPtr.head;
bufferPtr.head = bufferPtr.head + 1;
}
}
Aucun commentaire:
Enregistrer un commentaire