utils: Move the tests into the test submodule.

This commit is contained in:
Sergiu Ivanov 2020-05-16 23:09:00 +02:00
parent 95c0611e4f
commit ff9189270e
2 changed files with 204 additions and 170 deletions

View File

@ -1,170 +0,0 @@
#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)"))
(check-equal? (string->any "(or b (not a))") '(or b (not a)))
(check-equal? (string->any "14") 14)
(check-equal? (map-sexp add1 '(1 2 (4 10) 3)) '(2 3 (5 11) 4))
(check-equal? (read-org-sexp "((\"a\" \"(and a b)\") (\"b\" \"(or b (not a))\"))")
'((a (and a b)) (b (or b (not a)))))
(check-equal? (read-org-sexp "(#t \"#t\" \"#t \" '(1 2 \"#f\"))")
'(#t #t #t '(1 2 #f)))
(check-equal? (unstringify-pairs '(("a" . "1") ("b" . "(and a (not b))")))
'((a . 1) (b . (and a (not b)))))
(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 (unorgv "((\"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))))
(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)))
'("y x" "z" "" "t"))
(check-equal? (pretty-print-set-sets (set (set 'a 'b) (set 'c))) "{a b}{c}"))
(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)]
[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)))])
(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))
(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)))
(test-case "Pretty printing"
(check-equal? (pretty-print-set (set 'a 'b 1)) "1 a b"))
(test-case "Additional list utilties"
(let-values ([(e1 l1) (collect-by-key '((1 2) (1 3)) '(a b))]
[(e2 l2) (collect-by-key '((1 2) (1 2)) '(a b))]
[(e3 l3) (collect-by-key/sets '(a b a) '(1 2 1))])
(check-equal? e1 '((1 2) (1 3))) (check-equal? l1 '((a) (b)))
(check-equal? e2 '((1 2))) (check-equal? l2 '((b a)))
(check-equal? e3 '(a b)) (check-equal? l3 (list (set 1) (set 2))))
(check-equal? (ht-values/list->set #hash((a . (1 1))))
(hash 'a (set 1)))
(check-equal? (hash->list/ordered #hash((b . 1) (a . 1)))
'((a . 1) (b . 1)))
(let-values ([(l1 l2) (multi-split-at '((1 2 3) (a b c)) 2)])
(check-equal? l1 '((1 2) (a b))) (check-equal? l2 '((3) (c))))
(check-equal? (lists-transpose '((1 2) (a b))) '((1 a) (2 b))))
(test-case "Functions"
(check-true (procedure-fixed-arity? not))
(check-false (procedure-fixed-arity? +)))
(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))))
(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))))

204
utils.rkt
View File

@ -54,6 +54,10 @@
;; Syntax
auto-hash-ref/explicit auto-hash-ref/:)
(module+ test
(require rackunit))
;;; ===================
;;; HashTable Injection
;;; ===================
@ -81,6 +85,16 @@
#`[#,x (hash-ref ht '#,x)])
body)]))
(module+ test
(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)))
;;; Given an expression and a (HashTable Symbol a), looks up the
;;; symbols with a leading semicolon and binds them to the value they
;;; are associated to in the hash table.
@ -103,6 +117,16 @@
(hash-ref ht '#,(strip-colon x))])
body))]))
(module+ test
(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)))
;;; The helper functions for auto-hash-ref/:.
(begin-for-syntax
;; Collect all the symbols starting with a colon in datum.
@ -148,6 +172,11 @@
(for ([(x val) ht]) (namespace-set-variable-value! x val))
(eval expr)))
(module+ test
(check-equal? (let ([ht #hash((a . 1) (b . 1))])
(eval-with ht '(+ b a 1)))
3))
;;; Same as eval-with, but returns only the first value produced by
;;; the evaluated expression.
(define (eval-with1 ht expr)
@ -169,6 +198,10 @@
(extract-symbols x)))]
[else '()]))
(module+ test
(check-equal? (extract-symbols '(1 (2 3) x (y z 3)))
'(x y z)))
;;; =========================
;;; Interaction with Org-mode
@ -187,6 +220,11 @@
(define (any->string x)
(with-output-to-string (λ () (display x))))
(module+ test
(check-equal? (any->string 'a) "a")
(check-equal? (any->string '(a 1 (x y))) "(a 1 (x y))")
(check-equal? (any->string "hello") "hello"))
;;; A string variable mapping is a mapping from variables to strings.
(define (string-variable-mapping? dict) (hash/c symbol? string?))
@ -194,10 +232,19 @@
(define (stringify-variable-mapping ht)
(for/hash ([(key val) ht]) (values key (any->string val))))
(module+ test
(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)")))
;;; Reads any value from string.
(define (string->any str)
(with-input-from-string str (λ () (read))))
(module+ test
(check-equal? (string->any "(or b (not a))") '(or b (not a)))
(check-equal? (string->any "14") 14))
;;; Given a sexp, converts all "#f" to #f and "#t" to #t.
;;;
;;; When I read Org-mode tables, I pump them through a call to the
@ -222,6 +269,9 @@
[(? list?) (map ((curry map-sexp) func) sexp)]
[datum (func datum)]))
(module+ test
(check-equal? (map-sexp add1 '(1 2 (4 10) 3)) '(2 3 (5 11) 4)))
;;; Reads a sexp from a string produced by Org-mode for a named table.
;;; See example.org for examples.
(define read-org-sexp
@ -233,6 +283,12 @@
;;; A shortcut for read-org-sexp.
(define unorg read-org-sexp)
(module+ test
(check-equal? (read-org-sexp "((\"a\" \"(and a b)\") (\"b\" \"(or b (not a))\"))")
'((a (and a b)) (b (or b (not a)))))
(check-equal? (read-org-sexp "(#t \"#t\" \"#t \" '(1 2 \"#f\"))")
'(#t #t #t '(1 2 #f))))
;;; A contract allowing pairs constructed via cons or via list.
(define (general-pair/c key-contract val-contract)
(or/c (list/c key-contract val-contract)
@ -254,11 +310,28 @@
(string->any val)
val))])))
(module+ test
(check-equal? (unstringify-pairs '(("a" . "1") ("b" . "(and a (not b))")))
'((a . 1) (b . (and a (not b)))))
(check-equal? (unstringify-pairs '(("a" . 1) ("b" . "(and a (not b))")))
'((a . 1) (b . (and a (not b))))))
;;; Reads a variable mapping from a string, such as the one which
;;; Org-mode produces from tables.
(define read-org-variable-mapping
(compose make-immutable-hash unstringify-pairs string->any))
(module+ test
(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 (unorgv "((\"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)))))
;;; A synonym for read-org-variable-mapping.
(define unorgv read-org-variable-mapping)
@ -269,6 +342,9 @@
(define (read-symbol-list str)
(string->any (string-append "(" str ")")))
(module+ test
(check-equal? (read-symbol-list "a b c") '(a b c)))
;;; Removes the first and the last symbol of a given string.
;;;
;;; Useful for removing the parentheses in string representations of
@ -276,11 +352,18 @@
(define (drop-first-last str)
(substring str 1 (- (string-length str) 1)))
(module+ test
(check-equal? (drop-first-last "(a b)") "a b"))
;;; Converts a list of sets of symbols to a list of strings containing
;;; those symbols.
(define (list-sets->list-strings lst)
(map (compose drop-first-last any->string set->list) lst))
(module+ test
(check-equal? (list-sets->list-strings (list (set 'x 'y) (set 'z) (set) (set 't)))
'("y x" "z" "" "t")))
;;; Pretty-prints a set of sets of symbols.
;;;
;;; Typically used for pretty-printing the annotations on the edges of
@ -288,6 +371,9 @@
(define (pretty-print-set-sets ms)
(string-join (for/list ([m ms]) (format "{~a}" (pretty-print-set m))) ""))
(module+ test
(check-equal? (pretty-print-set-sets (set (set 'a 'b) (set 'c))) "{a b}{c}"))
;;; ==========================
;;; Additional graph utilities
@ -307,6 +393,27 @@
(match-let ([(list u v) e])
(list (func u) (func v))))))
(module+ test
(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))))
;;; Given a graph, apply a transformation v-func to every vertex label
;;; and, if the graph is a weighted graph, the transformation e-func
;;; to every edge label. Both transformations default to identity
@ -328,6 +435,36 @@
[else
(weighted-graph/directed edges)]))
(module+ test
(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-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)))])
(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)))
;;; ===============
;;; Pretty printing
@ -337,6 +474,9 @@
(define (pretty-print-set s)
(string-join (sort (set-map s any->string) string<?)))
(module+ test
(check-equal? (pretty-print-set (set 'a 'b 1)) "1 a b"))
;;; =========================
;;; Additional list utilities
@ -354,20 +494,38 @@
([e edges] [l labels])
(hash-update ht e (λ (ls) (cons l ls)) empty)))
(module+ test
(let-values ([(e1 l1) (collect-by-key '((1 2) (1 3)) '(a b))]
[(e2 l2) (collect-by-key '((1 2) (1 2)) '(a b))])
(check-equal? e1 '((1 2) (1 3))) (check-equal? l1 '((a) (b)))
(check-equal? e2 '((1 2))) (check-equal? l2 '((b a)))))
;;; Like collect-by-key, but returns a list of sets of weights.
(define (collect-by-key/sets edges labels)
(let-values ([(es ls) (collect-by-key edges labels)])
(values es (map list->set ls))))
(module+ test
(let-values ([(e3 l3) (collect-by-key/sets '(a b a) '(1 2 1))])
(check-equal? e3 '(a b)) (check-equal? l3 (list (set 1) (set 2)))))
;;; Converts the values of a hash table from lists to sets.
(define (ht-values/list->set ht)
(for/hash ([(k v) (in-hash ht)])
(values k (list->set v))))
(module+ test
(check-equal? (ht-values/list->set #hash((a . (1 1))))
(hash 'a (set 1))))
;;; Returns the key-value pairs of a given hash table in the order in
;;; which the hash table orders them for hash-map and hash-for-each.
(define (hash->list/ordered ht) (hash-map ht cons #t))
(module+ test
(check-equal? (hash->list/ordered #hash((b . 1) (a . 1)))
'((a . 1) (b . 1))))
;;; Given a list of lists, splits every single list at the given
;;; position, and then returns two lists: one consisting of the first
;;; halves, and the one consisting of the second halves.
@ -379,6 +537,10 @@
(match (foldr split-1 (cons '() '()) lsts)
[(cons lefts rights) (values lefts rights)]))
(module+ test
(let-values ([(l1 l2) (multi-split-at '((1 2 3) (a b c)) 2)])
(check-equal? l1 '((1 2) (a b))) (check-equal? l2 '((3) (c)))))
;;; Given a list of lists of the same length, transposes them.
;;;
;;; > (lists-transpose '((1 2) (a b)))
@ -391,6 +553,9 @@
in-values-sequence
((curry apply) in-parallel)))
(module+ test
(check-equal? (lists-transpose '((1 2) (a b))) '((1 a) (2 b))))
;;; =========
;;; Functions
@ -402,11 +567,18 @@
(match (procedure-arity func)
[(arity-at-least _) #f] [arity #t]))
(module+ test
(check-true (procedure-fixed-arity? not))
(check-false (procedure-fixed-arity? +)))
;;; ==========
;;; Randomness
;;; ==========
(module+ test
(random-seed 0))
;;; Generates a stream of inexact random numbers. The meaning of the
;;; arguments is the same as for the function random:
;;;
@ -424,6 +596,23 @@
[(k) (for/stream ([i (in-naturals)]) (random k))]
[(min max) (for/stream ([i (in-naturals)]) (random min max))]))
(module+ test
(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)))
;;; ===========================
;;; Additional stream utilities
@ -443,3 +632,18 @@
;; 1-value stream containing the empty list, which makes all the
;; lists proper.
(foldr cp-2 (sequence->stream (in-value (list))) ss))
(module+ test
(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))))