Sheet 7 - Syntax Based Testing
Sheet 7 - Syntax Based Testing
A. Grammer-Based Mutation
Question 1
a. Define a set of test cases that satisfied TSC but not PDC.
b. Complete number (A) to satisfy PDC.
c. Draw syntax-tree for: xx * 33
Answer
Step 1: Define all terminals & all production rules
➢ Terminals: "a", ..., "z" (26), "0", ..., "9" (10), “+”, “-“, “*”, “/” (4).
➢ Production rules:
o expr ::= id
o expr ::= num
o expr ::= expr op expr
o id ::= letter
o id ::= letter id
o num ::= digit
o num ::= digit num
o op ::= "+"
o op ::= "-"
o op ::= "*"
o op ::= "/"
o letter ::= "a" (and similarly for "b" to "z", 26 rules)
o digit ::= "0" (and similarly for "1" to "9", 10 rules)
1
Step 2: Extract TSC & PDC
(A)
➢ TSC: Ensure every ingredient (terminal) is used at least once
➢ TSC (Pass-by all terminals at least once)
o "a", ..., "z" (26 Tests).
o "0", ..., "9" (10 Tests).
o “+”, “-“, “*”, “/” (4 Tests).
➢ Exclude what have covered
What is left:
➢ id ::= letter id
➢ num ::= digit num
(B)
o "a", ..., "z" (26 Tests).
o "0", ..., "9" (10 Tests).
o “+”, “-“, “*”, “/” (4 Tests).
o 23
o Zy
2
(C)
Question 2
expr ::= term | expr "+" term
term ::= factor | term "*" factor
factor ::= num | var | "(" expr ")"
num ::= digit | num digit
digit ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
var ::= "x" | "y" | "z"
Answer
Step 1: Define all terminals & all production rules
3
Step 2: Answer the following
(A)
(B)
➢ TSC: Ensure every ingredient (terminal) is used at least once
➢ TSC (Pass-by all terminals at least once)
o "0", ..., "9" (10 Tests).
o “x”, “y“, “z” (3 Tests).
What is left:
➢ term ::= term * factor
➢ expr ::= expr + term
4
B. Program Mutation
Question 1
Answer
a. i = 1 is mutant of i=0
Question 2
Answer
a. Mutant is always reached even if x = null.
b. Infection always occurs even if x=null because i always has wrong value after initialization.
5
Question 3
Answer
a. If x = null or array is empty, then the mutant us never reached.
b. Any input with all zeros will reach but not infect as x = [0] or x = [0,0]
Question 4
Answer
c. An empty array skips the for loop entirely.
d. findIndex(new int[] {10, 5, 6}, 5)
a. Original: Starts from index 0 → doesn’t match 10 → matches 5 at index 1 → returns 1.
b. Mutant: Starts from index 1 → matches 5 at index 1 → returns 1.