Lect06 Syntax
Lect06 Syntax
Syntax-based testing
1 / 63
Grammars, syntax and language
Developers use grammars and syntax of all the time, though they
may not realize it.
Whenever we see a requirement like “a date should be in the format
YYYY-MM-DD”, we’re making use of a grammar (though only very
informally expressed).
2 / 63
Analysing a date
3 / 63
Grammars
4 / 63
Grammars
The following grammar is equivalent to the previous one – in that
they define the exact same set of strings – but provides a few hints
as to the semantics of bits of the string (and is probably a bit easier
to read).
5 / 63
Notation
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" |
"7" | "8" | "9"
<year> ::= <digit> <digit> <digit> <digit>
<month> ::= <digit> <digit>
<day> ::= <digit> <digit>
<date> ::= <year> "-" <month> "-" <day>
The things in strings are called terminal symbols – they are the
equivalent of “words” in our language.
They are like atoms, in that they are the smallest, indivisible parts
of our language.
In our case, the terminals are all strings containing a single digit.
7 / 63
Notation
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" |
"7" | "8" | "9"
<year> ::= <digit> <digit> <digit> <digit>
<month> ::= <digit> <digit>
<day> ::= <digit> <digit>
<date> ::= <year> "-" <month> "-" <day>
To be precise – at its simplest, the right-hand side (RHS) of a rule will be a sequence
of terminals and non-terminals.
10 / 63
Use in development
11 / 63
Use in development
Command-line programs often take arguments – sometimes
adhering to very complex rules, as we saw in the first lecture:
12 / 63
Use in development
13 / 63
Use in development
14 / 63
Use in development
15 / 63
Use in development
Syntactically ill-formed:
class { MyClass }
16 / 63
Questions
17 / 63
Using the Syntax to Generate Tests
18 / 63
Using the Syntax to Generate Tests
19 / 63
An example of syntax-generated tests
20 / 63
Example – arithmetic expressions
21 / 63
Example – arithmetic expressions
22 / 63
Example – arithmetic expressions
23 / 63
Example – arithmetic expressions
24 / 63
More on BNF grammars
25 / 63
Use of grammars
26 / 63
Coverage criteria
27 / 63
Coverage criteria (cont’d)
28 / 63
Coverage criteria – an impractical one
29 / 63
Bounds on coverage
Example grammar:
<integer> ::= <digit>|<integer><digit>
31 / 63
Data structures
It is either:
(1) a null pointer, or (2) a value prepended to a list.
32 / 63
Data structures
33 / 63
“Linked list” grammar
34 / 63
“Linked list” grammar
35 / 63
Data structures
What about a linked list, where the value type V is, say,
another class, Person:
class Person {
PersonID personID;
bool isStaff;
int age;
}
36 / 63
Data structures
37 / 63
Trees
(Here, “S” stands for “sentence”, “VP” for “verb phrase”, “V” for
“verb”, “DP” for “determiner phrase” – basically something that picks
out a particular entity.)
2
Image from https://commons.wikimedia.org/wiki/File:
Precedent_example_1_decl_sent.png
38 / 63
Trees
39 / 63
Generators
Then we can see that “Alice hires Bob” and “Bob defeats the hacker”
are valid strings in the language this grammar defines (modulo some
whitespace).
And we can see how we could easily generate random valid sentences
that conform to these rules.
Being able to generate things that follow a syntax-like structure is
extremely useful for testing.
40 / 63
Generators – network traffic
41 / 63
Generators – http traffic
HTTP requests for web pages also follow a syntax, so we could easily
generate random HTTP traffic (for instance, to stress-test a
web-server, and see how it performs under high load).
The full syntax for HTTP requests is larger than this,3 but the start
of a simplified version of it would look something like:
<request> ::= <GETrequest> | <POSTrequest>
<GETrequest> ::= "GET" <space> <URI> <space> <HTTPversion>
<lineend> <getheaders> <getbody>
...
(i.e., HTTP requests are either GET or POST requests, and GET
requests start with the keyword GET then a space, then a URI, and so
on. . . )
3
See IETF RFC 2616,
https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
42 / 63
Generators – http traffic
43 / 63
Generators
44 / 63
Generators
45 / 63
Generators and data structures
46 / 63
More complex rules for validity
47 / 63
Using generators for testing
48 / 63
Property-based testing
49 / 63
Property-based testing
50 / 63
Property-based testing
51 / 63
Applications of syntax-based testing
52 / 63
Mutation testing
53 / 63
What is mutation ?
54 / 63
Mutation testing – definitions
55 / 63
Killing Mutants
56 / 63
Syntax-based coverage criteria – mutant coverage
57 / 63
Coverage criteria – creating invalid strings
58 / 63
Mutation example
A grammar:
Stream ::= action*
action ::= actG | actB
actG ::= "G" s n
actB ::= "B" t n
s ::= digit{1-3}
t ::= digit{1-3}
n ::= digit{2} "." digit{2} "." digit{2}
digit ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" |
"7" | "8" | "9"
A ground string:
G 23 08.01.90
B 19 06.27.94
60 / 63
Mutation example (cont’d)
61 / 63
Mutation example (cont’d)
mutated to:
B 23 08.01.90
B 15 06.27.94
62 / 63
Mutation example (cont’d)
63 / 63