 11.1 Designing Issues 11.2 Debugging 11.3 Testing

advertisement

Lecture 11. Design, Testing and Documentation

11.1 Designing Issues

11.2 Debugging

11.3 Testing

11.4 Checking

11.5 Documentation

4/16/2020 CS3369 Realtime Control Computer

Software/DENG Xiaotie

Page 1

Designing

Top-down Design in OOP

Data abstraction

• classes and objects

• member functions

• hide data as private

Polymorphism

• allow commonly used member functions to have the same name but perform according to class of an object

Page 2

4/16/2020 CS3369 Realtime Control Computer

Software/DENG Xiaotie

OO Programming with C++

4/16/2020

 Top-down Design:

– break tasks down into subtasks and write a function for each

– make it easy for a team to work on large software projects

 Functional abstraction: function as a black box so users only need to look at the function prototype and comments to use the function.

– Function prototype tells programmers pre-conditions and post conditions for arguments(parameters) to function.

Page 3

CS3369 Realtime Control Computer

Software/DENG Xiaotie

Design of the Elevator

4/16/2020

 Functions to use

 showelevator() //show elevator include the door .

 //pre-conditions: there is no input. The inputs are the member variables of the class. door --decides the wide of the gap.

Door=25 if the door is closed. Door =3 if the door is open.

The process of opening a door is the process of changing the value of door. Other required variables, k-- the vertical position of the elevator. Position--the h-position of the elevator.

 ///post-conditions: the elevator is drawn.

Page 4

CS3369 Realtime Control Computer

Software/DENG Xiaotie

Design of the Elevator

4/16/2020

 Functions to use

 erease() // erease the elevator. Draw black lines just for the door and the elevator, not for the skeleton. //pre-conditions are the same as selevator(). //post-conditions: door is ereased.

 Stop() //stop will control the door to stop step by step. //preconditions: k--the vertical position and s--stands for if the door is really stopped. (both k and s are member variables.)

 //post-condition: k and s may be changed. Stop() will be called many times. A door will be closed automatically once s==1. To this this, is simple, door=door-1.

Page 5

CS3369 Realtime Control Computer

Software/DENG Xiaotie

Design of the Elevator

4/16/2020

 Functions to use

 close()// to change the value of door to close the door step by step. //pre-conditions: the value of door in used, a member variable close is used. Close ==0 if the door is not completely closed. Close ==1 if the door is completely closed. //post-conditions: the value of door is changed if the door is not completely closed. The value of door is not changed if the door is completely closed and close is set to be 1.

Page 6

CS3369 Realtime Control Computer

Software/DENG Xiaotie

Design of the Elevator

 The main function

4/16/2020

 test if the elevator need to be stopped

 if yes, call stop()

 test if the elevator need to move

 if yes, (a) call close(), and then change the position oof the elevator, i.e., k=k-1 or k=k-1.

 Selevator();

 delay(300);

 erease();

 repeat

Page 7

CS3369 Realtime Control Computer

Software/DENG Xiaotie

OO Programming with C++

4/16/2020

ADT: abstract data types: programmer using the data type does not have access to details of how the values and operations are implemented.

– interface:how to use ADT (for user)

– implementation: how the interface is is realized as

C++ code.

– tips: hide member variables as private

– access functions to access necessary data.

Page 8

CS3369 Realtime Control Computer

Software/DENG Xiaotie

Programming Practice

4/16/2020

 Art ? Science?

 Programming languages change all the time, focusing on different aspects of programming techniques.

 Even Experts do not agree on details of programming style. E.g.,

 How to pair up parenthesis {}? Do we line them up on the same column? Do we indent every pair which encloses an inner loop.

Page 9

CS3369 Realtime Control Computer

Software/DENG Xiaotie

Programming Practice

4/16/2020

 Criteria:

 reliable

 maintainable

 user friendly

 re-usability

 Good programming techniques are language independent. However, OO Languages provide useful tools to make it easier.

CS3369 Realtime Control Computer

Software/DENG Xiaotie

Page 10

Readability and comprehensibility

4/16/2020

 Naming

 named entities in programs:

 constants, variables, classes, objects, functions, files

 use meaningful names:

 names of objects should be closely related to the corresponding real world entities

 use more than one word, upper and lower case, delimiter characters

 avoid: ambiguities, cryptic abbreviations, unrelated names, single-letter identifies

CS3369 Realtime Control Computer

Software/DENG Xiaotie

Page 11

4/16/2020

Readability and comprehensibility

 Divide Program into logically meaningful groups:

 class/object: for logically related entities

 functions: important/related pieces of program

 files: related functions and classes.

 Write pre-conditions and post-conditions in comments for function prototypes/function bodies.

Page 12

CS3369 Realtime Control Computer

Software/DENG Xiaotie

4/16/2020

Readability and comprehensibility

 Use indentations (consistently and correctly) to indicate program structures

 Use header file to

 define classes and prototype member functions

 store constants and global variables

 Use Makefile to compile programs in different files and organize team project.

Page 13

CS3369 Realtime Control Computer

Software/DENG Xiaotie

Debugging/Testing/Checking

4/16/2020

 Debugging: Try to locate errors:

 Grammar Errors:

 Run Time Errors:

 Testing: Searching for errors.

 developer tests (gently) for delivery of good product.

 expert tester tries to break product for quality guarantee.

 Checking: Make sure the output for an input is OK

 an extra function accompanying a function which checks for correctness of the output for every run of the function.

