summaryrefslogtreecommitdiffstats
path: root/wttest.scm
blob: 4fa8d5a8e6f8e55fe14fc38452f23cef9e9f26d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
;;;  "wttest.scm" Test Weight balanced trees		-*-Scheme-*-
;;;  Copyright (c) 1993-1994 Stephen Adams
;;;
;;; Copyright (c) 1993-94 Massachusetts Institute of Technology
;;;
;;; This material was developed by the Scheme project at the
;;; Massachusetts Institute of Technology, Department of Electrical
;;; Engineering and Computer Science.  Permission to copy and modify
;;; this software, to redistribute either the original software or a
;;; modified version, and to use this software for any purpose is
;;; granted, subject to the following restrictions and understandings.
;;;
;;; 1. Any copy made of this software must include this copyright
;;; notice in full.
;;;
;;; 2. Users of this software agree to make their best efforts (a) to
;;; return to the MIT Scheme project any improvements or extensions
;;; that they make, so that these may be included in future releases;
;;; and (b) to inform MIT of noteworthy uses of this software.
;;;
;;; 3. All materials developed as a consequence of the use of this
;;; software shall duly acknowledge such use, in accordance with the
;;; usual standards of acknowledging credit in academic research.
;;;
;;; 4. MIT has made no warranty or representation that the operation
;;; of this software will be error-free, and MIT is under no
;;; obligation to provide any services, by way of maintenance, update,
;;; or otherwise.
;;;
;;; 5. In conjunction with products arising from the use of this
;;; material, there shall be no use of the name of the Massachusetts
;;; Institute of Technology nor of any adaptation thereof in any
;;; advertising, promotional, or sales literature without prior
;;; written consent from MIT in each case.

(require 'wt-tree)

;;  Test code, using maps from digit strings to the numbers they represent.

(define (wt-test)

  (define (make-map lo hi step)
    (let loop ((i lo) (map (make-wt-tree string-wt-type)))
      (if (> i hi)
          map
          (loop (+ i step) (wt-tree/add map (number->string i) i)))))

  (define (wt-tree->alist t)
    (wt-tree/fold (lambda (key datum rest) (cons (cons key datum) rest)) '() t))

  (define (try-all operation trees)
    (map (lambda (t1)
           (map (lambda (t2)
                  (operation t1 t2))
                trees))
         trees))

  (define (chunk tree)
    (let ((size  (wt-tree/size tree)))
      (if (< size 8)
          size
          (let* ((midpoint (if (even? size)
                               (/ size 2)
                               (/ (+ size 1) 2)))
                 (fulcrum  (wt-tree/index tree midpoint)))
            (list (chunk (wt-tree/split< tree fulcrum))
                  (list fulcrum)
                  (chunk (wt-tree/split> tree fulcrum)))))))

  (define (verify name result expected)
    (newline)
    (display "Test ") (display name)
    (if (equal? result expected)
        (begin
          (display " passed"))
        (begin
          (display " unexpected result")
          (newline)
          (display "Expected: " expected)
          (newline)
          (display "Got:      " result))))

  (let ((t1 (make-map 0 99 2))          ; 0,2,4,...,98
        (t2 (make-map 1 100 2))         ; 1,3,5,...,99
        (t3 (make-map 0 100 3)))        ; 0,3,6,...,99


    (verify 'alist (wt-tree->alist t3)  ;
            '(("0" . 0) ("12" . 12) ("15" . 15) ("18" . 18) ("21" . 21)
              ("24" . 24) ("27" . 27) ("3" . 3) ("30" . 30) ("33" . 33)
              ("36" . 36) ("39" . 39) ("42" . 42) ("45" . 45) ("48" . 48)
              ("51" . 51) ("54" . 54) ("57" . 57) ("6" . 6) ("60" . 60)
              ("63" . 63) ("66" . 66) ("69" . 69) ("72" . 72) ("75" . 75)
              ("78" . 78) ("81" . 81) ("84" . 84) ("87" . 87) ("9" . 9)
              ("90" . 90) ("93" . 93) ("96" . 96) ("99" . 99)))


    (verify 'union-sizes
            (try-all (lambda (t1 t2) (wt-tree/size (wt-tree/union t1 t2)))
                     (list t1 t2 t3))
            '((50 100 67) (100 50 67) (67 67 34)))

    (verify 'difference-sizes
            (try-all (lambda (t1 t2)
                       (wt-tree/size (wt-tree/difference t1 t2)))
                     (list t1 t2 t3))
            '((0 50 33) (50 0 33) (17 17 0)))

    (verify 'intersection-sizes
            (try-all (lambda (t1 t2)
                       (wt-tree/size (wt-tree/intersection t1 t2)))
                     (list t1 t2 t3))
            '((50 0 17) (0 50 17) (17 17 34)))

    (verify 'equalities
            (try-all (lambda (t1 t2)
                       (wt-tree/set-equal? (wt-tree/difference t1 t2)
                                           (wt-tree/difference t2 t1)))
                     (list t1 t2 t3))
            '((#t #f #f) (#f #t #f) (#f #f #t)))

    (verify 'indexing
            (chunk (make-map 0 99 1))
            '((((7 ("15") 5) ("20") (6 ("27") 4)) ("31")
               ((6 ("38") 5) ("43") (6 ("5") 4)))
              ("54")
              (((7 ("61") 5) ("67") (6 ("73") 4)) ("78")
               ((6 ("84") 5) ("9") (5 ("95") 4)))))
    (newline)))

(wt-test)

;;; Local Variables:
;;; eval: (put 'with-n-node 'scheme-indent-function 1)
;;; eval: (put 'with-n-node 'scheme-indent-hook 1)
;;; End: