summaryrefslogtreecommitdiff
path: root/src/test/kotlin/moe/nea89/mail/util/ProtocolSpec.kt
blob: 9a4bcbf12bb8b8219cbdfbf09e8f857e50111b3e (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
package moe.nea89.mail.util

import Protocol
import findSubarray
import io.kotest.core.spec.style.FreeSpec
import io.kotest.datatest.withData
import readUntil
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream


data class ByteArrayString(val hay: String, val needle: String, val position: Int?, val offset: Int = 0)
class ProtocolSpec : FreeSpec({
    "ByteArray.findSubarray" - {
        "should work correctly" - {
            withData(
                listOf(
                    ByteArrayString("abc", "a", 0),
                    ByteArrayString("abca", "a", 0),
                    ByteArrayString("abca", "a", 3, 2),
                    ByteArrayString("abca", "a", 3, 3),
                    ByteArrayString("bc", "a", null),
                    ByteArrayString("acbcab", "ab", 4),
                    ByteArrayString("abcbcab", "ab", 5, 1),
                )
            ) { (hay, needle, position, offset) ->
                val hay = hay.encodeToByteArray()
                val needle = needle.encodeToByteArray()
                assert(hay.findSubarray(needle, offset) == position)
            }
        }
    }
    "Protocol.IO" - {
        "readUntil" {
            val data = ("a".repeat(9) + "01" + "b".repeat(10) + "02").encodeToByteArray()
            val protIO = Protocol.IO.FromStreams(ByteArrayInputStream(data), ByteArrayOutputStream())
            assert(protIO.readUntil("01".encodeToByteArray()).decodeToString() == "a".repeat(9))
            assert(protIO.readUntil("02".encodeToByteArray()).decodeToString() == "b".repeat(10))
        }
    }
})