Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for splitting and chunking scrambles in PDF (#881)
* Migrate Enum.values() to Enum.entries * Add test cases for PDF chunk splitting * Yay code reuse * Add back tailrec modifiers
- Loading branch information
Showing
12 changed files
with
151 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...les/src/test/kotlin/org/worldcubeassociation/tnoodle/server/webscrambles/TokenizerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package org.worldcubeassociation.tnoodle.server.webscrambles | ||
|
||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
import org.worldcubeassociation.tnoodle.server.model.EventData | ||
import org.worldcubeassociation.tnoodle.server.webscrambles.pdf.util.FontUtil | ||
import org.worldcubeassociation.tnoodle.server.webscrambles.pdf.util.ScrambleStringUtil | ||
|
||
class TokenizerTest { | ||
@Test | ||
fun `test that tokenizing scramble strings does not lose information`() { | ||
for (event in EventData.entries) { | ||
println("Generating random scrambles for ${event.id}") | ||
|
||
event.scrambler.generateEfficientScrambles(SCRAMBLE_REPETITIONS) { | ||
val tokenizedScramble = ScrambleStringUtil.splitToTokens(it) | ||
val gluedTogetherScramble = tokenizedScramble.joinToString(ScrambleStringUtil.MOVES_DELIMITER) { (str, _) -> str.trim() } | ||
|
||
assertScramblesEqual(event, it, gluedTogetherScramble) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `test that splitting scramble strings does not lose information`() { | ||
for (event in EventData.entries) { | ||
println("Generating random scrambles for ${event.id}") | ||
|
||
event.scrambler.generateEfficientScrambles(SCRAMBLE_REPETITIONS) { | ||
val tokenizedScramble = ScrambleStringUtil.split(it) | ||
val gluedTogetherScramble = tokenizedScramble.joinToString(ScrambleStringUtil.MOVES_DELIMITER) | ||
|
||
assertScramblesEqual(event, it, gluedTogetherScramble) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `test that splitting to lines does not lose information`() { | ||
for (event in EventData.entries) { | ||
println("Generating random scrambles for ${event.id}") | ||
|
||
event.scrambler.generateEfficientScrambles(SCRAMBLE_REPETITIONS) { | ||
val tokenizedScramble = ScrambleStringUtil.splitToTokens(it) | ||
val lineSplitScramble = FontUtil.splitAtPossibleBreaks(tokenizedScramble) | ||
|
||
val gluedTogetherScramble = lineSplitScramble.joinToString(ScrambleStringUtil.MOVES_DELIMITER) { line -> | ||
line.joinToString(ScrambleStringUtil.MOVES_DELIMITER) { str -> str.trim() } | ||
} | ||
|
||
assertScramblesEqual(event, it, gluedTogetherScramble) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `test that splitting to fixed size does not lose information`() { | ||
for (event in EventData.entries) { | ||
println("Generating random scrambles for ${event.id}") | ||
|
||
event.scrambler.generateEfficientScrambles(SCRAMBLE_REPETITIONS) { | ||
val tokenizedScramble = ScrambleStringUtil.splitToTokens(it) | ||
val lineSplitScramble = FontUtil.splitAtPossibleBreaks(tokenizedScramble) | ||
|
||
for (fontSize in listOf(3f, 12f, 120f)) { | ||
for (lineWidth in listOf(12f, 120f, 1200f)) { | ||
for (unitToInches in listOf(.2f, 1f, 2f, 2000f)) { | ||
val lines = FontUtil.splitToFixedSizeLines( | ||
lineSplitScramble, | ||
fontSize, | ||
lineWidth, | ||
unitToInches, | ||
ScrambleStringUtil.MOVES_DELIMITER | ||
) | ||
|
||
val gluedTogetherScramble = lines.joinToString(ScrambleStringUtil.MOVES_DELIMITER) { str -> str.trim() } | ||
.replace(ScrambleStringUtil.NBSP_STRING, "") | ||
|
||
assertScramblesEqual(event, it, gluedTogetherScramble) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `test that splitting to max size does not lose information`() { | ||
for (event in EventData.entries) { | ||
println("Generating random scrambles for ${event.id}") | ||
|
||
event.scrambler.generateEfficientScrambles(SCRAMBLE_REPETITIONS) { | ||
val tokenizedScramble = ScrambleStringUtil.splitToTokens(it) | ||
val lineSplitScramble = FontUtil.splitAtPossibleBreaks(tokenizedScramble) | ||
|
||
for (boxHeight in listOf(12f, 120f, 1200f)) { | ||
for (boxWidth in listOf(12f, 120f, 1200f)) { | ||
for (leadingFactor in listOf(.9f, 1.2f, 2f)) { | ||
val lines = FontUtil.splitToMaxFontSizeLines( | ||
lineSplitScramble, | ||
boxHeight, | ||
boxWidth, | ||
leadingFactor, | ||
ScrambleStringUtil.MOVES_DELIMITER | ||
) | ||
|
||
val gluedTogetherScramble = lines.joinToString(ScrambleStringUtil.MOVES_DELIMITER) { str -> str.trim() } | ||
.replace(ScrambleStringUtil.NBSP_STRING, "") | ||
|
||
assertScramblesEqual(event, it, gluedTogetherScramble) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
const val SCRAMBLE_REPETITIONS = 20 | ||
|
||
fun assertScramblesEqual(event: EventData, original: String, reconstructed: String) { | ||
// With Megaminx, it is a bit bothersome to reconstruct where the original newlines were. | ||
// Since they are cosmetical anyways, we just ignore them even in the original scramble. | ||
val originalScramble = if (event.id == "minx") original.replace("\n", " ") else original | ||
|
||
Assertions.assertEquals(originalScramble, reconstructed) | ||
} | ||
} | ||
} |