dds/networks-tests.rkt
Sergiu Ivanov cf19859be8 Rewrite bn without types, rename it to networks.
I tried generalising my code from Boolean networks to any kinds of
networks, but got tired of having to fight the type system (for now at
least). The last drop was the fact that the graph library does not
seem to have a typed version. So I decided to go with contracts
instead.
2020-02-20 00:56:30 +01:00

46 lines
1.7 KiB
Racket

#lang racket
;;; Tests for dds/networks.
(require rackunit "networks.rkt")
;;; This test case sets up the following Boolean network:
;;; x1 = x1 AND NOT x2
;;; x2 = NOT x2
(test-case "Basic definitions"
(let* ([f1 (λ (s)
(let ([x1 (hash-ref s 'x1)]
[x2 (hash-ref s 'x2)])
(and x1 (not x2))))]
[f2 (λ (s)
(let ([x2 (hash-ref s 'x2)])
(not x2)))]
[bn (make-network-from-functions `((x1 . ,f1) (x2 . ,f2)))])
(test-case "One-step syncronous update"
(let* ([s (make-state '((x1 . #t) (x2 . #f)))]
[new-s (update bn s '(x2 x1))])
(check-equal? (hash-ref new-s 'x1) #t)
(check-equal? (hash-ref new-s 'x2) #t)
(check-equal? (length (hash-keys new-s)) 2)))
(test-case "One-step asynchronous update"
(let* ([s (make-state '((x1 . #f) (x2 . #f)))]
[new-s (update bn s '(x2 x1))])
(check-equal? (hash-ref new-s 'x1) #f)
(check-equal? (hash-ref new-s 'x2) #t)
(check-equal? (length (hash-keys new-s)) 2)))))
(test-case "Syntactic description of Boolean networks"
(let ([s (make-state '((x . #t) (y . #f)))]
[f (update-function-form->update-function '(and x y))])
(check-equal? (f s) #f))
(let ([bn1 (network-form->network (make-hash '((a . (and a b)) (b . (not b)))))]
[bn2 (make-network-from-forms '((a . (and a b))
(b . (not b))))]
[bn3 (nn '((a . (and a b))
(b . (not b))))]
[s (st '((a . #t) (b . #t)))])
(check-equal? ((hash-ref bn1 'a) s) #t)
(check-equal? ((hash-ref bn2 'a) s) #t)
(check-equal? ((hash-ref bn3 'a) s) #t)))