Commits

Moritz Heidkamp committed 41cd945

Fix transient maps mutating the original

  • Participants
  • Parent commits f5eb3a3

Comments (0)

Files changed (2)

persistent-hash-map.scm

 (: -inode-lookup (node fixnum fixnum * * -> *))
 
 (define-class (transient-hash-map tm-)
-  (make-transient-map (edit       : boolean)
+  (make-transient-map (edit       : (or boolean pair))
                       (root       : node)
                       (count      : number)
                       (has-false? : boolean)
           init)))
 
   (define (map->transient-map)
-    (make-transient-map #t
+    (make-transient-map (list 'edit)
                         (pm-root self)
                         (pm-count self)
                         (pm-has-false? self)
       (test 2 (handle-exceptions e 2 (map-add! tm 10 10) 1))
       (test 2 (handle-exceptions e 2 (persist-map! tm) 1))
       (test 2 (handle-exceptions e 2 (map-size tm) 1))
-      (test-assert (map-equal? m (persistent-map 0 0 1 1 2 2 4 4 6 6 8 8 9 9))))))
+      (test-assert (map-equal? m (persistent-map 0 0 1 1 2 2 4 4 6 6 8 8 9 9)))))
+
+  (let* ((m  (persistent-map 1 2 3 4))
+         (tm (map->transient-map m)))
+    (map-add! tm 1 3)
+    (test 2 (map-ref m 1))
+    (map-add! tm 5 6)
+    (test #f (map-ref m 5))
+    (test 6  (map-ref tm 5))))
 
 (test-group "map-reduce"
   (test 499500 (map-reduce + 0 (apply persistent-map (iota 1000)))))
             (test 2 (map-ref tm key-b)))))))
 
 (test-group "map-merge"
-  (let ((m (map-merge (persistent-map 1 2 3 4)
-                      (persistent-map 1 3 5 6))))
+  (let* ((m1 (persistent-map 1 2 3 4))
+         (m2 (persistent-map 1 3 5 6))
+         (m (map-merge m1 m2)))
     (test 3 (map-ref m 1))
     (test 4 (map-ref m 3))
-    (test 6 (map-ref m 5))))
+    (test 6 (map-ref m 5))
+    (test 2 (map-ref m1 1))
+    (test #f (map-ref m1 5))
+    (test 3 (map-ref m2 1))
+    (test 6 (map-ref m2 5))))
 
 (test-group "map-ref-in, map-update-in"
   (let ((m (-> (persistent-map 'foo (persistent-map 'bar 1))