Skip to content

Commit f38b5e6

Browse files
authored
Merge pull request #15 from cketti/remove_platform_test
Remove `PlatformComparisonTest` & change `CommonCodePoints.toChars()` behavior
2 parents 3d5df93 + 1bd0900 commit f38b5e6

File tree

5 files changed

+51
-111
lines changed

5 files changed

+51
-111
lines changed

build.gradle.kts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ kotlin {
2828
compilations.all {
2929
kotlinOptions.jvmTarget = "1.8"
3030
}
31-
testRuns["test"].executionTask.configure {
32-
useJUnitPlatform()
33-
}
3431
}
3532

3633
linuxArm64()
@@ -67,10 +64,6 @@ kotlin {
6764
val commonImplementation by creating {
6865
dependsOn(commonMain)
6966
}
70-
71-
val jvmTest by getting {
72-
dependsOn(commonImplementation)
73-
}
7467
}
7568

7669
targets.onEach {

src/commonImplementation/kotlin/CommonCodePoints.kt

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,24 @@ object CommonCodePoints {
5454
}
5555

5656
fun toChars(codePoint: Int, destination: CharArray, offset: Int): Int {
57-
val size = destination.size
58-
if (offset >= 0) {
59-
if (isBmpCodePoint(codePoint)) {
60-
if (offset < size) {
61-
destination[offset] = codePoint.toChar()
62-
return 1
63-
}
64-
} else if (offset < size - 1) {
65-
destination[offset] = highSurrogate(codePoint)
66-
destination[offset + 1] = lowSurrogate(codePoint)
67-
return 2
68-
}
57+
if (isBmpCodePoint(codePoint)) {
58+
destination.setSafe(offset, codePoint.toChar())
59+
return 1
60+
} else {
61+
// When writing the low surrogate succeeds but writing the high surrogate fails (offset = -1), the
62+
// destination will be modified even though the method throws. This feels wrong, but matches the behavior
63+
// of the Java stdlib implementation.
64+
destination.setSafe(offset + 1, lowSurrogate(codePoint))
65+
destination.setSafe(offset, highSurrogate(codePoint))
66+
return 2
6967
}
70-
throw IndexOutOfBoundsException("Size: $size, offset: $offset")
68+
}
69+
70+
private fun CharArray.setSafe(index: Int, value: Char) {
71+
if (index !in this.indices) {
72+
throw IndexOutOfBoundsException("Size: $size, offset: $index")
73+
}
74+
75+
this[index] = value
7176
}
7277
}

src/commonTest/kotlin/CodePointsTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ class CodePointsTest {
140140
assertFailsWith<IndexOutOfBoundsException> {
141141
CodePoints.toChars("\uD83E\uDD95".codePointAt(0), chars, -1)
142142
}
143-
assertContentEquals(charArrayOf('z', 'z'), chars)
143+
// We're bug-compatible with the Java stdlib implementation
144+
assertContentEquals(charArrayOf('\uDD95', 'z'), chars)
144145
}
145146

146147
@Test

src/commonTest/kotlin/StringExtensionsTest.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package de.cketti.codepoints
22

33
import kotlin.test.assertEquals
44
import kotlin.test.Test
5+
import kotlin.test.assertFailsWith
56

67
class StringExtensionsTest {
78
@Test
@@ -120,4 +121,34 @@ class StringExtensionsTest {
120121
assertEquals(0, "\uD83E\uDD95\uD83E\uDD96".offsetByCodePoints(index = 3, codePointOffset = -2))
121122
assertEquals(0, "\uD83E\uDD95\uD83E\uDD96".offsetByCodePoints(index = 1, codePointOffset = -1))
122123
}
124+
125+
@Test
126+
fun offsetByCodePoints_with_invalid_index() {
127+
assertFailsWith<IndexOutOfBoundsException> {
128+
"a".offsetByCodePoints(index = -1, codePointOffset = 0)
129+
}
130+
131+
assertFailsWith<IndexOutOfBoundsException> {
132+
"a".offsetByCodePoints(index = 2, codePointOffset = 0)
133+
}
134+
}
135+
136+
@Test
137+
fun offsetByCodePoints_with_invalid_codePointOffset() {
138+
assertFailsWith<IndexOutOfBoundsException> {
139+
"a".offsetByCodePoints(index = 0, codePointOffset = 2)
140+
}
141+
142+
assertFailsWith<IndexOutOfBoundsException> {
143+
"a".offsetByCodePoints(index = 1, codePointOffset = -2)
144+
}
145+
146+
assertFailsWith<IndexOutOfBoundsException> {
147+
"\uD83E\uDD95".offsetByCodePoints(index = 0, codePointOffset = 2)
148+
}
149+
150+
assertFailsWith<IndexOutOfBoundsException> {
151+
"\uD83E\uDD95".offsetByCodePoints(index = 2, codePointOffset = -2)
152+
}
153+
}
123154
}

src/jvmTest/kotlin/PlatformComparisonTest.kt

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)