forked from Kotlin/kotlinx-io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSInputStreamSourceTest.kt
More file actions
73 lines (59 loc) · 2.24 KB
/
Copy pathNSInputStreamSourceTest.kt
File metadata and controls
73 lines (59 loc) · 2.24 KB
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
/*
* Copyright 2017-2023 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
*/
package kotlinx.io
import platform.Foundation.NSInputStream
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.fail
private const val SEGMENT_SIZE = Segment.SIZE
class NSInputStreamSourceTest {
@Test
fun nsInputStreamSource() {
val nsis = NSInputStream(byteArrayOf(0x61).toNSData())
val source = nsis.asSource()
val buffer = Buffer()
source.readAtMostTo(buffer, 1)
assertEquals("a", buffer.readString())
}
@Test
fun sourceFromInputStream() {
val nsis = NSInputStream(
("a" + "b".repeat(SEGMENT_SIZE * 2) + "c").encodeToByteArray().toNSData(),
)
// Source: ab...bc
val source: RawSource = nsis.asSource()
val sink = Buffer()
// Source: b...bc. Sink: abb.
assertEquals(3, source.readAtMostTo(sink, 3))
assertEquals("abb", sink.readString(3))
// Source: b...bc. Sink: b...b.
assertEquals(SEGMENT_SIZE.toLong(), source.readAtMostTo(sink, 20000))
assertEquals("b".repeat(SEGMENT_SIZE), sink.readString())
// Source: b...bc. Sink: b...bc.
assertEquals((SEGMENT_SIZE - 1).toLong(), source.readAtMostTo(sink, 20000))
assertEquals("b".repeat(SEGMENT_SIZE - 2) + "c", sink.readString())
// Source and sink are empty.
assertEquals(-1, source.readAtMostTo(sink, 1))
}
@Test
fun sourceFromInputStreamWithSegmentSize() {
val nsis = NSInputStream(ByteArray(SEGMENT_SIZE).toNSData())
val source = nsis.asSource()
val sink = Buffer()
assertEquals(SEGMENT_SIZE.toLong(), source.readAtMostTo(sink, SEGMENT_SIZE.toLong()))
assertEquals(-1, source.readAtMostTo(sink, SEGMENT_SIZE.toLong()))
assertNoEmptySegments(sink)
}
@Test
fun sourceFromInputStreamBounds() {
val source = NSInputStream(ByteArray(100).toNSData()).asSource()
try {
source.readAtMostTo(Buffer(), -1)
fail()
} catch (expected: IllegalArgumentException) {
// expected
}
}
}