aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorMattias EngdegÄrd2023-05-19 12:32:28 +0200
committerMattias EngdegÄrd2023-05-19 15:50:42 +0200
commitbd6bba4780dcfdec97ab5e6469f7777c4b2a1b0d (patch)
tree68165f5e80829f975c9e740fd2ca1e06fcc40d2a /test
parent156973639cc57dec47705f76f63c2ef3dc00a61d (diff)
downloademacs-bd6bba4780dcfdec97ab5e6469f7777c4b2a1b0d.tar.gz
emacs-bd6bba4780dcfdec97ab5e6469f7777c4b2a1b0d.zip
Improved copy-tree documentation and test (bug#63509)
* etc/NEWS: Move entry since it's an incompatible change. * lisp/emacs-lisp/shortdoc.el (vector): Make the example relevant. * lisp/subr.el (copy-tree): Rename second argument, since 'vector-like' is a term with a specific meaning in Emacs but not the one intended here. * doc/lispref/lists.texi (Building Lists): Rename second argument, and make it clear that the input must be acyclic. * doc/lispref/records.texi (Record Functions): Be more precise: `copy-sequence` is used to copy records, `copy-tree` copies trees made of records etc. * test/lisp/subr-tests.el (subr--copy-tree): Extend and strengthen the test considerably, using the print-circle trick to detect structure sharing precisely.
Diffstat (limited to 'test')
-rw-r--r--test/lisp/subr-tests.el77
1 files changed, 48 insertions, 29 deletions
diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el
index 4ebb68556be..1c220b1da18 100644
--- a/test/lisp/subr-tests.el
+++ b/test/lisp/subr-tests.el
@@ -1207,35 +1207,54 @@ final or penultimate step during initialization."))
1207 (should (eq a a-dedup)))) 1207 (should (eq a a-dedup))))
1208 1208
1209(ert-deftest subr--copy-tree () 1209(ert-deftest subr--copy-tree ()
1210 (should (eq (copy-tree nil) nil)) 1210 ;; Check that values other than conses, vectors and records are
1211 (let* ((a (list (list "a") "b" (list "c") "g")) 1211 ;; neither copied nor traversed.
1212 (copy1 (copy-tree a)) 1212 (let ((s (propertize "abc" 'prop (list 11 12)))
1213 (copy2 (copy-tree a t))) 1213 (h (make-hash-table :test #'equal)))
1214 (should (equal a copy1)) 1214 (puthash (list 1 2) (list 3 4) h)
1215 (should (equal a copy2)) 1215 (dolist (x (list nil 'a "abc" s h))
1216 (should-not (eq a copy1)) 1216 (should (eq (copy-tree x) x))
1217 (should-not (eq a copy2))) 1217 (should (eq (copy-tree x t) x))))
1218 (let* ((a (list (list "a") "b" (list "c" (record 'foo "d")) (list ["e" "f"]) "g")) 1218
1219 (copy1 (copy-tree a)) 1219 ;; Use the printer to detect common parts of Lisp values.
1220 (copy2 (copy-tree a t))) 1220 (let ((print-circle t))
1221 (should (equal a copy1)) 1221 (cl-labels ((prn3 (x y z) (prin1-to-string (list x y z)))
1222 (should (equal a copy2)) 1222 (cat3 (x y z) (concat "(" x " " y " " z ")")))
1223 (should-not (eq a copy1)) 1223 (let ((x '(a (b ((c) . d) e) (f))))
1224 (should-not (eq a copy2))) 1224 (should (equal (prn3 x (copy-tree x) (copy-tree x t))
1225 (let* ((a (record 'foo "a" (record 'bar "b"))) 1225 (cat3 "(a (b ((c) . d) e) (f))"
1226 (copy1 (copy-tree a)) 1226 "(a (b ((c) . d) e) (f))"
1227 (copy2 (copy-tree a t))) 1227 "(a (b ((c) . d) e) (f))"))))
1228 (should (equal a copy1)) 1228 (let ((x '(a [b (c d)] #s(e (f [g])))))
1229 (should (equal a copy2)) 1229 (should (equal (prn3 x (copy-tree x) (copy-tree x t))
1230 (should (eq a copy1)) 1230 (cat3 "(a #1=[b (c d)] #2=#s(e (f [g])))"
1231 (should-not (eq a copy2))) 1231 "(a #1# #2#)"
1232 (let* ((a ["a" "b" ["c" ["d"]]]) 1232 "(a [b (c d)] #s(e (f [g])))"))))
1233 (copy1 (copy-tree a)) 1233 (let ((x [a (b #s(c d))]))
1234 (copy2 (copy-tree a t))) 1234 (should (equal (prn3 x (copy-tree x) (copy-tree x t))
1235 (should (equal a copy1)) 1235 (cat3 "#1=[a (b #s(c d))]"
1236 (should (equal a copy2)) 1236 "#1#"
1237 (should (eq a copy1)) 1237 "[a (b #s(c d))]"))))
1238 (should-not (eq a copy2)))) 1238 (let ((x #s(a (b [c d]))))
1239 (should (equal (prn3 x (copy-tree x) (copy-tree x t))
1240 (cat3 "#1=#s(a (b [c d]))"
1241 "#1#"
1242 "#s(a (b [c d]))"))))
1243 ;; Check cdr recursion.
1244 (let ((x '(a b . [(c . #s(d))])))
1245 (should (equal (prn3 x (copy-tree x) (copy-tree x t))
1246 (cat3 "(a b . #1=[(c . #s(d))])"
1247 "(a b . #1#)"
1248 "(a b . [(c . #s(d))])"))))
1249 ;; Check that we can copy DAGs (the result is a tree).
1250 (let ((x (list '(a b) nil [c d] nil #s(e f) nil)))
1251 (setf (nth 1 x) (nth 0 x))
1252 (setf (nth 3 x) (nth 2 x))
1253 (setf (nth 5 x) (nth 4 x))
1254 (should (equal (prn3 x (copy-tree x) (copy-tree x t))
1255 (cat3 "(#1=(a b) #1# #2=[c d] #2# #3=#s(e f) #3#)"
1256 "((a b) (a b) #2# #2# #3# #3#)"
1257 "((a b) (a b) [c d] [c d] #s(e f) #s(e f))")))))))
1239 1258
1240(provide 'subr-tests) 1259(provide 'subr-tests)
1241;;; subr-tests.el ends here 1260;;; subr-tests.el ends here