#lang racket ;;; randomized-test driven development (require "lec06-heap.rkt") (define (random-natural) (cond [(zero? (random 100)) 0] [else (+ 1 (random-natural))])) (define (random-list-of-naturals) (for/list ([i (in-range (random-natural))]) (random-natural))) ;; inserting-one-bumps-count? : heap? -> boolean ;; captures the property: ;; ∀ h ∈ heap. size(h)+1 == size(h′), ;; where h′ is h with one item inserted (define (inserting-one-bumps-count? h) (define old-size (heap-size h)) (insert! h (random-natural)) (define new-size (heap-size h)) (= new-size (+ old-size 1))) ;; can-sort-numbers? : (listof real?) -> boolean ;; captures the property: ;; ∀ l ∈ (listof number). heapsort(l,<) == sort(l,<) (define (can-sort-numbers? numbers) (define h (heap-with-numbers numbers)) (define heap-sorted (let loop () (cond [(zero? (heap-size h)) '()] [else (define fst (remove-min! h)) (define rst (loop)) (cons fst rst)]))) (equal? heap-sorted (sort numbers <))) ;; heap-with-numbers : (listof real?) -> heap? (define (heap-with-numbers numbers) (define h (new-heap)) (for ([n (in-list numbers)]) (insert! h n)) h) ;; check to see if inserting-one-bumps-count? finds a bug (for ([i (in-range 100)]) (define numbers (random-list-of-naturals)) ;(printf "trying.1 ~s\n" numbers) (define h (heap-with-numbers numbers)) (unless (inserting-one-bumps-count? h) (error 'badness! "~s" numbers))) ;; check to see if can-sort-numbers? finds a bug (for ([i (in-range 100)]) (define numbers (random-list-of-naturals)) (printf "trying.2 ~s\n" numbers) (unless (can-sort-numbers? numbers) (error 'badness.2! "~s" numbers)))