From cec1ff51b65476d50ad24aa232861f34c1d5d4d0 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Fri, 1 Jan 2021 21:46:58 +0100 Subject: [PATCH] graph.rkt: Tidy up and structure in sections. The sections are the same as in the documentation of the generic graph library: https://docs.racket-lang.org/graph/index.html --- graph.rkt | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/graph.rkt b/graph.rkt index 2d45323..eeec160 100644 --- a/graph.rkt +++ b/graph.rkt @@ -20,34 +20,58 @@ (module graph-wrapper racket (require (prefix-in g: graph)) - (provide graph? + (provide graph? has-vertex? has-edge? + directed-graph - has-vertex? - has-edge? + graphviz) + ;; Wrap the opaque graph structure coming from the generic + ;; graph library. (struct graph (g)) - (define (directed-graph es [ws #f]) - (graph (g:directed-graph es ws))) (define (has-vertex? g v) (g:has-vertex? (graph-g g) v)) (define (has-edge? g u v) (g:has-edge? (graph-g g) u v)) + + ;; 2 Graph constructors + ;; 2.2 Weighted graphs + (define (directed-graph es [ws #f]) + (graph (g:directed-graph es ws))) + + ;; 10 Graphviz (define (graphviz g #:output [output #f] #:colors [colors #f]) (g:graphviz (graph-g g) #:output output #:colors colors))) -(require/typed 'graph-wrapper + +(require/typed/provide 'graph-wrapper [#:opaque Graph graph?] - [directed-graph (->* ((Listof (List Any Any))) ((Listof Any)) Graph)] [has-vertex? (-> Graph Any Boolean)] [has-edge? (-> Graph Any Any Boolean)] + + ;; 2 Graph constructors + ;; 2.2 Weighted graphs + [directed-graph (->* ((Listof (List Any Any))) ((Listof Any)) Graph)] + + ;; 10 Graphviz [graphviz (->* (Graph) (#:output Output-Port #:colors (HashTable Any Natural)) String)]) -(define g (directed-graph '((a b) (b c)))) -(has-edge? g 'a 'c) -(has-vertex? g 'a) -(graphviz g) + +(module+ test + ;; The goal of the tests is to check that all of the provided + ;; functions can be invoked without errors. The tests do not check + ;; whether the results make sense. + (require typed/rackunit) + + (test-case "1 Generic Graph Interface" + (define g (directed-graph '((a b) (b c)))) + (check-true (has-edge? g 'a 'c)) + (check-true (has-vertex? g 'a))) + + (test-case "10 Graphviz" + (define g (directed-graph '((a b) (b c)))) + (graphviz g)))