Random Testing in 321

Similar documents
Administrative Stuff

Random Testing in 321

Functional Programming - 2. Higher Order Functions

User-defined Functions. Conditional Expressions in Scheme

Cost of Substitution

Functional Programming. Pure Functional Programming

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Typed Racket: Racket with Static Types

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Intro. Scheme Basics. scm> 5 5. scm>

Typed Scheme: Scheme with Static Types

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

CSC 533: Programming Languages. Spring 2015

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Racket: Modules, Contracts, Languages

CS 314 Principles of Programming Languages. Lecture 16

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CS2500 Exam 2 Fall 2011

The Typed Racket Guide

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

Vectors in Scheme. Data Structures in Scheme

Functional Programming. Pure Functional Languages

ITERATORS AND STREAMS 9

Higher-Order Functions

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

CS 314 Principles of Programming Languages

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Functional Programming. Pure Functional Languages

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

1. For every evaluation of a variable var, the variable is bound.

CSU211 Exam 2 Fall 2007

Documentation for LISP in BASIC

Chapter 15 Functional Programming Languages

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Repetition Through Recursion

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CS61A Midterm 2 Review (v1.1)

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

2D Syntax. Version October 30, 2017

Lisp Basic Example Test Questions

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

(scheme-1) has lambda but NOT define

Higher-Order Functions (Part I)

CS 275 Name Final Exam Solutions December 16, 2016

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Functional programming in μscheme

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Project 2: Scheme Interpreter

Programming Languages. Next: Building datatypes. Suppose I wanted. Next: Building datatypes 10/4/2017. Review so far. Datatypes.

Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

CSE341 Autumn 2017, Final Examination December 12, 2017

Programming Languages. Datatypes

Programming Languages

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

Bindings & Substitution

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

Programming Languages

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Principles of Programming Languages

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

FP Foundations, Scheme

Module 8: Local and functional abstraction

Name EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

CSE341 Spring 2017, Final Examination June 8, 2017

61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013

From Syntactic Sugar to the Syntactic Meth Lab:

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

Quicksort. Alternative Strategies for Dividing Lists. Fundamentals of Computer Science I (CS F)

Types of recursion. Readings: none. In this module: a glimpse of non-structural recursion. CS 135 Winter : Types of recursion 1

Programming Languages

How to Design Programs Languages

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

CSE341 Spring 2017, Final Examination June 8, 2017

Working with recursion

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

How to Design Programs

Scheme: Expressions & Procedures

Style and Submission Guide

CSE341 Spring 2016, Final Examination June 6, 2016

Structure and Interpretation of Computer Programs

Recap: ML s Holy Trinity

Scheme Quick Reference

COMP 105 Homework: Type Systems

CS450: Structure of Higher Level Languages Spring 2018 Assignment 7 Due: Wednesday, April 18, 2018

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

Fall 2017 December 4, 2017

Transcription:

Random Testing in 321 1

