Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | */ |
| 4 | |
Sergey Mashkov | 569b542 | 2017-07-25 12:17:11 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines.experimental.io |
| 6 | |
Roman Elizarov | c45f5fa | 2018-01-11 13:48:19 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 8 | import org.junit.Test |
Sergey Mashkov | 569b542 | 2017-07-25 12:17:11 +0300 | [diff] [blame] | 9 | import java.util.* |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 10 | import kotlin.test.assertEquals |
| 11 | import kotlin.test.assertFalse |
| 12 | import kotlin.test.assertTrue |
Sergey Mashkov | 569b542 | 2017-07-25 12:17:11 +0300 | [diff] [blame] | 13 | |
Roman Elizarov | c45f5fa | 2018-01-11 13:48:19 +0300 | [diff] [blame] | 14 | class ContentByteBufferTest : TestBase() { |
Sergey Mashkov | 569b542 | 2017-07-25 12:17:11 +0300 | [diff] [blame] | 15 | @Test |
| 16 | fun testEmptyContent() = runBlocking { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 17 | val ch = ByteReadChannel(ByteArray(0)) |
| 18 | assertEquals(0, ch.availableForRead) |
| 19 | assertEquals(-1, ch.readAvailable(ByteBuffer.allocate(100))) |
| 20 | assertTrue { ch.isClosedForRead } |
Sergey Mashkov | 569b542 | 2017-07-25 12:17:11 +0300 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | @Test |
| 24 | fun testSingleByteContent() = runBlocking { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 25 | val ch = ByteReadChannel(byteArrayOf(1)) |
| 26 | assertEquals(1, ch.availableForRead) |
| 27 | assertFalse { ch.isClosedForRead } |
| 28 | assertEquals(1, ch.readAvailable(ByteBuffer.allocate(100))) |
| 29 | assertEquals(0, ch.availableForRead) |
| 30 | assertTrue { ch.isClosedForRead } |
Sergey Mashkov | 569b542 | 2017-07-25 12:17:11 +0300 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | @Test |
| 34 | fun testSingleByteContent2() = runBlocking { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 35 | val ch = ByteReadChannel(byteArrayOf(0x34)) |
| 36 | assertEquals(1, ch.availableForRead) |
| 37 | assertFalse { ch.isClosedForRead } |
Sergey Mashkov | 569b542 | 2017-07-25 12:17:11 +0300 | [diff] [blame] | 38 | assertEquals(0x34, ch.readByte()) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 39 | assertEquals(0, ch.availableForRead) |
| 40 | assertTrue { ch.isClosedForRead } |
Sergey Mashkov | 569b542 | 2017-07-25 12:17:11 +0300 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Test |
| 44 | fun testMultipleByteContent2() = runBlocking { |
| 45 | val arr = ByteArray(16) |
| 46 | Random().nextBytes(arr) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 47 | val ch = ByteReadChannel(arr) |
| 48 | assertEquals(16, ch.availableForRead) |
| 49 | assertFalse { ch.isClosedForRead } |
Sergey Mashkov | 569b542 | 2017-07-25 12:17:11 +0300 | [diff] [blame] | 50 | ch.readByte() |
| 51 | ch.readShort() |
| 52 | ch.readInt() |
| 53 | ch.readLong() |
| 54 | ch.readByte() |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 55 | assertTrue { ch.isClosedForRead } |
Sergey Mashkov | 569b542 | 2017-07-25 12:17:11 +0300 | [diff] [blame] | 56 | } |
| 57 | } |