assert early and assert often practical hints on effective asserting tony hoare techfest february...

24
Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Upload: vanessa-cross

Post on 26-Mar-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Assert early and assert often

Practical hints on effective asserting

Tony Hoare

Techfest February 2002

Page 2: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Benefits of assertions today…

• Test probes• Program documentation• Interface specification• Code optimisation• Defect tracking• Reduction of noise from analysis• Hardening of retail code

Page 3: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

… and more tomorrow

• Accuracy of program analysis

• Test case generation/prioritisation

• Post-mortem dump-cracking

• Concurrency safety

• Validation of security

• Programming language design

Page 4: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Engineering test probes

• Analogy: engine on a test bench

• Instrumented at internal interfaces

• To test tolerances continuously

• And avoid test to destruction

• Opportunity to improve quality by

tightening the tolerances

Page 5: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Macros

#ifdef DEBUG#define CHECK(b,str) {if (b) { }else {report (str);

assert (false)} }#else #define CHECK(b,str)#endif

Page 6: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Explanations

• CHECK( assertion, “reason why I think the assertion is true”)

• Otherwise it’s easy to forget.

• Helps both writer and reader.

• Pinpoints risk of similar errors

• Helps to avoid them in future

Page 7: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Other variants

• VSASSERT Visual Studio

• MsoAssert Office

• Debug.Assert C#

• …

Page 8: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Documentation

• Protection for system against future changes

if (a >= b){ .. a++ ; .. };.. ..

CHECK(a != b, ‘a has just been incremented to avoid equality’) ;x = c/(a - b)

Page 9: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Assumptions

• Used only during early test

SIMPLIFYING_ASSUMPTION(strlen(input) < MAX_PATH, ‘’not yet checking for overflow’’)

• Failure indicates test was irrelevant

• Prohibited in ship code

Page 10: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Compile-time

• #define COMPILE_TIME_CHECK (b) extern dummy[(b)?1:-1]

• Generates report at compile time

• COMPILE_TIME_CHECK (sizeof(x)==sizeof(y), ‘addition undefined for arrays of different sizes)

Page 11: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Invariants

• True of every object …

• …before and after every method call

• bool invariant ( ){…tests that list is circular…}

Page 12: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Invariants

• Integrity checking

• Software audits

• Post-mortem dump-cracking.

Page 13: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Interface assertions

• Useful to implementer and all users

• Used again on each release

• Reduce need to examine code

• Aid the unit test of each module

• Permit modular analysis and proof

Page 14: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Preconditions

void insert(node *n){PRECONDITION ( n != NULL && invariant(), ‘don’t insert a non-existent object’);SIMPLIFYING-ASSUMPTION

(find(n)== 0); .. .. ..

Page 15: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Post-conditions

.. ..POST_CONDITION ( find(n)&&

invariant(), ‘the inserted object will be found in the list’)}

• obligation on method writer to verify

Page 16: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Optimisation

switch (condition) {case 0: .. .. ; break;case 1: .. .. ;break;default: UNREACHABLE(‘condition is really a boolean’);}

• Compiler emits less code

Page 17: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Defect tracking

• Office Watson keys defects by assertions

• Integrates with RAID data base

• Identifies bugs across builds/releases

• Integral to the programming process

Page 18: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

PREFIX_ASSUME

• Reduces PREFIX noise

• pointer = find (something);

PREFIX_ASSUME ( pointer != NULL,

“see the insertion three lines back”);

… pointer ->mumble = blat …

Page 19: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Rugged code in retail

• VSASSERT assertions are ignored

• VsVerifyThrow … generate exception

• VsVerify …user chooses

Page 20: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Life of an assertion

• Design discussions: record decisions

• Project planning: interface contracts

• Test planning: harness design

• Test case selection: violate post-conditions

• Coding: correctness concerns

• Prototyping: simplifying assumptions

Page 21: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

… continued

• In later release: detect regression

• Defect tracking: fault classification

• In retail: crash-proofing

• Defect analysis: dump-cracking

• Evolution of legacy: documentation

Page 22: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Conclusion

Assert early,

assert often,

and assert more strongly every time.

Page 23: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

Apologies to…

‘Vote early, vote often’

is the Politishun’s golden rule.

Josh Billings

American humorist, 1816-85.

Page 24: Assert early and assert often Practical hints on effective asserting Tony Hoare Techfest February 2002

[email protected]

Rick Andrews, Chris Antos, Tom Ball, Pete Collins, Terry Crowley, Mike Daly, Robert Deline, John Douceur, Sean Edmison, Kirk Glerum, David Greenspoon, Yuri Gurevich, Martyn Lovell, Bertrand Meyer, Jon Pincus, Harry Robinson, Hannes Ruescher, Marc Shapiro, Kevin Schofield, Wolfram Schulte, David Schwartz, Amitabh Srivastava, David Stutz, James Tierney