Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Exercise 2 (9 points)
Define a Timed Petri Net that captures the following behavior of a system.
The system emits sequences of signals that have the following characteristics. Each sequence starts and ends
with a signal b. Between two signals b there is a (non-empty) sequence of signals that starts with a signal s,
followed by a signal e; the sequence “s followed by e” is repeated, until the whole sequence s, e, s, e, … , s, e
(which must end with a signal e) is terminated by a signal b. After a sequence of signals b, s, e, s, e, …, s, e,
b, the system waits for exactly 10 seconds, and then a new sequence b, s, e, …, b is emitted. Between two
signals b there must be at least a signal s, followed by an e.
The following timing constraints must hold:
• Each signal e cannot follow the preceding s by more than 0.1 seconds.
• Every time a signal s is followed by an e, which is then followed by another s, the time distance
between the two s signals must be at least 0.5 seconds, and it cannot be more than 2 seconds.
• The b signals that “bookend” a sequence b, s, e, …, b must be no more than 20 seconds apart. If the
sequence has not yet finished 20 seconds after the starting b, the whole system halts.
Exercise 3 (6+4 points)
a.
Specify the pre- and post-conditions of a program findPairDouble(A) that takes as input an array A of
integer values and returns true if, in the array, there are two elements such that one is the double of the other,
and the one that is the double appears in the array after the other (with possibly other elements in between).
If no two such elements exist, the program returns false. Also, if the array is NIL, or it contains less than 3
elements, the program raises an error.
The input array must not be sorted in ascending order, and it must contain at least one negative element and a
positive one.
b.
Define a black-box test set for the specification of program findPairDouble(A) (indicate, for each test case,
the expected result).
Exercise 4 (5 points)
Consider the following fragment of code:
findPair(A)
1 if A = NIL or A.length ≤ 2
2 error('invalid input')
3 i := 1
4 while i < A.length
5 j := 1
6 while j ≤ A.length
7 if A[j] = 2*A[i]
8 return true
9 j := j +1
10 i := i +1
11 return false
1. Define, if possible, a minimal test set that covers all edges. If it is not possible, explain why.
2. Define a test set that 2-covers all feasible paths. For each test case, indicate the value returned by the
program. Does the program implement the specification of exercise 3? If it does not, explain why.
Solutions
Exercise 1
1.
2.
Exercise 2
0.5,0.5 1.5,1.5
s
b s e s e
0,0.1 0,0.1
Instead of waiting before
having another b, 0,0
the b could occur immediately b b
after the 10 seconds
0,0
10,10 wait
20,20 0,0
0,0
Exercise 3
a.
Pre:
A = NIL ∨ A.length < 3 ∨
( ∃i,j( 1 ≤ i ≤ A.length ∧ 1 ≤ j ≤ A.length ∧ A[i] < 0 ∧ A[j] > 0 )
∧
∃i,j( 1 ≤ i ≤ A.length ∧ 1 ≤ j ≤ A.length ∧ i < j ∧ A[i] > A[j] ) )
Post:
res = true ⇒ A.length > 2 ∧ ∃i,j( 1 ≤ i < j ≤ A.length ∧ A[j] = 2*A[i] )
res = false ⇒ A ≠ NIL ∧ A.length > 2 ∧
¬∃i,j( 1 ≤ i < j ≤ A.length ∧ A[j] = 2*A[i] )
A = NIL ∨ A.length ≤ 2 ⇒ error("invalid input")
b.
Normal behavior:
A = NIL (result: error)
A = [ 8, -7 ] (result: error)
A = [ 7, -3, 10, 6, -6, 5 ] (result: true)
A = [ -9, 8, 2, -4, 16, 9, 2 ] (result: true)
A = [ -5, 15, -3, -2, 42, 27, -3 ] (result: false)
A = [ 13, 16, -2, 8, -2, 10 ] (result: false)
Robustness:
A = [ -3, 4, 8 ]
A = [ -5, -2, -10 ]
Boundary values:
A = [ 9, -10, 18 ] (result: true)
A = [ -7, 16, 6 ] (result: false)
A = [ -8, 6, -4 ] (result: false)
Exercise 4
1.
A minimal test set that covers all edges is { A = NIL, A = [ -7, 4, -4, 8 ], A = [ 13, 4, 12 ] }
2.
A test set that 2-covers all feasible paths is:
{ A = NIL (result: error),
A = [ 0, 3, 18 ](result: true),
A = [ -5, -10, 8 ](result: true), }
It is clear that the code does not implement the specification of exercise 3. Indeed, it does not check that i is
different from j (so it returns true if there is a single 0), and it does not check that the index of the element
that is the double is bigger than the other one.