Skip to content

Commit ce335e9

Browse files
committed
docstrings and test asd fixes
1 parent aaabfd4 commit ce335e9

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

rope.asd

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
(asdf:defsystem #:rope/test
1010
:depends-on (#:alexandria #:fiasco #:rope)
1111
:components ((:module "test"
12-
:components ((:file "package")
13-
(:file "basic"))))
14-
:perform (asdf:test-op (o c) (uiop:symbol-call :fiasco :all-tests)))
12+
:components ((:file "basic")
13+
(:file "fuzz"))))
14+
:perform (asdf:test-op
15+
(o c)
16+
(multiple-value-bind (stat result)
17+
(uiop:symbol-call :fiasco :all-tests)
18+
(print result)
19+
(assert (eql t stat)))))
1520

1621
(asdf:defsystem #:rope/dev
1722
:depends-on (#:cl-dot #:rope)

rope.lisp

+23-7
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,24 @@
2121
(right
2222
:initarg :right
2323
:initform nil
24-
:accessor branch-right)))
24+
:accessor branch-right))
25+
(:documentation "A node with left and right children."))
2526

2627
(defclass leaf (rope)
2728
((string
2829
:initarg :string
2930
:initform ""
30-
:accessor leaf-string)))
31+
:accessor leaf-string))
32+
(:documentation "A string segment of a rope."))
3133

3234
;;-------;;
3335
;; Utils ;;
3436
;;-------;;
3537

3638
(defgeneric make-rope (source)
39+
(:documentation "Create a new rope from a string.")
40+
(:method ((source rope))
41+
source)
3742
(:method ((source string))
3843
(let ((length (length source)))
3944
(if (<= *long-leaf* length)
@@ -58,6 +63,7 @@
5863
;;-----------;;
5964

6065
(defgeneric walk-rope (rope func)
66+
(:documentation "Call `func' on each leaf of a rope in order.")
6167
(:method ((rope leaf) func)
6268
(funcall func rope)
6369
(values))
@@ -66,6 +72,7 @@
6672
(walk-rope (branch-right rope) func)))
6773

6874
(defun write-rope (rope out)
75+
"Write a rope to a stream or string, like `format', nil output returns a string."
6976
(if (null out)
7077
(with-output-to-string (s) (write-rope rope s))
7178
(walk-rope rope
@@ -77,6 +84,10 @@
7784
(walk-rope rope (lambda (leaf) (push leaf leaves)))
7885
(nreverse leaves)))
7986

87+
;;-----------;;
88+
;; Balancing ;;
89+
;;-----------;;
90+
8091
(defun normalize-leaves (leaves &optional carry)
8192
(let ((leaf (car leaves)))
8293
(cond ((and carry (null leaf))
@@ -91,11 +102,8 @@
91102
(t
92103
(cons leaf (normalize-leaves (cdr leaves)))))))
93104

94-
;;-----------;;
95-
;; Balancing ;;
96-
;;-----------;;
97-
98105
(defgeneric balancedp (rope)
106+
(:documentation "Check if a rope is a height-balanced tree.")
99107
(:method ((rope leaf))
100108
t)
101109
(:method ((rope branch))
@@ -114,6 +122,7 @@
114122
(merge-leaves leaves mid end)))))))
115123

116124
(defun balance-rope (rope &optional forcep)
125+
"Balance a rope by reconstructing it from the bottom up."
117126
(if (and (balancedp rope) (not forcep))
118127
rope
119128
(let ((leaves (normalize-leaves (collect-rope rope))))
@@ -124,18 +133,21 @@
124133
;;--------;;
125134

126135
(defgeneric prepend-rope (rope source)
136+
(:documentation "Return a new rope with a string or rope inserted at the beginning of a rope.")
127137
(:method (rope (source string))
128138
(concat-rope (make-rope source) rope))
129139
(:method (rope (source rope))
130140
(concat-rope source rope)))
131141

132142
(defgeneric append-rope (rope source)
143+
(:documentation "Return a new rope with a string or rope inserted at the end of a rope.")
133144
(:method (rope (source string))
134145
(concat-rope rope (make-rope source)))
135146
(:method (rope (source rope))
136147
(concat-rope rope source)))
137148

138149
(defun insert-rope (rope index str)
150+
"Return a new rope with a string or rope inserted at the specified index of a rope."
139151
(cond ((= index 0) (prepend-rope rope str))
140152
((= index (rope-length rope)) (append-rope rope str))
141153
(t (multiple-value-bind (ante post) (split-rope rope index)
@@ -146,6 +158,7 @@
146158
;;-------;;
147159

148160
(defgeneric index-rope (rope index)
161+
(:documentation "Get a character at the specified index of a rope.")
149162
(:method ((rope leaf) index)
150163
(char (leaf-string rope) index))
151164
(:method ((rope branch) index)
@@ -158,7 +171,8 @@
158171
;; Concat ;;
159172
;;--------;;
160173

161-
(defmethod concat-rope (left right)
174+
(defun concat-rope (left right)
175+
"Returns a balanced concatenation of two ropes."
162176
(balance-rope
163177
(make-instance 'branch
164178
:length (+ (rope-length left) (rope-length right))
@@ -171,6 +185,7 @@
171185
;;-------;;
172186

173187
(defgeneric split-rope (rope index)
188+
(:documentation "Return balanced ropes split at index as multiple values.")
174189
(:method ((rope leaf) index)
175190
(values (make-rope (subseq (leaf-string rope) 0 index))
176191
(make-rope (subseq (leaf-string rope) index))))
@@ -193,6 +208,7 @@
193208
;;------;;
194209

195210
(defun kill-rope (rope from &optional to)
211+
"Return a new rope without the characters in the specified range."
196212
(multiple-value-bind (ante _) (split-rope rope from)
197213
(declare (ignore _))
198214
(multiple-value-bind (_ post) (split-rope rope (or to from))

test/fuzz.lisp

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
(deftest fuzz-index ()
3232
(dotimes (i 100)
33-
(let* ((length (random 1000))
33+
(let* ((length (1+ (random 1000)))
3434
(string (random-string :length length))
3535
(rope (rope:make-rope string))
3636
(index (random length)))

0 commit comments

Comments
 (0)