Program Realisation 2 Todays Topics Unit testing, manual versus - - PowerPoint PPT Presentation

program realisation 2 today s topics unit testing manual
SMART_READER_LITE
LIVE PREVIEW

Program Realisation 2 Todays Topics Unit testing, manual versus - - PowerPoint PPT Presentation

Program Realisation 2 Todays Topics Unit testing, manual versus automated http://www.win.tue.nl/hemerik/2IP20/ ADT relationships: Lecture 4 Multiple implementations of the same ADT contract Kees Hemerik Generalization,


slide-1
SLIDE 1

Program Realisation 2 http://www.win.tue.nl/˜hemerik/2IP20/ Lecture 4 Kees Hemerik Tom Verhoeff Technische Universiteit Eindhoven Faculteit Wiskunde en Informatica Software Engineering & Technology Feedback to T.Verhoeff@TUE.NL

c 2007, T. Verhoeff @ TUE.NL 1 Program Realization 2: Lecture 4

Today’s Topics

  • Unit testing, manual versus automated
  • ADT relationships:

– Multiple implementations of the same ADT contract – Generalization, specialization, subtyping

  • Inheritance
  • Method binding (“polymorphism”)
  • virtual, override, abstract , as, is, protected

c 2007, T. Verhoeff @ TUE.NL 2 Program Realization 2: Lecture 4

Quality of Submitted Bounded and Unbounded Queues Year 2005–2006 Assignment # Submissions # Incorrect % Incorrect Bounded Queue 52 ≥ 15 > 25% Unbounded Queue 48 ≥ 19 > 40% Year 2006–2007 Assignment # Submissions # Incorrect % Incorrect Bounded Queue 60 ≥ 9 15% Unbounded Queue 48 ≥ 18 > 35%

c 2007, T. Verhoeff @ TUE.NL 3 Program Realization 2: Lecture 4

Workflow for Development of ADT as Product

  • 1. Describe and analyse requirements (informal)
  • 2. Design class interface (public methods, parameters, incl. types)
  • 3. Design contract (method pre/post, public invariants)
  • 4. Design/implement unit tests (test driver)
  • 5. Design internal representation (private fields, private invariants,

abstraction function)

  • 6. Implement method bodies (incl. assertion checking)
  • 7. Execute unit tests

c 2007, T. Verhoeff @ TUE.NL 4 Program Realization 2: Lecture 4

slide-2
SLIDE 2

Unit Test for ADTs

  • Test for correct functionality ; input satisfies precondition

– No exception → check the postcondition – Exception → FAILURE

  • Test for correct checking ; input does not satisfy precondition

– Exception → check the kind of exception – No exception → FAILURE How to design test cases : choosing inputs and checking outputs How to execute tests cases : manually or automatically via test driver

c 2007, T. Verhoeff @ TUE.NL 5 Program Realization 2: Lecture 4

A Confession Various approaches exist that attempt to improve our ability to do modular design of complex software systems. This has led to many new language constructs and tools. The theoretical foundation for today’s topics is not mature. We treat some language mechanisms that can easily be abused. Use with care and discipline! History: goto statement

c 2007, T. Verhoeff @ TUE.NL 6 Program Realization 2: Lecture 4

Liskov Substitutability Principle (LSP) for Types Type S is a subtype of type T, when any value of type S can be used in every place where a value of type T is acceptable: Every S is a T. S can be viewed as a specialization of T. T can be viewed as a generalization of S. type PositiveInteger = 1 .. MaxInt; { the positive integers } Digit = ’0’ .. ’9’; { the digit characters } Every PositiveInteger is an Integer. Every Digit is a Char.

c 2007, T. Verhoeff @ TUE.NL 7 Program Realization 2: Lecture 4

Liskov Substitutability Principle (LSP) for ADTs The ‘ is a ’ relationship can also be defined for ADT contracts. ADT S to be a subtype of ADT T, when every operation p of T is an operation of S with the same† signature (parameters etc.), such that

  • [ precondition of S.p ⇐ precondition of T.p ]
  • [ postcondition of S.p ⇒ postcondition of T.p ]

N.B. S can have other operations besides those of T.

c 2007, T. Verhoeff @ TUE.NL 8 Program Realization 2: Lecture 4

slide-3
SLIDE 3

