Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1704 Introduction to Data Structures and Software Engineering.

Similar presentations

Presentation on theme: "CS 1704 Introduction to Data Structures and Software Engineering."— Presentation transcript:

1 CS 1704 Introduction to Data Structures and Software Engineering

2 Module 1 Outline What is Software Engineering? What is Requirements Engineering What is Design? –Structure Charts What is Testing? Intro to Pointers Intro to ADT’s Intro to Classes

3 What does Correctness Mean? “Program correctness" is not easily defined. –The programmer and user of a program may interpret "correctness" quite differently and hence have different expectations of program performance. Various interpretations of correctness are listed below in order of increasing difficulty of achievement:

4 Correctness Continued 1.The program contains no syntax errors that can be detected during translation by the language processor. 2.The program contains no errors, either of syntax or invalid operation, that can be automatically detected during translation or execution of the program.

5 Correctness Continued 3.There exists some set of test data for which the program will yield the correct answer. 4.For a typical (reasonable or random) set of test data the program will yield the correct answer.

6 Correctness Continued 5.For deliberately difficult sets of test data the program will yield the correct answers. 6.For all possible sets of data that are valid with respect to the problem specification, the program yields the correct answers.

7 Correctness Continued 7.For all possible sets of valid test data, and for all likely conditions of erroneous input, the program gives a correct (or at least reasonable) answer. 8.For all possible input, the program gives correct or reasonable answers.

8 Correctness Continued Some programmers never mature beyond level 3 in their attitude toward correctness. From the user's point of view a reasonable definition of correctness is certainly not less than level 6. Level 7 is better and level 8 is what he would really like. The programmer may maintain that a literal interpretation of problem specifications cannot demand more than level 6, while the user will maintain that certain implied requirements do not have to be explicitly stated.

9 Obtaining Correctness Deskchecking –Code traces Walkthrough –Expert Review Board: Mental execution of text cases before the review board –Code Inspections Programmer explains logic of code (statement by statement) formally to a peer group Demonstrated Some Correctness May be Incorrect

10 Correctness is Unobtainable Testing –Case data –Computer aided Proof –Formal (mathematical) Proven Correct Demonstrated Some Correctness

11 Discovered vs Undiscovered Errors Probability Increases as Number of Errors Increases Probability of Existence of More Errors Number of Errors Found to Date

12 Testing 40-50% of all development time is spent in the testing process Humans (programmers) are NOT good at testing. The process of testing admits that one has produced code with errors. Successful testing can be thought of as successfully finding errors and testing failure implies not discovering any errors "Testing can establish the presence of errors, but never their absence." -Edsger Dijkstra

13 Testing Phases Requirements Specification High Level Design Low Level Design Coding Integration Testing Deployment Maintanence Acceptance Test Function Test System Test Integration Test Unit Test Regression Test

14 Testing Phases Defined Regression Testing involves fixing errors during testing and the re-execution of all previous passed tests. Unit Testing utilizes module testing techniques (white-box / black-box techniques). Integration Testing involves checking subsets of the system. Acceptance, Function and System testing is performed upon the entire system.

15 Integration Testing Bottom-Up Testing –Unit Test (Black & White box techniques) –discovers errors in individual modules –requires coding (& testing) of driver routines Top-Down Testing –Main module & immediate subordinate routines are tested first –requires coding of routine stubs to simulate lower level routines –system developed as a skeleton

16 Integration Testing cont’d Sandwich Integration –combination of top-down & bottom-up testing Big Bang –No integration testing –modules developed alone –All modules are connected together at once

17 Function Testing Proof of Correctness vs. Testing Functional Verification Testing Establishes Level of Confidence Proof of Correctness

18 Function Testing cont’d Checks that the system satisfies its external specification Entire system is viewed as a "Black Box" Techniques: –Boundary-value Analysis –Cause-Effect Graphing

19 Function Testing cont’d Doesn’t verify that the requirements or user documentation are correct External Specifications Program Requirements User Documentation Program

20 Acceptance Testing Tests the program against the current needs of the users and its original objectives. Usually performed by the end user (customer) Contract may require, as part of acceptance test: –performance tests (throughput, statistics collection,...) –stress tests (system limits) If performed by system developers may consist of  (alpha),  (beta) testing

