-
Circular Buffer
I am writing a circular/ring buffer of a specified size. I am struggling with figuring out if my code is working. It compiles and runs with no problem, but I try testing it with print statements and I get confused. Can anyone give me some advice for how to test that my code is functioning properly - access up to a defined number of buffers and checks if the buffer is full or empty when requesting the next and freeing the last buffer.
-
For starters:This doesn't do anything.
Quzah.
-
I was trying to set a value to the dummy_val in my struct so I could test to see if numbers were be stored properly then to see if the code circled through it correctly. how do I do so?
-
Assignment: =
Comparison: ==
Compile with warnings on and it will tell you about that sort of thing.
Quzah.
-
ok thanks, how can I test it now to see if it is working properly?
-
when running it I get "Bus error"
-
1. Why does incrementing the head always increase the count, but incrementing the tail always decreases the amount?
2. It's generally a poor idea to name variables the same thing as functions. (See: increment_head)
3. Your first call to 'requestNextBuffer()' is probably returning NULL, because it thinks the buffer is full, but then you go ahead and immediately assign something to that anyway.
Quzah.
-
1. my thought process was that i needed to somehow keep track of how many parts of the buffer were full. So the incrementing up when allocating a buffer space, and decreasing when the buffer space is free would tell if the number of buffers ever reaches 16 (therefore being full).
2.do you mean increment_head, and head?
3. How can I assign something to the buffer then test if it is there and if it is, then test to see the program realizes that the buffer is full?
-
No, I mean this:
Code:
int isBufferEmpty (int increment_head)
{
if (increment_head <=0)
Well, you need to set your buffer up right first. As it is, it's evaluating to full right at the start. Also, you don't seem to actually be using 'num_allocated' anywhere. You might consider using that to see if it's full or not. But, any time you use a pointer, it's best to actually see if it's NULL before you start trying to dereference it.
Quzah.
-
oh oops, I meant that to be :
Code:
int isBufferEmpty (int num_allocated)
{
if (num_allocated <=0)
{
return 1;
}
I could have also used num_allocated for if the buffer was full (i.e. if(num_allocated >=16))
Ok, so i am still a little hazy on how to check my buffer, does fixing the num_allocated in the bufferempty change how I am evaluating it?
-
Code:
isfull
if num allocated >= buffer size
return full
else
return not full
That's basically how you want to check for fullness. If you're using globals, you don't really need to pass it anything, provided you're remembering to actually increase the number allocated when you do allocate something.
Code:
getnew
if not isfull
return buffer[ currentposition ++ ]
else
return full
Basically all you want to do is return the current spot, and update the current position. How you exactly do that is up to you, but it goes basically something like that.
Code:
freelast
if not isempty
return (buffer[ tail-- ] = 0)
else
return failure
Similar to allocating, you probably want to return some indicator of success or failure. Not that you really need to return anything at all.
It doesn't really matter what you call tail or currentposition, or whatever. You just need to make sure you update your actual variables correctly. I'm not sure why you even really need a circular buffer, if all you're doing is freeing the last thing allocated. I mean, it's not like you're ever going to go below zero, and it's not like you're going to ever go above the top.
Quzah.
-
Awesome, someone is actually asking about how they can unit test their code. Well I'm impressed!
It looks like everyone else is helping you with the bugs they can see, rather than stating how it can be tested.
Hmm, this would be so much easier if it were C++, but oh well...
Before testing these functions, I would first correct the interface of isBufferEmpty and isBufferFull. It doesn't make sense to me that these would logically require any parameters. Well not the way you're doing it, and in C++ it would only require the implicit 'this' parameter. Then I'd start by testing those.
The first test of each of these would be to check their result on an empty buffer. isBufferEmpty should return true whilst isBufferFull should return false. Note that I'm assuming that your buffer size will never be zero :)
Then you try putting different numbers of items in there and check the results again. Unfortunately beyond this it becomes a little difficult because you're missing some kind of reset function. In C++ you'd just declare a new buffer in each test, but here you're effectively using the same "instance" every time. You'll therefore need some kind of reset function (which in itself kinda also needs testing).
I'll help more once you get into it a bit more.