Test Cases So Far Each test relates a particular input to a particular output. (test (bound-ids (with 'x (id 'y) (id 'x))) '(x)) (test (binding-ids (with 'x (id 'y) (id 'x))) '(x)) 2

Property-based Testing But we can only write so many tests by hand. To find additional bugs, we can automate testing. We start with what we hope is a fact about our program. For example, If bound-ids says 'x is bound, then binding-ids says 'x is binding. 3

Property Violation If we can find some WAE for which the property doesn t hold... (define a-wae...) (bound-ids a-wae) ; '(x) (binding-ids a-wae) ; '()... we ve found a bug. 4

Property Testing We can test this property in the usual style. ; bound=>binding? : WAE -> boolean ; checks if bound ids are also binding (define (bound=>binding? e)...) (test (bound=>binding? (id 'x)) true) (test (bound=>binding? (with 'x (num 0) (id 'x))) true) Expected result is always true, so if we had lots of WAEs, then we d have lots of tests. 5

Automated Property Testing Write a program to generate test inputs! 6

Random WAEs ; random-wae: -> WAE (define (random-wae) (case (random 5) [(0) (num (random-nat))] [(1) (id (random-symbol))] [(2) (add (random-wae) (random-wae))] [(3) (sub (random-wae) (random-wae))] [(4) (with (random-symbol) (random-wae) (random-wae))])) Watch out that code is buggy... (read on for why) 7

Random WAEs ; random-nat: -> nat (define (random-nat) (case (random 2) [(0) 0] [(1) (add1 (random-nat))])) ; random-symbol: -> symbol (define (random-symbol) (random-elem '(x y z a b c))) ; random-elem: (listof X) -> X (define (random-elem xs) (list-ref xs (random (length xs)))) 8

Generation Strategy To build a WAE, 1/5 of the time, build a number 1/5 of the time, build a symbol 3/5 of the time, first build two more WAEs 9

Expected Progress On average, we reduce the problem from Generate 1 WAE. to Generate 1.2 WAEs. since 1.2 = (2/5)*0 + (3/5)*2 10

Height Bound Limit WAE size by bounding tree height. ; random-wae/b: nat -> WAE (define (random-wae/b h) (case (random (if (zero? h) 2 5)) [(0) (num (random-nat))] [(1) (id (random-symbol))] [(2) (add (random-wae/b (sub1 h)) (random-wae/b (sub1 h)))] [(3) (sub (random-wae/b (sub1 h)) (random-wae/b (sub1 h)))] [(4) (with (random-symbol) (random-wae/b (sub1 h)) (random-wae/b (sub1 h)))])) (Alternatively, tweak weights.) 11

Property Implementation ; bound=>binding: WAE -> boolean (define (bound=>binding e) (sublist? (bound-ids e) (binding-ids e))) ; sublist?: (listof X) (listof X) -> boolean ; Expects xs and ys to be sorted and have no dups. (define (sublist? xs ys) (cond [(null? xs) #t] [(null? ys) #f] [(equal? (car xs) (car ys)) (sublist? (cdr xs) (cdr ys))] [else (sublist? xs (cdr ys))])) 12

Running Tests ; test-bound=>binding: nat nat -> (or 'passed WAE) (define (test-bound=>binding size attempts) (if (zero? attempts) 'passed (let ([test-input (random-wae/b size)]) (if (bound=>binding test-input) (test-bound=>binding size (sub1 attempts)) test-input)))) (test-bound=>binding 5 1000) 13

HW2 Test Results We ran random tests on last year s HW2 submissions. Received 99 submissions Tested 6 properties Found a bug in 53 out of those 99 submissions 14

Interpreter Properties Interpreter does not crash Produces same result as another implementation (e.g., DrRacket) Type checker accurately predicts result (later) Program equivalences hold 15

With Elimination Example For example, we should be able to replace a with with a new function. {with {x {+ 7 2}} {+ x x}} {deffun {f x} {+ x x}} {f {+ 7 2}} 16

With Elimination Rule, an Attempt In general, {... {with {an-id a-wae} another-wae}...} 17

With Elimination Rule, an Attempt In general, {... {with {an-id a-wae} another-wae}...} {deffun {new-id an-id} another-wae} {... {new-id a-wae}...} 18

With Elimination Rule, an Attempt In general, Different free variables! {... {with {an-id a-wae} another-wae}...} {deffun {new-id an-id} another-wae} {... {new-id a-wae}...} 19

Rule Example {with {x {+ 2 7}} {with {y {+ x x}} {+ x y}}} {deffun {f y} {+ x y}} {with {x {+ 2 7}} {f {+ x x}}} 20

Rule Example free {with {x {+ 2 7}} {with {y {+ x x}} {+ x y}}} {deffun {f y} {+ x y}} {with {x {+ 2 7}} {f {+ x x}}} bound 21

With Elimination, Fixed Pass free variables of another-wae as arguments. {... {with {an-id a-wae} another-wae}...} {deffun {new-id an-id id 1...} another-wae} {... {new-id a-wae id 1...}...} where (equal? (free-ids another-wae) (list id 1...)) 22

Rule Example x becomes a parameter of f {with {x {+ 2 7}} {with {y {+ x x}} {+ x y}}} {deffun {f y x} {+ x y}} {with {x {+ 2 7}} {f {+ x x} x}} 23