dds/utils-tests.rkt
2020-02-23 19:17:16 +01:00

83 lines
3.4 KiB
Racket

#lang racket
;;; Tests for dds/utils.
(require rackunit graph "utils.rkt")
(test-case "HashTable Injection"
(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))
#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)))
5)))
(test-case "eval-with"
(check-equal? (let ([ht #hash((a . 1) (b . 1))])
(eval-with ht '(+ b a 1)))
3)))
(test-case "Analysis of quoted expressions"
(check-equal? (extract-symbols '(1 (2 3) x (y z 3)))
'(x y z)))
(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))")
(check-equal? (any->string "hello") "hello")
(let ([mp (stringify-variable-mapping #hash((a . (and a b)) (b . (not b))))])
(check-equal? (hash-ref mp 'a) "(and a b)")
(check-equal? (hash-ref mp 'b) "(not b)"))
(let ([mp (sgfy #hash((a . (and a b)) (b . (not b))))])
(check-equal? (hash-ref mp 'a) "(and a b)")
(check-equal? (hash-ref mp 'b) "(not b)"))
(check-equal? (string->any "(or b (not a))") '(or b (not a)))
(check-equal? (string->any "14") 14)
(check-equal? (read-org-table "((\"a\" \"(and a b)\") (\"b\" \"(or b (not a))\"))")
'(("a" "(and a b)") ("b" "(or b (not a))")))
(check-equal? (unstringify-pairs '(("a" . "1") ("b" . "(and a (not b))")))
'((a . 1) (b . (and a (not b)))))
(let ([m1 (read-org-variable-mapping "((\"a\" \"(and a b)\") (\"b\" \"(or b (not a))\"))")]
[m2 (read-org-variable-mapping "((\"a\" . \"(and a b)\") (\"b\" . \"(or b (not a))\"))")]
[m3 (unorg "((\"a\" . \"(and a b)\") (\"b\" . \"(or b (not a))\"))")])
(check-equal? (hash-ref m1 'a) '(and a b))
(check-equal? (hash-ref m2 'a) '(and a b))
(check-equal? (hash-ref m3 'a) '(and a b))
(check-equal? (hash-ref m1 'b) '(or b (not a)))
(check-equal? (hash-ref m2 'b) '(or b (not a)))
(check-equal? (hash-ref m3 'b) '(or b (not a)))))
(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)]
[new-gr2 (update-vertices/unweighted gr2 dbl)])
(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))
(check-true (has-edge? new-gr2 'cc 'bb))))