From: Michael D. Lowis Date: Thu, 9 Nov 2017 14:03:01 +0000 (-0500) Subject: fixed join operation and some broken unit tests. Also implemented crude ascii-only... X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=aab468b4528bfebc91f27ebd200cc37a70640012;p=archive%2Ftide-ocaml.git fixed join operation and some broken unit tests. Also implemented crude ascii-only version of putc --- diff --git a/docs/Rope.html b/docs/Rope.html index c68a1a5..56ebad4 100644 --- a/docs/Rope.html +++ b/docs/Rope.html @@ -76,7 +76,7 @@
val split : t -> int -> t * t
val del : t -> int -> int -> t
val puts : t -> string -> int -> t
-
val putc : 'a -> 'b -> 'c -> 'a
+
val putc : t -> int -> int -> t
val nextc : t -> int -> int
val prevc : t -> int -> int
val is_bol : t -> int -> bool
diff --git a/lib/rope.ml b/lib/rope.ml index 9b0653d..d8211a2 100644 --- a/lib/rope.ml +++ b/lib/rope.ml @@ -106,28 +106,12 @@ let gets rope i j = 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 @@ -178,7 +162,8 @@ let rec puts rope s i = 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 diff --git a/lib/rope.mli b/lib/rope.mli index 57be4b4..fd9e545 100644 --- a/lib/rope.mli +++ b/lib/rope.mli @@ -15,6 +15,7 @@ val height : rope -> int val limit_index : rope -> int -> int val last : rope -> int +val flatten : rope -> rope val join : rope -> rope -> rope val split : rope -> int -> (rope * rope) val del : rope -> int -> int -> rope diff --git a/tests/rope_tests.ml b/tests/rope_tests.ml index 11dddd5..156b8d5 100644 --- a/tests/rope_tests.ml +++ b/tests/rope_tests.ml @@ -16,30 +16,38 @@ let () = 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) ); @@ -92,6 +100,7 @@ let () = 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' ); @@ -100,6 +109,7 @@ let () = 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' ); @@ -108,6 +118,7 @@ let () = 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' );