Search code examples
code-coveragevariable-declarationdataflowtest-coverage

How to achieve data flow coverage on unused variable assignments?


In data flow coverage, DU-paths are used to isolate a variable that is defined and used all over the code part.

In such a function scenario, how can I write test cases that cover all definitions of the variable x?

Since it's only assigned a new value each time but not used anywhere in this function:

Function x (int y, int z) {
   int x = 0;
   M = y + z;
   My_Array = [1,2,3];        
   if (0 < M < 10)
       x = My_Array[1];
   else if (10 < M < 20)
       x = My_Array[2];
   else
       x = 0;
}

Solution

  • The way to use data flow analysis in testing is to

    • analyze the code
    • if the data flow analysis found any problems in the code, construct an ideal data flow without those problems
    • write tests based on the ideal data flow. The tests will pass, because we're designing them based on a cleaned-up view of the code.
    • correct any problems in the code to match the ideal data flow. The tests will still pass, as long as you don't make any mistakes when correcting the code.

    DU-path analysis assumes that all variables are Defined and then Used. When a variable isn't used before the end of the code, it's an error in the code and we correct it as above. DUK-path analysis considers "kills" (places where a variable is reassigned or exits the program), but it considers a kill without a use an error that needs to be corrected in the same way.

    So, in your example, we eliminate all of the unused assignments and find that there is no code left, nothing to test, and the function can be inlined!