Presentation is loading. Please wait.

Presentation is loading. Please wait.

50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Similar presentations


Presentation on theme: "50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;"— Presentation transcript:

1 50.530 Exercise Solutions 2014 Fall Term

2 Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false; } if (mystack.empty() != mystack.elements().hasMoreElements()) { return false; } if (mystack.isEmpty() && mystack.size() != 0) { return false; } //... return true; }

3 Week 2: Exercise 2 Valid test case: 1. OrdSet newobj = new OrdSet(5); 2. OrdSet newobj = new OrdSet(5); newobj. add(3); Redundant test case: OrdSet newobj = new OrdSet(5); newobj.toString(); Illegal test case: OrdSet newobj = new OrdSet(-1);

4 Week 3: Exercise 1 DD+({1..8}, {}, 2) DD+({1..8}, {}, 4) try again DD+({1..4,7,8}, {5,6}, 8) test({1..4}) =? = test({5...8}) test({5,6}) = PASS try again test({5,6,8}) = FAIL DD+({8}, {5,6}, 2) found in {8} return {8};

5 Week 3: Exercise 2 Use DDmin algorithm to minimize the input Cut the input in a half and test on the first half It fails and thus apply DDmin again and repeat. Once the minimum input is reached, identify the bug by tracing through the code based on the minimum test case.

6 Week 5: Exercise 1 Using Set Union: bug is at line 7 (since only line 7 is unique to the failed test case). Using Set Intersection: the result is {} and thus no predication.

7 Week 5: Exercise 2 Using Set Union: the result is {} and thus no predication. Using Set Intersection: the result is {} and thus no predication.

8 Week 5: Exercise 3 The nearest neighbor would be the second last test case and the predicate would be: line 7.

9 Week 5: Exercise 4 The nearest neighbor would be the first test case and the predicate would be: none.

10 Week 5: Exercise 5

11 Week 5: Exercise 6 To apply the method A/B/C/D, you need to instruct the program to monitor its execution (one for each block of code without any branches). None of the methods would locate the bug (for instance, method E would not work as the buggy line is not updating any variable) except perhaps method D (which depends on the test cases you generate).

12 Week 6: Exercise 1 The slice of the assertion includes all the statements.

13 Week 8: Exercise 1 Answer 1: No, there is no race condition according to the happens- before approach. Answer 2: Yes, there is a race condition on variable count (the first and last line).

14 Week 8: Exercise 2 The lockset algorithm finds the race condition at line 1 since count is a shared variable and it is accessed without any lock held (so that the intersection is {}).

15 Week 8: Exercise 3 For variable count, after the first count++, it moves from state Virgin to state Exclusive After the second count++, it moves from state Exclusive to Shared- Modified state since it is accessed by a new thread with write privilege. The checking then happens and since there is no lock held, a race condition is reported.

16 Week 8: Exercise 4 The report would be there is no race.

17 Week 8: Exercise 5 Lockset-based algorithm Race on x: line 10 and 1 Race on z: line 7 and 5 Happens-before filtering No race on Z since there is happens-before from unlock(L) to lock(L)

18 Week 8: Exercise 6 Assume that thread 1 is scheduled first, after executing line 1, it is postponed; Thread 2 is scheduled to run line 7 and then is blocked at line 8. Thread 1 moves on and thus no race is reported.

19 Week 9: Exercise 1 (3*y + z)*y - z > 0

20 Week 9: Exercise 2 5 * y - z > 0

21 Week 9: Exercise 3 We get the following Hoare triple first. {K>0&&M>0&&k=K&&m=M} while(k!=m) { if (k > m) {k = k-m;} else {m = m-k;} } {k=GCD(K,M)} We discharge it using the loop invariant: GCD(K, M) = gcd(k,m)

22 Week 9: Exercise 4 First row from left to right: yes, yes, no Second row from left to right: yes, yes, yes

23 Week 9: Exercise 5 For the inner loop, the ranking function is x-y and termination can be proved by establishing the following Hoare triple. {x-y=V&&y 0} y = 2*y {x-y < V} For the outer loop, the ranking function is x and termination can be proved by establishing the following Hoare triple {x>=0&&x=V} y := 1; while (y < x) { y = 2*y; } x = x – 1; {x < V} which can be proved using a loop invariant (i.e., x is unchanged) for the inner loop.

24 Week 10: Exercise 1 (Monday or Wednesday or Thursday) and (!Wednesday) and (!Friday) and (!Tuesday and !Thursday) SAT solving would lead to a solution: Monday = true; the rest = false;

25 Week 10: Exercise 2 The assertion can be demonstrated possible by symbolically executing the following path. !a && b && !a && c && y = 1 && z = 2 && !(x+y+z!=3) The solution is: a = false; b = true; c = true

26 Week 10: Exercise 3 Example: x+y<=4

27 Week 10: Exercise 4 Symbolic execution the left most path, apply interpolantion and get y>=1 at node 3. Skip the last alternative; backtrack … 2 33 444 2 33 444 1 x = x+1 [y>=1]x = x+2 [y<1]x = x+4 * y<1 y>=1 44 [y>=1]x = x+2 y<1 [y<1]x = x+4 y>=1

28 Week 10: Exercise 5 The only exception that could rise at array[x]=2 when x = 0. Base case: assume input is false, the following is true: {x>=0}rec(){x>=0} which is basically {x>=0}x=x+1{x>=0} (*). Induction step: assume that {x>=0}rec(){x>=0}. {x>=0}rec(){x>=0} implies {x>=0}rec(); rec();{x>=0} (**). {x>=0} method-body {x>=0} since (*) and (**) {x>=0} rec() {x>=0}

29 Week 11: Exercise 1 We can prove that the error is not occurring using a Hoare triple as below: {x=0.1&&y=0}loop-body{y>=0}, which can be proved using the following loop invariant: y>=0.

30 Week 11: Exercise 2 Sample: y=1, orig(x) > 0, x > y, return = *x, return = orig(*x) + y

31 Week 11: Exercise 3 The same tree (the proof) will be built. L0 L1 L2 L3 new!=old L4 L5 new=old+1 L1 L6 L2 L5 L1 subsumed by new!=old

32 Week 12: Exercise 1

33 Week 12: Exercise 2 1: if (*) { 3: got_lock = 0; 4: if (*) { 5: lock(); 6: got_lock ++; 7: } 8: if (got_lock) { 9: unlock(); 10: } 11: if (*) {goto 3;} 12: } 14: lock(); 15: old = new; 16: if (*) { 17: unlock(); 18: new ++; 19: } 20: if (new != old) {goto 14;} 21: unlock (); 1: if (*) { 3: skip; 4: if (*) { 5: locked=true 6: skip; 7: } 8: if (*) { 9: locked=false; 10: } 11: if (*) {goto 3;} 12: } 14: locked=true; 15: skip; 16: if (*) { 17: locked=false; 18: skip; 19: } 20: if (*) {goto 14;} 21: locked=true;

34 Week 12: Exercise 3 r gy

35 Week 12: Exercise 4 2 3 4 5 6 7 8 {!locked, ne} {locked, ne} {locked, !ne} {!locked, !ne} {!locked, ne} 8 {locked, !ne}


Download ppt "50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;"

Similar presentations


Ads by Google