diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-07-10 01:10:08 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-07-10 01:10:08 +0100 |
commit | b662f16eff306ef70809d55673dbb2fa126c815d (patch) | |
tree | 7e2d04a61e07aabd342585181b1efb5ca07b324c | |
parent | 3592ea3134cbda63d164c158054a6b18ece9ab49 (diff) | |
download | algorithms-b662f16eff306ef70809d55673dbb2fa126c815d.tar.gz algorithms-b662f16eff306ef70809d55673dbb2fa126c815d.tar.bz2 algorithms-b662f16eff306ef70809d55673dbb2fa126c815d.zip |
(powerset)+some tests for powerset
-rw-r--r-- | powerset.rkt | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/powerset.rkt b/powerset.rkt index a746f90..bbece52 100644 --- a/powerset.rkt +++ b/powerset.rkt @@ -22,3 +22,48 @@ (map (lambda (n) (subsets-of lst n)) ; get subset of size n (range 0 (+ 1 (length lst)))))) ; n from 0 to |lst| + +(define (test? name expected got [equality equal?]) + ;; Print success of test (i.e. (equality expected got)) and return a + ;; boolean representing if it worked. + (printf "[TEST ~a]: " name) + (if (equality expected got) + (begin + (displayln "Success") + #t) + (begin + (printf "Failure (expected=~a, got=~a)~n" expected got) + #f))) + +(define-syntax (perform-tests stx) + (with-syntax + ([t-clauses + (datum->syntax + stx + (map + (lambda (clause) + (syntax-case clause () + [(name expected got) + #'(test? 'name expected got)])) + (cdr (syntax->list stx))))]) + #'(and (~@ . t-clauses)))) + +(let ([small (range 10)] + [medium (range 50)] + [large (range 100)]) + (perform-tests + ;; Base case on lst + ("subsets-of->lst=null->n=0" 0 (length (subsets-of null 0))) + ("subsets-of->lst=null->n=1024" 0 (length (subsets-of null 1024))) + ;; Base case on n + ("subsets-of->lst=[10]->n=0" 1 (length (subsets-of small 0))) + ("subsets-of->lst=[50]->n=0" 1 (length (subsets-of medium 0))) + ("subsets-of->lst=[100]->n=0" 1 (length (subsets-of large 0))) + ;; Singletons + ("subsets-of->lst=[10]->n=1" 10 (length (subsets-of small 1))) + ("subsets-of->lst=[50]->n=1" 50 (length (subsets-of medium 1))) + ("subsets-of->lst=[100]->n=1" 100 (length (subsets-of large 1))) + ;; Binomial theorem tests (symmetry along the middle of Pascal's triangle) + ("subsets-of->lst=[10]->n=2~~n=8" (length (subsets-of small 2)) (length (subsets-of small 8))) + ("subsets-of->lst=[10]->n=3~~n=7" (length (subsets-of small 3)) (length (subsets-of small 7))) + ("subsets-of->lst=[10]->n=4~~n=6" (length (subsets-of small 4)) (length (subsets-of small 6))))) |