-
Notifications
You must be signed in to change notification settings - Fork 92
/
TokenizerTest.kt
129 lines (105 loc) · 5.51 KB
/
TokenizerTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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)
}
}
}