Assignment 2 – Testing Properties

Assignment 2 – Testing Properties

Due Fri 10/15 11:59pm

1.

The function below is a buggy implementation of a Topological Sort and it uses graph.rkt (which, as far as I know, is not buggy).

Run the file and observe that the test cases in the file (which are correct) all pass. Formulate a property that should hold for topological sort and then find an input that fails to satisfy that property. Use random testing. Once you have that input, turn it into a small test case. Fix the bug.

  #lang racket
  (require "graph.rkt")
     
  ;; taken from wikipedia: https://en.wikipedia.org/wiki/Topological_sorting
  (define (kahns-algorithm original-graph)
    (define ig (invert original-graph))
    (define g (copy-graph original-graph))
    (define L '())
    (define S (no-incoming-edges g))
    (let loop ()
      (unless (empty? S)
        (define n (first S))
        (set! L (cons n L))
        (set! S (rest S))
        (for ([m (in-list (neighbors g n))])
          (remove-edge! g n m)
          (remove-edge! ig m n)
          (when (empty? (neighbors ig n))
            (set! S (cons m S))))
        (loop)))
    (cond
      [(has-edges? g) #f]
      [else (reverse L)]))
   
  ;; test cases go into the submodule `test`
  ;; by convention. DrRacket will run these
  ;; for your automatically; at the command-line,
  ;; use `raco test file.rkt` to run them
  (module+ test
    (require rackunit)
    (check-equal?
     (kahns-algorithm
      (new-graph '()))
     (list))
     
    (check-equal?
     (kahns-algorithm
      (new-graph '(("a" "a"))))
     #f)
     
    (check-equal?
     (kahns-algorithm
      (new-graph '(("a" "b")
                   ("b" "c"))))
     (list "a" "b" "c")))
   

2.

Implement the other topological sorting algorithm from the wikipedia page and use random testing to try to find bugs in your implementation, using the property that you developed for the previous version. The graph library comes with a function to make up a graph. Strive to make a correct implementation, using the random testing as well as unit testing of your own to help you.

Note: to implement the marking, have two local variables that hold hash tables mapping strings to booleans. Do not need to change the graph library.

Submit a single racket file with your solution via Canvas.