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.
Code → Graph → Coverage → Test Cases method / function code if · while · for ... CFG (nodes + edges) Coverage criteria node · edge · prime path all-defs · all-uses · all-du-paths Test Cases ✓
Fig 1 · Week 3 ka core pipeline — white-box unit testing of a single method.

CFG patterns — statement by statement

if – else if (no else) 1 2 3 4 x < y x >= y y=0; x=x+1 x=y+1 z=x+1 then & else alag nodes → merge after if 1 2 3 x < y x >= y false branch seedha aage — 2 execution paths
Fig 2 · If statements. Har return ka hamesha alag node hota hai (2 returns = 2 final nodes).
while loop for loop do–while 1 2 3 4 x<y back edge x>=y x=0 y=f(x,y); x=x+1 condition-check ka ALAG node (dashed = dummy) 1 2 3 4 x<y x=0 y=f(x,y); x++ x>=y init → check(dummy) → body → back edge 1 2 3 x<y x=0 x>=y y=f(x,y); x=x+1 body PEHLE, check BAAD — self loop
Fig 3 · Loops. While/for mein condition node alag kyu? — repeat hone pe wapas check pe jaana hai, initialization pe nahi.
while + break + continue (lecture ka 8-node example) 1 2 3 4 5 6 7 8 x=0 x<y y==0 break; y<0 y=y*2; continue; x=x+1 print(y) x>=y break → loop ke BAAD wale node pe continue / body-end → wapas condition node 2 pe (back edges)
Fig 4 · Node 3 = y=f(x,y) + if(y==0) ek saath (basic block). Break ka node 4, continue ka node 6.
switch–case (fall-through ka funda) read(c) switch(c) z=25; x=50; break; x=0; break; print(x); NO break → fall-through! 'N' 'Y' default
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
Statistics program ka CFG (8 nodes, 2 loops) 1 2 3 4 5 6 7 8 inits i=0 for #1 check sum += ... med, mean, varsum=0, i=0 for #2 check varsum += ... var, sd, prints Edge coverage TR: {[1,2],[2,3],[3,4],[4,3],[3,5],[5,6],[6,7],[7,6],[7,8]} · ek test path kaafi: [1,2,3,4,3,5,6,7,6,8] Prime path test paths: [1,2,3,4,3,5,6,7,6,8] · [1,2,3,5,6,8] (skip) · [1,2,3,4,3,4,3,5,6,7,6,7,6,8] (2 iters)
Fig 6 · Nodes 3 & 6 = dummy loop-check nodes. Nodes 5 & 8 = bade basic blocks (club kiye hue statements).
Structural criteriaCode pe matlab
Node coverageHar basic block / statement execute karo
Edge coverageHar transfer of control (decision, loop entry/exit) execute karo
Prime path coverageLoops 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...).
def-clear path & du-path — THE definition of Week 3 x = 5 y = 2 z = x+1 if(x>0) def of x intervening USE — allowed ✓ use of x du-path (w.r.t. x) = SIMPLE path + DEF-CLEAR (beech mein x ko dobara value NAHI) agar beech mein kahin x = kuchh_aur; aa gaya → path def-clear NAHI ✗ def "reaches" the use ✓
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-paths du(ni, v) = def-path set — ni ke def se shuru saare du-paths du(ni, v) = ∪nj du(ni, nj, v)

c-use vs p-use

c-use (computation use)p-use (predicate use)
KahanComputational / output statement → CFG node peConditional statement → CFG edge pe
Notationdcu(li, lj, v)DO pairs bante hain: dpu(li,(lj,lt),v) [true edge] & dpu(li,(lj,lf),v) [false edge]
Course mein dono ko bas "use" bolte hain — node pe ya edge pe.

Pattern Matching example (data flow graph)

Subject string mein pattern dhundhne wala program. CFG ~11 nodes; usse defs/uses se augment kiya:

Locationdefuse
node 1{subject, pattern}
node 2 (inits){NOTFOUND, iSub, rtnIndex, isPat, subjectLen, patternLen}{subject, pattern}
edges (3,4)=(3,11){iSub, patternLen, subjectLen, isPat}
node 5{rtnIndex, isPat, iPat}{iSub}
edges (6,7)=(6,10){iPat, patternLen}
node 8{rtnIndex, isPat}{NOTFOUND}
node 9{iPat}{iPat}
node 10{iSub}{iSub}
node 11{rtnIndex}
Tricky point ⚡ : iPat++ = iPat = iPat + 1use PEHLE hota hai, def BAAD mein. Isliye iPat ke liye node 5 → node 9 tak def-clear path jaata hai.

3.3Data Flow Graph Coverage Criteria

du-paths bahut saare ho sakte hain, isliye unhe group karke criteria banate hain — def-path sets aur def-pair sets ke through.

