let buf = Bytes.create (j - i) in
iteri
(fun n c ->
- Bytes.set buf (n - i) (getb rope i);
+ Bytes.set buf (n - i) (Char.chr c);
(n <= j))
rope i;
Bytes.unsafe_to_string buf
(******************************************************************************)
-(*
-
-If both arguments are short leaves, we produce a flat rope (leaf) consisting of the concatenation.
-
-If the left argument is a concatenation node whose right son is a short leaf, and the right argument is also a short leaf, then we concatenate the two leaves, and then concatenate the result to the left son of the left argument.
-
-let join_special left right =
- let rlen = (length right) in
- match left with
- | Leaf(s,o,l) ->
- if (l + rlen) <= 256 then
- flatten ()
-
- | Node(ln,Leaf(s,o,l),_,_) ->
- if (l + rlen) <= 256 then
-*)
let flatten rope =
let s = (gets rope 0 (length rope)) in
let middle = from_string s in
(join (join left middle) right)
-let putc rope i c = rope (* TODO: convert and call puts *)
+let putc rope i c =
+ puts rope (String.make 1 (Char.chr c)) i
let nextc rope pos =
let next = limit_index rope (pos + 1) in
assert( length rope == 2 )
);
+ (* flatten() tests *)
+ test "flatten : flatten a tree to a leaf" (fun () ->
+ let tree = Node (Leaf("a", 0, 1), Leaf("b", 0, 1), 2, 2) in
+ let leaf = (flatten tree) in
+ assert( match leaf with
+ | Leaf ("ab",0,2) -> true
+ | _ -> false)
+ );
+
(* join() tests *)
test "join : join two leaves into rope" (fun () ->
let left = Leaf("a", 0, 1) in
- let right = Leaf("a", 0, 1) in
+ let right = Leaf("b", 0, 1) in
let rope = (join left right) in
assert( match rope with
- | Node (l,r,h,2) -> (l == left && r == right)
+ | Leaf ("ab",0,2) -> true
| _ -> false)
);
test "join : join a rope with a leaf (l to r)" (fun () ->
- let left = join (Leaf("a", 0, 1)) (Leaf("a", 0, 1)) in
- let right = Leaf("a", 0, 1) in
+ let left = join (Leaf("a", 0, 1)) (Leaf("b", 0, 1)) in
+ let right = Leaf("c", 0, 1) in
let rope = (join left right) in
- Printf.printf "length %d \n" (length rope);
assert( match rope with
- | Node (l,r,2,3) -> true
+ | Leaf ("abc",0,3) -> true
| _ -> false)
);
test "join : join a rope with a leaf (r to l)" (fun () ->
let left = Leaf("a", 0, 1) in
- let right = join (Leaf("a", 0, 1)) (Leaf("a", 0, 1)) in
+ let right = Node (Leaf("b", 0, 1), Leaf("c", 0, 1), 2, 2) in
let rope = (join left right) in
assert( match rope with
- | Node (l,r,h,3) -> (l == left && r == right)
+ | Leaf ("abc",0,3) -> true
| _ -> false)
);
let rope = Leaf("bc", 0, 2) in
let rope = (puts rope "a" 0) in
assert( (length rope) == 3 );
+ assert( (gets rope 0 3) = "abc" );
assert( (getc rope (0)) == Char.code 'a' );
assert( (getc rope (1)) == Char.code 'b' );
assert( (getc rope (2)) == Char.code 'c' );
let rope = Leaf("ac", 0, 2) in
let rope = (puts rope "b" 1) in
assert( (length rope) == 3 );
+ assert( (gets rope 0 3) = "abc" );
assert( (getc rope (0)) == Char.code 'a' );
assert( (getc rope (1)) == Char.code 'b' );
assert( (getc rope (2)) == Char.code 'c' );
let rope = Leaf("ab", 0, 2) in
let rope = (puts rope "c" 2) in
assert( (length rope) == 3 );
+ assert( (gets rope 0 3) = "abc" );
assert( (getc rope (0)) == Char.code 'a' );
assert( (getc rope (1)) == Char.code 'b' );
assert( (getc rope (2)) == Char.code 'c' );