Software Testing · IIT Madras BSc / IIIT-B · Prof. Meenakshi D'Souza
Week 3 — Unit Testing Based on Graphs
Big picture: Code (method/function) ko graph ke roop mein model karo → graph pe coverage criteria lagao → test cases nikalo. Is week 2 families dekhi: structural coverage (sirf control flow) aur data flow coverage (defs & uses ke saath).
def (definition) use yaad rakhne wala point dummy node
3.1Graph Coverage Criteria Applied to Test Code
Control Flow Graph (CFG) kya hai?
CFG ek method ke saare possible executions ko model karta hai. Nodes = statements ya statements ke sequences (basic blocks), edges = ek statement se doosre mein transfer of control. CFG code ka coarse abstraction hai — variables/exact statements ki info drop, sirf control flow retain.
Basic block: statements ka aisa continuous sequence jisme koi branching nahi — agar pehla statement chala, toh saare chalenge. Bina branch wale consecutive statements = ek hi node.
Fig 1 · Week 3 ka core pipeline — white-box unit testing of a single method.
CFG patterns — statement by statement
Fig 2 · If statements. Har return ka hamesha alag node hota hai (2 returns = 2 final nodes).Fig 3 · Loops. While/for mein condition node alag kyu? — repeat hone pe wapas check pe jaana hai, initialization pe nahi.Fig 4 · Node 3 = y=f(x,y) + if(y==0) ek saath (basic block). Break ka node 4, continue ka node 6.Fig 5 · case 'N' mein break nahi → edge seedha case 'Y' ke node tak. Break wale cases print(x) pe jaate hain.
Running example: Statistics program
// computeStats(int[] numbers)
int length = numbers.length; sum = 0.0;
for(int i=0; i<length; i++) { sum += numbers[i]; }
med = numbers[length/2]; mean = sum/(double)length; varsum = 0.0;
for(int i=0; i<length; i++) { varsum += (numbers[i]-mean)*(numbers[i]-mean); }
var = varsum/(length-1); sd = Math.sqrt(var);
// 4 print statements
Har transfer of control (decision, loop entry/exit) execute karo
Prime path coverage
Loops structurally test karo — skip / once / many times, bina loop counter jaane. Prime path = maximal simple path.
3.2Data Flow in Graphs
Ab CFG mein data ki info add karte hain — kahan variable ko value milti hai (def) aur kahan value access hoti hai (use).
Def: location jahan variable ki value memory mein store hoti hai (input, assignment...).
Use: location jahan value access hoti hai (RHS of assignment, if/while condition, output...).
Har node n ke liye def(n), use(n); har edge e ke liye def(e), use(e).
Programs ke graphs mein edges pe defs typically nahi hote (FSM designs mein edge actions ho sakte hain). Decision predicates ke uses edges pe aate hain — dono outgoing edges (true/false) pe same use set.
du-pair = pair (li, lj): v defined at li, used at lj.
Fig 7 · Def-clear: path ke kisi node/edge pe v ko another value nahi di jaati. Uses OK, re-defs NOT OK.
Notation (assignment mein direct pucha jaata hai)
du(ni, nj, v) = def-pair set — ni se nj tak v ke du-pathsdu(ni, v) = def-path set — ni ke def se shuru saare du-pathsdu(ni, v) = ∪nj du(ni, nj, v)
Multiple out-edges wale har node pe: har out-edge pe at least one variable use hota hai, aur same variables har out-edge pe.
Data flow testing: process
Fig 9 · Process. Notes: du-pairs bahut zyada ho sakte hain; kuch infeasible; infeasible du-pairs pehchaanna usually undecidable. Test data generation: explicit search, random testing, symbolic execution, model checking.
3.4Data Flow Testing Example — Statistics
Statistics ke CFG ko defs/uses se augment kiya (nodes 3, 6 dummy — predicates edges pe). Phir har variable ke du-pairs nikale:
mean: def at 5, used at 7 & 8 → pairs (5,7),(5,8) → du-paths [5,6,7], [5,6,8]
[1,2,3,5,…] type skip paths (dhyaan: empty array pe division-by-zero jaisa bug expose ho sakta hai — assignments ki favourite theme!)
3.5Summary — Master Subsumption Diagram 🏆
Ye diagram exam ki jaan hai — Prof. ne bola "this is the summary slide I want you all to remember."
Fig 11 · Arrow A → B = "A subsumes B". Prime path kar liya = kaafi kuch free mein mil gaya. Useful picks: prime path, all-du-paths, all-uses, edge, node.
Prime path ⇒ all-du-paths kyun? Prime paths har branch/decision ko lene AUR skip karne dono account karte hain — toh har def-to-use path bhi cover.
All-uses ⇒ edge kyun? (Assumption #3 ke under) har edge pe at least ek use hota hai → saare uses = saare edges.
IDE (Eclipse/IntelliJ/VS) se CFG generate kar sakte ho; course mein hand se. Next week: call graphs.
PAWeek 3 Practice Assignment — Q&A
Q1Which best describes a du-path for variable v?
✅ (a) Simple path that is def-clear from a def of v to a use of v
Prime paths: [3,4,5,2,3], [2,3,4,5,2], [1,2,3,4,5], [3,4,5,2,6], [5,2,3,4,5], [4,5,2,3,4], [2,3,5,2], [1,2,3,5], [3,5,2,3], [5,2,3,5], [3,5,2,6], [1,2,6]. Options (c) & (d) ke test paths sabko tour kar lete hain (loop skip + if-true + if-false + alternations).
Q9Kaunsa JUnit test method error reveal karta hai?
✅ (c) testCase3() — iArr = {6}, expected 6
min = 0 se initialize hai. Agar list ke saare numbers positive hain, min kabhi update nahi hota → 0 return hota hai jo list mein hai hi nahi! testCase3 mein sab positive → assertEquals(6,...) fail → bug caught. Fix: min ko iarr[0] se initialize karna chahiye tha.
GAWeek 3 Graded Assignment — Q&A
Q1All-uses subsumes edge coverage — kyun?
✅ (b) Each edge has at least one use
(Assumption #3) har out-edge pe use hota hai → saare uses cover = saare edges cover. Note: Jan 2025 term mein ambiguity ki wajah se sabko full marks mile the.
Q2True statements?
✅ (a) Side trips have to be def-clear · ✅ (d) Prime path coverage subsumes all-du-paths
Har du-path def-clear chahiye — side trips bhi. Prime paths har branch ko lene & skip karne dono account karte hain → har def-use path cover. (c) ulta direction hai — galat.
Common data Q3–5
x=0; y=1;
while(x<y){ y = x+y; x = x+1; }
Fig 13 · Convention: statements same branch pe = same node.
Empty array → n = 0 → else branch → loop skip → avg = totalLen/n = division by zero exception. Baaki inputs valid hain.
Pattern notice karo 👀 : Practice ka min=0 bug ho ya Graded ka division-by-zero — dono boundary inputs (empty array, all-positive array) se hi pakde jaate hain. Coverage criteria aapko in boundary paths (loop-skip path!) tak zabardasti le jaate hain — yehi unki power hai. Exam mein ye theme repeat hone ke full chances hain.
⚡Rapid Revision — quiz se pehle 5 minute
du-path = simple + def-clear (def → use). Intervening uses OK, re-defs NOT OK.