functional-search-tree-check.scm 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Mike Sperber
  3. (define-test-suite functional-search-trees-tests)
  4. (define (alist->search-tree = < l)
  5. (let loop ((st (make-search-tree = <))
  6. (l l))
  7. (if (null? l)
  8. st
  9. (loop (search-tree-insert st (caar l) (cdar l))
  10. (cdr l)))))
  11. (define-test-case simple functional-search-trees-tests
  12. (let* ((l (map (lambda (n) (cons n (+ n 1)))
  13. '(0 1 2 3 4 5 6 7 8 9)))
  14. (st (alist->search-tree = < l)))
  15. (for-each (lambda (p)
  16. (check-that (search-tree-ref st (car p))
  17. (is (cdr p))))
  18. l)
  19. (check-that (invalid-search-tree st) (is #f))))
  20. (define (random-unique-list count n)
  21. (let loop ((count count)
  22. (l '()))
  23. (if (zero? count)
  24. l
  25. (let ((x (random-integer n)))
  26. (if (memv x l)
  27. (loop count l)
  28. (loop (- count 1) (cons x l)))))))
  29. (define-test-case random functional-search-trees-tests
  30. (let* ((l (map (lambda (n) (cons n (+ n 1)))
  31. (random-unique-list 1000 100000)))
  32. (st (alist->search-tree = < l)))
  33. (for-each (lambda (p)
  34. (check-that (search-tree-ref st (car p))
  35. (is (cdr p))))
  36. l)
  37. (check-that (invalid-search-tree st) (is #f))))
  38. (define-test-case walk functional-search-trees-tests
  39. (let* ((l (map (lambda (n) (cons n (+ n 1)))
  40. (random-unique-list 1000 10000)))
  41. (st (alist->search-tree = < l)))
  42. (let ((c '()))
  43. (search-tree-walk (lambda (key val)
  44. (set! c (cons (cons key val) c)))
  45. st)
  46. (check-that (length c) (is (length l)))
  47. (for-each (lambda (p)
  48. (check (member p c)))
  49. l))))
  50. (define-test-case simple-delete functional-search-trees-tests
  51. (let ((l (map (lambda (n) (cons n (+ n 1)))
  52. '(0 1 2 3 4 5 6 7 8 9))))
  53. (let loop ((st (alist->search-tree = < l))
  54. (l l))
  55. (if (pair? l)
  56. (let ((st (search-tree-delete st (caar l))))
  57. (check-that (search-tree-ref st (caar l)) (is #f))
  58. (check-that (invalid-search-tree st) (is #f))
  59. (loop st (cdr l)))))))
  60. (define-test-case random-delete functional-search-trees-tests
  61. (let ((l (map (lambda (n) (cons n (+ n 1)))
  62. (random-unique-list 1000 10000))))
  63. (let loop ((st (alist->search-tree = < l))
  64. (l l))
  65. (if (pair? l)
  66. (let ((st (search-tree-delete st (caar l))))
  67. (check-that (search-tree-ref st (caar l)) (is #f))
  68. (check-that (invalid-search-tree st) (is #f))
  69. (loop st (cdr l)))))))