Skip to content
Snippets Groups Projects
Verified Commit 075c4c06 authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Further tests

parent 00aa4fb3
No related branches found
No related tags found
1 merge request!2Draft: Jetpack compose rewrite
Pipeline #577 passed
Showing
with 386 additions and 153 deletions
...@@ -30,7 +30,7 @@ import kotlin.concurrent.getOrSet ...@@ -30,7 +30,7 @@ import kotlin.concurrent.getOrSet
abstract class StringSerializer( abstract class StringSerializer(
private val charset: Charset, private val charset: Charset,
private val nullLimited: Boolean = false, private val nullLimited: Boolean,
) : QtSerializer<String?> { ) : QtSerializer<String?> {
override val qtType = QtType.QString override val qtType = QtType.QString
override val javaType: Class<out String> = String::class.java override val javaType: Class<out String> = String::class.java
...@@ -38,10 +38,8 @@ abstract class StringSerializer( ...@@ -38,10 +38,8 @@ abstract class StringSerializer(
private val encoderLocal = ThreadLocal<StringEncoder>() private val encoderLocal = ThreadLocal<StringEncoder>()
private fun encoder() = encoderLocal.getOrSet { StringEncoder(charset) } private fun encoder() = encoderLocal.getOrSet { StringEncoder(charset) }
@Suppress("NOTHING_TO_INLINE") private fun addNullBytes(before: Int) = if (nullLimited) before + 1 else before
private inline fun addNullBytes(before: Int) = if (nullLimited) before + 1 else before private fun removeNullBytes(before: Int) = if (nullLimited) before - 1 else before
@Suppress("NOTHING_TO_INLINE")
private inline fun removeNullBytes(before: Int) = if (nullLimited) before - 1 else before
override fun serialize(buffer: ChainedByteBuffer, data: String?, featureSet: FeatureSet) { override fun serialize(buffer: ChainedByteBuffer, data: String?, featureSet: FeatureSet) {
if (data == null) { if (data == null) {
......
...@@ -19,4 +19,4 @@ ...@@ -19,4 +19,4 @@
package de.kuschku.libquassel.protocol.serializers.primitive package de.kuschku.libquassel.protocol.serializers.primitive
object StringSerializerUtf16 : StringSerializer(Charsets.UTF_16BE) object StringSerializerUtf16 : StringSerializer(Charsets.UTF_16BE, false)
...@@ -19,4 +19,4 @@ ...@@ -19,4 +19,4 @@
package de.kuschku.libquassel.protocol.serializers.primitive package de.kuschku.libquassel.protocol.serializers.primitive
object StringSerializerUtf8 : StringSerializer(Charsets.UTF_8) object StringSerializerUtf8 : StringSerializer(Charsets.UTF_8, false)
...@@ -30,12 +30,12 @@ object TimeSerializer : QtSerializer<LocalTime> { ...@@ -30,12 +30,12 @@ object TimeSerializer : QtSerializer<LocalTime> {
override val javaType: Class<out LocalTime> = LocalTime::class.java override val javaType: Class<out LocalTime> = LocalTime::class.java
override fun serialize(buffer: ChainedByteBuffer, data: LocalTime, featureSet: FeatureSet) { override fun serialize(buffer: ChainedByteBuffer, data: LocalTime, featureSet: FeatureSet) {
val millisecondOfDay = (data.toNanoOfDay() / 1000).toInt() val millisecondOfDay = (data.toNanoOfDay() / 1_000_000).toInt()
IntSerializer.serialize(buffer, millisecondOfDay, featureSet) IntSerializer.serialize(buffer, millisecondOfDay, featureSet)
} }
override fun deserialize(buffer: ByteBuffer, featureSet: FeatureSet): LocalTime { override fun deserialize(buffer: ByteBuffer, featureSet: FeatureSet): LocalTime {
val millisecondOfDay = IntSerializer.deserialize(buffer, featureSet).toLong() val millisecondOfDay = IntSerializer.deserialize(buffer, featureSet).toLong()
return LocalTime.ofNanoOfDay(millisecondOfDay * 1000) return LocalTime.ofNanoOfDay(millisecondOfDay * 1_000_000)
} }
} }
...@@ -21,6 +21,8 @@ package de.kuschku.libquassel.protocol.types ...@@ -21,6 +21,8 @@ package de.kuschku.libquassel.protocol.types
import de.kuschku.bitflags.Flag import de.kuschku.bitflags.Flag
import de.kuschku.bitflags.Flags import de.kuschku.bitflags.Flags
import de.kuschku.bitflags.toEnumSet
import de.kuschku.libquassel.protocol.features.LegacyFeature
enum class BufferActivity( enum class BufferActivity(
override val value: UInt, override val value: UInt,
...@@ -33,7 +35,7 @@ enum class BufferActivity( ...@@ -33,7 +35,7 @@ enum class BufferActivity(
companion object : Flags<UInt, BufferActivity> { companion object : Flags<UInt, BufferActivity> {
private val values = values().associateBy(BufferActivity::value) private val values = values().associateBy(BufferActivity::value)
override fun get(value: UInt) = values[value] override fun get(value: UInt) = values[value]
override fun all() = values.values override val all: BufferActivities = values.values.toEnumSet()
} }
} }
......
...@@ -19,12 +19,12 @@ ...@@ -19,12 +19,12 @@
package de.kuschku.libquassel.protocol.types package de.kuschku.libquassel.protocol.types
import de.kuschku.bitflags.of import de.kuschku.bitflags.none
data class BufferInfo( data class BufferInfo(
val bufferId: BufferId = BufferId(-1), val bufferId: BufferId = BufferId(-1),
val networkId: NetworkId = NetworkId(-1), val networkId: NetworkId = NetworkId(-1),
val type: BufferTypes = BufferType.of(), val type: BufferTypes = BufferType.none(),
val groupId: Int = -1, val groupId: Int = -1,
val bufferName: String? = null, val bufferName: String? = null,
) )
...@@ -21,6 +21,7 @@ package de.kuschku.libquassel.protocol.types ...@@ -21,6 +21,7 @@ package de.kuschku.libquassel.protocol.types
import de.kuschku.bitflags.Flag import de.kuschku.bitflags.Flag
import de.kuschku.bitflags.Flags import de.kuschku.bitflags.Flags
import de.kuschku.bitflags.toEnumSet
enum class BufferType( enum class BufferType(
override val value: UShort, override val value: UShort,
...@@ -34,7 +35,7 @@ enum class BufferType( ...@@ -34,7 +35,7 @@ enum class BufferType(
companion object : Flags<UShort, BufferType> { companion object : Flags<UShort, BufferType> {
private val values = values().associateBy(BufferType::value) private val values = values().associateBy(BufferType::value)
override fun get(value: UShort) = values[value] override fun get(value: UShort) = values[value]
override fun all() = values.values override val all: BufferTypes = values.values.toEnumSet()
} }
} }
......
...@@ -21,6 +21,7 @@ package de.kuschku.libquassel.protocol.types ...@@ -21,6 +21,7 @@ package de.kuschku.libquassel.protocol.types
import de.kuschku.bitflags.Flag import de.kuschku.bitflags.Flag
import de.kuschku.bitflags.Flags import de.kuschku.bitflags.Flags
import de.kuschku.bitflags.toEnumSet
enum class MessageFlag( enum class MessageFlag(
override val value: UInt, override val value: UInt,
...@@ -34,7 +35,7 @@ enum class MessageFlag( ...@@ -34,7 +35,7 @@ enum class MessageFlag(
companion object : Flags<UInt, MessageFlag> { companion object : Flags<UInt, MessageFlag> {
private val values = values().associateBy(MessageFlag::value) private val values = values().associateBy(MessageFlag::value)
override fun get(value: UInt) = values[value] override fun get(value: UInt) = values[value]
override fun all() = values.values override val all: MessageFlags = values.values.toEnumSet()
} }
} }
......
...@@ -21,6 +21,7 @@ package de.kuschku.libquassel.protocol.types ...@@ -21,6 +21,7 @@ package de.kuschku.libquassel.protocol.types
import de.kuschku.bitflags.Flag import de.kuschku.bitflags.Flag
import de.kuschku.bitflags.Flags import de.kuschku.bitflags.Flags
import de.kuschku.bitflags.toEnumSet
enum class MessageType( enum class MessageType(
override val value: UInt, override val value: UInt,
...@@ -48,7 +49,7 @@ enum class MessageType( ...@@ -48,7 +49,7 @@ enum class MessageType(
companion object : Flags<UInt, MessageType> { companion object : Flags<UInt, MessageType> {
private val values = values().associateBy(MessageType::value) private val values = values().associateBy(MessageType::value)
override fun get(value: UInt) = values[value] override fun get(value: UInt) = values[value]
override fun all() = values.values override val all: MessageTypes = values.values.toEnumSet()
} }
} }
......
...@@ -38,42 +38,10 @@ sealed class QVariant<T> constructor( ...@@ -38,42 +38,10 @@ sealed class QVariant<T> constructor(
open val serializer: QtSerializer<T>, open val serializer: QtSerializer<T>,
) { ) {
class Typed<T> internal constructor(data: T, serializer: QtSerializer<T>) : class Typed<T> internal constructor(data: T, serializer: QtSerializer<T>) :
QVariant<T>(data, serializer) { QVariant<T>(data, serializer)
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Typed<*>) return false
if (data != other.data) return false
if (serializer.qtType != other.serializer.qtType) return false
return true
}
override fun hashCode(): Int {
var result = data?.hashCode() ?: 0
result = 31 * result + serializer.qtType.hashCode()
return result
}
}
class Custom<T> internal constructor(data: T, override val serializer: QuasselSerializer<T>) : class Custom<T> internal constructor(data: T, override val serializer: QuasselSerializer<T>) :
QVariant<T>(data, serializer) { QVariant<T>(data, serializer)
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Custom<*>) return false
if (data != other.data) return false
if (serializer.quasselType != other.serializer.quasselType) return false
return true
}
override fun hashCode(): Int {
var result = data?.hashCode() ?: 0
result = 31 * result + serializer.quasselType.hashCode()
return result
}
}
fun value(): T = data fun value(): T = data
......
...@@ -105,8 +105,7 @@ enum class QtType(val id: kotlin.Int) { ...@@ -105,8 +105,7 @@ enum class QtType(val id: kotlin.Int) {
QVariant(138),//, VariantSerializer), QVariant(138),//, VariantSerializer),
User(256), User(256),
UserType(127), UserType(127);
LastType(-1);
val serializableName = val serializableName =
if (name.startsWith("Q")) name if (name.startsWith("Q")) name
......
...@@ -37,7 +37,8 @@ enum class QuasselType( ...@@ -37,7 +37,8 @@ enum class QuasselType(
NetworkInfo("NetworkInfo"), NetworkInfo("NetworkInfo"),
NetworkServer("Network::Server"), NetworkServer("Network::Server"),
QHostAddress("QHostAddress"), QHostAddress("QHostAddress"),
PeerPtr("PeerPtr"); PeerPtr("PeerPtr"),
Unknown("");
companion object { companion object {
private val values = values().associateBy(QuasselType::typeName) private val values = values().associateBy(QuasselType::typeName)
......
...@@ -16,46 +16,37 @@ ...@@ -16,46 +16,37 @@
* You should have received a copy of the GNU General Public License along * You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package de.kuschku.libquassel.protocol.serializers.handshake package de.kuschku.libquassel.protocol.serializers.handshake
import de.kuschku.bitflags.none
import de.kuschku.libquassel.protocol.features.FeatureSet import de.kuschku.libquassel.protocol.features.FeatureSet
import de.kuschku.libquassel.protocol.features.LegacyFeature
import de.kuschku.libquassel.protocol.messages.handshake.ClientInit import de.kuschku.libquassel.protocol.messages.handshake.ClientInit
import de.kuschku.libquassel.protocol.testutil.byteBufferOf import de.kuschku.libquassel.protocol.testutil.byteBufferOf
import de.kuschku.libquassel.protocol.testutil.testDeserialize import de.kuschku.libquassel.protocol.testutil.handshakeSerializerTest
import de.kuschku.libquassel.protocol.testutil.testHandshakeSerializerDirect import org.junit.jupiter.api.Test
import de.kuschku.libquassel.protocol.testutil.testHandshakeSerializerEncoded
import org.junit.Test
class ClientInitSerializerTest { class ClientInitSerializerTest {
@Test @Test
fun testSimple() { fun testSimple() = handshakeSerializerTest(
val value = ClientInit( ClientInitSerializer,
ClientInit(
clientVersion = "Quasseldroid test", clientVersion = "Quasseldroid test",
buildDate = "Never", buildDate = "Never",
clientFeatures = flags(), clientFeatures = LegacyFeature.none(),
featureList = emptyList() featureList = emptyList()
),
byteBufferOf(0x00u, 0x00u, 0x00u, 0x0Au, 0x00u, 0x00u, 0x00u, 0x0Cu, 0x00u, 0x00u, 0x00u, 0x00u, 0x07u, 0x4Du, 0x73u, 0x67u, 0x54u, 0x79u, 0x70u, 0x65u, 0x00u, 0x00u, 0x00u, 0x0Au, 0x00u, 0x00u, 0x00u, 0x00u, 0x14u, 0x00u, 0x43u, 0x00u, 0x6Cu, 0x00u, 0x69u, 0x00u, 0x65u, 0x00u, 0x6Eu, 0x00u, 0x74u, 0x00u, 0x49u, 0x00u, 0x6Eu, 0x00u, 0x69u, 0x00u, 0x74u, 0x00u, 0x00u, 0x00u, 0x0Cu, 0x00u, 0x00u, 0x00u, 0x00u, 0x0Du, 0x43u, 0x6Cu, 0x69u, 0x65u, 0x6Eu, 0x74u, 0x56u, 0x65u, 0x72u, 0x73u, 0x69u, 0x6Fu, 0x6Eu, 0x00u, 0x00u, 0x00u, 0x0Au, 0x00u, 0x00u, 0x00u, 0x00u, 0x22u, 0x00u, 0x51u, 0x00u, 0x75u, 0x00u, 0x61u, 0x00u, 0x73u, 0x00u, 0x73u, 0x00u, 0x65u, 0x00u, 0x6Cu, 0x00u, 0x64u, 0x00u, 0x72u, 0x00u, 0x6Fu, 0x00u, 0x69u, 0x00u, 0x64u, 0x00u, 0x20u, 0x00u, 0x74u, 0x00u, 0x65u, 0x00u, 0x73u, 0x00u, 0x74u, 0x00u, 0x00u, 0x00u, 0x0Cu, 0x00u, 0x00u, 0x00u, 0x00u, 0x0Au, 0x43u, 0x6Cu, 0x69u, 0x65u, 0x6Eu, 0x74u, 0x44u, 0x61u, 0x74u, 0x65u, 0x00u, 0x00u, 0x00u, 0x0Au, 0x00u, 0x00u, 0x00u, 0x00u, 0x0Au, 0x00u, 0x4Eu, 0x00u, 0x65u, 0x00u, 0x76u, 0x00u, 0x65u, 0x00u, 0x72u, 0x00u, 0x00u, 0x00u, 0x0Cu, 0x00u, 0x00u, 0x00u, 0x00u, 0x08u, 0x46u, 0x65u, 0x61u, 0x74u, 0x75u, 0x72u, 0x65u, 0x73u, 0x00u, 0x00u, 0x00u, 0x03u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x0Cu, 0x00u, 0x00u, 0x00u, 0x00u, 0x0Bu, 0x46u, 0x65u, 0x61u, 0x74u, 0x75u, 0x72u, 0x65u, 0x4Cu, 0x69u, 0x73u, 0x74u, 0x00u, 0x00u, 0x00u, 0x0Bu, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u)
) )
testHandshakeSerializerDirect(ClientInitSerializer, value)
testHandshakeSerializerEncoded(ClientInitSerializer, value)
// @formatter:off
testDeserialize(ClientInitSerializer, value, byteBufferOf(0x00u, 0x00u, 0x00u, 0x0Au, 0x00u, 0x00u, 0x00u, 0x0Cu, 0x00u, 0x00u, 0x00u, 0x00u, 0x07u, 0x4Du, 0x73u, 0x67u, 0x54u, 0x79u, 0x70u, 0x65u, 0x00u, 0x00u, 0x00u, 0x0Au, 0x00u, 0x00u, 0x00u, 0x00u, 0x14u, 0x00u, 0x43u, 0x00u, 0x6Cu, 0x00u, 0x69u, 0x00u, 0x65u, 0x00u, 0x6Eu, 0x00u, 0x74u, 0x00u, 0x49u, 0x00u, 0x6Eu, 0x00u, 0x69u, 0x00u, 0x74u, 0x00u, 0x00u, 0x00u, 0x0Cu, 0x00u, 0x00u, 0x00u, 0x00u, 0x0Du, 0x43u, 0x6Cu, 0x69u, 0x65u, 0x6Eu, 0x74u, 0x56u, 0x65u, 0x72u, 0x73u, 0x69u, 0x6Fu, 0x6Eu, 0x00u, 0x00u, 0x00u, 0x0Au, 0x00u, 0x00u, 0x00u, 0x00u, 0x22u, 0x00u, 0x51u, 0x00u, 0x75u, 0x00u, 0x61u, 0x00u, 0x73u, 0x00u, 0x73u, 0x00u, 0x65u, 0x00u, 0x6Cu, 0x00u, 0x64u, 0x00u, 0x72u, 0x00u, 0x6Fu, 0x00u, 0x69u, 0x00u, 0x64u, 0x00u, 0x20u, 0x00u, 0x74u, 0x00u, 0x65u, 0x00u, 0x73u, 0x00u, 0x74u, 0x00u, 0x00u, 0x00u, 0x0Cu, 0x00u, 0x00u, 0x00u, 0x00u, 0x0Au, 0x43u, 0x6Cu, 0x69u, 0x65u, 0x6Eu, 0x74u, 0x44u, 0x61u, 0x74u, 0x65u, 0x00u, 0x00u, 0x00u, 0x0Au, 0x00u, 0x00u, 0x00u, 0x00u, 0x0Au, 0x00u, 0x4Eu, 0x00u, 0x65u, 0x00u, 0x76u, 0x00u, 0x65u, 0x00u, 0x72u, 0x00u, 0x00u, 0x00u, 0x0Cu, 0x00u, 0x00u, 0x00u, 0x00u, 0x08u, 0x46u, 0x65u, 0x61u, 0x74u, 0x75u, 0x72u, 0x65u, 0x73u, 0x00u, 0x00u, 0x00u, 0x03u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x0Cu, 0x00u, 0x00u, 0x00u, 0x00u, 0x0Bu, 0x46u, 0x65u, 0x61u, 0x74u, 0x75u, 0x72u, 0x65u, 0x4Cu, 0x69u, 0x73u, 0x74u, 0x00u, 0x00u, 0x00u, 0x0Bu, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u))
// @formatter:on
}
@Test @Test
fun testRealistic() { fun testRealistic() = handshakeSerializerTest(
val features = FeatureSet.all() ClientInitSerializer,
val value = ClientInit( ClientInit(
clientVersion = "Quasseldroid <a href=\"https://git.kuschku.de/justJanne/QuasselDroid-ng/commit/b622ad63056b6054b06e09f8e1f1ef2b0c3aaf9a\">v1.3.3</a>", clientVersion = "Quasseldroid <a href=\"https://git.kuschku.de/justJanne/QuasselDroid-ng/commit/b622ad63056b6054b06e09f8e1f1ef2b0c3aaf9a\">v1.3.3</a>",
buildDate = "2020-04-27T22:21:17Z", buildDate = "2020-04-27T22:21:17Z",
clientFeatures = features.legacyFeatures(), clientFeatures = FeatureSet.all().legacyFeatures(),
featureList = features.featureList() featureList = FeatureSet.all().featureList()
)
) )
testHandshakeSerializerDirect(ClientInitSerializer, value)
testHandshakeSerializerEncoded(ClientInitSerializer, value)
}
} }
...@@ -16,31 +16,24 @@ ...@@ -16,31 +16,24 @@
* You should have received a copy of the GNU General Public License along * You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package de.kuschku.libquassel.protocol.serializers.primitive package de.kuschku.libquassel.protocol.serializers.primitive
import de.kuschku.libquassel.protocol.testutil.byteBufferOf import de.kuschku.libquassel.protocol.testutil.byteBufferOf
import de.kuschku.libquassel.protocol.testutil.testDeserialize import de.kuschku.libquassel.protocol.testutil.qtSerializerTest
import de.kuschku.libquassel.protocol.testutil.testQtSerializerDirect import org.junit.jupiter.api.Test
import de.kuschku.libquassel.protocol.testutil.testQtSerializerVariant
import org.junit.Test
class BoolSerializerTest { class BoolSerializerTest {
@Test @Test
fun testTrue() { fun testTrue() = qtSerializerTest(
testQtSerializerDirect(BoolSerializer, true) BoolSerializer,
testQtSerializerVariant(BoolSerializer, true) true,
// @formatter:off byteBufferOf(1)
testDeserialize(BoolSerializer, true, byteBufferOf(1)) )
// @formatter:on
}
@Test @Test
fun testFalse() { fun testFalse() = qtSerializerTest(
testQtSerializerDirect(BoolSerializer, false) BoolSerializer,
testQtSerializerVariant(BoolSerializer, false) false,
// @formatter:off byteBufferOf(0)
testDeserialize(BoolSerializer, false, byteBufferOf(0)) )
// @formatter:on
}
} }
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2020 Janne Mareike Koschinski
* Copyright (c) 2020 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.libquassel.protocol.serializers.primitive
import de.kuschku.bitflags.none
import de.kuschku.bitflags.validValues
import de.kuschku.libquassel.protocol.testutil.byteBufferOf
import de.kuschku.libquassel.protocol.testutil.quasselSerializerTest
import de.kuschku.libquassel.protocol.types.BufferId
import de.kuschku.libquassel.protocol.types.BufferInfo
import de.kuschku.libquassel.protocol.types.BufferType
import de.kuschku.libquassel.protocol.types.NetworkId
import org.junit.jupiter.api.Test
class BufferInfoSerializerTest {
@Test
fun testBaseCase() = quasselSerializerTest(
BufferInfoSerializer,
BufferInfo(
BufferId(-1),
NetworkId(-1),
BufferType.none(),
-1,
""
),
byteBufferOf(0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0x00u, 0x00u, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0x00u, 0x00u, 0x00u, 0x00u)
)
@Test
fun testNormal() = quasselSerializerTest(
BufferInfoSerializer,
BufferInfo(
BufferId.MAX_VALUE,
NetworkId.MAX_VALUE,
BufferType.validValues(),
Int.MAX_VALUE,
"äẞ\u0000\uFFFF"
),
byteBufferOf(127, -1, -1, -1, 127, -1, -1, -1, 0, 15, 127, -1, -1, -1, 0, 0, 0, 9, -61, -92, -31, -70, -98, 0, -17, -65, -65)
)
}
...@@ -16,31 +16,27 @@ ...@@ -16,31 +16,27 @@
* You should have received a copy of the GNU General Public License along * You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package de.kuschku.libquassel.protocol.serializers.primitive package de.kuschku.libquassel.protocol.serializers.primitive
import de.kuschku.libquassel.protocol.testutil.byteBufferOf import de.kuschku.libquassel.protocol.testutil.byteBufferOf
import de.kuschku.libquassel.protocol.testutil.matchers.ByteBufferMatcher import de.kuschku.libquassel.protocol.testutil.matchers.ByteBufferMatcher
import de.kuschku.libquassel.protocol.testutil.testDeserialize import de.kuschku.libquassel.protocol.testutil.qtSerializerTest
import de.kuschku.libquassel.protocol.testutil.testQtSerializerDirect import org.junit.jupiter.api.Test
import org.junit.Test
class ByteBufferSerializerTest { class ByteBufferSerializerTest {
@Test @Test
fun testBaseCase() { fun testBaseCase() = qtSerializerTest(
val value = byteBufferOf(0) ByteBufferSerializer,
testQtSerializerDirect(ByteBufferSerializer, value, ByteBufferMatcher(value)) byteBufferOf(0),
// @formatter:off byteBufferOf(0, 0, 0, 1, 0),
testDeserialize(ByteBufferSerializer, ByteBufferMatcher(value), byteBufferOf(0, 0, 0, 1, 0)) ::ByteBufferMatcher
// @formatter:on )
}
@Test @Test
fun testNormal() { fun testNormal() = qtSerializerTest(
val value = byteBufferOf(1, 2, 3, 4, 5, 6, 7, 8, 9) ByteBufferSerializer,
testQtSerializerDirect(ByteBufferSerializer, value, ByteBufferMatcher(value)) byteBufferOf(1, 2, 3, 4, 5, 6, 7, 8, 9),
// @formatter:off byteBufferOf(0, 0, 0, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9),
testDeserialize(ByteBufferSerializer, ByteBufferMatcher(value), byteBufferOf(0, 0, 0, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9)) ::ByteBufferMatcher
// @formatter:on )
}
} }
...@@ -16,55 +16,39 @@ ...@@ -16,55 +16,39 @@
* You should have received a copy of the GNU General Public License along * You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package de.kuschku.libquassel.protocol.serializers.primitive package de.kuschku.libquassel.protocol.serializers.primitive
import de.kuschku.libquassel.protocol.testutil.byteBufferOf import de.kuschku.libquassel.protocol.testutil.byteBufferOf
import de.kuschku.libquassel.protocol.testutil.testDeserialize import de.kuschku.libquassel.protocol.testutil.qtSerializerTest
import de.kuschku.libquassel.protocol.testutil.testQtSerializerDirect import org.junit.jupiter.api.Test
import de.kuschku.libquassel.protocol.testutil.testQtSerializerVariant
import org.junit.Test
import kotlin.experimental.inv import kotlin.experimental.inv
class ByteSerializerTest { class ByteSerializerTest {
@Test @Test
fun testZero() { fun testZero() = qtSerializerTest(
val value = 0.toByte() ByteSerializer,
testQtSerializerDirect(ByteSerializer, value) 0.toByte(),
testQtSerializerVariant(ByteSerializer, value) byteBufferOf(0)
// @formatter:off )
testDeserialize(ByteSerializer, value, byteBufferOf(0))
// @formatter:on
}
@Test @Test
fun testMinimal() { fun testMinimal() = qtSerializerTest(
val value = Byte.MIN_VALUE ByteSerializer,
testQtSerializerDirect(ByteSerializer, value) Byte.MIN_VALUE,
testQtSerializerVariant(ByteSerializer, value) byteBufferOf(-128)
// @formatter:off )
testDeserialize(ByteSerializer, value, byteBufferOf(-128))
// @formatter:on
}
@Test @Test
fun testMaximal() { fun testMaximal() = qtSerializerTest(
val value = Byte.MAX_VALUE ByteSerializer,
testQtSerializerDirect(ByteSerializer, value) Byte.MAX_VALUE,
testQtSerializerVariant(ByteSerializer, value) byteBufferOf(127)
// @formatter:off )
testDeserialize(ByteSerializer, value, byteBufferOf(127))
// @formatter:on
}
@Test @Test
fun testAllOnes() { fun testAllOnes() = qtSerializerTest(
val value = 0.toByte().inv() ByteSerializer,
0.toByte().inv(),
testQtSerializerDirect(ByteSerializer, value) byteBufferOf(-1)
testQtSerializerVariant(ByteSerializer, value) )
// @formatter:off
testDeserialize(ByteSerializer, value, byteBufferOf(-1))
// @formatter:on
}
} }
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2020 Janne Mareike Koschinski
* Copyright (c) 2020 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.libquassel.protocol.serializers.primitive
import de.kuschku.libquassel.protocol.testutil.byteBufferOf
import de.kuschku.libquassel.protocol.testutil.matchers.TemporalMatcher
import de.kuschku.libquassel.protocol.testutil.qtSerializerTest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.threeten.bp.*
import org.threeten.bp.chrono.JapaneseDate
class DateTimeSerializerTest {
private val serializer = DateTimeSerializer
@Test
fun testEpoch() = qtSerializerTest(
DateTimeSerializer,
Instant.EPOCH,
byteBufferOf(0, 37, 61, -116, 0, 0, 0, 0, 2),
matcher = ::TemporalMatcher
)
@Test
fun testEpochAtTimezone() = qtSerializerTest(
DateTimeSerializer,
Instant.EPOCH.atOffset(ZoneOffset.ofTotalSeconds(1234)),
byteBufferOf(0x00u, 0x25u, 0x3Du, 0x8Cu, 0x00u, 0x12u, 0xD4u, 0x50u, 0x03u, 0x00u, 0x00u, 0x04u, 0xD2u),
matcher = ::TemporalMatcher
)
@Test
fun testEpochByCalendarAtTimezone() = qtSerializerTest(
DateTimeSerializer,
LocalDateTime
.of(1970, 1, 1, 0, 0)
.atZone(ZoneId.of("Europe/Berlin"))
.toInstant(),
byteBufferOf(0, 37, 61, -117, 4, -17, 109, -128, 2),
matcher = ::TemporalMatcher
)
@Test
fun testNormalCase() = qtSerializerTest(
DateTimeSerializer,
LocalDateTime
.of(2019, Month.JANUARY, 15, 20, 25)
.atZone(ZoneId.of("Europe/Berlin"))
.toInstant(),
byteBufferOf(0, 37, -125, -125, 4, 42, -106, -32, 2),
matcher = ::TemporalMatcher
)
@Test
fun testLocalDateTime() = qtSerializerTest(
DateTimeSerializer,
LocalDateTime
.of(2019, Month.JANUARY, 15, 20, 25),
byteBufferOf(0x00u, 0x25u, 0x83u, 0x83u, 0x04u, 0x61u, 0x85u, 0x60u, 0xFFu),
matcher = ::TemporalMatcher
)
@Test
fun testZonedDateTime() = qtSerializerTest(
DateTimeSerializer,
LocalDateTime
.of(2019, Month.JANUARY, 15, 20, 25)
.atZone(ZoneId.systemDefault()),
matcher = ::TemporalMatcher
)
@Test
fun testUnknownDateTime() = qtSerializerTest(
DateTimeSerializer,
LocalDateTime
.of(2019, Month.JANUARY, 15, 20, 25),
byteBufferOf(0x00u, 0x25u, 0x83u, 0x83u, 0x04u, 0x61u, 0x85u, 0x60u, 0x07u),
matcher = ::TemporalMatcher
)
@Test
fun testOldJavaDate() {
assertThrows<IllegalArgumentException>("Unsupported Format: org.threeten.bp.chrono.JapaneseDate") {
qtSerializerTest(
DateTimeSerializer,
JapaneseDate.now(),
matcher = ::TemporalMatcher
)
}
}
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2020 Janne Mareike Koschinski
* Copyright (c) 2020 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.libquassel.protocol.serializers.primitive
import de.kuschku.libquassel.protocol.testutil.byteBufferOf
import de.kuschku.libquassel.protocol.testutil.qtSerializerTest
import org.junit.jupiter.api.Test
class DoubleSerializerTest {
@Test
fun testZero() = qtSerializerTest(
DoubleSerializer,
0.0,
byteBufferOf(0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u)
)
@Test
fun testMinimal() = qtSerializerTest(
DoubleSerializer,
Double.MIN_VALUE,
byteBufferOf(0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x01u)
)
@Test
fun testMaximal() = qtSerializerTest(
DoubleSerializer,
Double.MAX_VALUE,
byteBufferOf(0x7Fu, 0xEFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu)
)
@Test
fun testInfinityPositive() = qtSerializerTest(
DoubleSerializer,
Double.POSITIVE_INFINITY,
byteBufferOf(0x7Fu, 0xF0u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u)
)
@Test
fun testInfinityNegative() = qtSerializerTest(
DoubleSerializer,
Double.NEGATIVE_INFINITY,
byteBufferOf(0xFFu, 0xF0u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u)
)
@Test
fun testNotANumber() = qtSerializerTest(
DoubleSerializer,
Double.NaN,
byteBufferOf(0x7Fu, 0xF8u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u)
)
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2020 Janne Mareike Koschinski
* Copyright (c) 2020 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.libquassel.protocol.serializers.primitive
import de.kuschku.libquassel.protocol.testutil.byteBufferOf
import de.kuschku.libquassel.protocol.testutil.qtSerializerTest
import org.junit.jupiter.api.Test
class FloatSerializerTest {
@Test
fun testZero() = qtSerializerTest(
FloatSerializer,
0f,
byteBufferOf(0x00u, 0x00u, 0x00u, 0x00u)
)
@Test
fun testMinimal() = qtSerializerTest(
FloatSerializer,
Float.MIN_VALUE,
byteBufferOf(0x00u, 0x00u, 0x00u, 0x01u)
)
@Test
fun testMaximal() = qtSerializerTest(
FloatSerializer,
Float.MAX_VALUE,
byteBufferOf(0x7Fu, 0x7Fu, 0xFFu, 0xFFu)
)
@Test
fun testInfinityPositive() = qtSerializerTest(
FloatSerializer,
Float.POSITIVE_INFINITY,
byteBufferOf(0x7Fu, 0x80u, 0x00u, 0x00u)
)
@Test
fun testInfinityNegative() = qtSerializerTest(
FloatSerializer,
Float.NEGATIVE_INFINITY,
byteBufferOf(0xFFu, 0x80u, 0x00u, 0x00u)
)
@Test
fun testNotANumber() = qtSerializerTest(
FloatSerializer,
Float.NaN,
byteBufferOf(0x7Fu, 0xC0u, 0x00u, 0x00u)
)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment