Software Testing (ii)

Source: Internet
Author: User

Second assignment

For the following two programs

1. Find out fault

2, if possible, find out a test case does not execute fault

3, if possible, find a test case execution fault, but do not generate error

4. If possible, find a test case that produces an error, but does not cause failure

Public Intfindlast (int[] x, inty) {//effects:if x==null throw nullpointerexception//else return the index of the last El ement//in X is equals y.//If no such element exists, return-1 for    (int i=x.length-1; i> 0; i--)    {        if ( X[i] = = y)        {            return i;         }    }    return-1;} Test:x=[2, 3, 5]; y = 2//expected = 0

  

public static Intlastzero (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)        {            ret Urn i;        }    } return-1;} Test:x=[0, 1, 0]//expected = 2

For the first paragraph of code

(1), for loop cannot traverse each element in the array, the control condition in the for loop should be modified to I >= 0

(2), if the x array is empty, it throws a null pointer and does not execute the fault

(3), test:x = [3,5,2]; y = 2;

Expected = 2

The first element in the array is not the element that needs to be found, so no error is generated

(4), test:x = [2,5,2]; y = 3;

Expected =-1

There is no element to be found in the array, so it returns-1, producing an error, but not generating failure.

For the second paragraph of code

(1), unable to find the last 0 position in the array as required, but to find out the position of the first 0 appears.

(2), if the x array is empty, it throws a null pointer and does not execute the fault

(3), test:x = [0,1,2];

Expected = 0

There is only one 0 in the array, no matter how it is found, it returns the 0 index, executes the fault, but does not produce an error

(4), test:x = [1,2,0,3];

Expected = 2

    

Software Testing (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.