Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright  1997 Oxford University Press All Rights Reserved

Similar presentations


Presentation on theme: "Copyright  1997 Oxford University Press All Rights Reserved"— Presentation transcript:

1 Copyright  1997 Oxford University Press All Rights Reserved
Transparency Masters for Chapter 3 of Data Structures via C++ Objects by Evolution A. Michael Berman Copyright  1997 Oxford University Press All Rights Reserved

2 Copyright  1997 Oxford University Press All Rights Reserved
Chapter 3 Software Reliability Overview A discussion of the risks of faulty software and techniques for making software more reliable. 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

3 Copyright  1997 Oxford University Press All Rights Reserved
Chapter Objectives 1. To understand the professional responsibility of the computer scientist and software engineer to avoid risks. 2. To be able to use testing to identify and correct faulty software 3. To use program correctness techniques to reduce software errors. 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

4 Copyright  1997 Oxford University Press All Rights Reserved
Exercises 3-1 In the United States, engineers in traditional fields such as civil or mechanical engineering are usually licensed by state boards, which set minimum standards for education and competence. Currently, no state has a similar system for Software Engineers. What would be the advantages, and disadvantages, of adopting licensing for Software Engineers? 3-2 Access the Usenet Newsgroup “comp.risks” and start reading the messages found there. You’ll find it’s a gold mine of interesting and important information about problems caused by the use and misuse of computers and related technologies. 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

5 Copyright  1997 Oxford University Press All Rights Reserved
Exercises 3-3 Suppose all the computers in the world suddenly stopped working tomorrow. What would the effect be on you and your family? 3-4 Give an example of a problem you’ve encountered due to faulty software and how it affected you. 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

6 Copyright  1997 Oxford University Press All Rights Reserved
A Taxonomy of Errors System Errors Validity Errors Verification Errors Run-Time Errors Maintenance Errors 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

7 Copyright  1997 Oxford University Press All Rights Reserved
Exercises 3-5 Give two or three examples of syntax errors you frequently encounter in C++. 3-6 Write a simple function in C++ that will result in a run-time termination. Run the program and observe the results. How could the function be modified to avoid termination, or to terminate “gracefully”? 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

8 Copyright  1997 Oxford University Press All Rights Reserved
Exercise 3-7 The following function contains a validity error of a sort that’s quite common. int sum(int a[], int n) { // precondition: a is an array subscripted // from 0 to n-1 int i, total(0); for (i = 0; i <= n; i++) total += a[i]; return total; } Find the bug and fix it. 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

9 Copyright  1997 Oxford University Press All Rights Reserved
Approaches to Testing Black-box vs. Glass-box Unit vs. System 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

10 Guidelines for Creating Test Plans
1. Test typical cases 2. Test extreme cases 3. Test invalid inputs 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

11 Table 3-1: Test plan for Max program
5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

12 Copyright  1997 Oxford University Press All Rights Reserved
Exercises 3-8 Write a test plan for a function void sort(int a[], int n) that sorts the array a. 3-9 Write a test plan for a function float mpg(float milesDriven, float gallons) that computes miles per gallon for a specified number of miles driven and gallons purchased. 3-10 Write the mpg function from Exercise 3-9, a main program to test it, and use your test plan to test the function. 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

13 Code Example 3-1: Test driver for max function (Part 1 of 2)
// cx3-1.cpp // Code Example 3-1: Test Driver for Max Function #include <iostream.h> int max(int a[], int n); // defined in cx3-3.cpp; link with the driver int main() { int a[100], i; cout << "Max driver\n"; cout << "Enter each input to max terminated by -9999\n"; cout << "Length of input must be <= 100\n"; 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

14 Code Example 3-1: Test driver for max function (Part 2 of 2)
for (i = 0; i < 100; i++) { int val; cin >> val; if (val == -9999) // termination sentinel break; else a[i] = val; } cout << "\nMax is " << max(a, i) << '\n'; cout << "\n\n"; return 0; 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

15 Code Example 3-2: Sample stub for function median
// cx3-2.cpp // Code Example 3-2: Stub for Median Function #include <iostream.h> int median(int a[], int n) { // This is a stub for function median cout << "function median called with n = " << n << ", a[] = "; int i; for (i = 0; i < n; i++) cout << a[i] << '\t'; cout << "\nType in value you want median to return:"; int return_value; cin >> return_value; return return_value; } 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

16 Copyright  1997 Oxford University Press All Rights Reserved
Exercises 3-11 Write a stub for a function void sort(int a[], int n) that sorts the array a. 3-12 Write a driver for a function void sort(int a[], int n) that sorts the array a. 3-13 The function double min2(double a, double b) takes two floating point numbers a and b, and returns the smaller of the two. Write a stub for min2 that prints out a and b and prompts the user to enter the smaller number. 3-14 Write the function min2 described in Exercise Write a test plan for min2. Write a driver for min2 and use it with your test plan to test min2. 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

17 Code Example 3-3: Function max
// cx3-3.cpp // Code Example 3-3: max function int max (int a[], int n) { // assertion 1: a is an array with subscripts ranging from 0 to n-1 int max_val(a[0]), i; for (i = 1; i < n; i++) // assertion 2: (max_val >= a[k] for 0 <= k < i) and // (max_val = a[j] for some j, 0 <= j < i) if (max_val < a[i]) max_val = a[i]; // assertion 3: (max_val >= a[k] for 0 <= k < n) and // (max_val == a[j] for some j, 0 <= j < n) // i.e., max_val is equal the value of largest int in array a return max_val; } 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

18 Copyright  1997 Oxford University Press All Rights Reserved
Exercises 3-15 Write appropriate preconditions and postconditions for a min function; that is, a function that takes an array and its length as arguments and returns the value of the smallest item in the array. 3-16 Write appropriate preconditions and postconditions for a maxpos function. Maxpos is similar to max, but instead of returning the value of the largest item it returns its index in the array. 3-17 Write appropriate preconditions and postconditions for a function addone(int a[], int n), that takes as input an array a of ints of length n and adds one to each element in the array. Use the notation apre and apost to refer to the state of a before and after calling addone. 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

19 Figure 3-1: Illustrated loop invariant for function max
max_val >= everything in here this part is unknown 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

20 Steps for Analyzing a Loop
1. Establish the precondition 2. Find the invariant 3. Establish the precondition 4. Prove termination 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

21 Code Example 3-4: sum function
// cx3-4.cpp // Code Example 3-4: sum function int sum(int a[], int n) { // Precondition: a is an array with subscripts ranging from 0 to n-1 int i; int total(0); for (i = 0; i < n; i++) // Loop invariant: total = a[0] + a[1] a[i] total += a[i]; // Postcondition: total = a[0] + a[1] a[n-1] return total; } 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

22 Copyright  1997 Oxford University Press All Rights Reserved
Exercises 3-18 Write a function int min(int a[], int n) that finds the smallest item in array a. Use an appropriate loop invariant, and prove termination. 3-19 Write a function int maxpos(int a[], int n) that returns the position of the largest item in the array. Use an appropriate loop invariant, and prove termination. 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

23 Figure 3-2: Basic idea of Insertion Sort
23 7 11 17 26 33 7 11 17 23 26 33 Unprocessed Items 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

24 Code Example 3-5: Insertion Sort
// cx3-5.cpp // Code Example 3-5: Insertion Sort void insertNextItem(int a[], int i); // in cx3-6.cpp void insertionSort(int a[], int n) { // Precondition: a is an array with subscripts ranging from 0 to n-1 int i; for (i = 1; i < n; i++) // Loop invariant: items in range from 0 to i-1 are sorted; // items from i to n-1 have not yet been examined. insertNextItem(a, i); // see cx3-6.cpp // Postcondition: array a is sorted } 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

25 Code Example 3-6: InsertNextItem
// cx3-6.cpp // Code Example 3-6: insertNextItem (used by Insertion Sort) void insertNextItem(int a[], int i) { // Precondition: array a is sorted from 0 to i-1 int newItem(a[i]), insertPos(i); for ( ; insertPos && newItem < a[insertPos-1]; insertPos--) // Loop Invariant: newItem <= a[insertPos+1] .. a[i] && // a[insertPos+1] .. a[i] are sorted a[insertPos] = a[insertPos-1]; a[insertPos] = newItem; // Postcondition: array a is sorted from 0 to i } 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

26 Copyright  1997 Oxford University Press All Rights Reserved
Exercises 3-20 Prove the termination of the loop in Code Example on Page 54 (Slide 23). 3-21 Suppose the insertNextItem function (Code Example 3-6 on Page 54, Slide 24) is called with a negative value of i  does the loop terminate? Explain. Can we be certain that i is always positive? 3-22 Trace the operation of insertionSort for a = {17, 3, 9, 6, 14, 25, 2}. As you work, check with the actual numbers to make sure that the loop invariants really are invariant. 3-23 Use your driver from Exercise 3-12 to test insertionSort. 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

27 Chapter Summary (part 1 of 2)
Computer scientists and software engineers have a moral and ethical responsibility to apply techniques that lead to reliable software. Software errors can be categorized by the source from which they arise. Systematic testing procedures, including the use of test plans, can help find software errors and improve reliability. You can use drivers and stubs to help with unit testing. 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

28 Chapter Summary (Part 2 of 2)
Applying program correctness techniques to software development can reduce errors and improve reliability. Preconditions and postconditions provide a precise specification of the operation of a function. Loop invariants and proving loop termination can help us avoid errors when writing loops. The insertion sort algorithm can be derived using loop invariants. 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

29 Programming Laboratory Problems
3-1 Write a test driver that can be used to test sorting routines. The driver should read a sequence of integers from standard input, call a sorting routine, and then check for the following: 1. The array of data contains the same items before and after the sort. (Of course, the order will probably be different.) 2. The items are in sorted order. Step two is quite easy to check; step one is trickier  you will want to think carefully about how you will do this. Use your test driver to test the insertion sort algorithm from this chapter. Be sure to use the guidelines in the chapter to develop a test plan that you then use to guide your testing. Your deliverables will include the test driver, the test plan, and a log of the results of all tests. 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

30 Programming Laboratory Problems
3-2 Write a function median with the following specificiation: int median(int a[], int n) Preconditions: a is an array indexed from 0 to n-1. Postconditions: none. Returns: the median of a; that is, an element of a such that: the number of elements in a less than (or equal to) the median is equal to the number of elements in a greater than (or equal to) the median. (When n is even, return the average of the two elements closest to the median.) (continued on next slide) 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

31 Programming Laboratory Problems
For example, the median of 1, 3, 5, 6, 8 is 5, and the median of 1, 3, 5, 7, 9, 20 is 6. (Note, however, the inputs will not necessary be in sorted order.) The easiest way to compute the median is to sort the list first (use the Insertion Sort code, Code Examples 3-5 and on page 54) and then pick the middle element or elements from the array. 1. Write a test plan for median. 2. Write a driver for median. 3. Compile and run your driver using the stub in Code Example on Page 45. 4. Write the median function, and test it using your test plan. Deliverables: test plan, driver, median function, results of executing your test plan. 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

32 Programming Laboratory Problems
3-3 For the function countChars with the following specification: int countChars(const string & s, char c) Preconditions: None Postcondition: None Returns: The number of appearances of the char c in the string s. 1. Write a test plan for countChars 2. Write a stub for the countChars function 3. Write a test driver for the countChars function. Compile and run the driver with the stub. 4. Implement countChars, using a meaningful loop invariant. (Note: some string libraries provide a function that can do the whole thing; for the purposes of this exercise, implement countChars yourself with a loop.) 5. Test countChars using your test plan. Deliverables: test plan, stub, driver, countChars function (including a loop invariant), results of executing test plan. 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

33 Programming Laboratory Problems
3-4 For the function remDups with the following specification: void remDups(int a[], int & n) Preconditions: a is an array of integers, indexed from 0 to n-1. Postconditions: a is an array of integers containing the same values as the original array, except that any adjacent duplicated elements in apre have been replaced by a single element in apost, and n has been set to the length of apost. For example, if apre = {1, 3, 4, 4, 3, 3, 2} and npre = 7, then apost = {1, 3, 4, 3, 2} and npost = 5. 1. Write a test plan for remDups 2. Write a stub for the remDups function 3. Write a test driver for the remDups function. Compile and run the driver with the stub. 4. Implement remDups, using a meaningful loop invariant. 5. Test remDups using your test plan. Deliverables: test plan, stub, driver, remDups function (including a loop invariant), results of executing test plan. 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

34 Programming Laboratory Problems
3-5 Write a function longestIncreasingSequence with the following specification: int longestIncreasingSequence(double a[], int n) Precondition: a is an array of integers, indexed from 0 to n-1 Postcondition: None Returns: The length of the longest continuous increasing sequence in the array a. For example, if a = {1.1, 2.2, 3.3, 1.0, 2.0, 7.5, 7.6, 7.6, 6.5, 6.1}, then the value returned is 4, since the sequence 1.0, 2.0, 7.5, 7.6 is the longest continuous increasing sequence. Note that 0 is a valid answer when there is no increasing sequence in the array. Write a careful test plan, use a loop invariant to write the function, and use a test driver to carry out your test plan. Deliverables: test plan, function with loop invariant, test driver, results of test plan execution. 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

35 Programming Laboratory Problems
3-6 In a Caesar cipher, each letter in a message is encoded by the letter that appears k positions later in the alphabet, for some parameter k. For letters near the end of the alphabet, you wrap around to the beginning. To simplify things, we’ll use only capital letters and spaces (with spaces remaining unencoded). For example suppose we encode the following: Something tells me it’s all happening at the zoo. First, we convert everything to upper case and throw out the punctuation, giving: SOMETHING TELLS ME ITS ALL HAPPENING AT THE ZOO Then for k = 3, S gets encoded as V, since V is 3 letters past S, and O will becomes R. Since Z is at the end of the alphabet, we wrap back around to the beginning, using C to encode the V. The complete message is: VRPHWKLQJ WHOOV PH LWV DOO KDSSHQLQJ DW WKH CRR (continued on next slide) 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

36 Programming Laboratory Problems
Write a function encodeCaesar with the following specification: encodeCaesar(string & s, int k) Precondition: s is a string containing upper case letters and spaces. Postcondition: spost contains the string representing the results of encoding spre as a Caesar cipher with parameter k. (Note: to decode, call the encodeCaesar function with a negative value of k.) Write a test plan, the encodeCaesar function (using a loop invariant), and a test driver. Test your function thoroughly with positive and negative values of k. Deliverables: the test plan, encodeCaesar function, test driver, and results of executing the test plan. (Note: in order to assure the reliability of your function, create the test plan by hand  using your encodeCaesar function to create the test plan may mask errors in your program.) 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved

37 Programming Laboratory Problems
Lab 3-7 Write a function merge with the following specification: void merge(int a[], int n1, int b[], int n2, int c[]) Preconditions: a is a sorted array of integers, indexed from 0 to n1-1; b is a sorted array of integers, indexed from 0 to n2-1. Postcondition: c contains all the items from a, plus all the items from b, in sorted order, indexed from 0 to n1+n2-1; that is, the array c contains the merge of the items in a and b. Your merge function should contain two loops. The first iterates as long as there are items in both a and b, picking the next item to put into c. Eventually you run out of items in one array or the other, at which time a second loop will take all the items from the array that still has data left and moves it to c. (It may be convenient to write the code with three loops, although on any single pass you will only execute two of them.) For each loop in your program, write a loop invariant. Be sure to review the section of the book on loop invariants so that you write a meaningful invariant that “captures” the essence of the loop. You will need to write a test driver for the merge program, and to test the program thoroughly. Deliverables: the merge function (with loop invariants); a test driver; test plan; log of results of all tests. 5/9/2019 Copyright  1997 Oxford University Press All Rights Reserved


Download ppt "Copyright  1997 Oxford University Press All Rights Reserved"

Similar presentations


Ads by Google