Skip to content

Commit

Permalink
Add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
jzbrooks committed Nov 29, 2024
1 parent 64a7679 commit f7452e2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class MergePaths(
val currentLength = current.commands.joinToString("", transform = constraints.commandPrinter::print).length
val accumulatedLength = pathLength + currentLength

if (accumulatedLength >= constraints.maxLength || unableToMerge(previous, current)) {
if (accumulatedLength > constraints.maxLength || unableToMerge(previous, current)) {
mergedPaths.add(current)
pathLength = currentLength
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.jzbrooks.vgo.core.graphic.Group
import com.jzbrooks.vgo.core.graphic.command.Command
import com.jzbrooks.vgo.core.graphic.command.CommandVariant
import com.jzbrooks.vgo.core.graphic.command.EllipticalArcCurve
import com.jzbrooks.vgo.core.graphic.command.FakeCommandPrinter
import com.jzbrooks.vgo.core.graphic.command.LineTo
import com.jzbrooks.vgo.core.graphic.command.MoveTo
import com.jzbrooks.vgo.core.graphic.command.QuadraticBezierCurve
Expand Down Expand Up @@ -417,4 +418,41 @@ class MergePathsTests {

assertThat(graphic::elements).hasSize(2)
}

@Test
fun pathLengthConstraints() {
val paths =
listOf(
createPath(
listOf(MoveTo(CommandVariant.ABSOLUTE, listOf(Point(0f, 0f)))),
),
createPath(
listOf(MoveTo(CommandVariant.ABSOLUTE, listOf(Point(10f, 10f)))),
),
createPath(
listOf(MoveTo(CommandVariant.ABSOLUTE, listOf(Point(40f, 40f)))),
),
createPath(
listOf(MoveTo(CommandVariant.ABSOLUTE, listOf(Point(50f, 50f), Point(10f, 10f), Point(20f, 30f), Point(40f, 0f)))),
),
)

val graphic = createGraphic(paths)
val optimization = MergePaths(MergePaths.Constraints.PathLength(FakeCommandPrinter(), 16))

traverseBottomUp(graphic) { it.accept(optimization) }

assertThat(graphic::elements).containsExactly(
createPath(
listOf(
MoveTo(CommandVariant.ABSOLUTE, listOf(Point(0f, 0f))),
MoveTo(CommandVariant.ABSOLUTE, listOf(Point(10f, 10f))),
MoveTo(CommandVariant.ABSOLUTE, listOf(Point(40f, 40f))),
),
),
createPath(
listOf(MoveTo(CommandVariant.ABSOLUTE, listOf(Point(50f, 50f), Point(10f, 10f), Point(20f, 30f), Point(40f, 0f)))),
),
)
}
}

0 comments on commit f7452e2

Please sign in to comment.