|
11 | 11 | :length (1- length)
|
12 | 12 | :acc (rope::strcat acc (string (a:random-elt *charset*))))))
|
13 | 13 |
|
| 14 | +(defun random-rope (total-length) |
| 15 | + (with-open-file (source "/dev/urandom" :element-type '(unsigned-byte 8)) |
| 16 | + (labels ((read-leaves (&optional acc (acc-length 0)) |
| 17 | + (let* ((string* (make-array (min rope::*long-leaf* (- total-length acc-length)) |
| 18 | + :element-type '(unsigned-byte 8))) |
| 19 | + (length (read-sequence string* source)) |
| 20 | + (string (map 'string #'code-char string*)) |
| 21 | + (leaf (rope::make-leaf (subseq string 0 length) length))) |
| 22 | + (if (and (= rope::*long-leaf* length) |
| 23 | + (not (= total-length (+ length acc-length)))) |
| 24 | + (read-leaves (cons leaf acc) (+ length acc-length)) |
| 25 | + (cons leaf acc))))) |
| 26 | + (let ((leaves (nreverse (read-leaves)))) |
| 27 | + (rope::merge-leaves leaves 0 (length leaves)))))) |
| 28 | + |
| 29 | +(defgeneric balancedp (rope) |
| 30 | + (:method ((rope rope::leaf)) |
| 31 | + t) |
| 32 | + (:method ((rope rope::branch)) |
| 33 | + (and (>= 1 (abs (rope::balance-factor rope))) |
| 34 | + (balancedp (rope::branch-left rope)) |
| 35 | + (balancedp (rope::branch-right rope))))) |
| 36 | + |
14 | 37 | (deftest fuzz-basic-tests ()
|
15 | 38 | "Run the basic test suite with different leaf sizes."
|
16 | 39 | (loop :for i :from 1 :to 64
|
|
20 | 43 | (is (run-tests :rope/test/basic)))))
|
21 | 44 |
|
22 | 45 | (deftest fuzz-split ()
|
23 |
| - (dotimes (i 10) |
| 46 | + (dotimes (i 1000) |
24 | 47 | (let* ((length (random 10000))
|
25 | 48 | (string (random-string :length length))
|
26 | 49 | (rope (rope:make-rope string))
|
27 | 50 | (index (random length)))
|
28 | 51 | (multiple-value-bind (ante post) (rope:split-rope rope index)
|
| 52 | + (is (balancedp ante)) |
| 53 | + (is (balancedp post)) |
29 | 54 | (is (string= (subseq string 0 index) (rope:write-rope ante nil)))
|
30 | 55 | (is (string= (subseq string index) (rope:write-rope post nil)))))))
|
31 | 56 |
|
32 | 57 | (deftest fuzz-index ()
|
33 |
| - (dotimes (i 100) |
| 58 | + (dotimes (i 1000) |
34 | 59 | (let* ((length (1+ (random 1000)))
|
35 | 60 | (string (random-string :length length))
|
36 | 61 | (rope (rope:make-rope string))
|
37 | 62 | (index (random length)))
|
38 | 63 | (is (char= (char string index) (rope:index-rope rope index))))))
|
39 | 64 |
|
40 | 65 | (deftest fuzz-concat ()
|
41 |
| - (dotimes (i 100) |
| 66 | + (dotimes (i 1000) |
42 | 67 | (let* ((string-a (random-string :length (random 1000)))
|
43 | 68 | (string-b (random-string :length (random 1000)))
|
44 | 69 | (rope-a (rope:make-rope string-a))
|
45 |
| - (rope-b (rope:make-rope string-b))) |
46 |
| - (is (string= (rope::strcat string-a string-b) |
47 |
| - (rope:write-rope (rope:concat-rope rope-a rope-b) nil)))))) |
| 70 | + (rope-b (rope:make-rope string-b)) |
| 71 | + (new-string (rope::strcat string-a string-b)) |
| 72 | + (new-rope (rope:concat-rope rope-a rope-b))) |
| 73 | + (is (balancedp new-rope)) |
| 74 | + (is (string= new-string (rope:write-rope new-rope nil)))))) |
48 | 75 |
|
49 | 76 | (deftest fuzz-kill ()
|
50 |
| - (dotimes (i 10000) |
| 77 | + (dotimes (i 1000) |
51 | 78 | (let* ((length (+ 10 (random 1000)))
|
52 | 79 | (string (random-string :length length))
|
53 | 80 | (rope (rope:make-rope string))
|
54 | 81 | (end (1+ (random (1- length))))
|
55 |
| - (start (random end))) |
56 |
| - (is (string= (rope::strcat (subseq string 0 start) |
57 |
| - (subseq string end)) |
58 |
| - (rope:write-rope (rope:kill-rope rope start end) nil)))))) |
| 82 | + (start (random end)) |
| 83 | + (new-string (rope::strcat (subseq string 0 start) (subseq string end))) |
| 84 | + (new-rope (rope:kill-rope rope start end))) |
| 85 | + (is (balancedp new-rope)) |
| 86 | + (is (string= new-string (rope:write-rope new-rope nil)))))) |
| 87 | + |
| 88 | +#+ignore |
| 89 | +(deftest fuzz-insert-balance () |
| 90 | + (setf rope::*long-leaf* 128) |
| 91 | + (dotimes (i 10) |
| 92 | + (let ((rope (random-rope 1000))) |
| 93 | + (dotimes (i 100) |
| 94 | + (setf rope |
| 95 | + (rope:insert-rope rope |
| 96 | + (random (rope:rope-length rope)) |
| 97 | + (random-string :length (random 512)))) |
| 98 | + (unless (balancedp rope) |
| 99 | + (return-from fuzz-insert-balance rope)) |
| 100 | + (is (balancedp rope)))))) |
0 commit comments