2020-02-19 22:49:50 +01:00
|
|
|
#lang racket
|
2020-02-16 21:39:42 +01:00
|
|
|
|
|
|
|
;;; Tests for dds/utils.
|
|
|
|
|
2020-02-23 19:17:16 +01:00
|
|
|
(require rackunit graph "utils.rkt")
|
2020-02-16 21:39:42 +01:00
|
|
|
|
2020-02-19 22:09:09 +01:00
|
|
|
(test-case "HashTable Injection"
|
2020-02-16 21:39:42 +01:00
|
|
|
(test-case "auto-hash-ref/explicit"
|
|
|
|
(let ([mytable #hash((a . 3) (b . 4))])
|
|
|
|
(check-equal? (auto-hash-ref/explicit (mytable b a)
|
|
|
|
(* a b))
|
|
|
|
12))
|
|
|
|
(let ([ht #hash((a . #t) (b . #f))])
|
|
|
|
(check-equal? (auto-hash-ref/explicit (ht a b)
|
|
|
|
(and (not a) b))
|
2020-02-17 00:16:44 +01:00
|
|
|
#f)))
|
|
|
|
|
|
|
|
(test-case "auto-hash-ref/:"
|
|
|
|
(let ([ht #hash((x . #t) (y . #t) (t . #f))]
|
|
|
|
[z #t])
|
|
|
|
(check-equal? (auto-hash-ref/: ht
|
|
|
|
(and :x (not :y) z (or (and :t) :x)))
|
|
|
|
#f))
|
|
|
|
(let ([ht #hash((a . 1) (b . 2))])
|
|
|
|
(check-equal? (auto-hash-ref/: ht (+ :a (* 2 :b)))
|
2020-02-17 23:52:15 +01:00
|
|
|
5)))
|
|
|
|
|
|
|
|
(test-case "eval-with"
|
|
|
|
(check-equal? (let ([ht #hash((a . 1) (b . 1))])
|
2020-02-19 23:12:58 +01:00
|
|
|
(eval-with ht '(+ b a 1)))
|
2020-02-17 23:52:15 +01:00
|
|
|
3)))
|
2020-02-19 22:11:44 +01:00
|
|
|
|
|
|
|
(test-case "Analysis of quoted expressions"
|
|
|
|
(check-equal? (extract-symbols '(1 (2 3) x (y z 3)))
|
|
|
|
'(x y z)))
|
2020-02-21 18:01:08 +01:00
|
|
|
|
|
|
|
(test-case "Variable mapping and Org-mode"
|
|
|
|
(check-equal? (any->string 'a) "a")
|
|
|
|
(check-equal? (any->string '(a 1 (x y))) "(a 1 (x y))")
|
2020-02-22 10:29:34 +01:00
|
|
|
(check-equal? (any->string "hello") "hello")
|
2020-02-22 12:12:57 +01:00
|
|
|
(let ([mp (stringify-variable-mapping #hash((a . (and a b)) (b . (not b))))])
|
2020-02-22 10:40:40 +01:00
|
|
|
(check-equal? (hash-ref mp 'a) "(and a b)")
|
|
|
|
(check-equal? (hash-ref mp 'b) "(not b)"))
|
2020-02-22 12:11:37 +01:00
|
|
|
(check-equal? (string->any "(or b (not a))") '(or b (not a)))
|
2020-02-22 12:18:37 +01:00
|
|
|
(check-equal? (string->any "14") 14)
|
2020-04-14 15:57:26 +02:00
|
|
|
(check-equal? (map-sexp add1 '(1 2 (4 10) 3)) '(2 3 (5 11) 4))
|
2020-03-02 18:42:54 +01:00
|
|
|
(check-equal? (read-org-sexp "((\"a\" \"(and a b)\") (\"b\" \"(or b (not a))\"))")
|
2020-05-06 23:03:15 +02:00
|
|
|
'((a (and a b)) (b (or b (not a)))))
|
2020-04-05 22:35:21 +02:00
|
|
|
(check-equal? (read-org-sexp "(#t \"#t\" \"#t \" '(1 2 \"#f\"))")
|
2020-05-06 23:03:15 +02:00
|
|
|
'(#t #t #t '(1 2 #f)))
|
2020-02-22 12:27:28 +01:00
|
|
|
(check-equal? (unstringify-pairs '(("a" . "1") ("b" . "(and a (not b))")))
|
2020-02-22 19:15:39 +01:00
|
|
|
'((a . 1) (b . (and a (not b)))))
|
2020-02-23 21:29:00 +01:00
|
|
|
(check-equal? (unstringify-pairs '(("a" . 1) ("b" . "(and a (not b))")))
|
|
|
|
'((a . 1) (b . (and a (not b)))))
|
2020-02-22 19:20:45 +01:00
|
|
|
(let ([m1 (read-org-variable-mapping "((\"a\" \"(and a b)\") (\"b\" \"(or b (not a))\"))")]
|
2020-02-22 19:48:39 +01:00
|
|
|
[m2 (read-org-variable-mapping "((\"a\" . \"(and a b)\") (\"b\" . \"(or b (not a))\"))")]
|
2020-03-25 00:16:29 +01:00
|
|
|
[m3 (unorgv "((\"a\" . \"(and a b)\") (\"b\" . \"(or b (not a))\"))")])
|
2020-02-22 19:20:45 +01:00
|
|
|
(check-equal? (hash-ref m1 'a) '(and a b))
|
|
|
|
(check-equal? (hash-ref m2 'a) '(and a b))
|
2020-02-22 19:48:39 +01:00
|
|
|
(check-equal? (hash-ref m3 'a) '(and a b))
|
2020-02-22 19:20:45 +01:00
|
|
|
(check-equal? (hash-ref m1 'b) '(or b (not a)))
|
2020-02-22 19:48:39 +01:00
|
|
|
(check-equal? (hash-ref m2 'b) '(or b (not a)))
|
2020-03-02 23:00:46 +01:00
|
|
|
(check-equal? (hash-ref m3 'b) '(or b (not a))))
|
2020-03-02 23:37:37 +01:00
|
|
|
(check-equal? (read-symbol-list "a b c") '(a b c))
|
|
|
|
(check-equal? (drop-first-last "(a b)") "a b")
|
|
|
|
(check-equal? (list-sets->list-strings (list (set 'x 'y) (set 'z) (set) (set 't)))
|
2020-03-02 23:59:37 +01:00
|
|
|
'("y x" "z" "" "t"))
|
|
|
|
(check-equal? (pretty-print-set-sets (set (set 'a 'b) (set 'c))) "{a b}{c}"))
|
2020-02-23 19:17:16 +01:00
|
|
|
|
|
|
|
(test-case "Additional graph utilities"
|
|
|
|
(let* ([gr1 (directed-graph '((a b) (b c)))]
|
|
|
|
[gr2 (undirected-graph '((a b) (b c)))]
|
|
|
|
[dbl (λ (x) (let ([x-str (symbol->string x)])
|
|
|
|
(string->symbol (string-append x-str x-str))))]
|
|
|
|
[new-gr1 (update-vertices/unweighted gr1 dbl)]
|
2020-02-29 17:21:56 +01:00
|
|
|
[new-gr2 (update-vertices/unweighted gr2 dbl)]
|
|
|
|
[new-gr1-ug (update-graph gr1 #:v-func dbl)]
|
|
|
|
[new-gr2-ug (update-graph gr2 #:v-func dbl)]
|
|
|
|
[gr3 (weighted-graph/directed '((10 a b) (11 b c)))]
|
|
|
|
[new-gr3 (update-graph gr3 #:v-func dbl #:e-func (λ (x) (* 2 x)))])
|
2020-02-23 19:17:16 +01:00
|
|
|
(check-false (has-vertex? new-gr1 'a))
|
|
|
|
(check-true (has-vertex? new-gr1 'aa))
|
|
|
|
(check-false (has-vertex? new-gr1 'b))
|
|
|
|
(check-true (has-vertex? new-gr1 'bb))
|
|
|
|
(check-false (has-vertex? new-gr1 'c))
|
|
|
|
(check-true (has-vertex? new-gr1 'cc))
|
|
|
|
(check-true (has-edge? new-gr1 'aa 'bb))
|
|
|
|
(check-true (has-edge? new-gr1 'bb 'cc))
|
|
|
|
|
|
|
|
(check-true (has-edge? new-gr2 'aa 'bb))
|
|
|
|
(check-true (has-edge? new-gr2 'bb 'aa))
|
|
|
|
(check-true (has-edge? new-gr2 'bb 'cc))
|
2020-02-29 17:21:56 +01:00
|
|
|
(check-true (has-edge? new-gr2 'cc 'bb))
|
|
|
|
|
|
|
|
(check-false (has-vertex? new-gr1-ug 'a))
|
|
|
|
(check-true (has-vertex? new-gr1-ug 'aa))
|
|
|
|
(check-false (has-vertex? new-gr1-ug 'b))
|
|
|
|
(check-true (has-vertex? new-gr1-ug 'bb))
|
|
|
|
(check-false (has-vertex? new-gr1-ug 'c))
|
|
|
|
(check-true (has-vertex? new-gr1-ug 'cc))
|
|
|
|
(check-true (has-edge? new-gr1-ug 'aa 'bb))
|
|
|
|
(check-true (has-edge? new-gr1-ug 'bb 'cc))
|
|
|
|
|
|
|
|
(check-true (has-edge? new-gr2-ug 'aa 'bb))
|
|
|
|
(check-true (has-edge? new-gr2-ug 'bb 'aa))
|
|
|
|
(check-true (has-edge? new-gr2-ug 'bb 'cc))
|
|
|
|
(check-true (has-edge? new-gr2-ug 'cc 'bb))
|
|
|
|
|
|
|
|
(check-true (has-edge? new-gr3 'aa 'bb))
|
|
|
|
(check-false (has-edge? new-gr3 'bb 'aa))
|
|
|
|
(check-true (has-edge? new-gr3 'bb 'cc))
|
|
|
|
(check-false (has-edge? new-gr3 'cc 'bb))
|
|
|
|
(check-equal? (edge-weight new-gr3 'aa 'bb) 20)
|
|
|
|
(check-equal? (edge-weight new-gr3 'bb 'cc) 22)))
|
2020-03-01 15:15:52 +01:00
|
|
|
|
|
|
|
(test-case "Pretty printing"
|
|
|
|
(check-equal? (pretty-print-set (set 'a 'b 1)) "1 a b"))
|
2020-03-01 16:53:17 +01:00
|
|
|
|
|
|
|
(test-case "Additional list utilties"
|
|
|
|
(let-values ([(e1 l1) (collect-by-key '((1 2) (1 3)) '(a b))]
|
2020-03-02 18:15:41 +01:00
|
|
|
[(e2 l2) (collect-by-key '((1 2) (1 2)) '(a b))]
|
|
|
|
[(e3 l3) (collect-by-key/sets '(a b a) '(1 2 1))])
|
2020-03-01 16:53:17 +01:00
|
|
|
(check-equal? e1 '((1 2) (1 3))) (check-equal? l1 '((a) (b)))
|
2020-03-02 18:15:41 +01:00
|
|
|
(check-equal? e2 '((1 2))) (check-equal? l2 '((b a)))
|
|
|
|
(check-equal? e3 '(a b)) (check-equal? l3 (list (set 1) (set 2))))
|
2020-03-02 18:17:05 +01:00
|
|
|
(check-equal? (ht-values/list->set #hash((a . (1 1))))
|
2020-03-21 19:16:24 +01:00
|
|
|
(hash 'a (set 1)))
|
|
|
|
(check-equal? (hash->list/ordered #hash((b . 1) (a . 1)))
|
2020-03-23 22:32:39 +01:00
|
|
|
'((a . 1) (b . 1)))
|
|
|
|
(let-values ([(l1 l2) (multi-split-at '((1 2 3) (a b c)) 2)])
|
2020-03-23 22:47:52 +01:00
|
|
|
(check-equal? l1 '((1 2) (a b))) (check-equal? l2 '((3) (c))))
|
|
|
|
(check-equal? (lists-transpose '((1 2) (a b))) '((1 a) (2 b))))
|
2020-03-15 16:12:05 +01:00
|
|
|
|
|
|
|
(test-case "Functions"
|
|
|
|
(check-true (procedure-fixed-arity? not))
|
|
|
|
(check-false (procedure-fixed-arity? +)))
|
2020-03-20 01:19:14 +01:00
|
|
|
|
|
|
|
(test-case "Randomness"
|
|
|
|
(begin
|
|
|
|
(random-seed 0)
|
|
|
|
(check-equal? (stream->list (stream-take (in-random 100) 10))
|
|
|
|
'(85 65 20 40 89 45 54 38 26 62))
|
|
|
|
(check-equal? (stream->list (stream-take (in-random 50 100) 10))
|
|
|
|
'(75 59 82 85 61 85 59 64 75 53))
|
|
|
|
(check-equal? (stream->list (stream-take (in-random) 10))
|
|
|
|
'(0.1656109603231493
|
|
|
|
0.9680391127132195
|
|
|
|
0.051518813640790355
|
|
|
|
0.755901955353936
|
|
|
|
0.5923534604277275
|
|
|
|
0.5513340634474264
|
|
|
|
0.7022057040731392
|
|
|
|
0.48375400938578744
|
|
|
|
0.7538961707172924
|
|
|
|
0.01828428516237329))))
|
2020-03-20 16:07:34 +01:00
|
|
|
|
|
|
|
(test-case "Additional stream utilities"
|
|
|
|
(check-equal? (stream->list (cartesian-product/stream (in-range 3) (in-range 4 6) '(a b)))
|
|
|
|
'((0 4 a)
|
|
|
|
(0 4 b)
|
|
|
|
(0 5 a)
|
|
|
|
(0 5 b)
|
|
|
|
(1 4 a)
|
|
|
|
(1 4 b)
|
|
|
|
(1 5 a)
|
|
|
|
(1 5 b)
|
|
|
|
(2 4 a)
|
|
|
|
(2 4 b)
|
|
|
|
(2 5 a)
|
|
|
|
(2 5 b))))
|