ST第二次作业
课程复习:第二节课我知道了failure,fault,error的区别。
Failure:External, incorrect behavior with respect to the requirements or other description of the expected behavior;
Fault: A static defect in the software;
Error:An incorrect internal state that is the manifestation of some fault;(eg: some symptoms);
除此之外我还学会了General Technique-Tarantula的debug方法。
作 业:Below are four faulty programs. Each includes a test case that results in failure. Answer the following questions (in the next slide) about each program.
public int findLast (int[] x, int y) { //Effects: If x==null throw NullPointerException // else return the index of the last element // in x that equals y. // If no such element exists, return -1 for(int i = 0; i < x.length; i++) { if (x[i] == 0) { return i; } } return -1; } // test: x=[2, 3, 5]; y = 2 // Expected = 0
public static int lastZero (int[] x) { //Effects: if x==null throw NullPointerException // else return the index of the LAST 0 in x. // Return -1 if 0 does not occur in x for (int i = 0; i < x.length; i++) { if (x[i] == 0) { return i; } }ruturen -1; } // test: x=[0, 1, 0] // Expected = 2
1.Identify the fault.
(1) 'i > 0' 应该改为 'i >= 0';
(2) 返回的是第一个'0',而不是最后一个'0'
int k = -1; for (int i= 0; i< x.length; i++) { if (x[i] == 0) { k = i; } } return k;
2.If possible, identify a test case that does not execute the fault. (Reachability)
(1) 测试用例:x = null.
(2) 执行是错误的。
3.If possible, identify a test case that executes the fault, but does not result in an error state.
(1) 测试用例: x = [1,2,3 ]; y = 3; Expected: 2; Actual: 2.
(2) 任何用例都会出现错误。
4.If possible identify a test case that results in an error, but not a failure.
(1) 测试用例: x = [1,2,3 ]; y = 4; Expected: -1; Actual: -1.
(2) 测试用例: x = [1,1,0 ]; Expected:2; Actual:2.

浙公网安备 33010602011771号