Subclasses in Object Pascal class as module — class as type type TParent = class(TObject) { descendant of TObject } {...} end; { class TParent } TChild = class(TParent) { descendant of TParent } {...} end; { class TChild } TChild is a subclass , or descendant , derived from TParent. TChild inherits all fields and methods from TParent. TChild can have additional fields and methods.

c 2007, T. Verhoeff @ TUE.NL 9 Program Realization 2: Lecture 4

Inheritance: Substitutability Example With each stored string, TStringList can store an associated TObject: TStringList.AddObject(const S: string; AObject: TObject)

1 type 2

TMyObject = class(TObject)

3

{...}

4

end; { class TMyObject }

5 6 var 7

s : TStringList;

8

v : TMyObject; { v can also be used as a TObject }

9 10 begin 11

s := TStringList.Create

12 ; v := TMyObject.Create 13 ; s.AddObject(’Tom’, v)

{ TMyObject substituted for TObject }

c 2007, T. Verhoeff @ TUE.NL 10 Program Realization 2: Lecture 4

Check Class Type of Object; Safe Cast to Derived Class v is T ≡ object v is an instance of (a descendent of) class T v as T = object v treated as an instance of class T

1 type 2

TMyObject = class(TObject)

3

public

4

procedure M; { method that does not exist in TObject }

5

end; { class TMyObject }

6 7 var 8

v : TObject;

9 10 begin 11

{ somehow create v } (* v.M gives COMPILER ERROR *)

12 ; if v is TMyObject then 13

with v as TMyObject do M

c 2007, T. Verhoeff @ TUE.NL 11 Program Realization 2: Lecture 4

Inheritance of Methods

1 type 2

TParent = class(TObject)

3

procedure M; // pre: P0; post: Q0

4

end; { class TParent }

5 6

TChild = class(TParent)

7

{ ... no procedure M here ... }

8

end; { class TChild }

9 10 var 11

v : TChild; { v also is a TParent }

12 13 begin 14

v := TChild.Create

15 ; v.M

// calls M defined in TParent

c 2007, T. Verhoeff @ TUE.NL 12 Program Realization 2: Lecture 4

slide-4
SLIDE 4

Static and Virtual Method Binding

  • Static (default): invoke method in type of variable’s declaration .

Determined at compile time.

  • Virtual : invoke method in type of variable’s actual value .

Determined at run time.

c 2007, T. Verhoeff @ TUE.NL 13 Program Realization 2: Lecture 4

Overriding Method Definitions

1 type 2

TParent = class(TObject)

3

procedure M; virtual; // pre: P0; post: Q0

4

end; { class TParent }

5 6

TChild = class(TParent)

7

procedure M; override; // pre: P1; post: Q1

8

end; { class TChild } // P0 => P1, Q1 => Q0

9 10 var 11

v : TParent;

12 13 begin 14

v := TChild.Create

15 ; v.M

// calls M defined in TChild

c 2007, T. Verhoeff @ TUE.NL 14 Program Realization 2: Lecture 4

Abstract Methods An abstract method is a placeholder for a contract. It does not have an implementation. It cannot be called. It must be implemented in a derived class.

c 2007, T. Verhoeff @ TUE.NL 15 Program Realization 2: Lecture 4

Abstract Classes

1 type 2

TParent = class(TObject) { abstract class }

3

procedure M; virtual; abstract; // pre: P0; post: Q0

4

end; { class TParent }

5 6

TChild = class(TParent) { concrete descendent }

7

procedure M; override; // pre: P1; post: Q1

8

end; { class TChild } // P0 => P1, Q1 => Q0

9 10 (* procedure TParent.M cannot be implemented *) 11 12 procedure TChild.M;

{ implementation of M in derived class }

13

begin {...} end;

c 2007, T. Verhoeff @ TUE.NL 16 Program Realization 2: Lecture 4

slide-5
SLIDE 5

ADTs with Multiple Implementations

  • The contract is defined in a separate class without private fields

and with virtual, abstract methods . Derived queries can already be implemented as virtual methods in terms of basic queries. This is called an abstract base class .

  • Each implementation inherits from the abstract base class, and

provides a data representation in protected or private fields and implementations for all abstract methods using override.

c 2007, T. Verhoeff @ TUE.NL 17 Program Realization 2: Lecture 4

Abstract TRectangle with two implementations programs-lecture-4.zip contains

  • abstractrectanglesexample/rectangles.pas
  • abstractrectanglesexample/rectanglestest.pas
  • methodbindingexample/methodbindingexample.pas