Teen criteria — ek nazar mein

Ek def, teen uses — teeno criteria ka farak All-Defs def v use use use har def → koi EK use "koi def waste na jaaye" All-Uses def v use use use har def → HAR use (path koi bhi ek chalega) All-du-Paths def v use use use har def → har use, HAR path se most expressive criteria Best-effort touring allowed — lekin side trips bhi DEF-CLEAR hone chahiye!
Fig 8 · Formal: All-Defs — har du(n,v) ka ≥1 path TR mein · All-Uses — har du(ni,nj,v) ka ≥1 path · All-du-Paths — har du(ni,nj,v) ka EVERY path.

Worked example — du(10, iSub) in Pattern Matching

Def-path set: du(10, iSub) = {[10,3,4], [10,3,4,5], [10,3,4,5,6,7,8], [10,3,4,5,6,7,9], [10,3,4,5,6,10], [10,3,4,5,6,7,8,10], [10,3,4,10]}

Def-pair sets mein todo: du(10,4,iSub)={[10,3,4]} · du(10,5,iSub)={[10,3,4,5]} · du(10,8,iSub)={[10,3,4,5,6,7,8]} · du(10,9,iSub)={[10,3,4,5,6,7,9]} · du(10,10,iSub)={[10,3,4,5,6,10],[10,3,4,5,6,7,8,10],[10,3,4,10]}

Subsumption — 3 assumptions pe based

  1. Har use se pehle kahin ek def hota hai.
  2. Har def kam se kam ek use tak pahunchta hai.
  3. 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

Input program Data flowanalysis Def–use pairsset Test datageneration Is du-pair covered? Test cases ✓ False → wapas du-pair set pe True
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:

Counting: total 38 du-paths (variables across), duplicates hatao → 12 unique du-paths.
12 unique du-paths → 3 buckets (loops ke hisaab se) SKIP the loop (4) [1,2,3,5] [1,2,3,5,6,8] [2,3,5] · [5,6,8] 3→5, 6→8: loop-entry avoid ≥ 1 iteration (6) [1,2,3,4] · [2,3,4] [4,3,5] · [5,6,7] [7,6,8] · ... loop mein ghusna & niklna Repeat / 2+ iterations [4,3,4] [7,6,7] inko repeat karke jitni chahe iterations bana lo Bucketing kyun? — pata rehta hai har du-path loop-testing mein kya role play karta hai.
Fig 10 · Buckets, prime paths jaisi hi soch: skip / one iteration / many iterations.

Test cases

Test caseInputTest pathKya cover hota hai
#1numbers = (44), length 1[1,2,3,4,3,5,6,7,6,8]du-paths [1,2,3,4], [2,3,4], [4,3,5], [5,6,7], [7,6,8] — sab "≥1 iteration" wale
#2numbers = (2, 10, 15), length 3dono loops 2+ iterations[4,3,4], [7,6,7] wale repeat paths
#3empty arrayloops skip[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."

Graph coverage criteria: kaun kisko subsume karta hai Complete Path Coverage Prime Path Coverage ⭐ Edge-Pair Coverage Edge Coverage Node Coverage Complete Round Trip Simple Round Trip All-du-Paths Coverage All-Uses Coverage All-Defs Coverage NEW! NEW! 2 cross arrows ratta maaro: Prime Path ⇒ All-du-Paths (⇒ All-Uses ⇒ All-Defs bhi)  ·  All-Uses ⇒ Edge
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.

PAWeek 3 Practice Assignment — Q&A

Q1 Which 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
Dono conditions zaroori: simple + def-clear. (Lec 3.2, slide 14)
Q2 T/F: Best effort touring (side trips & detours) allowed for data flow criteria?
✅ (a) True
Structural jaisa hi — bas side trips def-clear hone chahiye. (Lec 3.3, slide 8)
Q3 T/F: Test cases are generated after identifying du-pairs?
✅ (a) True
Process flowchart yaad karo (Fig 9): du-pairs → test data generation. (Lec 3.3, slide 18)
Q4 Def-clear path w.r.t. v — which is true?
✅ (b) v is not given another value along the path
Re-defs mana hain; uses allowed hain!
Q5 Simple path, def-clear, def→use — kya kehlata hai?
✅ (d) du-path
Q6 T/F: CFGs of programs define & use variables at nodes and edges?
✅ (a) True

Common data Q7–9: findMin()

