aboutsummaryrefslogtreecommitdiff
path: root/okio/src/commonTest/kotlin/okio/Utf8KotlinTest.kt
blob: 1ade417948e64f57ab3a28e55516315146219e7f (plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright (C) 2018 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package okio

import okio.ByteString.Companion.decodeHex
import okio.internal.commonAsUtf8ToByteArray
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class Utf8KotlinTest {
  @Test fun oneByteCharacters() {
    assertEncoded("00", 0x00) // Smallest 1-byte character.
    assertEncoded("20", ' '.toInt())
    assertEncoded("7e", '~'.toInt())
    assertEncoded("7f", 0x7f) // Largest 1-byte character.
  }

  @Test fun twoByteCharacters() {
    assertEncoded("c280", 0x0080) // Smallest 2-byte character.
    assertEncoded("c3bf", 0x00ff)
    assertEncoded("c480", 0x0100)
    assertEncoded("dfbf", 0x07ff) // Largest 2-byte character.
  }

  @Test fun threeByteCharacters() {
    assertEncoded("e0a080", 0x0800) // Smallest 3-byte character.
    assertEncoded("e0bfbf", 0x0fff)
    assertEncoded("e18080", 0x1000)
    assertEncoded("e1bfbf", 0x1fff)
    assertEncoded("ed8080", 0xd000)
    assertEncoded("ed9fbf", 0xd7ff) // Largest character lower than the min surrogate.
    assertEncoded("ee8080", 0xe000) // Smallest character greater than the max surrogate.
    assertEncoded("eebfbf", 0xefff)
    assertEncoded("ef8080", 0xf000)
    assertEncoded("efbfbf", 0xffff) // Largest 3-byte character.
  }

  @Test fun fourByteCharacters() {
    assertEncoded("f0908080", 0x010000) // Smallest surrogate pair.
    assertEncoded("f48fbfbf", 0x10ffff) // Largest code point expressible by UTF-16.
  }

  @Test fun unknownBytes() {
    assertCodePointDecoded("f8", REPLACEMENT_CODE_POINT) // Too large
    assertCodePointDecoded("f0f8", REPLACEMENT_CODE_POINT, REPLACEMENT_CODE_POINT)
    assertCodePointDecoded("ff", REPLACEMENT_CODE_POINT) // Largest
    assertCodePointDecoded("f0ff", REPLACEMENT_CODE_POINT, REPLACEMENT_CODE_POINT)

    // Lone continuation
    assertCodePointDecoded("80", REPLACEMENT_CODE_POINT) // Smallest
    assertCodePointDecoded("bf", REPLACEMENT_CODE_POINT) // Largest
  }

  @Test fun overlongSequences() {
    // Overlong representation of the NUL character
    assertCodePointDecoded("c080", REPLACEMENT_CODE_POINT)
    assertCodePointDecoded("e08080", REPLACEMENT_CODE_POINT)
    assertCodePointDecoded("f0808080", REPLACEMENT_CODE_POINT)

    // Maximum overlong sequences
    assertCodePointDecoded("c1bf", REPLACEMENT_CODE_POINT)
    assertCodePointDecoded("e09fbf", REPLACEMENT_CODE_POINT)
    assertCodePointDecoded("f08fbfbf", REPLACEMENT_CODE_POINT)
  }

  @Test fun danglingHighSurrogate() {
    assertStringEncoded("3f", "\ud800") // "?"
    assertCodePointDecoded("eda080", REPLACEMENT_CODE_POINT)
  }

  @Test fun lowSurrogateWithoutHighSurrogate() {
    assertStringEncoded("3f", "\udc00") // "?"
    assertCodePointDecoded("edb080", REPLACEMENT_CODE_POINT)
  }

  @Test fun highSurrogateFollowedByNonSurrogate() {
    assertStringEncoded("3fee8080", "\ud800\ue000") // "?\ue000": Following character is too high.
    assertCodePointDecoded("f090ee8080", REPLACEMENT_CODE_POINT, '\ue000'.toInt())

    assertStringEncoded("3f61", "\ud800\u0061") // "?a": Following character is too low.
    assertCodePointDecoded("f09061", REPLACEMENT_CODE_POINT, 'a'.toInt())
  }

  @Test fun doubleLowSurrogate() {
    assertStringEncoded("3f3f", "\udc00\udc00") // "??"
    assertCodePointDecoded("edb080edb080", REPLACEMENT_CODE_POINT, REPLACEMENT_CODE_POINT)
  }

  @Test fun doubleHighSurrogate() {
    assertStringEncoded("3f3f", "\ud800\ud800") // "??"
    assertCodePointDecoded("eda080eda080", REPLACEMENT_CODE_POINT, REPLACEMENT_CODE_POINT)
  }

  @Test fun lowSurrogateHighSurrogate() {
    assertStringEncoded("3f3f", "\udc00\ud800") // "??"
    assertCodePointDecoded("edb080eda080", REPLACEMENT_CODE_POINT, REPLACEMENT_CODE_POINT)
  }

  @Test fun writeSurrogateCodePoint() {
    assertStringEncoded("ed9fbf", "\ud7ff") // Below lowest surrogate is okay.
    assertCodePointDecoded("ed9fbf", '\ud7ff'.toInt())

    assertStringEncoded("3f", "\ud800") // Lowest surrogate gets '?'.
    assertCodePointDecoded("eda080", REPLACEMENT_CODE_POINT)

    assertStringEncoded("3f", "\udfff") // Highest surrogate gets '?'.
    assertCodePointDecoded("edbfbf", REPLACEMENT_CODE_POINT)

    assertStringEncoded("ee8080", "\ue000") // Above highest surrogate is okay.
    assertCodePointDecoded("ee8080", '\ue000'.toInt())
  }

  @Test fun size() {
    assertEquals(0, "".utf8Size())
    assertEquals(3, "abc".utf8Size())
    assertEquals(16, "təˈranəˌsôr".utf8Size())
  }

  @Test fun sizeWithBounds() {
    assertEquals(0, "".utf8Size(0, 0))
    assertEquals(0, "abc".utf8Size(0, 0))
    assertEquals(1, "abc".utf8Size(1, 2))
    assertEquals(2, "abc".utf8Size(0, 2))
    assertEquals(3, "abc".utf8Size(0, 3))
    assertEquals(16, "təˈranəˌsôr".utf8Size(0, 11))
    assertEquals(5, "təˈranəˌsôr".utf8Size(3, 7))
  }

  @Test fun sizeBoundsCheck() {
    assertFailsWith<IllegalArgumentException> {
      "abc".utf8Size(-1, 2)
    }

    assertFailsWith<IllegalArgumentException> {
      "abc".utf8Size(2, 1)
    }

    assertFailsWith<IllegalArgumentException> {
      "abc".utf8Size(1, 4)
    }
  }

  private fun assertEncoded(hex: String, vararg codePoints: Int) {
    assertCodePointDecoded(hex, *codePoints)
  }

  private fun assertCodePointDecoded(hex: String, vararg codePoints: Int) {
    val bytes = hex.decodeHex().toByteArray()
    var i = 0
    bytes.processUtf8CodePoints(0, bytes.size) { codePoint ->
      if (i < codePoints.size) assertEquals(codePoints[i], codePoint, "index=$i")
      i++
    }
    assertEquals(i, codePoints.size) // Checked them all
  }

  private fun assertStringEncoded(hex: String, string: String) {
    val expectedUtf8 = hex.decodeHex()

    // Confirm our expectations are consistent with the platform.
    val platformUtf8 = ByteString.of(*string.asUtf8ToByteArray())
    assertEquals(expectedUtf8, platformUtf8)

    // Confirm our implementations matches those expectations.
    val actualUtf8 = ByteString.of(*string.commonAsUtf8ToByteArray())
    assertEquals(expectedUtf8, actualUtf8)

    // TODO Confirm we are consistent when writing one code point at a time.

    // Confirm we are consistent when measuring lengths.
    assertEquals(expectedUtf8.size.toLong(), string.utf8Size())
    assertEquals(expectedUtf8.size.toLong(), string.utf8Size(0, string.length))
  }
}