21 Acceptance Testing cont’d Is the user satisfied with final product? Program Requirements User Documentation Program External Specifications

22 Testing Example Program reads 3 integer values from a line. The 3 values represent the lengths of the sides of a triangle. The program outputs whether the triangle is equilateral, isosceles, or scalene. Write a set of test cases which would adequately test this program!

23 Test cases Valid scalene triangle. Valid equilateral triangle. Valid Isosceles triangle. All possible permutations of Isosceles triangles (e.g. (3,3,4) (3,4,3) (4,3,3)) One side having a zero value. One side having a negative value. Degenerate Triangle (e.g. 1- Dim  (1,2,3) All possible permutations of Degenerate Triangles (e.g. (1,2,3) (3,1,2) (1,3,2)) Invalid Triangle (e.g. (1,2,4)) All possible permutations of invalid triangles. All sides = 0. Non-integer values. Incorrect number of sides … 1 2 3

24 Exhaustive Testing How long will it take to try all possible inputs at a rate of one test/second? 2 32 tests * 1 second / test = 2 32 seconds = 2 32 / (60 * 60 * 24 * 365) years > 2 32 / (2 6 * 2 6 * 2 5 * 2 9 ) years = 2 32 / 2 26 years = 2 6 years = 64 years Impractical!!!

25 Goal of Testing No method (Black/White Box, etc.) can be used to detect all errors. Errors may exist due to a testing error instead of a program error. A finite number of test cases must be chosen to maximize the probability of locating errors. Perform testing to ensure that the probability of program/system failure due to undiscovered errors is acceptably small.

26 Testing Principles The expected output for each test case should be defined in advance of the actual testing. The test output should be thoroughly inspected. Test cases must be written for invalid & unexpected, as well as valid and expected input conditions. Test cases should be saved and documented for use during the maintenance / modification phase of the life cycle. New test cases must be added as new errors are discovered. The test cases must be a demanding exercise of the component under test. Tests should be carried out by a third party independent tester, developer engineers should not privatize testing due to conflict of interest Testing must be planned as the system is being developed, NOT after coding.

27 Drivers vs. Stubs Test Case Inputs Valid Test Outputs Driver Routine X Stub a b d c Component Under Testing Required by X but NOT coded

28 White Box Testing Structural Testing –Exercise of Source code and internal data structures –Test cases are derived from analysis of internal module logic and external module specifications Functional Description and actual implementation Correct I/O relationships are verified using:

29 White Box: Logic Testing Statement Coverage –Every statement is executed at least once. Decision Coverage –Each decision is tested for TRUE & FALSE. –Correctness of conditions within the decisions are NOT tested Condition Coverage –Each condition in a decision takes on all possible outcomes at least once. –Does not necessarily test all decision outcomes. –Test cases do not take into account how the conditions affect the decisions.

30 White Box: Logic Testing cont’d Decision/Condition Coverage –Satisfies both decision coverage and condition coverage. –Does NOT necessarily test all possible combinations of conditions in a decision. Multiple Condition Coverage –Test all possible combinations of conditions in a decision –Does not test all possible combinations of decision branches. Eg: if (x >= 5 && y != 7 && z= =9) { ;//2^3=8 tests!}

31 White Box: Path Testing Exercise a program by testing all possible execution paths through the code. Method –Enumerate the paths to be tested –Find the Input Domain of each –Select 1 or more test cases from domains Problem: Loops (  number of paths) Paths: ABC; ABBC; AB... BC Solution: –Restrict loop to N iterations Small number of paths that yield reasonable testing A C B D

32 Control Flow Graph Node: sequence of statements ending in a branch Arc: transfer of control

33 Exhaustive Path Testing (analogue of exhaustive input testing) requires executing the total number of ways of going from the top of the graph to the bottom approx. 100 trillion, where 5 = number of unique paths assuming all decisions are independent of each other specification errors could still exist does not detect missing paths does not check data-dependent errors

34 Test Path Determination Independent Path –any path that introduces at least one new set of processing statements (nodes), i.e. it must traverse an edge not previously covered. Independent Paths: –1 - 2 - 6 –1 - 2 - 3 - 5 - 2 - 6 –1 - 2 - 3 - 4 - 5 - 2 – 6 2 6 3 1 4 5

35 Cyclomatic Complexity Upper bound on the number of independent paths, i.e. number of tests that must be executed in order to cover all statements. CC –edges - Nodes + 2 = E - N + 2 = 7 - 6 + 2 = 3 2 6 3 1 4 5

36 When to Stop Testing When no more errors exist.(Impossible to ascertain.) –(1) How reliable is the set of test cases? Data Domain –(2) How reliable is the software being developed? Time Domain

37 When to Stop Testing When no more errors exist.(Impossible to ascertain.) –(1) How reliable is the set of test cases? Data Domain –(2) How reliable is the software being developed? Time Domain RELIABILITY Data Domain Time Domain Coverage Mutation Analysis Error Seeding Shooman Jelinski-Moranda Musa

38 Reliability: Time Domain Time Domain Reliability –MTBF : mean time between failures –MTTF : mean time to failure –MTTR: mean time to repair MTBF = MTTF + MTTR Availability = MTTF / (MTTF + MTTR) * 100 Estimate Methods: –Predictions based on calendar time –Predictions based on CPU time

39 Data Domain: Mutation Analysis Tests the test data Mutate Code to determine the adequacy of the test data. Determines whether all deliberately introduced (mutant) errors are detected by the original test cases.

40 Mutation Analysis cont’d Mutation Testing Process –Program P is executed for test case T –If errors occur test case T has succeeded Errors are corrected & retested until no errors with test case T are observed. Program is Mutated P’ –Mutant P’ is executed for test case T –IF no errors are found { test case T is inadequate; further testing is required; new test cases are added & step 3 is repeated until all mutations are discovered; entire process is started again at step 1 with the new test cases –} ELSE { all mutations located by tests T T is adequate and no further testing is required}

41 Error Seeding Estimate of the number of original undiscovered errors remaining in a system. –Intentionally introduce (seed) errors into the source code. –Execute test cases upon source code. –Count the number of seeded errors & original errors (unseeded errors) discovered. –Estimate the total number of original errors

42 Error Scattergram Graph (mutations)

43 Debug Error Checking The assert macro pseudo-function defined in, and, (new style header), is used to check a condition, (pre- condition, post-condition, etc.). If the condition is false, assert prints an error message containing the line number, the condition tested, and the file name containing the assert, calls the abort function in and to halt program execution. If the condition is true execution continues normally. assert ( (index>=0) && (index < MAXDIM) );

44 Debug Error Checking Release builds and assertions –assert functions need not be removed after testing is complete. –Defining the preprocessor symbolic constant NDEBUG will force the preprocessor to ignore all of the assertions. NDEBUG must be defined at the beginning of the program files. #define NDEBUG

45 Debug Error Checking Considerations –Assertions do not allow for programs to recover from errors. –It is good programming practice to precede all array accesses with assertions for bounds checking.

46 Command Line Parameters main(int argc, char* argv[]) –When a program name is typed at the Operating System Line prompt, it is treated as a command. The name of the executable program file and any other strings typed on the line, (usually file names), are considered command line parameters. –C/C++ compilers provide access to command line parameters.

47 Command Line Parameters Cont argc –gives the number of command line parameters (arguments) that the user typed on the command line. –always equals at least 1 for the command itself argv[] –an array of pointers to C-style strings that holds the command line arguments. –argv[0] holds the name of the program (command) –may hold full pathname on some systems –may be capitalized on DOS systems

48 Command Line Parameters Cont. #include #include // for EXIT_SUCCESS using namespace std; int main (int argc, char* argv[]) { cout << "argc = " << argc << endl; for (int i =0 ; i < argc; i++) { cout << "argv[ " << i << " ] = " << argv[i] << endl; } return EXIT_SUCCESS; }


50 Static Variables Size is fixed throughout execution Size is known at compile time Space/memory is allocated at execution

51 Dynamic Variables Created during execution –"dynamic allocation" No space allocated at compilation time Size may vary –Structures are created and destroyed during execution. Knowledge of structure size not needed Memory is not wasted by non-used allocated space. Storage is required for addresses.

52 Example of Pointers Assume: –Houses represent data –Addresses represent the locations of the houses. Notice: –To get to a house you must have an address. –No houses can exist without addresses. –An address can exist without a house (vacant lot / NULL pointer)

53 Memory and Addresses On modern computers, memory is organized in a manner similar to a one- dimensional array: –memory is a sequence of bytes (8 bits) –each byte is assigned a numerical address, similar to array indexing –addresses are nonnegative integers; valid range is determined by physical system and OS memory management scheme

54 Memory and Addresses –OS (should) keep track of which addresses each process (executing program) is allowed to access, and attempts to access addresses that are not allocated to a process should result in intervention by the OS –OS usually reserves a block of memory starting at address 0 for its own use –addresses are usually expressed in hexadecimal (base 16), typically indicated by use of a prefix: 0xF4240

55 Memory Organization run-time stack used for statically allocated storage heap used for dynamically allocated storage

56 Pointer Type Simple type of variables for storing the memory addresses of other memory locations

57 Pointer Declaration Pointer Variables Declarations –The asterisk ‘ * ’ character is used for pointer variable declarations: –iptr is a pointer to an integer –fptr is a pointer to a real int*iptr; float* fptr, fptr2; recommended form int* iptr1, iptr2; common declaration not a pointer

58 Pointer Declaration Given the equivalent declaration: †Declares iptr1 to be a pointer variable, but iptr2 is a simple integer variable. Typedef declaration: †Declare all pointer variables in separate declaration statements. int*iptr1; int iptr2; typedef int* intPtr; intPtr iptr1;

59 Pointers in Memory 5 y yPtr 600000 yPtr 500000 5 y 600000

60 Pointer References “Address Of” Operator: & (ampersand) –Unary operator that returns the hardware memory location address of it’s operand. Given: –Address Assignment: int*iptr1; int*iptr2; int numa, numb; numa = 1; numb = 2; iptr1 = &numa; iptr2 = &numb;

61 “Contents of” Operator Dereference / Indirection Operator: * (asterisk) –unary ‘pointer’ operator that returns the memory contents at the address contained in the pointer variable. –Pointer Output: –(Possible) results: cout << iptr1 << *iptr1 << endl; cout << iptr2 << *iptr2 << endl; 0xF42401 0x3B9ACA002

62 NULL Pointer Pointer constant, address 0 Named constant in the include header ( old style header). Represents the empty pointer –points nowhere, unique pointer/address value Illegal: NEVER dereference a pointer that equals NULL * NULL



65 Intro to ADT’s Data Type –a collection of related data elements plus operations that can be performed upon values of the data type. Types –Built-In (Language Defined) Array, Structures (Records), Classes –Programmer Defined, Abstract Data Types (ADT) Lists, Stacks, Queues

66 ADT Views Application –usage in a particular program (variant box view) Abstract (Logical) –organization viewed by the user (black box view) Implementation (Physical) –coding methods used to represent the data and the operations (open box view)

67 Example Two-Dimensional Array –Application View: maze, surface points –Logical View: table, matrix –Physical View Stored sequentially –(implies logical to physical mapping) Index Limits (L1.. U1, L2.. U2) Length = (U1 - L1 + 1)*(U2 - L2 + 1)

68 Accessing Column Major: all elements in a column are stored in sequence FORTRAN and Java - Column Major Row Major: all elements in a row are stored in sequence “C”, PASCAL - Row Major

69 Row Major Accessing: Location of [i] [j] element (Row Major)  + [ (U2-L2+1) * (i-L1) + (j-L2) ] * size of element  = base address of array (U2 - L2 + 1) = Size of Column (i - L1) = number of columns to skip (j - L2) = number of rows to skip 1,11,21,31,4 2,12,22,32,4 3,13,23,33,4 Logical user view mappings

70 Row vs. Column Major 1,11,21,31,42,12,22,32,4 3,13,23,33,4 Physical (row-major) compiler programmer linear view 1,12,13,11,22,2 3,21,32,3 3,31,42,43,4 Physical (column-major) compiler programmer linear view 

71 ADT New data type defined by programmer Includes: † Set of Data Objects † Set of Abstract Operations create empty delete insert object

72 Data Abstraction Design of abstract data objects and operations upon those objects. Abstraction in programming separates a data type’s logical properties from its implementation.

73 Information Hiding Used in the design of functions and new data types. Each component should hide as much information as possible from the users of the component (function). Implementation details are hidden from the user by providing access through a well-defined communication interface.

74 Encapsulation the bundling of data and actions in such a way that the logical properties of the data and actions are separated from the implementation details [Dale]. access to an ADT is restricted to a specified set of supplied operations Implies: †User has no "Need to Know" †User may not directly manipulate data elements Advantages †Changes to the underlying operators or representations does not affect code in a client of the data type †Extends programming languages

75 Info Hiding vs Encapsulation Information Hiding –a question of program design –In many cases: Language Supported (functions, procedures) Encapsulation –a question of language design –" abstraction is effectively encapsulated only when the language prohibits access to information hidden within the abstraction." –Ada packages –C++ classes † “C” modules


77 Rationale for Classes The aim of the C++ class construct is to provide the programmer with a tool for creating new types that can be used as conveniently as the built-in types. A type is a concrete representation of a concept. For example, the C++ built-in type double with its operations +, -, *, etc., provides a concrete approximation of the mathematical concept of a real number. A class is a user- defined type.

78 Rationale for Classes We design a new type to provide a definition of a concept that has no counterpart among the built-in types. A program that provides types that closely match the concepts of the application tends to be easier to understand and to modify than a program that does not. A well-chosen set of user-defined types makes a program more concise. In addition, it makes many sorts of code analysis feasible. In particular, it enables the compiler to detect illegal uses of objects that would otherwise remain undetected until the program is thoroughly tested.

79 The C++ Class Type The C++ class type provides a means to encapsulate heterogeneous data elements and the operations that can be performed on them in a single entity. A class type is like a struct type in that it may contain data elements, called data members, of any simple or structured type. A class type may also contain functions, called function members or methods, that may be invoked to perform operations on the data members.

80 The C++ Class Type The class type also provides mechanisms for controlling access to members, both data and function, via the use of the keywords public, private and protected. (Default access mode is private.) A variable of a class type is referred to as an object, or as an instance of the class. The struct language construct was extended to make it equivalent with a class, except its members are by default public. While structs may be used the same as classes they are rarely employed as such.

81 A Simple Date Class class DateType { public: void Initialize(int newMonth, int newDay, int newYear); int YearIs( ) const; // returns year int MonthIs( ) const; // returns month int DayIs( ) const; // returns day private: int Year; int Month; int Day; }; Indicates Fn is a const member and cannot change any data member values.

82 A Simple Date Class The DateType class incorporates three data members, Year, Month, and Day, and four function members. Note the class type declaration defines a data type — it does not declare a variable of that type. Also note that the class type declaration above includes only prototypes of the function members, not their definitions.

83 Simple Date Class Continued Given the class type declaration, a user may declare variables of that type in the usual way: DateType Today, Tomorrow, AnotherDay; No default initializations are performed. It is up to the user to assign values to the data members of these variables.

84 Simple Date Class Continued The data members of the DateType class are declared as being private. The effect is that the data members cannot be accessed in the way fields of a struct variable are accessed: Today.Month = 9; will generate a compile-time error. A user of a DateType variable may access only those members which were declared public. So, the user could initialize Today by using the public member function Initialize( ): Today.Initialize(9, 28, 1998);

85 Simple Date Class Continued Similarly, a user can only obtain the value of the Year member by using the public member function YearIs( ): int ThisYear = Today.YearIs( ); Note the use of the field selection operator ‘.’.

86 Module 1 Outline Completed What is Software Engineering? What is Requirements Engineering What is Design? –Structure Charts What is Testing? Intro to Pointers Intro to ADT’s Intro to Classes

Download ppt "CS 1704 Introduction to Data Structures and Software Engineering."

Similar presentations

Ads by Google