public int findMin(){
  int min = 0;                      // ← BUG yahan chhupa hai!
  for(int i = 0; i < iarr.length; i++){
    if(min > iarr[i]) min = iarr[i];
  }
  return min;
}
findMin() ka CFG 1 2 3 4 5 6 min=0 for check if(min>iarr[i]) min=iarr[i] i++ return min back edge 5→2 exit 2→6 (i ≥ length)
Fig 12 · Edges: 1→2, 2→3 (i<len), 3→4 (if true), 3→5 (if false), 4→5, 5→2 (back), 2→6 (exit).
Q7 Test paths for prime path coverage?
✅ (c) AUR (d) — dono correct
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).
Q8 Test cases covering all prime paths?
✅ (c) {iarr=[], iarr=[-5], iarr=[6], iarr=[-6,-5,-10], iarr=[6,5,10]}
Empty = loop skip [1,2,6]; singles = 1 iteration (if true/false); decreasing triple = if baar-baar true; increasing = if false iterations.
Q9 Kaunsa 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

Q1 All-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.
Q2 True 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; }
Q3–5 ka CFG: 4 nodes, 1 dummy 1 2 3 4 x=0; y=1 (EK basic block) while check = DUMMY y=x+y; x=x+1 (EK node) exit x<y x>=y back edge
Fig 13 · Convention: statements same branch pe = same node.
Q3 Total nodes & dummy nodes?
✅ (b) Four nodes, one dummy node
1 = {x=0; y=1}, 2 = while check (dummy — koi statement execute nahi karta), 3 = loop body, 4 = exit.
Q4 T/F: Pehle 2 statements ka ek hi node?
✅ (a) True
Same branch pe hain → ek basic block, ek node.
Q5 Loop body ke 2 statements ke liye kitne nodes?
✅ (a) One
Dono while body (same branch) mein → group into one node.

Common data Q6–10: getAvgWordLen()

public double getAvgWordLen() {
  double avg = 0.0;  int totalLen = 0;
  int n = words.length;
  if(n == 1) { avg = words[0].length(); }
  else {
    for(int i = 0; i < n; i++) { totalLen += words[i].length(); }
    avg = (double)totalLen / n;   // BUG: n==0 → division by zero!
  }
  return avg;
}
Correct CFG (option b) — nodes 1..7 1 2 3 4 5 6 7 inits if(n==1) avg=words[0].length() for check totalLen += ... avg=totalLen/n return avg n==1 else i>=n (exit)
Fig 14 · Edges: 1→2, 2→3, 2→4, 3→7, 4→5, 5→4 (back), 4→6, 6→7. Elimination: (a) loop nahi ✗ · (d) branching nahi ✗ · (c) loop if se pehle ✗ → (b)
Q6 Correct CFG?
✅ (b)
Elimination technique (yaad rakho): code mein loop hai → CFG mein cycle chahiye (option a out); code mein if hai → branching node chahiye (option d out); code mein if pehle, loop baad mein → cycle conditional ke baad honi chahiye (option c out). Bacha (b) ✓
Q7 Saare prime paths?
✅ (b) {[1,2,4,6,7], [1,2,4,5], [1,2,3,7], [5,4,6,7], [5,4,5], [4,5,4]}
Maximal simple paths — loop segments [5,4,5], [4,5,4], loop enter/exit combos, aur if-true path [1,2,3,7].
Q8 Prime path coverage ke test paths?
✅ (b) {[1,2,4,5,4,5,4,6,7], [1,2,3,7], [1,2,4,6,7]}
Pehla: loop 2 baar → saare loop prime paths tour. Doosra: n==1 branch. Teesra: loop skip (n==0). Milke saare 6 prime paths cover.
Q9 In test paths ke test cases?
✅ (d) {words = {"duck"}, words = {}, words = {"duck","crow"}}
{"duck"} → n==1 → [1,2,3,7] · {} → loop skip → [1,2,4,6,7] (division-by-zero!) · {"duck","crow"} → 2 iterations → [1,2,4,5,4,5,4,6,7].
Q10 Kaunsa test method error uncover karta hai?
✅ (c) testCase3() — String[] words = {};
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

  1. du-path = simple + def-clear (def → use). Intervening uses OK, re-defs NOT OK.
  2. Subsumption chain: All-du-Paths ⇒ All-Uses ⇒ All-Defs.
  3. Cross arrows: Prime Path ⇒ All-du-Paths aur All-Uses ⇒ Edge — master diagram ki jaan.
  4. Data flow mein side trips def-clear hone chahiye.
  5. Loop-check node = dummy node; bina branching wale consecutive statements = ek basic block / ek node.
  6. x++ type statements: use pehle, def baad mein.
  7. Switch-case bina break = fall-through edge agle case tak; har return ka alag node.
  8. Infeasible du-pairs identify karna usually undecidable hai; du-pairs bahut zyada ho sakte hain.
  9. Boundary test cases (empty array, single element, all-positive) hi classic bugs pakadte hain — min=0 bug, division-by-zero bug.
  10. Process: program → data flow analysis → du-pairs → test data generation → coverage check → test cases.