1 unit Rectangles; 2

{ provides ADT for axis-parallel grid rectangles

3

with abstract base class and two implementations }

c 2007, T. Verhoeff @ TUE.NL 18 Program Realization 2: Lecture 4

Using TRectangle with two implementations

1 program RectanglesTest; 2 3 uses 4

Rectangles;

5 6 var 7

r, s: TRectangle;

8 9 begin 10

r := TRectangleLowHigh.Create(0, 0, 200, 100)

11 ; s := TRectangleLowDim.Create(100, 50, 300, 150) 12 ; r.Intersect(s) 13 ; if r.Contains(50, 50) then writeln(’Huh?’) 14 end. c 2007, T. Verhoeff @ TUE.NL 19 Program Realization 2: Lecture 4

Visibility attributes of class members

  • private : access restricted to the same unit
  • protected : like private but also accessible in any derived class
  • public : accessible anywhere

c 2007, T. Verhoeff @ TUE.NL 20 Program Realization 2: Lecture 4

slide-6
SLIDE 6

Dynamic Method Binding: Advanced Example

1 type 2

TParent = class(TObject)

3

procedure M; virtual; { possibly abstract; }

4

procedure D; virtual;

5

end; { class TParent }

6 7

TChild = class(TParent)

8

procedure M; override;

9

end; { class TChild }

10 11 procedure TParent.M; begin Write(’TParent.M ’) end; { omit if abstract } 12 13 procedure TParent.D; begin M ; M end; 14 15 procedure TChild.M; begin Write(’TChild.M ’) end; 16 17 begin 18

with TChild.Create do D { outputs: ’TChild.M TChild.M ’ }

c 2007, T. Verhoeff @ TUE.NL 21 Program Realization 2: Lecture 4

Static, Virtual, and Abstract Methods: Summary TParent = class(TObject) public procedure SI; {static} procedure SH; {static} procedure SO; {static} procedure VI; virtual; procedure VH; virtual; procedure VO; virtual; procedure AI; virtual; abstract; procedure AH; virtual; abstract; procedure AO; virtual; abstract; end; { class TParent } TChild = class(TParent) public { procedure SI; inherit } procedure SH; { hide } (* procedure SO; override;*) { procedure VI; inherit } procedure VH; (* HIDE *) procedure VO; override; { procedure AI; inherit } procedure AH; (* HIDE *) procedure AO; override; end; { class TChild }

c 2007, T. Verhoeff @ TUE.NL 22 Program Realization 2: Lecture 4

Static, Virtual, and Abstract Methods: Summary

1 var 2

PP, PC: TParent;

3

CP, CC: TChild;

4 5 begin 6

PP := TParent.Create (* COMPILER WARNING *)

7 ; PC :=

TChild.Create (* COMPILER WARNING *) (*

8 ; CP := TParent.Create

(* COMPILER ERROR *)

9 ; CC :=

TChild.Create (* COMPILER WARNING *)

10 11 ; PP.{method} 12 ; PC.{method} 13 ; CC.{method} 14 end. c 2007, T. Verhoeff @ TUE.NL 23 Program Realization 2: Lecture 4

Static, Virtual, and Abstract Methods: Summary method TParent TChild PP.method PC.method CC.method SI static inherit TParent TParent TParent SH ” hide TParent TParent TChild SO ”

  • verride
  • verride not allowed

VI virtual inherit TParent TParent TParent VH ” hide TParent TParent TChild VO ”

  • verride

TParent TChild TChild AI abstract inherit Exception Exception Exception AH ” hide Exception Exception TChild AO ”

  • verride

Exception TChild TChild

c 2007, T. Verhoeff @ TUE.NL 24 Program Realization 2: Lecture 4

slide-7
SLIDE 7

Inheritance as (another) way of using a class A class can now be used in two different ways:

  • 1. By instantiation , to create and manipulate objects
  • 2. By inheritance , to derive subclasses

Instantiation is ruled by the contract of the class. Inheritance is trickier to formalize and to apply properly. A subclass is tightly coupled to its superclass (parent, ancestor, base). This can create maintenance problems when code is reused.

c 2007, T. Verhoeff @ TUE.NL 25 Program Realization 2: Lecture 4

What Lies Ahead

  • More on inheritance
  • Memory management
  • Pascal pointer type

c 2007, T. Verhoeff @ TUE.NL 26 Program Realization 2: Lecture 4