add another test for 'PersistentVector.delete

This commit is contained in:
2026-01-17 23:45:20 +00:00
parent 1c947eab7d
commit 83bb852ae2
2 changed files with 87 additions and 34 deletions

View File

@@ -547,18 +547,45 @@ struct
fun merge (left, right) = raise Fail "unimplemennted" fun merge (left, right) = raise Fail "unimplemennted"
fun delete (start, finish, tree) = fun delete (start, length, tree) =
let let
val finish = start + length
val matchBeforeStart = prevMatch (start, tree, 1)
val matchAfterFinish = nextMatch (finish, tree, 1) val matchAfterFinish = nextMatch (finish, tree, 1)
val left = splitLeft (start, tree)
in in
if matchAfterFinish = ~1 then if matchBeforeStart = ~1 andalso matchAfterFinish = ~1 then
empty
else if matchAfterFinish = ~1 then
(* there is no match after 'finish', so just split left. (* there is no match after 'finish', so just split left.
* No need to decrement or split right, * No need to decrement or split right,
* because the right vector would be empty. *) * because the right vector would be empty. *)
left splitLeft (start, tree)
else if matchBeforeStart = ~1 then
(* We don't want to use left split
* because there are no elements
* before the deletion range's 'start'.
* So we make a right split and then decrement it. *)
let
val right = splitRight (finish, tree)
in
if isEmpty right then
empty
else
let
val startIdx = getStartIdx right
val difference = matchAfterFinish - startIdx
in
if difference = 0 then
right
else if isEmpty right then
empty
else
decrementBy (difference, right)
end
end
else else
let let
val left = splitLeft (start, tree)
val right = splitRight (finish, tree) val right = splitRight (finish, tree)
in in
if isEmpty right then if isEmpty right then

View File

@@ -285,38 +285,64 @@ struct
] ]
val deleteTests = describe "PersistentVector.delete" val deleteTests = describe "PersistentVector.delete"
[test [ test "returns empty vector when deletion range includes every element"
"returns the left side of the vector \ (fn _ =>
\when 'finish' is greater than any element in the vector" let
(fn _ => (* arrange *)
let val inputList =
(* arrange *) [ {start = 1, finish = 1}
val inputList = , {start = 2, finish = 2}
[ {start = 1, finish = 1} , {start = 3, finish = 3}
, {start = 2, finish = 2} , {start = 4, finish = 4}
, {start = 3, finish = 3} , {start = 5, finish = 5}
, {start = 4, finish = 4} , {start = 6, finish = 6}
, {start = 5, finish = 5} , {start = 7, finish = 7}
, {start = 6, finish = 6} , {start = 8, finish = 8}
, {start = 7, finish = 7} ]
, {start = 8, finish = 8} val pv = PersistentVector.fromList inputList
]
val pv = PersistentVector.fromList inputList
(* act *) (* act *)
val pv = PersistentVector.delete (5, 9, pv) val pv = PersistentVector.delete (0, 11, pv)
(* assert *) (* assert *)
val outputList = PersistentVector.toList pv val outputList = PersistentVector.toList pv
val expectedOutput = val expectedOutput = []
[ {start = 1, finish = 1} in
, {start = 2, finish = 2} Expect.isTrue (outputList = expectedOutput)
, {start = 3, finish = 3} end)
, {start = 4, finish = 4} , test
] "returns the left side of the vector \
in \when 'length' is greater than any element in the vector"
Expect.isTrue (outputList = expectedOutput) (fn _ =>
end)] let
(* arrange *)
val inputList =
[ {start = 1, finish = 1}
, {start = 2, finish = 2}
, {start = 3, finish = 3}
, {start = 4, finish = 4}
, {start = 5, finish = 5}
, {start = 6, finish = 6}
, {start = 7, finish = 7}
, {start = 8, finish = 8}
]
val pv = PersistentVector.fromList inputList
(* act *)
val pv = PersistentVector.delete (5, 4, pv)
(* assert *)
val outputList = PersistentVector.toList pv
val expectedOutput =
[ {start = 1, finish = 1}
, {start = 2, finish = 2}
, {start = 3, finish = 3}
, {start = 4, finish = 4}
]
in
Expect.isTrue (outputList = expectedOutput)
end)
]
val tests = [appendTests, toListTests, splitLeftTests, deleteTests] val tests = [appendTests, toListTests, splitLeftTests, deleteTests]
end end