Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have to write state-coverage test code for this function (amongst 3 others...

ID: 1810197 • Letter: I

Question

I have to write state-coverage test code for this function (amongst 3 others... i just need to get a little kick-start). This is obviously really easy since this function has only 1 statement... However, I'm having trouble implementing state coverage..


/*

* heap_init -- empty the heap

* INPUTS: none

* OUTPUTS: none

* RETURN VALUE: 1 on success, or 0 on failure

* SIDE EFFECTS: eliminates any events from the heap

*/

int32_t

heap_init ()

{

event_heap_len = 0;

return 1;

}

This is the test code i need to use (provided by my professor) in order to properly test the functions... i'm confused as how to access the "event_heap_len" variable in order to test if it is truly equal to 0... for example I could use a simple IF statement to check if the variable is equal to 0. If it is not, then I can print an error message. However, I do not know how to access the variable from a separate file. Also, the "event_heap_file" is declared as STATIC


int32_t

test_heap_init (int32_t ret_val)

{

int32_t rv;


rv = heap_init ();

if (ret_val != rv) {

fprintf (stderr, "heap_init returned %d instead of %d ",

rv, ret_val);

return 0;

}

}

Explanation / Answer

If you had to do coverage ob-board, you would need to use a coverage profiler that created an instrumented version of the source. There are both commercial and open source tools available to do this, but IMO, it adds a lot of work for not much gain.

100% coverage is ambitious, as you will need a lot of fault injection to get into all your error handlers and exception handlers. IMO, this would also be easier to do in a harness than onboard.

It is also worth pointing out to whoever has asked for 100% code coverage that 100% code coverage in no way equates to 100% test coverage. Consider for example the following function;

100% code coverage only requires us to call this function once, 100% test coverage would require many more calls. My own test strategey involves developing automated testcases to give me an acceptable level of test coverage and using a code coverage tool purely as an aid to look for untested areas. To some extent it depends on your testing budget; for me 100% code coverage is way to expensive for what it delivers.