Skip to content

Commit

Permalink
make some tests more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
stchang committed Feb 19, 2016
1 parent 8f7ed67 commit 43e8e13
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 73 deletions.
60 changes: 28 additions & 32 deletions graph/tests/graph-fns-basic-tests.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,39 @@
(mk-unweighted-graph/undirected
'((r v) (r s) (s w) (w t) (w x) (x t) (t u) (x u) (x y) (y u))))

(check-seqs-as-equal-sets? '(v r s w t x u y) (in-vertices g22.3))

(define-values (d22.3 π22.3) (bfs g22.3 's))

(check-equal?
d22.3
(make-hash '((t . 2) (y . 3) (u . 3) (s . 0) (v . 2) (x . 2) (w . 1) (r . 1))))
(check-equal?
π22.3
(make-hash '((t . w) (y . x) (u . x) (s . #f) (v . r) (x . w) (w . s) (r . s))))

(check-equal? '(s r v) (fewest-vertices-path g22.3 's 'v))
(check-equal? '(s w t) (fewest-vertices-path g22.3 's 't))
(check-equal? '(s w x) (fewest-vertices-path g22.3 's 'x))
(check-equal? '(s w x u) (fewest-vertices-path g22.3 's 'u))
(check-equal? '(s w x y) (fewest-vertices-path g22.3 's 'y))
(define (check-22.3 G)
(check-seqs-as-equal-sets? '(v r s w t x u y) (in-vertices G))

(define-values (d22.3 π22.3) (bfs G 's))

(check-equal?
d22.3
(make-hash
'((t . 2) (y . 3) (u . 3) (s . 0) (v . 2) (x . 2) (w . 1) (r . 1))))

(let ([partial-expected '((s . #f) (w . s) (r . s) (v . r) (t . w) (x . w))])
(check-true
(or
(equal? π22.3 (make-hash (append partial-expected '((y . x) (u . x)))))
(equal? π22.3 (make-hash (append partial-expected '((y . x) (u . t)))))
(equal? π22.3 (make-hash (append partial-expected '((y . x) (u . x)))))
(equal? π22.3 (make-hash (append partial-expected '((y . u) (u . t))))))))

(check-equal? '(s r v) (fewest-vertices-path G 's 'v))
(check-equal? '(s w t) (fewest-vertices-path G 's 't))
(check-equal? '(s w x) (fewest-vertices-path G 's 'x))
(let ([path-s-u (fewest-vertices-path G 's 'u)])
(check-true (or (equal? '(s w x u) path-s-u)
(equal? '(s w t u) path-s-u))))
(check-equal? '(s w x y) (fewest-vertices-path G 's 'y)))

(check-22.3 g22.3)

;; same as above, but use mk-undirected-graph constructor
(define g22.3b (mk-undirected-graph
'((r v) (r s) (s w) (w t) (w x) (x t) (t u) (x u) (x y) (y u))))

(check-seqs-as-equal-sets? '(v r s w t x u y) (in-vertices g22.3b))

(define-values (d22.3b π22.3b) (bfs g22.3b 's))

(check-equal?
d22.3b
(make-hash '((t . 2) (y . 3) (u . 3) (s . 0) (v . 2) (x . 2) (w . 1) (r . 1))))
(check-equal?
π22.3b
(make-hash '((t . w) (y . x) (u . x) (s . #f) (v . r) (x . w) (w . s) (r . s))))

(check-equal? '(s r v) (fewest-vertices-path g22.3b 's 'v))
(check-equal? '(s w t) (fewest-vertices-path g22.3b 's 't))
(check-equal? '(s w x) (fewest-vertices-path g22.3b 's 'x))
(check-equal? '(s w x u) (fewest-vertices-path g22.3b 's 'u))
(check-equal? '(s w x y) (fewest-vertices-path g22.3b 's 'y))
(check-22.3 g22.3b)

;; dfs utils ------------------------------------------------------------------

Expand Down
13 changes: 10 additions & 3 deletions graph/tests/graph-fns-maxflow-tests.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,15 @@
(4 5))))
(define L-Rwiki2 (bipartite? gwiki2))
(check-not-false L-Rwiki2)
(check-equal? (apply set (first L-Rwiki2)) (set 1 3 5))
(check-equal? (apply set (second L-Rwiki2)) (set 0 2 4))
(let ([computed-L (apply set (first L-Rwiki2))]
[computed-R (apply set (second L-Rwiki2))]
[L (set 1 3 5)]
[R (set 0 2 4)])
(check-true (or (and (equal? computed-L L)
(equal? computed-R R))
(and (equal? computed-L R)
(equal? computed-R L)))
"bipartite graph 1,3,5 and 0,2,4"))

(define g26.8
(mk-unweighted-graph/undirected '((1 6) (2 6) (2 8) (3 7) (3 8) (3 9) (4 8) (5 8))))
Expand All @@ -117,4 +124,4 @@
(e 5))))

(check-not-false (bipartite? bipartite-cmu))
(check-equal? (length (maximum-bipartite-matching bipartite-cmu)) 5)
(check-equal? (length (maximum-bipartite-matching bipartite-cmu)) 5)
104 changes: 66 additions & 38 deletions graph/tests/graphviz.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,74 @@
(mk-unweighted-graph/undirected '((1 2) (2 3) (1 3) (3 4) (3 5) (4 5))))

(define butterfly-viz (graphviz butterfly))
;"digraph G {\n\t1;\n\t2;\n\t3;\n\t4;\n\t5;\n\tsubgraph U {\n\t\tedge [dir=none];\n\t\t1 -> 2;\n\t\t1 -> 3;\n\t\t2 -> 3;\n\t\t3 -> 4;\n\t\t3 -> 5;\n\t\t4 -> 5;\n\t}\n\tsubgraph D {\n\t}\n}\n"
(check-true (string-contains? butterfly-viz "1 -> 2"))
(check-true (string-contains? butterfly-viz "1 -> 3"))
(check-true (string-contains? butterfly-viz "2 -> 3"))
(check-true (string-contains? butterfly-viz "3 -> 4"))
(check-true (string-contains? butterfly-viz "3 -> 5"))
(check-true (string-contains? butterfly-viz "4 -> 5"))
;; eg,
;; digraph G {
;; 5;
;; 1;
;; 2;
;; 3;
;; 4;
;; subgraph U {
;; edge [dir=none];
;; 5 -> 3;
;; 5 -> 4;
;; 1 -> 3;
;; 1 -> 2;
;; 2 -> 3;
;; 3 -> 4;
;; }
;; subgraph D {
;; }
;; }
(define (check-butterfly-viz-output viz-str)
(check-true (or (string-contains? viz-str "1 -> 2")
(string-contains? viz-str "2 -> 1")))
(check-true (or (string-contains? viz-str "1 -> 3")
(string-contains? viz-str "3 -> 1")))
(check-true (or (string-contains? viz-str "2 -> 3")
(string-contains? viz-str "3 -> 2")))
(check-true (or (string-contains? viz-str "3 -> 4")
(string-contains? viz-str "4 -> 3")))
(check-true (or (string-contains? viz-str "3 -> 5")
(string-contains? viz-str "5 -> 3")))
(check-true (or (string-contains? viz-str "4 -> 5")
(string-contains? viz-str "5 -> 4"))))
(check-butterfly-viz-output butterfly-viz)

(define-values (buttr-num-colors buttr-coloring) (coloring/greedy butterfly))

(define bf-viz-color (graphviz butterfly #:colors buttr-coloring))
(check-true (string-contains? bf-viz-color "1 [color=\"0.0 1.0 1.0\"]"))
(check-true (string-contains? bf-viz-color "2 [color=\"0.333 1.0 1.0\"]"))
(check-true (string-contains? bf-viz-color "3 [color=\"0.666 1.0 1.0\"]"))
(check-true (string-contains? bf-viz-color "4 [color=\"0.0 1.0 1.0\"]"))
(check-true (string-contains? bf-viz-color "5 [color=\"0.333 1.0 1.0\"]"))
(check-true (string-contains? bf-viz-color "1 -> 2"))
(check-true (string-contains? bf-viz-color "1 -> 3"))
(check-true (string-contains? bf-viz-color "2 -> 3"))
(check-true (string-contains? bf-viz-color "3 -> 4"))
(check-true (string-contains? bf-viz-color "3 -> 5"))
(check-true (string-contains? bf-viz-color "4 -> 5"))
;; eg,
;; digraph G {
;; 5 [color="0.333 1.0 1.0"];
;; 1 [color="0.666 1.0 1.0"];
;; 2 [color="0.333 1.0 1.0"];
;; 3 [color="0.0 1.0 1.0"];
;; 4 [color="0.666 1.0 1.0"];
;; subgraph U {
;; edge [dir=none];
;; 5 -> 3;
;; 5 -> 4;
;; 1 -> 3;
;; 1 -> 2;
;; 2 -> 3;
;; 3 -> 4;
;; }
;; subgraph D {
;; }
;; }
(check-true (string-contains? bf-viz-color "[color=\"0.0 1.0 1.0\"]"))
(check-true (string-contains? bf-viz-color "[color=\"0.333 1.0 1.0\"]"))
(check-true (string-contains? bf-viz-color "[color=\"0.666 1.0 1.0\"]"))
(check-true (string-contains? bf-viz-color "[color=\"0.0 1.0 1.0\"]"))
(check-true (string-contains? bf-viz-color "[color=\"0.333 1.0 1.0\"]"))
(check-butterfly-viz-output bf-viz-color)

#;(check-equal?
(graphviz butterfly #:colors buttr-coloring)
"digraph G {\n\t1 [color=\"0.0 1.0 1.0\"];\n\t2 [color=\"0.333 1.0 1.0\"];\n\t3 [color=\"0.666 1.0 1.0\"];\n\t4 [color=\"0.0 1.0 1.0\"];\n\t5 [color=\"0.333 1.0 1.0\"];\n\tsubgraph U {\n\t\tedge [dir=none];\n\t\t1 -> 2;\n\t\t1 -> 3;\n\t\t2 -> 3;\n\t\t3 -> 4;\n\t\t3 -> 5;\n\t\t4 -> 5;\n\t}\n\tsubgraph D {\n\t}\n}\n")

(define bf-viz-color-brelaz (graphviz butterfly #:colors (coloring/brelaz butterfly)))
(check-true (string-contains? bf-viz-color-brelaz "1 [color=\"0.333 1.0 1.0\"]"))
(check-true (string-contains? bf-viz-color-brelaz "2 [color=\"0.666 1.0 1.0\"]"))
(check-true (string-contains? bf-viz-color-brelaz "3 [color=\"0.0 1.0 1.0\"]"))
(check-true (string-contains? bf-viz-color-brelaz "4 [color=\"0.333 1.0 1.0\"]"))
(check-true (string-contains? bf-viz-color-brelaz "5 [color=\"0.666 1.0 1.0\"]"))
(check-true (string-contains? bf-viz-color-brelaz "1 -> 2"))
(check-true (string-contains? bf-viz-color-brelaz "1 -> 3"))
(check-true (string-contains? bf-viz-color-brelaz "2 -> 3"))
(check-true (string-contains? bf-viz-color-brelaz "3 -> 4"))
(check-true (string-contains? bf-viz-color-brelaz "3 -> 5"))
(check-true (string-contains? bf-viz-color-brelaz "4 -> 5"))

#;(check-equal?
(graphviz butterfly #:colors (coloring/brelaz butterfly))
"digraph G {\n\t1 [color=\"0.333 1.0 1.0\"];\n\t2 [color=\"0.666 1.0 1.0\"];\n\t3 [color=\"0.0 1.0 1.0\"];\n\t4 [color=\"0.333 1.0 1.0\"];\n\t5 [color=\"0.666 1.0 1.0\"];\n\tsubgraph U {\n\t\tedge [dir=none];\n\t\t1 -> 2;\n\t\t1 -> 3;\n\t\t2 -> 3;\n\t\t3 -> 4;\n\t\t3 -> 5;\n\t\t4 -> 5;\n\t}\n\tsubgraph D {\n\t}\n}\n")
(define bf-viz-color-brelaz
(graphviz butterfly #:colors (coloring/brelaz butterfly)))
(check-true (string-contains? bf-viz-color-brelaz "[color=\"0.333 1.0 1.0\"]"))
(check-true (string-contains? bf-viz-color-brelaz "[color=\"0.666 1.0 1.0\"]"))
(check-true (string-contains? bf-viz-color-brelaz "[color=\"0.0 1.0 1.0\"]"))
(check-true (string-contains? bf-viz-color-brelaz "[color=\"0.333 1.0 1.0\"]"))
(check-true (string-contains? bf-viz-color-brelaz "[color=\"0.666 1.0 1.0\"]"))
(check-butterfly-viz-output bf-viz-color-brelaz)

0 comments on commit 43e8e13

Please sign in to comment.