begin adding tests for persistent-vector.sml, and add bug fix to 'PersistentVector.delete' in light of one of the tests. (We were decrementing by the wrong value previously, but I fixed it and added a comment of how we arrive at the value we want to decrement by
This commit is contained in:
@@ -349,8 +349,16 @@ struct
|
|||||||
|
|
||||||
fun getMaxSize tree =
|
fun getMaxSize tree =
|
||||||
case tree of
|
case tree of
|
||||||
BRANCH (_, sizes) => Vector.sub (sizes, Vector.length sizes - 1)
|
BRANCH (_, sizes) =>
|
||||||
| LEAF (_, sizes) => Vector.sub (sizes, Vector.length sizes - 1)
|
if Vector.length sizes = 0 then
|
||||||
|
0
|
||||||
|
else
|
||||||
|
Vector.sub (sizes, Vector.length sizes - 1)
|
||||||
|
| LEAF (_, sizes) =>
|
||||||
|
if Vector.length sizes = 0 then
|
||||||
|
0
|
||||||
|
else
|
||||||
|
Vector.sub (sizes, Vector.length sizes - 1)
|
||||||
|
|
||||||
fun splitLeft (offset, tree) =
|
fun splitLeft (offset, tree) =
|
||||||
case tree of
|
case tree of
|
||||||
@@ -893,7 +901,7 @@ struct
|
|||||||
fun delete (start, length, tree) =
|
fun delete (start, length, tree) =
|
||||||
let
|
let
|
||||||
val finishIdx = start + length
|
val finishIdx = start + length
|
||||||
val {start, finish} = startNextMatch (finishIdx, tree)
|
val {start, finish} = startNextMatch (start, tree)
|
||||||
in
|
in
|
||||||
if start = ~1 then
|
if start = ~1 then
|
||||||
(* nothing to delete *)
|
(* nothing to delete *)
|
||||||
@@ -902,19 +910,40 @@ struct
|
|||||||
let
|
let
|
||||||
(* split left and right side *)
|
(* split left and right side *)
|
||||||
val left = splitLeft (start, tree)
|
val left = splitLeft (start, tree)
|
||||||
val left = root left
|
|
||||||
|
|
||||||
val right = splitRight (finishIdx, tree)
|
val right = splitRight (finishIdx, tree)
|
||||||
|
in
|
||||||
|
if isEmpty right then
|
||||||
|
root left
|
||||||
|
else
|
||||||
|
let
|
||||||
|
val left = root left
|
||||||
val right = root right
|
val right = root right
|
||||||
|
|
||||||
(* calculate what the new index should be
|
(* calculate what the new index should be
|
||||||
* for the first match in the right tree,
|
* for the first match in the right tree,
|
||||||
* and decrement the right tree to reach this index
|
* and decrement the right tree to reach this index
|
||||||
* if necessary. *)
|
* if necessary.
|
||||||
|
*
|
||||||
|
* We calculate the index the right node should start at
|
||||||
|
* using this method:
|
||||||
|
* 1. We get the first match after finishIdx from the original * tree
|
||||||
|
* 2. We subtract this by the length provided to us,
|
||||||
|
* which tells us where the right node should start at.
|
||||||
|
* 3. We get the first item in the right node,
|
||||||
|
* and add the last item in the left node
|
||||||
|
* to get an absolute value.
|
||||||
|
* 4. We subtract the match from the original tree
|
||||||
|
* by the first item in the right node.
|
||||||
|
* This step gives us the difference: how much
|
||||||
|
* we need to decrement the right node by.
|
||||||
|
* *)
|
||||||
|
val {start = shouldBeStart, ...} =
|
||||||
|
startNextMatch (finishIdx, tree)
|
||||||
|
val shouldBeStart = shouldBeStart - length
|
||||||
|
|
||||||
val leftSize = getMaxSize left
|
val leftSize = getMaxSize left
|
||||||
val {start = rightStart, ...} = getFirstItem right
|
val {start = rightStart, ...} = getFirstItem right
|
||||||
val rightStart = rightStart + leftSize
|
val difference = rightStart - shouldBeStart
|
||||||
val difference = start - rightStart
|
|
||||||
val right =
|
val right =
|
||||||
if difference = 0 then
|
if difference = 0 then
|
||||||
right
|
right
|
||||||
@@ -924,6 +953,7 @@ struct
|
|||||||
join (left, right)
|
join (left, right)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
(* conversion functions for testing *)
|
(* conversion functions for testing *)
|
||||||
fun helpFromList ([], acc) = acc
|
fun helpFromList ([], acc) = acc
|
||||||
|
|||||||
@@ -71,4 +71,5 @@ test/test-utils.sml
|
|||||||
test/normal-move.sml
|
test/normal-move.sml
|
||||||
test/normal-delete.sml
|
test/normal-delete.sml
|
||||||
test/regression.sml
|
test/regression.sml
|
||||||
|
test/persistent-vector-tests.sml
|
||||||
test/test.sml
|
test/test.sml
|
||||||
|
|||||||
55
test/persistent-vector-tests.sml
Normal file
55
test/persistent-vector-tests.sml
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
structure PersistentVectorTests =
|
||||||
|
struct
|
||||||
|
open Railroad
|
||||||
|
open Railroad.Test
|
||||||
|
|
||||||
|
structure Pv = PersistentVector
|
||||||
|
|
||||||
|
val appendTests = describe "PersistentVector.append"
|
||||||
|
[test "appends new values to end" (fn _ =>
|
||||||
|
let
|
||||||
|
(* arrange *)
|
||||||
|
val pv = Pv.fromList [(1, 1), (3, 3)]
|
||||||
|
|
||||||
|
(* act *)
|
||||||
|
val pv = Pv.append (5, 7, pv)
|
||||||
|
|
||||||
|
(* assert *)
|
||||||
|
val outputList = Pv.toList pv
|
||||||
|
val expectedList = [(1, 1), (3, 3), (5, 7)]
|
||||||
|
in
|
||||||
|
Expect.isTrue (outputList = expectedList)
|
||||||
|
end)]
|
||||||
|
|
||||||
|
fun printList lst =
|
||||||
|
let
|
||||||
|
val str =
|
||||||
|
List.map
|
||||||
|
(fn (start, finish) =>
|
||||||
|
"(" ^ Int.toString start ^ ", " ^ Int.toString finish ^ ")") lst
|
||||||
|
val str = "[" ^ String.concatWith ", " str ^ "]\n"
|
||||||
|
in
|
||||||
|
print str
|
||||||
|
end
|
||||||
|
|
||||||
|
val deleteTests = describe "PersistentVector.delete"
|
||||||
|
[test
|
||||||
|
"deletes last value correctly \
|
||||||
|
\when only last value is in deletion range"
|
||||||
|
(fn _ =>
|
||||||
|
let
|
||||||
|
(* arrange *)
|
||||||
|
val pv = Pv.fromList [(0, 0), (3, 3), (5, 5)]
|
||||||
|
|
||||||
|
(* act *)
|
||||||
|
val pv = Pv.delete (5, 1, pv)
|
||||||
|
|
||||||
|
(* assert *)
|
||||||
|
val outputList = Pv.toList pv
|
||||||
|
val expectedList = [(0, 0), (3, 3)]
|
||||||
|
in
|
||||||
|
Expect.isTrue (outputList = expectedList)
|
||||||
|
end)]
|
||||||
|
|
||||||
|
val tests = [appendTests, deleteTests]
|
||||||
|
end
|
||||||
@@ -5,12 +5,12 @@ struct
|
|||||||
|
|
||||||
fun main () =
|
fun main () =
|
||||||
let
|
let
|
||||||
val tests =
|
val tests = List.concat
|
||||||
List.concat
|
|
||||||
[ NormalMove.tests
|
[ NormalMove.tests
|
||||||
, NormalDelete.tests
|
, NormalDelete.tests
|
||||||
, Regression.tests
|
, Regression.tests
|
||||||
, RegexTests.tests
|
, RegexTests.tests
|
||||||
|
, PersistentVectorTests.tests
|
||||||
]
|
]
|
||||||
val tests = concat tests
|
val tests = concat tests
|
||||||
in
|
in
|
||||||
|
|||||||
Reference in New Issue
Block a user