diff --git a/collects/net/private/rbtree.rkt b/collects/net/private/rbtree.rkt index 52aec1a71c..e7621670cb 100644 --- a/collects/net/private/rbtree.rkt +++ b/collects/net/private/rbtree.rkt @@ -273,108 +273,106 @@ (loop (tree-right t) (+ d (kv (tree-v t))))) null))) -#| -Tests: +(module+ test + (require racket/pretty) + (print-struct #t) -(require rbtree racket/pretty) -(print-struct #t) + (define t (new-tree)) -(define t (new-tree)) -(define (test n v) - (expunge-insert! t n) - (unless (equal? (expunge-tree->list t) v) - (error 'bad "~s != ~s" (tree->list t) v))) + (define (test n v) + (expunge-insert! t n) + (unless (equal? (expunge-tree->list t) v) + (error 'bad "~s != ~s" (expunge-tree->list t) v))) -(test 12 '(12)) -(test 8 '(8 12)) -(test 1 '(1 8 12)) + (test 12 '(12)) + (test 8 '(8 12)) + (test 1 '(1 8 12)) -(define t (new-tree)) + (set! t (new-tree)) -(test 10 '(10)) -(test 8 '(8 10)) -(test 10 '(8 10 12)) -(test 8 '(8 9 10 12)) -(test 8 '(8 9 10 11 12)) -(test 100 '(8 9 10 11 12 105)) -(test 1 '(1 8 9 10 11 12 105)) -(test 105 '(1 8 9 10 11 12 105 112)) -(test 99 '(1 8 9 10 11 12 105 106 112)) -(test 2 '(1 3 8 9 10 11 12 105 106 112)) -(test 6 '(1 3 8 9 10 11 12 13 105 106 112)) -(test 5 '(1 3 7 8 9 10 11 12 13 105 106 112)) -(test 15 '(1 3 7 8 9 10 11 12 13 24 105 106 112)) -(test 15 '(1 3 7 8 9 10 11 12 13 24 25 105 106 112)) -(test 15 '(1 3 7 8 9 10 11 12 13 24 25 26 105 106 112)) + (test 10 '(10)) + (test 8 '(8 10)) + (test 10 '(8 10 12)) + (test 8 '(8 9 10 12)) + (test 8 '(8 9 10 11 12)) + (test 100 '(8 9 10 11 12 105)) + (test 1 '(1 8 9 10 11 12 105)) + (test 105 '(1 8 9 10 11 12 105 112)) + (test 99 '(1 8 9 10 11 12 105 106 112)) + (test 2 '(1 3 8 9 10 11 12 105 106 112)) + (test 6 '(1 3 8 9 10 11 12 13 105 106 112)) + (test 5 '(1 3 7 8 9 10 11 12 13 105 106 112)) + (test 15 '(1 3 7 8 9 10 11 12 13 24 105 106 112)) + (test 15 '(1 3 7 8 9 10 11 12 13 24 25 105 106 112)) + (test 15 '(1 3 7 8 9 10 11 12 13 24 25 26 105 106 112)) -(define t (new-tree)) -(define (test n v) - (cond - [(< n 0) (fetch-delete! t (- n))] - [(inexact? n) (fetch-shift! t (inexact->exact n))] - [else (fetch-insert! t (list n))]) - (printf "Check ~a\n" v) - (let ([v (map list v)]) - (unless (equal? (fetch-tree->list t) v) - (error 'bad "~s != ~s" (fetch-tree->list t) v)))) + (set! t (new-tree)) -(test 10 '(10)) -(test 12 '(10 12)) -(test 8 '(8 10 12)) -(test 10 '(8 10 12)) -(test -10 '(8 12)) -(test -10 '(8 12)) -(test 10.0 '(8 11)) -(test 100.0 '(8 11)) -(test 5.0 '(7 10)) -(test 1 '(1 7 10)) -(test 2 '(1 2 7 10)) -(test 3 '(1 2 3 7 10)) -(test 4 '(1 2 3 4 7 10)) -(test 5 '(1 2 3 4 5 7 10)) -(test 6 '(1 2 3 4 5 6 7 10)) -(test -6 '(1 2 3 4 5 7 10)) -(test -5 '(1 2 3 4 7 10)) -(test -4 '(1 2 3 7 10)) -(test -3 '(1 2 7 10)) -(test -2 '(1 7 10)) -(test -7 '(1 10)) -(test -1 '(10)) -(test -10 '()) + (define (test2 n v) + (cond + [(< n 0) (fetch-delete! t (- n))] + [(inexact? n) (fetch-shift! t (inexact->exact n))] + [else (fetch-insert! t (list n))]) + ;; (printf "Check ~a\n" v) + (let ([v (map list v)]) + (unless (equal? (fetch-tree->list t) v) + (error 'bad "~s != ~s" (fetch-tree->list t) v)))) -(define (in-all-positions n l) - (if (null? l) - (list (list n)) - (cons - (cons n l) - (map (lambda (r) (cons (car l) r)) - (in-all-positions n (cdr l)))))) + (test2 10 '(10)) + (test2 12 '(10 12)) + (test2 8 '(8 10 12)) + (test2 10 '(8 10 12)) + (test2 -10 '(8 12)) + (test2 -10 '(8 12)) + (test2 10.0 '(8 11)) + (test2 100.0 '(8 11)) + (test2 5.0 '(7 10)) + (test2 1 '(1 7 10)) + (test2 2 '(1 2 7 10)) + (test2 3 '(1 2 3 7 10)) + (test2 4 '(1 2 3 4 7 10)) + (test2 5 '(1 2 3 4 5 7 10)) + (test2 6 '(1 2 3 4 5 6 7 10)) + (test2 -6 '(1 2 3 4 5 7 10)) + (test2 -5 '(1 2 3 4 7 10)) + (test2 -4 '(1 2 3 7 10)) + (test2 -3 '(1 2 7 10)) + (test2 -2 '(1 7 10)) + (test2 -7 '(1 10)) + (test2 -1 '(10)) + (test2 -10 '()) -(define (permutations l) - (if (or (null? l) - (null? (cdr l))) - (list l) - (apply - append - (map (lambda (lol) - (in-all-positions (car l) lol)) - (permutations (cdr l)))))) + (define (in-all-positions n l) + (if (null? l) + (list (list n)) + (cons + (cons n l) + (map (lambda (r) (cons (car l) r)) + (in-all-positions n (cdr l)))))) -(define perms (permutations '(1 2 3 4 5 6 7 8))) + (define (permutations l) + (if (or (null? l) + (null? (cdr l))) + (list l) + (apply + append + (map (lambda (lol) + (in-all-positions (car l) lol)) + (permutations (cdr l)))))) -(for-each (lambda (l) - (let ([t (new-tree)]) - (for-each (lambda (i) - (fetch-insert! t (list i))) - l) - (unless (equal? (fetch-tree->list t) '((1) (2) (3) (4) (5) (6) (7) (8))) - (error 'perms "bad: ~a" l)) - (for-each (lambda (i) - (fetch-delete! t i)) - l) - (unless (equal? (fetch-tree->list t) '()) - (error 'perms "remove bad: ~a" l)))) - perms) + (define perms (permutations '(1 2 3 4 5 6 7 8))) -|# + (for-each (lambda (l) + (let ([t (new-tree)]) + (for-each (lambda (i) + (fetch-insert! t (list i))) + l) + (unless (equal? (fetch-tree->list t) '((1) (2) (3) (4) (5) (6) (7) (8))) + (error 'perms "bad: ~a" l)) + (for-each (lambda (i) + (fetch-delete! t i)) + l) + (unless (equal? (fetch-tree->list t) '()) + (error 'perms "remove bad: ~a" l)))) + perms))