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:
2025-12-09 11:42:30 +00:00
parent 51bce52ea4
commit e6bda83309
4 changed files with 114 additions and 28 deletions

View File

@@ -349,8 +349,16 @@ struct
fun getMaxSize tree =
case tree of
BRANCH (_, sizes) => Vector.sub (sizes, Vector.length sizes - 1)
| LEAF (_, sizes) => Vector.sub (sizes, Vector.length sizes - 1)
BRANCH (_, sizes) =>
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) =
case tree of
@@ -893,7 +901,7 @@ struct
fun delete (start, length, tree) =
let
val finishIdx = start + length
val {start, finish} = startNextMatch (finishIdx, tree)
val {start, finish} = startNextMatch (start, tree)
in
if start = ~1 then
(* nothing to delete *)
@@ -902,19 +910,40 @@ struct
let
(* split left and right side *)
val left = splitLeft (start, tree)
val left = root left
val right = splitRight (finishIdx, tree)
in
if isEmpty right then
root left
else
let
val left = root left
val right = root right
(* calculate what the new index should be
* for the first match in the right tree,
* 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 {start = rightStart, ...} = getFirstItem right
val rightStart = rightStart + leftSize
val difference = start - rightStart
val difference = rightStart - shouldBeStart
val right =
if difference = 0 then
right
@@ -924,6 +953,7 @@ struct
join (left, right)
end
end
end
(* conversion functions for testing *)
fun helpFromList ([], acc) = acc

View File

@@ -71,4 +71,5 @@ test/test-utils.sml
test/normal-move.sml
test/normal-delete.sml
test/regression.sml
test/persistent-vector-tests.sml
test/test.sml

View 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

View File

@@ -5,12 +5,12 @@ struct
fun main () =
let
val tests =
List.concat
val tests = List.concat
[ NormalMove.tests
, NormalDelete.tests
, Regression.tests
, RegexTests.tests
, PersistentVectorTests.tests
]
val tests = concat tests
in