isn t tes ng enough soiware tes ng
play

Isnt tes<ng enough? SoIware Tes<ng Methods Assuming - PDF document

Proofs about Programs Why make you study logic? Program Verification Why make you do proofs? (Rosen, Sections 5.5) Because we want to prove proper<es of


  1. Proofs ¡about ¡Programs ¡ • Why ¡make ¡you ¡study ¡logic? ¡ Program Verification • Why ¡make ¡you ¡do ¡proofs? ¡ (Rosen, Sections 5.5) • Because ¡we ¡want ¡to ¡prove ¡proper<es ¡of ¡ TOPICS programs: ¡ • Program Correctness – In ¡par<cular, ¡we ¡want ¡to ¡prove ¡proper<es ¡of ¡ • Preconditions & Postconditions • Program Verification variables ¡at ¡specific ¡points ¡in ¡a ¡program. ¡ • Assignment Statements – For ¡example, ¡we ¡may ¡want ¡prove ¡that ¡a ¡program ¡ • Conditional Statements segment ¡or ¡method ¡gets ¡the ¡right ¡answer. ¡ • Loops • Composition Rule CS160 - Fall Semester 2015 Isn’t ¡tes<ng ¡enough? ¡ SoIware ¡Tes<ng ¡ • Methods ¡ • Assuming ¡the ¡program ¡compiles, ¡we ¡can ¡go ¡ – Black-­‑box, ¡white-­‑box ¡ ahead ¡and ¡perform ¡some ¡amount ¡of ¡tes<ng. ¡ • Levels ¡ • Tes<ng ¡shows ¡that ¡for ¡specific ¡examples ¡(test ¡ – Unit ¡(Method), ¡Module ¡(Class), ¡Integra<on, ¡System ¡ cases) ¡the ¡program ¡is ¡doing ¡what ¡was ¡intended. ¡ ¡ • Types ¡ • Tes<ng ¡can ¡only ¡show ¡existence ¡of ¡some ¡bugs ¡ – Func<onality, ¡Configura<on, ¡Usability, ¡Reliability, ¡ but ¡cannot ¡exhaus<vely ¡iden<fy ¡all ¡of ¡them. ¡ ¡ Performance, ¡Compa<bility, ¡Error, ¡Localiza<on, ¡… ¡ • Program ¡verifica<on ¡can ¡be ¡used ¡to ¡prove ¡the ¡ • Processes ¡ correctness ¡of ¡the ¡program ¡with ¡any ¡input. ¡ – Regression, ¡Automa<on, ¡Test-­‑Driven ¡Development, ¡ Code ¡Coverage, ¡… ¡ CS160 - Fall Semester 2015 CS160 - Fall Semester 2015 1

  2. Program ¡Verifica<on ¡ Program ¡Correctness ¡Proofs ¡ • We ¡consider ¡a ¡program ¡to ¡be ¡ correct ¡if ¡it ¡produces ¡ ¡the ¡ • Part ¡1 ¡-­‑ ¡Prove ¡program ¡produces ¡correct ¡ expected ¡output ¡ for ¡all ¡possible ¡inputs . ¡ answer ¡when ¡(if) ¡it ¡terminates. ¡ • Domain ¡of ¡input ¡values ¡can ¡be ¡very ¡large, ¡how ¡many ¡ • Part ¡2 ¡-­‑ ¡Prove ¡that ¡the ¡program ¡does ¡indeed ¡ possible ¡values ¡of ¡an ¡integer? ¡2 32 ¡ ¡ ¡ ¡ ¡ ¡int ¡divide ¡(int ¡operand1, ¡int ¡operand2) ¡{ ¡ terminate ¡at ¡some ¡point. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡operand1 ¡/ ¡operand2; ¡ • We ¡can ¡only ¡Part ¡1, ¡because ¡Part ¡2 ¡has ¡been ¡ ¡ ¡ ¡ ¡} ¡ proven ¡to ¡be ¡undecidable: ¡ • 2 32 ¡* ¡2 32 ¡= ¡2 64 , ¡a ¡large ¡number, ¡so ¡we ¡clearly ¡cannot ¡test ¡ exhaus<vely! ¡ – Thus ¡we ¡try ¡to ¡prove ¡that ¡a ¡method ¡is ¡correct, ¡ • Instead ¡we ¡formally ¡specify ¡program ¡behavior, ¡then ¡use ¡ assuming ¡that ¡it ¡terminates ¡(par<al ¡correctness). ¡ logic ¡techniques ¡to ¡infer ¡(prove) ¡program ¡correctness. ¡ ¡ ¡ ¡ ¡ ¡ ¡ CS160 - Fall Semester 2015 CS160 - Fall Semester 2015 Predicate ¡Logic ¡and ¡Programs ¡ Asser<ons ¡ • Variables ¡in ¡programs ¡are ¡like ¡variables ¡in ¡ • Two ¡parts: ¡ predicate ¡logic: ¡ – Ini$al ¡Asser$on : ¡a ¡statement ¡of ¡what ¡must ¡be ¡true ¡about ¡ – They ¡have ¡a ¡domain ¡of ¡discourse ¡(data ¡type) ¡ the ¡input ¡values ¡or ¡values ¡of ¡variables ¡at ¡the ¡beginning ¡of ¡ the ¡program ¡segment ¡ – They ¡have ¡values ¡(drawn ¡from ¡the ¡data ¡type) ¡ • For ¡Example: ¡Method ¡that ¡determines ¡the ¡square ¡root ¡of ¡a ¡ • Variables ¡in ¡programs ¡are ¡different ¡from ¡variables ¡ number, ¡requires ¡the ¡input ¡(parameters) ¡to ¡be ¡>= ¡0 ¡ in ¡predicate ¡logic: ¡ – Final ¡Asser$on : ¡a ¡statement ¡of ¡what ¡must ¡be ¡true ¡about ¡ – Their ¡values ¡change ¡over ¡<me ¡(i.e., ¡loca<ons ¡in ¡the ¡ the ¡output ¡values ¡or ¡values ¡of ¡variables ¡at ¡the ¡end ¡of ¡the ¡ program) ¡ program ¡segment ¡ – Associate ¡the ¡predicate ¡with ¡specific ¡program ¡points ¡ • For ¡Example: ¡Can ¡we ¡specify ¡that ¡the ¡output ¡or ¡result ¡is ¡ • Immediately ¡before ¡or ¡aIer ¡a ¡statement ¡ exactly ¡correct ¡aIer ¡a ¡call ¡to ¡the ¡method? ¡ CS160 - Fall Semester 2015 CS160 - Fall Semester 2015 2

  3. Pre ¡and ¡Post ¡Condi<ons ¡ Hoare ¡Triple ¡ • “ A ¡program, ¡or ¡program ¡segment, ¡ • Ini$al ¡Asser$on : ¡some<mes ¡ ¡ Pre-condition S , ¡is ¡said ¡to ¡be ¡par<ally ¡correct ¡with ¡ called ¡the ¡ pre-­‑condi2on ¡ before code executes Pre-condition (p) respect ¡to ¡the ¡ini<al ¡asser<on ¡(pre-­‑ x = 1 before code executes condi<on) ¡ p ¡and ¡the ¡final ¡asser<on ¡ • Final ¡Asser$on : ¡some<mes ¡ ¡ (post-­‑condi<on) ¡ q , ¡if, ¡whenever ¡ p ¡is ¡ { { called ¡the ¡ post-­‑condi2on ¡ // Program segment true ¡for ¡the ¡input ¡values ¡of ¡ S , ¡and ¡if ¡ // Program segment: (S) } S ¡terminates, ¡then ¡ q ¡is ¡true ¡for ¡the ¡ } • Note : ¡these ¡asser<ons ¡can ¡be ¡ output ¡values ¡of ¡ S . ” ¡ ¡ represented ¡as ¡proposi<ons ¡or ¡ Post-condition Post-condition (q) – [Rosen ¡7th ¡edi<on, ¡p. ¡372] ¡ ¡ predicates. ¡For ¡simplicity, ¡we ¡will ¡ after code executes after code executes write ¡them ¡generally ¡as ¡proposi<ons. ¡ • Nota<on: ¡ p ¡{S} ¡q ¡ z = 3 CS160 - Fall Semester 2015 CS160 - Fall Semester 2015 Program ¡Verifica<on ¡ Program ¡Verifica<on ¡ Example ¡#1: ¡Assignment ¡Statements ¡ Example ¡#1: ¡Assignment ¡Statements ¡ • Prove ¡that ¡the ¡program ¡segment: ¡ • Assume ¡that ¡our ¡proof ¡system ¡already ¡includes ¡ rules ¡of ¡arithme<c, ¡and ¡theorems ¡about ¡ y = 2; y = 2; z = x + y; z = x + y; divisibility ¡… ¡ • Is ¡correct ¡with ¡respect ¡to: ¡ • Consider ¡the ¡ ¡following ¡code: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡pre-­‑condi<on: ¡x ¡= ¡1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡post-­‑condi<on: ¡z ¡= ¡3 ¡ y = 2; • Suppose ¡x ¡= ¡1 ¡is ¡true ¡as ¡program ¡begins: ¡ What is true z = x + y; BEFORE code – Then ¡y ¡is ¡assigned ¡the ¡value ¡of ¡2 ¡ executes – Then ¡z ¡is ¡assigned ¡the ¡value ¡of ¡x ¡+ ¡y ¡= ¡1 ¡+ ¡2 ¡= ¡3 ¡ • Pre-­‑condi<on: ¡ ¡ p ( x ) , x =1 ¡ • Thus, ¡the ¡program ¡segment ¡is ¡correct ¡with ¡regards ¡to ¡the ¡ What is true pre-­‑condi<on ¡that ¡x ¡= ¡1 ¡and ¡post-­‑condi<on ¡z ¡= ¡3. ¡ • Post-­‑condi<on: ¡ ¡ q ( z ) , z =3 ¡ AFTER code executes CS160 - Fall Semester 2015 CS160 - Fall Semester 2015 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend