Skip to content

Commit

Permalink
add more test cases to BigText transform offset calculator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny-chung committed Sep 21, 2024
1 parent f36eef7 commit b1b7d8b
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal class BigTextVerifyImpl(bigTextImpl: BigTextImpl) : BigText {
val originalLength: Int
get() = length - transformOffsetsByPosition.values.sum()

private data class TransformOp(val originalRange: IntRange, val replaceMapping: BigTextTransformOffsetMapping)
private data class TransformOp(val originalRange: IntRange, val offsetMapping: BigTextTransformOffsetMapping)

override fun buildString(): String {
val r = bigTextImpl.buildString()
Expand Down Expand Up @@ -86,6 +86,7 @@ internal class BigTextVerifyImpl(bigTextImpl: BigTextImpl) : BigText {
println("VerifyImpl pos $pos offset $it")
}
stringImpl.insertAt(pos, text)
// transformOps += TransformOp(pos until pos + text.length, BigTextTransformOffsetMapping.WholeBlock)
verify()
return r
}
Expand All @@ -104,6 +105,7 @@ internal class BigTextVerifyImpl(bigTextImpl: BigTextImpl) : BigText {
val offset = transformOffsetsByPosition.subMap(0, start).values.sum()
stringImpl.delete(offset + start, offset + endExclusive)
}
transformOps += TransformOp(start until endExclusive, BigTextTransformOffsetMapping.WholeBlock)
println("new len = ${bigTextImpl.length}")
verify()
return r
Expand Down Expand Up @@ -219,10 +221,10 @@ internal class BigTextVerifyImpl(bigTextImpl: BigTextImpl) : BigText {
val mapped = i + offsetSum
if (mapped > transformedPosition) {
if (result != null) {
return transformOps.firstOrNull { it.replaceMapping == BigTextTransformOffsetMapping.WholeBlock && it.originalRange.first == start }
return transformOps.firstOrNull { it.offsetMapping == BigTextTransformOffsetMapping.WholeBlock && it.originalRange.first == start }
?.originalRange
?.endInclusive
?.let { it + 1 }
?.let { maxOf(it + 1, result!!) }
?: result
}
return i
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ class BigTextTransformPositionCalculatorTest {
v.verifyPositionCalculation()
}

@ParameterizedTest
@ValueSource(ints = [1048576, 64, 16])
fun transformDeleteWholeText(chunkSize: Int) {
val t = BigTextImpl(chunkSize = chunkSize).apply {
append("1234567890<234567890<bcdefghij<BCDEFGHIJ<row break< should h<appen her<e.")
}
val tt = BigTextTransformerImpl(t).apply {
setLayouter(MonospaceTextLayouter(FixedWidthCharMeasurer(16f)))
setContentWidth(16f * 10)
}
val v = BigTextVerifyImpl(tt)
val originalLength = v.originalLength
v.delete(0 until originalLength)
v.verifyPositionCalculation()
}

@ParameterizedTest
@ValueSource(ints = [1048576, 64, 16])
fun simpleIncrementalTransformReplaces(chunkSize: Int) {
Expand Down Expand Up @@ -261,4 +277,110 @@ class BigTextTransformPositionCalculatorTest {
replaceMapping = BigTextTransformOffsetMapping.WholeBlock
)
}

@ParameterizedTest
@ValueSource(ints = [1048576, 64, 16])
fun mixedTransformReplaces(chunkSize: Int) {
val t = BigTextImpl(chunkSize = chunkSize).apply {
append("1234567890<234567890<bcdefghij<BCDEFGHIJ<row break< should h<appen her<e.")
}
val tt = BigTextTransformerImpl(t).apply {
setLayouter(MonospaceTextLayouter(FixedWidthCharMeasurer(16f)))
setContentWidth(16f * 10)
}
val v = BigTextVerifyImpl(tt)
val originalLength = v.originalLength

v.replace(32 .. 36, "long replacement", BigTextTransformOffsetMapping.Incremental)
v.verifyPositionCalculation()

v.replace(15 .. 23, "!?", BigTextTransformOffsetMapping.WholeBlock)
v.verifyPositionCalculation()

v.replace(0 .. 2, "-+-+-", BigTextTransformOffsetMapping.WholeBlock)
v.verifyPositionCalculation()

v.replace(originalLength - 12 until originalLength, "*-*-*", BigTextTransformOffsetMapping.Incremental)
v.verifyPositionCalculation()

v.replace(3 .. 3, "some relatively long string that is longer than a chunk", BigTextTransformOffsetMapping.Incremental)
v.verifyPositionCalculation()

v.replace(4 .. 5, "=", BigTextTransformOffsetMapping.WholeBlock)
v.verifyPositionCalculation()
}

@ParameterizedTest
@ValueSource(ints = [1048576, 64, 16])
fun mixedTransforms(chunkSize: Int) {
val t = BigTextImpl(chunkSize = chunkSize).apply {
append("1234567890<234567890<bcdefghij<BCDEFGHIJ<row break< should h<appen her<e.")
}
val tt = BigTextTransformerImpl(t).apply {
setLayouter(MonospaceTextLayouter(FixedWidthCharMeasurer(16f)))
setContentWidth(16f * 10)
}
val v = BigTextVerifyImpl(tt)
val originalLength = v.originalLength // 73

v.insertAt(8, "inserted text 8")
v.verifyPositionCalculation()

v.delete(40 .. 52)
// isD = true
v.verifyPositionCalculation()

v.replace(32 .. 36, "long replacement", BigTextTransformOffsetMapping.Incremental)
v.verifyPositionCalculation()

v.delete(26 .. 28)
v.verifyPositionCalculation()

v.insertAt(29, "XYZ")
// isD = true
v.verifyPositionCalculation()

v.delete(24 .. 24)
v.verifyPositionCalculation()

v.replace(15 .. 23, "!?", BigTextTransformOffsetMapping.WholeBlock)
isD = true
v.verifyPositionCalculation()

v.replace(0 .. 2, "-+-+-", BigTextTransformOffsetMapping.WholeBlock)
v.verifyPositionCalculation()

v.insertAt(0, "inserted text at the beginning")
v.verifyPositionCalculation()

v.replace(originalLength - 12 until originalLength - 8, "*-*-*", BigTextTransformOffsetMapping.WholeBlock)
v.verifyPositionCalculation()

v.delete(originalLength - 8 until originalLength)
v.verifyPositionCalculation()

v.replace(3 .. 3, "some relatively long string that is longer than a chunk", BigTextTransformOffsetMapping.Incremental)
v.verifyPositionCalculation()

v.insertAt(3, "inserted text 3")
v.verifyPositionCalculation()

v.replace(4 .. 5, "=", BigTextTransformOffsetMapping.WholeBlock)
v.verifyPositionCalculation()

v.insertAt(53, "^")
v.verifyPositionCalculation()

v.delete(54 .. 54)
v.verifyPositionCalculation()

v.replace(55 .. 55, "_")
v.verifyPositionCalculation()

v.delete(56 .. 59)
v.verifyPositionCalculation()

v.insertAt(56, "qwerty")
v.verifyPositionCalculation()
}
}

0 comments on commit b1b7d8b

Please sign in to comment.