aboutsummaryrefslogtreecommitdiffstats
path: root/test/src/buffer-tests.el
diff options
context:
space:
mode:
authorMatt Armstrong2022-10-08 09:28:29 -0700
committerMatt Armstrong2022-10-08 20:37:28 -0700
commit92a0bf6ce2d2afe909d8a075ad9760eb003a8e73 (patch)
tree0dc3007e31b2c1cc0ab30848a4455db813ae4125 /test/src/buffer-tests.el
parent30f52202775155c1d301af3634d0122c3d7851f8 (diff)
downloademacs-92a0bf6ce2d2afe909d8a075ad9760eb003a8e73.tar.gz
emacs-92a0bf6ce2d2afe909d8a075ad9760eb003a8e73.zip
; * test/src/buffer-tests.el (test-overlay-randomly): new test.
Diffstat (limited to 'test/src/buffer-tests.el')
-rw-r--r--test/src/buffer-tests.el92
1 files changed, 92 insertions, 0 deletions
diff --git a/test/src/buffer-tests.el b/test/src/buffer-tests.el
index a12d15bc798..01780a15cc1 100644
--- a/test/src/buffer-tests.el
+++ b/test/src/buffer-tests.el
@@ -1508,6 +1508,98 @@ with parameters from the *Messages* buffer modification."
1508 (ovshould nonempty-eob-end 4 5) 1508 (ovshould nonempty-eob-end 4 5)
1509 (ovshould empty-eob 5 5))))) 1509 (ovshould empty-eob 5 5)))))
1510 1510
1511(ert-deftest test-overlay-randomly ()
1512 "Exercise overlay code, but perform few assertions.
1513
1514This test works best when Emacs is configured with
1515--enable-checking=yes. This is a little bit like fuzz testing,
1516except this test has no way to reduce to a minimal failng test
1517case. Regardless, by exercising many corner cases bugs can be
1518found using Emacs' internal consistency assertions."
1519 (let* (
1520 ;; The size and slack for the test buffer size.
1521 (buffer-size-target 1000)
1522 (buffer-size-slack 200)
1523
1524 ;; Use up to 100 overlays. We need not use more to observe
1525 ;; reasonable variation in the overlay data structures.
1526 (overlay-count-limit 100)
1527
1528 ;; This test maintains a vector of overlays. Each iteration
1529 ;; may append or erase one overlay.
1530 (overlays (make-vector overlay-count-limit nil))
1531 (overlay-count 0)
1532
1533 ;; The test is either slowly growing or shrinking the overlay
1534 ;; count. Deletions still occur while growing, and additions
1535 ;; still occur while shrinking. The GROWING variable only
1536 ;; controls the relative probability of doing one or the
1537 ;; other.
1538 (growing t)
1539
1540 ;; Loop up to 1M times.
1541 (iteration-count 0)
1542 (iteration-target 100000))
1543 (with-temp-buffer
1544 (while (< (buffer-size) buffer-size-target)
1545 (insert "Sed ut perspiciatis, unde omnis iste natus error sit voluptatem
1546accusantium doloremque laudantium, totam rem aperiam eaque ipsa,
1547quae ab illo inventore veritatis et quasi architecto beatae vitae
1548dicta sunt, explicabo. "))
1549
1550 (while (< iteration-count iteration-target)
1551 (cl-incf iteration-count)
1552
1553 ;; Toggle GROWING if we've reached a size boundary. The idea
1554 ;; is to initially steadily increase the overlay count, then
1555 ;; steadily decrease it, then repeat.
1556 (when (and growing (= overlay-count overlay-count-limit))
1557 (message "now shrinking")
1558 (setq growing nil))
1559 (when (and (not growing) (= overlay-count 0))
1560 (message "now growing")
1561 (setq growing t))
1562
1563 ;; Create or delete a random overlay according to a
1564 ;; probability chosen by GROWING.
1565 (let ((create-overlay (>= (random 100) (if growing 40 60))))
1566 (cond
1567 ;; Possibly create a new overlay in a random place in the
1568 ;; buffer. We have two easy choices. We can choose the
1569 ;; overlay BEGIN randomly, then choose its END among the
1570 ;; valid remaining buffer posiitions. Or we could choose
1571 ;; the overlay width randomly, then choose a valid BEGIN.
1572 ;; We take the former approach, because the overlay data
1573 ;; structure is ordered primarily by BEGIN.
1574 ((and create-overlay (< overlay-count overlay-count-limit))
1575 (let* ((begin (random (buffer-size)))
1576 (end (+ begin (random (- (buffer-size) begin))))
1577 (ov (make-overlay begin end nil
1578 (= 0 (random 2)) (= 0 (random 2)))))
1579 (aset overlays overlay-count ov)
1580 (cl-incf overlay-count)))
1581 ((and (not create-overlay) (> overlay-count 0))
1582
1583 ;; Possibly delete a random overlay.
1584 (let* ((last-index (1- overlay-count))
1585 (index (random overlay-count))
1586 (ov (aref overlays index)))
1587 (when (< index last-index)
1588 (aset overlays index (aref overlays last-index)))
1589 (aset overlays last-index nil)
1590 (cl-decf overlay-count)
1591 (delete-overlay ov)))))
1592
1593 ;; Modify the buffer on occasion, which exercises the
1594 ;; insert/remove gap logic in the overlay implementation.
1595 (when (and (< (buffer-size) (+ buffer-size-target buffer-size-slack))
1596 (zerop (random 10)))
1597 (goto-char (1+ (random (buffer-size))))
1598 (insert (+ ?a (random 26))))
1599 (when (and (> (buffer-size) (- buffer-size-target buffer-size-slack))
1600 (zerop (random 10)))
1601 (goto-char (1+ (random (buffer-size))))
1602 (delete-char 1))))))
1511 1603
1512 1604
1513 1605