aboutsummaryrefslogtreecommitdiffstats
path: root/csc/hash-map-test.csc
blob: 788590885c24b26be3ded0035bbb515d1edd40c4 (plain) (blame)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
(import (scheme base)
        (only (csc format)
          sprintf)
        (only (csc loop)
          loop
          return)
        (only (csc sort) sort)
        (only (csc testing)
              assert-equal
              assert-raises
              test)
        (csc hash-map))


(define (hash-symbol s)
  (hash-bytevector (string->utf8 (symbol->string s))))


(define (cmp-symbols s1 s2)
  (cond
    ((symbol=? s1 s2) 0)
    ((string<? (symbol->string s1) (symbol->string s2)) -1)
    (else 1)))


(define (alist->map->alist l)
  (map->alist (alist->map hash-symbol cmp-symbols l)))


(test map->alist-singleton
  (assert-equal
    '((a . 1))
    (alist->map->alist '((a . 1)))))


(test map->alist-two
  (assert-equal
    '((a . 1) (b . 2))
    (alist->map->alist '((a . 1) (b . 2))))


(test map->alist-longer
  (assert-equal
    '((a . 1) (b . 2) (c . 3) (d . 4) (e . 5) (f . 6))
    (alist->map->alist '((a . 1) (b . 2) (c . 3) (d . 4) (e . 5) (f . 6))))))


(test map->alist-larger
  (assert-equal
    '((f . 5) (m . 1) (n . 7) (q . 3) (x . 8))
    (alist->map->alist '((m . 1) (n . 2) (q . 3) (f . 5) (n . 7) (x . 8)))))


(test map->alist-in-order
  (assert-equal
    '((a . ()) (b . ()) (c . ()) (d . ()) (e . ()) (f . ()) (g . ()) (h . ()))
    (alist->map->alist '((a . ()) (b . ()) (c . ()) (d . ()) (e . ()) (f . ()) (g . ()) (h . ())))))


(test map->alist-reversed
  (assert-equal
    '((h . ()) (g . ()) (f . ()) (e . ()) (d . ()) (c . ()) (b . ()) (a . ()))
    (alist->map->alist '((a . ()) (b . ()) (c . ()) (d . ()) (e . ()) (f . ()) (g . ()) (h . ())))))


(test map->alist-overwrite
  (assert-equal
    '((a . 2))
    (alist->map->alist '((a . 1) (a . 2)))))


(test map->alist-alternating
  (assert-equal
    '((h . ()) (g . ()) (i . ()) (f . ()) (j . ()) (e . ()) (k . ()) (d . ()) (l . ()) (c . ()))
    (alist->map->alist '((c . ()) (d . ()) (e . ()) (f . ()) (g . ()) (h . ()) (i . ()) (j . ()) (k . ()) (l . ())))))


(test lookup
  (assert-equal
    2
    (lookup (alist->map hash-symbol cmp-symbols '((a . 1) (b . 2) (c . 3))) 'b)))


(test lookup-notfound
  (assert-raises key-not-found-error?
    (lookup (alist->map hash-symbol cmp-symbols '((a . 1) (b . 2) (c . 3))) 'd)))


(test lookup-default
  (assert-equal
    #f
    (lookup (alist->map hash-symbol cmp-symbols '((a . #t) (b . #t))) 'c #f)))


(define transform-map
  (list
    (cons map? map->alist)))


(define (test-map . bindings)
  (alist->map hash-symbol cmp-symbols bindings))


(test merge
  (assert-equal
    '((a . 1) (b . 2) (c . 3) (d . 4))
    (merge
      (test-map '(a . 1) '(b . 2))
      (test-map '(c . 3) '(d . 4)))
    transform-map))


(test delete
  (assert-equal
    '((a . 1) (b . 2) (d . 4))
    (delete
      (test-map '(a . 1) '(b . 2) '(c . 3) '(d . 4))
      'c)
    transform-map))


(test delete-only
  (assert-equal
    '()
    (delete
      (test-map '(a . 1))
      'a)
    transform-map))


(test delete-first
  (assert-equal
    '((b . 2) (c . 3) (d . 4))
    (delete
      (test-map '(a . 1) '(b . 2) '(c . 3) '(d . 4))
      'a)
    transform-map))


(test delete-last
  (assert-equal
    '((a . 1) (b . 2) (c . 3))
    (delete
      (test-map '(a . 1) '(b . 2) '(c . 3) '(d . 4))
      'd)
    transform-map))


(test delete-many
  (assert-equal
    '()
    (loop with m = (loop with m = (test-map)
                         for i from 1 to 100
                         do (set! m (insert m (string->symbol (sprintf "key{}" i)) i))
                         finally (return m))
          for i from 1 to 100
          do (set! m (delete m (string->symbol (sprintf "key{}" i))))
          finally (return m))
    transform-map))