Page 14

CS3369 Realtime Control Computer

Software/DENG Xiaotie

Debugging

4/16/2020

 Grammar Errors: Usually a good programming language comes with compiler tools which locate

(usually the first occurrence of) errors.

 Run Time Errors.

 Tools in Turbo C:

 trace: statements are executed line by line

 break point: execution stops at the line a break point is set

 Print-out statements: print out values of certain variables to see if execution goes as expected

Page 15

CS3369 Realtime Control Computer

Software/DENG Xiaotie

Debugging: An Example

4/16/2020

 original function:

 int summation(int x, int y) {

 int z;

 z=x-y;

 return z}

 printout statements added to DEBUG:

 int summation(int x, int y) {

 int z; cout<<“x=“ <<x<< “y=“ <<y<<endl;

 z=x-y; cout << “z=“ << z;

 return z}

CS3369 Realtime Control Computer

Software/DENG Xiaotie

Page 16

Testing

4/16/2020

 Testing process:

 top-down testing

 bottom-up testing

 real-time thread testing

 White Box Testing: knowing all codes and apply path testing

 exhaustive testing: make sure all paths are executed at least once

 selective testing: chooses some paths to test

 Black Box Testing: compare the output of the program with the expected output for many inputs.

Page 17

CS3369 Realtime Control Computer

Software/DENG Xiaotie

Testing Process

4/16/2020

 Top-down testing: usually for functionality

 Test in order of top level system, sub-system, module...

 Bottom-up testing

 test functions at the lowest level and work up hierarchy

 Real-time thread testing

 real time systems consist of co-operating processes and maybe interrupt driven

 external events may cause control transfers.

 thread testing identifies and executes each possible thread (of control flow of processes.)

Page 18

CS3369 Realtime Control Computer

Software/DENG Xiaotie

Input Data for Testing

4/16/2020

 Choose input data according to the empirical distribution

 Choose degenerated patterns of data

 For the example of sorting function:

 all data are the same value

 input of size 0/1/2

 Choose simple patterns of data

 For the example of sorting function:

 input already sorted

 input are reversals of a sorted list

CS3369 Realtime Control Computer

Software/DENG Xiaotie

Page 19

Checking

4/16/2020

 Make sure the output for an input is OK

 Write a checker function accompanying a function which checks for correctness of the output

 Invoke the checker function for every run of the original function.

 For the sorting function as an example: write an function to check the output of the sorting function is indeed sorted and check if the output data are the same as the input data

Page 20

CS3369 Realtime Control Computer

Software/DENG Xiaotie

An Example for Checking

4/16/2020

 sort(A,n)

 {old_sort(A,n); //input is an array A of size n and output

//is in the same array A but sorted.

 if (!check(A,n)) report-error();}

 int check(A,n) {

 int I;

 if (n==1) return TRUE;

 for (I=0; I<n-1; I++) {

 if A[I]>A[I+1] return FALSE;}

 return TRUE; } //here we only check A is sorted. In reality //we need also check the output array contains the same //set of elements as the input.

Page 21

CS3369 Realtime Control Computer

Software/DENG Xiaotie

4/16/2020

An Example for Checking

 max(A,n)

 {int tmp;

 tmp=old_max(A,n);

 if (!checkmax(tmp,A,n)) report-error();}

 int check(int a, int A[], int n) {

 int I;

 if (n==1) return TRUE;

 for (I=0; I<n-1; I++) {

 if A[I]>a return FALSE;}

 return TRUE; }

CS3369 Realtime Control Computer

Software/DENG Xiaotie

Page 22

Another Example for Global Variables

The roots of a quadric equation ax 2 +bx+c=0 are

4/16/2020 x1= (-b+( b2-4ac))/2ac x2=x1= (-b- ( b2-4ac))/2ac include<iostream.h> float x1, x2; int flag=0; void roots(float a, float b, float c) void main(void)

{ roots(1.0,2.0, 1.0); if(flag == 0) cout<<“The roots are”<<x1<<x2; else cout<<“No real root”;

} void roots(float a, float b, float c)

{ if (b*b-4*a*c>=0)

{ x1=(-b+sqrt(b*b-4*a*c))/(2*a*c); x2 =(-b-sqrt(b*b-4*a*c))/(2*a*c);

} else flag=1;

} x1, x2 and flag are used as global variables. Otherwise, roots must return two values that know how to do it.

CS3369 Realtime Control Computer

Software/DENG Xiaotie we do not

Page 23

More about Equation

Write a function that returns two roots of an equation ax 2 +bx +c =0.

Write a function that checks if the roots a correct.

4/16/2020 CS3369 Realtime Control Computer

Software/DENG Xiaotie

Page 24

Documentation:

Description of control software system

functional specification

design specification

system manuel:

 for system maintenance people

 detailed description of system structure/component

user manuel

 how system is to be operated

 written at the level of expertise of the operator

system integration tests and test results:

Page 25

4/16/2020 CS3369 Realtime Control Computer

Software/DENG Xiaotie

Flow Chart

Beginning of game: display the board

Player 2’s next move

Player 1’s next move no

Display the new board yes

Does it kill some stones?

resign

End of the Game

Display the new move

Choose a move yes no

Is it legal

Note: only flow chart of player 1 is displayed and that of player 2 is similar.

Page 26

4/16/2020 CS3369 Realtime Control Computer

Software/DENG Xiaotie

Download