How to use Arb.Companion.byte method of io.kotest.property.arbitrary.bytes class

Best Kotest code snippet using io.kotest.property.arbitrary.bytes.Arb.Companion.byte

Arb.kt

Source:Arb.kt Github

copy

Full Screen

1package arrow.fx.coroutines.stream2import io.kotest.property.Arb3import io.kotest.property.RandomSource4import io.kotest.property.Sample5import io.kotest.property.Shrinker6import io.kotest.property.arbitrary.arb7import io.kotest.property.arbitrary.bool8import io.kotest.property.arbitrary.byte9import io.kotest.property.arbitrary.choice10import io.kotest.property.arbitrary.choose11import io.kotest.property.arbitrary.double12import io.kotest.property.arbitrary.float13import io.kotest.property.arbitrary.int14import io.kotest.property.arbitrary.list15import io.kotest.property.arbitrary.long16import io.kotest.property.arbitrary.map17import io.kotest.property.arbitrary.set18import io.kotest.property.arbitrary.short19import kotlin.random.nextInt20@JvmOverloads21inline fun <reified A> Arb.Companion.array(22 gen: Arb<A>,23 range: IntRange = 0..10024): Arb<Array<A>> {25 check(!range.isEmpty())26 check(range.first >= 0)27 return arb(edgecases = listOf(emptyArray<A>()) + gen.edgecases().map { arrayOf(it) }) {28 sequence {29 val genIter = gen.generate(it).iterator()30 while (true) {31 val targetSize = it.random.nextInt(range)32 val list = ArrayList<A>(targetSize)33 while (list.size < targetSize && genIter.hasNext()) {34 list.add(genIter.next().value)35 }36 check(list.size == targetSize)37 yield(list.toArray() as Array<A>)38 }39 }40 }41}42@PublishedApi43internal fun <A, B> arrayChunkGenerator(44 arb: Arb<A>,45 shrinker: Shrinker<B>,46 range: IntRange = 0..10,47 build: (values: List<A>, offset: Int, length: Int) -> B48): Arb<B> {49 check(!range.isEmpty())50 check(range.first >= 0)51 val edgecases =52 arb.edgecases().map { a -> build(listOf(a), 0, 1) } + build(emptyList(), 0, 0)53 return arb(edgecases, shrinker) {54 val genIter = arb.generate(it).iterator()55 sequence {56 while (true) {57 val targetSize = it.random.nextInt(range)58 val list = ArrayList<A>(targetSize)59 while (list.size < targetSize && genIter.hasNext()) {60 list.add(genIter.next().value)61 }62 val offset = (0..list.size).random(it.random)63 val length = (0..(list.size - offset)).random(it.random)64 yield(build(list, offset, length))65 }66 }67 }68}69class ChunkShrinker<A> : Shrinker<Chunk<A>> {70 override fun shrink(value: Chunk<A>): List<Chunk<A>> =71 if (value.isEmpty()) emptyList()72 else listOf(73 Chunk.empty(),74 value.takeLast(1),75 value.take(value.size() / 3),76 value.take(value.size() / 2),77 value.take(value.size() * 2 / 3),78 value.dropLast(1)79 )80}81inline fun <reified A> Arb.Companion.chunk(arb: Arb<A>): Arb<Chunk<A>> =82 object : Arb<Chunk<A>>() {83 override fun edgecases(): List<Chunk<A>> =84 listOf(Chunk.empty<A>()) + arb.edgecases().map { Chunk(it) }85 override fun values(rs: RandomSource): Sequence<Sample<Chunk<A>>> =86 Arb.choose(87 5 to arb.map { Chunk.just(it) },88 10 to Arb.list(arb, 0..20).map { Chunk.iterable(it) },89 10 to Arb.set(arb, 0..20).map { Chunk.iterable(it) },90 10 to Arb.array(arb, 0..20).map { Chunk.array(it) },91 10 to Arb.boxedChunk(arb)92 ).values(rs)93 }94inline fun <reified A> Arb.Companion.boxedChunk(arb: Arb<A>): Arb<Chunk<A>> =95 object : Arb<Chunk<A>>() {96 override fun edgecases(): List<Chunk<A>> =97 listOf(Chunk.empty<A>()) + arb.edgecases().map { Chunk(it) }98 override fun values(rs: RandomSource): Sequence<Sample<Chunk<A>>> =99 arrayChunkGenerator(arb, ChunkShrinker()) { values, offset, length ->100 Chunk.boxed(values.toTypedArray(), offset, length)101 }.values(rs)102 }103fun Arb.Companion.booleanChunk(): Arb<Chunk<Boolean>> =104 object : Arb<Chunk<Boolean>>() {105 override fun edgecases(): List<Chunk<Boolean>> =106 listOf(Chunk.empty<Boolean>()) + Arb.bool().edgecases().map { Chunk(it) }107 override fun values(rs: RandomSource): Sequence<Sample<Chunk<Boolean>>> =108 Arb.choice(109 arrayChunkGenerator(Arb.bool(), ChunkShrinker()) { values, offset, length ->110 Chunk.booleans(values.toBooleanArray(), offset, length)111 },112 arrayChunkGenerator(Arb.bool(), ChunkShrinker()) { values, _, _ ->113 Chunk.array(values.toTypedArray())114 }115 ).values(rs)116 }117fun Arb.Companion.byteChunk(): Arb<Chunk<Byte>> =118 object : Arb<Chunk<Byte>>() {119 override fun edgecases(): List<Chunk<Byte>> =120 listOf(Chunk.empty<Byte>()) + Arb.byte().edgecases().map { Chunk(it) }121 override fun values(rs: RandomSource): Sequence<Sample<Chunk<Byte>>> =122 Arb.choice(123 arrayChunkGenerator(Arb.byte(), ChunkShrinker()) { values, offset, length ->124 Chunk.bytes(values.toByteArray(), offset, length)125 },126 arrayChunkGenerator(Arb.byte(), ChunkShrinker()) { values, _, _ ->127 Chunk.array(values.toTypedArray())128 }129 ).values(rs)130 }131fun Arb.Companion.intChunk(): Arb<Chunk<Int>> =132 object : Arb<Chunk<Int>>() {133 override fun edgecases(): List<Chunk<Int>> =134 listOf(Chunk.empty<Int>()) + Arb.int().edgecases().map { Chunk(it) }135 override fun values(rs: RandomSource): Sequence<Sample<Chunk<Int>>> =136 Arb.choice(137 arrayChunkGenerator(Arb.int(), ChunkShrinker()) { values, offset, length ->138 Chunk.ints(values.toIntArray(), offset, length)139 },140 arrayChunkGenerator(Arb.int(), ChunkShrinker()) { values, _, _ ->141 Chunk.array(values.toTypedArray())142 }143 ).values(rs)144 }145fun Arb.Companion.longChunk(): Arb<Chunk<Long>> =146 object : Arb<Chunk<Long>>() {147 override fun edgecases(): List<Chunk<Long>> =148 listOf(Chunk.empty<Long>()) + Arb.long().edgecases().map { Chunk(it) }149 override fun values(rs: RandomSource): Sequence<Sample<Chunk<Long>>> =150 Arb.choice(151 arrayChunkGenerator(Arb.long(), ChunkShrinker()) { values, offset, length ->152 Chunk.longs(values.toLongArray(), offset, length)153 },154 arrayChunkGenerator(Arb.long(), ChunkShrinker()) { values, _, _ ->155 Chunk.array(values.toTypedArray())156 }157 ).values(rs)158 }159fun Arb.Companion.doubleChunk(): Arb<Chunk<Double>> =160 object : Arb<Chunk<Double>>() {161 override fun edgecases(): List<Chunk<Double>> =162 listOf(Chunk.empty<Double>()) + Arb.double().edgecases().map { Chunk(it) }163 override fun values(rs: RandomSource): Sequence<Sample<Chunk<Double>>> =164 Arb.choice(165 arrayChunkGenerator(Arb.double(), ChunkShrinker()) { values, offset, length ->166 Chunk.doubles(values.toDoubleArray(), offset, length)167 },168 arrayChunkGenerator(Arb.double(), ChunkShrinker()) { values, _, _ ->169 Chunk.array(values.toTypedArray())170 }171 ).values(rs)172 }173fun Arb.Companion.floatChunk(): Arb<Chunk<Float>> =174 object : Arb<Chunk<Float>>() {175 override fun edgecases(): List<Chunk<Float>> =176 listOf(Chunk.empty<Float>()) + Arb.float().edgecases().map { Chunk(it) }177 override fun values(rs: RandomSource): Sequence<Sample<Chunk<Float>>> =178 Arb.choice(179 arrayChunkGenerator(Arb.float(), ChunkShrinker()) { values, offset, length ->180 Chunk.floats(values.toFloatArray(), offset, length)181 },182 arrayChunkGenerator(Arb.float(), ChunkShrinker()) { values, _, _ ->183 Chunk.array(values.toTypedArray())184 }185 ).values(rs)186 }187fun Arb.Companion.shortChunk(): Arb<Chunk<Short>> =188 object : Arb<Chunk<Short>>() {189 override fun edgecases(): List<Chunk<Short>> =190 listOf(Chunk.empty<Short>()) + Arb.short().edgecases().map { Chunk(it) }191 override fun values(rs: RandomSource): Sequence<Sample<Chunk<Short>>> =192 Arb.choice(193 arrayChunkGenerator(Arb.short(), ChunkShrinker()) { values, offset, length ->194 Chunk.shorts(values.toShortArray(), offset, length)195 },196 arrayChunkGenerator(Arb.short(), ChunkShrinker()) { values, _, _ ->197 Chunk.array(values.toTypedArray())198 }199 ).values(rs)200 }...

Full Screen

Full Screen

ByteBufferInputStreamTest.kt

Source:ByteBufferInputStreamTest.kt Github

copy

Full Screen

1/*2 * Copyright (C) 2022 Edgar Asatryan3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package io.github.nstdio.http.ext17import io.kotest.assertions.throwables.shouldThrowExactly18import io.kotest.matchers.booleans.shouldBeTrue19import io.kotest.matchers.collections.shouldBeEmpty20import io.kotest.matchers.ints.shouldBeZero21import io.kotest.matchers.shouldBe22import io.kotest.matchers.throwable.shouldHaveMessage23import io.kotest.property.Arb24import io.kotest.property.arbitrary.int25import io.kotest.property.arbitrary.next26import org.assertj.core.api.Assertions.assertThat27import org.assertj.core.api.Assertions.assertThatIOException28import org.junit.jupiter.api.Assertions29import org.junit.jupiter.api.Assertions.assertEquals30import org.junit.jupiter.api.Assertions.assertThrows31import org.junit.jupiter.api.Named32import org.junit.jupiter.api.RepeatedTest33import org.junit.jupiter.api.Test34import org.junit.jupiter.params.ParameterizedTest35import org.junit.jupiter.params.provider.MethodSource36import java.io.ByteArrayOutputStream37import java.io.IOException38import java.util.stream.IntStream39import java.util.stream.Stream40internal class ByteBufferInputStreamTest {41 private val arbByteBuffer = Arb.byteBuffer(Arb.int(8..32))42 private val arbByteArray = Arb.byteArray(Arb.int(16, 48))43 @ParameterizedTest44 @MethodSource("fullReadingData")45 fun fullReading(bytes: ByteArray) {46 //given47 val s = ByteBufferInputStream()48 Helpers.toBuffers(bytes).forEach(s::add)49 //when50 val available = s.available()51 val actual = s.readAllBytes()52 //then53 available shouldBe bytes.size54 actual.shouldBe(bytes)55 }56 @Test57 fun shouldReturnNegativeWhenInputIsEmpty() {58 //given59 val stream = ByteBufferInputStream()60 stream.add(arbByteBuffer.next())61 //when62 val actual = stream.read(ByteArray(0))63 //then64 actual.shouldBeZero()65 }66 @RepeatedTest(4)67 fun shouldReadSingleProperly() {68 //given69 val s = ByteBufferInputStream()70 val bytes = arbByteArray.next()71 Helpers.toBuffers(bytes).forEach(s::add)72 val out = ByteArrayOutputStream()73 //when74 var read: Int75 while (s.read().also { read = it } != -1) {76 out.write(read)77 }78 //then79 s.read().shouldBe(-1)80 out.toByteArray().shouldBe(bytes)81 }82 @Test83 fun shouldFlipBuffer() {84 //given85 val bytes = arbByteArray.next()86 val stream = ByteBufferInputStream()87 Helpers.toBuffers(bytes)88 .map { it.position(it.limit()) }89 .forEach(stream::add)90 stream.add(ByteArray(0).toBuffer())91 //when92 val actual = stream.readAllBytes()93 //then94 Assertions.assertArrayEquals(bytes, actual)95 }96 @Test97 fun shouldThrowWhenClosed() {98 //given99 val s = ByteBufferInputStream()100 //when101 s.close()102 //then103 assertThatIOException().isThrownBy { s.read() }104 assertThatIOException().isThrownBy { s.read(ByteArray(0)) }105 assertThatIOException().isThrownBy { s.read(ByteArray(5), 0, 5) }106 assertThatIOException().isThrownBy { s.readAllBytes() }107 assertThatIOException().isThrownBy { s.available() }108 }109 @Test110 fun shouldReportAvailable() {111 //given112 val bytes = arbByteArray.next()113 val s = ByteBufferInputStream()114 Helpers.toBuffers(bytes).forEach(s::add)115 //when116 val actual = s.available()117 //then118 assertEquals(bytes.size, actual)119 }120 @Test121 fun shouldReadAllBytes() {122 //given123 val bytes = arbByteArray.next()124 val s = ByteBufferInputStream()125 Helpers.toBuffers(bytes).forEach(s::add)126 //when127 val actual = s.readAllBytes()128 //then129 actual.shouldBe(bytes)130 }131 @Test132 fun shouldThrowWhenRequestedBytesNegative() {133 //given134 val `is` = ByteBufferInputStream()135 //when + then136 assertThrows(IllegalArgumentException::class.java) { `is`.readNBytes(-1) }137 }138 @Test139 fun shouldReadUpToNBytes() {140 //given141 val bytes = arbByteArray.next()142 val count = bytes.size143 val s = ByteBufferInputStream()144 Helpers.toBuffers(bytes).forEach(s::add)145 //when146 val actual = s.readNBytes(count + 1)147 //then148 Assertions.assertArrayEquals(bytes, actual)149 }150 @Test151 fun shouldSupportMark() {152 //given + when + then153 ByteBufferInputStream().markSupported().shouldBeTrue()154 }155 @Test156 fun shouldDumpBuffersToList() {157 //given158 val s = ByteBufferInputStream()159 val buffers = Helpers.toBuffers(arbByteArray.next())160 buffers.forEach(s::add)161 //when162 val actual = s.drainToList()163 //then164 assertEquals(-1, s.read())165 assertThat(actual)166 .hasSameSizeAs(buffers)167 .containsExactlyElementsOf(buffers)168 }169 @Test170 fun `Should not add when closed`() {171 //given172 val s = ByteBufferInputStream().also { it.close() }173 //when174 s.add(arbByteBuffer.next())175 //then176 s.drainToList().shouldBeEmpty()177 }178 @Test179 fun `Should throw when reset called but not marked`() {180 //given181 val s = ByteBufferInputStream()182 //when + then183 shouldThrowExactly<IOException>(s::reset)184 .shouldHaveMessage("nothing to reset")185 }186 @Test187 fun `Should restore marked on reset`() {188 //given189 val bytes = "abcd".toByteArray()190 val s = ByteBufferInputStream().also { it.add(bytes.toBuffer()) }191 //when192 s.mark(2)193 s.read(); s.read()194 s.reset()195 //then196 s.read().shouldBe(bytes[0])197 s.read().shouldBe(bytes[1])198 }199 @Test200 fun `Should drop mark when read limit exceeds`() {201 //given202 val bytes = "abcd".toByteArray()203 val s = ByteBufferInputStream().also { it.add(bytes.toBuffer()) }204 //when205 s.mark(1)206 s.read(); s.read()207 //then208 s.read().shouldBe(bytes[2])209 s.read().shouldBe(bytes[3])210 }211 @Test212 fun `Should drop mark when limit is negative`() {213 //given214 val bytes = "abcd".toByteArray()215 val s = ByteBufferInputStream().also { it.add(bytes.toBuffer()) }216 //when217 s.mark(1)218 s.mark(-1)219 //then220 s.read().shouldBe(bytes[0])221 shouldThrowExactly<IOException> { s.reset() }222 .shouldHaveMessage("nothing to reset")223 }224 companion object {225 @JvmStatic226 fun fullReadingData(): Stream<Named<ByteArray>> {227 return IntStream.of(8192, 16384, 65536)228 .mapToObj { n: Int ->229 Named.named(230 "Size: $n", Arb.byteArray(n).next()231 )232 }233 }234 }235}...

Full Screen

Full Screen

ExtensionTest.kt

Source:ExtensionTest.kt Github

copy

Full Screen

1import io.harmor.msgpack.ExtensionValue2import io.harmor.msgpack.internal.MessageType.EXT163import io.harmor.msgpack.internal.MessageType.EXT324import io.harmor.msgpack.internal.MessageType.EXT85import io.harmor.msgpack.internal.MessageType.FIXEXT16import io.harmor.msgpack.internal.MessageType.FIXEXT167import io.harmor.msgpack.internal.MessageType.FIXEXT28import io.harmor.msgpack.internal.MessageType.FIXEXT49import io.harmor.msgpack.internal.MessageType.FIXEXT810import io.harmor.msgpack.nextExtension11import io.kotest.matchers.shouldBe12import io.kotest.property.Arb13import io.kotest.property.Exhaustive14import io.kotest.property.arbitrary.arbitrary15import io.kotest.property.checkAll16import io.kotest.property.exhaustive.exhaustive17import utils.MessagePackerFactory18import utils.MessageUnpackerFactory19import utils.TypedElement20import utils.pack21import utils.convert22import utils.with23import kotlin.random.Random24class ExtensionTest : AbstractMessagePackTest() {25 init {26 "Packer" should {27 "use fixext1" {28 checkAll(Arb.extension(1)) {29 it with packer shouldBe fixext1(it)30 }31 }32 "use fixext2" {33 checkAll(Arb.extension(2)) {34 it with packer shouldBe fixext2(it)35 }36 }37 "use fixext4" {38 checkAll(Arb.extension(4)) {39 it with packer shouldBe fixext4(it)40 }41 }42 "use fixext8" {43 checkAll(Arb.extension(8)) {44 it with packer shouldBe fixext8(it)45 }46 }47 "use fixext16" {48 checkAll(Arb.extension(16)) {49 it with packer shouldBe fixext16(it)50 }51 }52 "use ext8" {53 checkAll(Exhaustive.extension(17..255)) {54 it with packer shouldBe ext8(it)55 }56 }57 "use ext16" {58 checkAll(Arb.extension(256..65535)) {59 it with packer shouldBe ext16(it)60 }61 }62 "use ext32" {63 checkAll(Arb.extension(65536..100_000)) {64 it with packer shouldBe ext32(it)65 }66 }67 }68 "Unpacker" should {69 "decode fixext1" {70 checkAll(Arb.extension(1)) {71 it with unpacker shouldBe fixext1(it)72 }73 }74 "decode fixext2" {75 checkAll(Arb.extension(2)) {76 it with unpacker shouldBe fixext2(it)77 }78 }79 "decode fixext4" {80 checkAll(Arb.extension(4)) {81 it with unpacker shouldBe fixext4(it)82 }83 }84 "decode fixext8" {85 checkAll(Arb.extension(8)) {86 it with unpacker shouldBe fixext8(it)87 }88 }89 "decode fixext16" {90 checkAll(Arb.extension(16)) {91 it with unpacker shouldBe fixext16(it)92 }93 }94 "decode ext8" {95 checkAll(Arb.extension(17..255)) {96 it with unpacker shouldBe ext8(it)97 }98 }99 "decode ext16" {100 checkAll(Arb.extension(256..65535)) {101 it with unpacker shouldBe ext16(it)102 }103 }104 "decode ext32" {105 checkAll(Arb.extension(65536..100_000)) {106 it with unpacker shouldBe ext32(it)107 }108 }109 }110 }111}112private fun Arb.Companion.extension(length: Int) =113 extension(length..length)114private fun Arb.Companion.extension(length: IntRange) = arbitrary {115 ExtensionValue(it.random.nextInt().toByte(), it.random.nextBytes(length.random(it.random)))116}117private fun Exhaustive.Companion.extension(length: IntRange) =118 length.map {119 ExtensionValue(Random.nextInt().toByte(), Random.nextBytes(it))120 }.toList().exhaustive()121private fun fixext1(extension: ExtensionValue) = TypedElement(FIXEXT1, extension)122private fun fixext2(extension: ExtensionValue) = TypedElement(FIXEXT2, extension)123private fun fixext4(extension: ExtensionValue) = TypedElement(FIXEXT4, extension)124private fun fixext8(extension: ExtensionValue) = TypedElement(FIXEXT8, extension)125private fun fixext16(extension: ExtensionValue) = TypedElement(FIXEXT16, extension)126private fun ext8(extension: ExtensionValue) = TypedElement(EXT8, extension)127private fun ext16(extension: ExtensionValue) = TypedElement(EXT16, extension)128private fun ext32(extension: ExtensionValue) = TypedElement(EXT32, extension)129private infix fun ExtensionValue.with(packerFactory: MessagePackerFactory): TypedElement =130 this with packerFactory131private infix fun ExtensionValue.with(unpacker: MessageUnpackerFactory): TypedElement =132 convert().pack().let {133 TypedElement(it[0], unpacker(it).nextExtension())134 }...

Full Screen

Full Screen

ByteBufIntSmart.kt

Source:ByteBufIntSmart.kt Github

copy

Full Screen

1/*2 * Copyright 2018-2021 Guthix3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package io.guthix.buffer.bytebuf17import io.guthix.buffer.*18import io.kotest.core.spec.style.StringSpec19import io.kotest.matchers.ints.shouldBeNonNegative20import io.kotest.matchers.shouldBe21import io.kotest.property.Arb22import io.kotest.property.Shrinker23import io.kotest.property.arbitrary.*24import io.kotest.property.checkAll25import io.netty.buffer.ByteBuf26import io.netty.buffer.ByteBufAllocator27import kotlin.random.nextUInt28private suspend fun doIntSmartRWTest(29 writer: ByteBuf.(Int) -> ByteBuf,30 reader: ByteBuf.() -> Int31) = checkAll(Arb.intArray(collectionSizeArb, Arb.int(Smart.MIN_INT_VALUE, Smart.MAX_INT_VALUE))) { testData ->32 val buf = ByteBufAllocator.DEFAULT.buffer(testData.size * Short.SIZE_BYTES)33 try {34 testData.forEach { expected -> buf.writer(expected) }35 testData.forEach { expected ->36 val read = buf.reader()37 read shouldBe expected38 }39 } finally {40 buf.release()41 }42}43@ExperimentalUnsignedTypes44private suspend fun doUIntSmartRWTest(45 writer: ByteBuf.(Int) -> ByteBuf,46 reader: ByteBuf.() -> Int47) = checkAll(48 Arb.uIntArray(collectionSizeArb, Arb.uInt(USmart.MIN_INT_VALUE.toUInt(), USmart.MAX_INT_VALUE.toUInt()))49) { testData ->50 val buf = ByteBufAllocator.DEFAULT.buffer(testData.size * Short.SIZE_BYTES)51 try {52 testData.forEach { expected -> buf.writer(expected.toInt()) }53 testData.forEach { expected ->54 val read = buf.reader()55 read.shouldBeNonNegative()56 read shouldBe expected.toInt()57 }58 } finally {59 buf.release()60 }61}62fun Arb.Companion.nullableUInt(min: UInt = UInt.MIN_VALUE, max: UInt = UInt.MAX_VALUE) = nullableUInt(min..max)63fun Arb.Companion.nullableUInt(range: UIntRange = UInt.MIN_VALUE..UInt.MAX_VALUE): Arb<UInt?> {64 val edges = listOf(range.first, 1u, range.last).filter { it in range }.distinct()65 return arbitrary(edges, NullableUIntShrinker(range)) {66 if (it.random.nextFloat() < 0.40) null else it.random.nextUInt(range)67 }68}69class NullableUIntShrinker(private val range: UIntRange) : Shrinker<UInt?> {70 override fun shrink(value: UInt?): List<UInt?> = when (value) {71 null -> emptyList()72 0u -> emptyList()73 1u -> listOf(0u).filter { it in range }74 else -> {75 val a = listOf(0u, 1u, value / 3u, null, value / 2u, value * 2u / 3u)76 val b = (1u..5u).map { value - it }.reversed().filter { it > 0u }77 (a + b).distinct().filterNot { it == value }.filter { it in range }78 }79 }80}81@ExperimentalUnsignedTypes82private suspend fun doNullableUIntSmartRWTest(83 writer: ByteBuf.(Int?) -> ByteBuf,84 reader: ByteBuf.() -> Int?85) = checkAll(86 Arb.list(Arb.nullableUInt(USmart.MIN_INT_VALUE.toUInt(), USmart.MAX_INT_VALUE.toUInt()))87) { testData ->88 val buf = ByteBufAllocator.DEFAULT.buffer(testData.size * Short.SIZE_BYTES)89 try {90 testData.forEach { expected -> buf.writer(expected?.toInt()) }91 testData.forEach { expected ->92 val read = buf.reader()93 read?.shouldBeNonNegative()94 read shouldBe expected?.toInt()95 }96 } finally {97 buf.release()98 }99}100@ExperimentalUnsignedTypes101class ByteBufIntSmartTest : StringSpec({102 "Read/Write Int Smart" { doIntSmartRWTest(ByteBuf::writeIntSmart, ByteBuf::readIntSmart) }103 "Unsigned Read/Write Int Smart" {104 doUIntSmartRWTest(ByteBuf::writeUnsignedIntSmart, ByteBuf::readUnsignedIntSmart)105 }106 "Unsigned Read/Write Nullable Int Smart" {107 doNullableUIntSmartRWTest(ByteBuf::writeNullableUnsignedIntSmart, ByteBuf::readNullableUnsignedIntSmart)108 }109})...

Full Screen

Full Screen

JagMessageSerializationMediumTest.kt

Source:JagMessageSerializationMediumTest.kt Github

copy

Full Screen

1/*2 * Copyright 2018-2021 Guthix3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package io.guthix.buffer17import io.kotest.core.spec.style.StringSpec18import io.kotest.matchers.shouldBe19import io.kotest.property.Arb20import io.kotest.property.arbitrary.int21import io.kotest.property.arbitrary.uInt22import io.kotest.property.checkAll23import io.netty.buffer.ByteBufAllocator24import kotlinx.serialization.ExperimentalSerializationApi25import kotlinx.serialization.Serializable26@ExperimentalSerializationApi27@Serializable28private data class MediumTest(29 @JMedium(JMediumType.DEFAULT) val default: Int,30 @JMedium(JMediumType.LME) val lme: Int,31 @JMedium(JMediumType.RME) val rme: Int32)33@ExperimentalSerializationApi34@Serializable35private data class UMediumTest(36 @JMedium(JMediumType.DEFAULT) val default: UInt,37 @JMedium(JMediumType.LME) val le: UInt,38 @JMedium(JMediumType.RME) val add: UInt39)40private fun Arb.Companion.medium() = int(Medium.MIN_VALUE, Medium.MAX_VALUE)41private fun Arb.Companion.uMedium() = uInt(UMedium.MIN_VALUE, UMedium.MAX_VALUE)42@ExperimentalUnsignedTypes43@ExperimentalSerializationApi44class JagMessageSerializationMediumTest : StringSpec({45 "Encode/Decode Test" {46 checkAll(Arb.medium(), Arb.medium(), Arb.medium()) { default, lme, rme ->47 val expectedByteBuf = ByteBufAllocator.DEFAULT.jBuffer(Medium.SIZE_BYTES * 3).apply {48 writeMedium(default)49 writeMediumLME(lme)50 writeMediumRME(rme)51 }52 try {53 val expectedTest = MediumTest(default, lme, rme)54 val actualByteBuf = JagMessage.encodeToByteBuf(MediumTest.serializer(), expectedTest)55 try {56 actualByteBuf shouldBe expectedByteBuf57 val actualTest = JagMessage.decodeFromByteBuf(MediumTest.serializer(), expectedByteBuf)58 actualTest shouldBe expectedTest59 } finally {60 actualByteBuf.release()61 }62 } finally {63 expectedByteBuf.release()64 }65 }66 }67 "Unsigned Encode/Decode Test" {68 checkAll(Arb.uMedium(), Arb.uMedium(), Arb.uMedium()) { default, lme, rme ->69 val expectedByteBuf = ByteBufAllocator.DEFAULT.jBuffer(UMedium.SIZE_BYTES * 3).apply {70 writeMedium(default.toInt())71 writeMediumLME(lme.toInt())72 writeMediumRME(rme.toInt())73 }74 try {75 val expectedTest = UMediumTest(default, lme, rme)76 val actualByteBuf = JagMessage.encodeToByteBuf(UMediumTest.serializer(), expectedTest)77 try {78 actualByteBuf shouldBe expectedByteBuf79 val actualTest = JagMessage.decodeFromByteBuf(UMediumTest.serializer(), expectedByteBuf)80 actualTest shouldBe expectedTest81 } finally {82 actualByteBuf.release()83 }84 } finally {85 expectedByteBuf.release()86 }87 }88 }89})...

Full Screen

Full Screen

JagMessageSerializationByteArrayTest.kt

Source:JagMessageSerializationByteArrayTest.kt Github

copy

Full Screen

1/*2 * Copyright 2018-2021 Guthix3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package io.guthix.buffer17import io.kotest.core.spec.style.StringSpec18import io.kotest.matchers.shouldBe19import io.kotest.property.Arb20import io.kotest.property.arbitrary.byte21import io.kotest.property.arbitrary.byteArray22import io.kotest.property.arbitrary.int23import io.kotest.property.checkAll24import io.netty.buffer.ByteBufAllocator25import kotlinx.serialization.ExperimentalSerializationApi26import kotlinx.serialization.Serializable27@ExperimentalSerializationApi28@Serializable29private data class ByteArrayTest(30 @JByteArray(10, JByteArrayType.DEFAULT) val default: ByteArray,31 @JByteArray(10, JByteArrayType.REVERSED) val reversed: ByteArray,32 @JByteArray(10, JByteArrayType.ADD) val add: ByteArray,33 @JByteArray(10, JByteArrayType.REVERSED_ADD) val reversedAdd: ByteArray,34) {35 override fun equals(other: Any?): Boolean {36 if (this === other) return true37 if (javaClass != other?.javaClass) return false38 other as ByteArrayTest39 if (!default.contentEquals(other.default)) return false40 if (!reversed.contentEquals(other.reversed)) return false41 if (!add.contentEquals(other.add)) return false42 if (!reversedAdd.contentEquals(other.reversedAdd)) return false43 return true44 }45 override fun hashCode(): Int {46 var result = default.contentHashCode()47 result = 31 * result + reversed.contentHashCode()48 result = 31 * result + add.contentHashCode()49 result = 31 * result + reversedAdd.contentHashCode()50 return result51 }52}53private fun Arb.Companion.byteArray() = byteArray(int(10, 10), byte())54@ExperimentalUnsignedTypes55@ExperimentalSerializationApi56class JagMessageSerializationByteArrayTest : StringSpec({57 "Encode/Decode Test" {58 checkAll(Arb.byteArray(), Arb.byteArray(), Arb.byteArray(), Arb.byteArray()) { default, reversed, add, reversedAdd ->59 val expectedByteBuf = ByteBufAllocator.DEFAULT.jBuffer(0).apply {60 writeBytes(default)61 writeBytesReversed(reversed)62 writeBytesAdd(add)63 writeBytesReversedAdd(reversedAdd)64 }65 try {66 val expectedTest = ByteArrayTest(default, reversed, add, reversedAdd)67 val actualByteBuf = JagMessage.encodeToByteBuf(ByteArrayTest.serializer(), expectedTest)68 try {69 actualByteBuf shouldBe expectedByteBuf70 val actualTest = JagMessage.decodeFromByteBuf(ByteArrayTest.serializer(), expectedByteBuf)71 actualTest shouldBe expectedTest72 } finally {73 actualByteBuf.release()74 }75 } finally {76 expectedByteBuf.release()77 }78 }79 }80})...

Full Screen

Full Screen

bytes.kt

Source:bytes.kt Github

copy

Full Screen

1package io.kotest.property.arbitrary2import io.kotest.property.Arb3import io.kotest.property.Gen4import io.kotest.property.bimap5import kotlin.random.nextUInt6/**7 * Returns an [Arb] that produces [Byte]s from [min] to [max] (inclusive).8 * The edge cases are [min], -1, 0, 1 and [max] which are only included if they are in the provided range.9 */10fun Arb.Companion.byte(min: Byte = Byte.MIN_VALUE, max: Byte = Byte.MAX_VALUE): Arb<Byte> =11 arbitrary(byteArrayOf(min, -1, 0, 1, max).filter { it in min..max }.distinct(), ByteShrinker) {12 generateSequence { it.random.nextBytes(1).first() }.filter { it in min..max }.first()13 }14val ByteShrinker = IntShrinker(Byte.MIN_VALUE..Byte.MAX_VALUE).bimap({ it.toInt() }, { it.toByte() })15/**16 * Returns an [Arb] that produces positive [Byte]s from 1 to [max] (inclusive).17 * The edge cases are 1 and [max] which are only included if they are in the provided range.18 */19fun Arb.Companion.positiveByte(max: Byte = Byte.MAX_VALUE): Arb<Byte> = byte(1, max)20/**21 * Returns an [Arb] that produces negative [Byte]s from [min] to -1 (inclusive).22 * The edge cases are [min] and -1 which are only included if they are in the provided range.23 */24fun Arb.Companion.negativeByte(min: Byte = Byte.MIN_VALUE): Arb<Byte> = byte(min, -1)25/**26 * Returns an [Arb] that produces [ByteArray]s where [length] produces the length of the arrays and27 * [content] produces the content of the arrays.28 */29fun Arb.Companion.byteArray(length: Gen<Int>, content: Arb<Byte>): Arb<ByteArray> =30 toPrimitiveArray(length, content, Collection<Byte>::toByteArray)31@Deprecated("use byteArray", ReplaceWith("byteArray(generateArrayLength, generateContents)"))32fun Arb.Companion.byteArrays(generateArrayLength: Gen<Int>, generateContents: Arb<Byte>): Arb<ByteArray> =33 byteArray(generateArrayLength, generateContents)34/**35 * Returns an [Arb] that produces [UByte]s from [min] to [max] (inclusive).36 * The edge cases are [min], 1 and [max] which are only included if they are in the provided range.37 */38fun Arb.Companion.uByte(min: UByte = UByte.MIN_VALUE, max: UByte = UByte.MAX_VALUE): Arb<UByte> =39 arbitrary(listOf(min, 1u, max).filter { it in min..max }.distinct(), UByteShrinker) {40 it.random.nextUInt(min..max).toUByte()41 }42val UByteShrinker = UIntShrinker(UByte.MIN_VALUE..UByte.MAX_VALUE).bimap({ it.toUInt() }, { it.toUByte() })43/**44 * Returns an [Arb] that produces [UByteArray]s where [length] produces the length of the arrays and45 * [content] produces the content of the arrays.46 */47@ExperimentalUnsignedTypes48fun Arb.Companion.uByteArray(length: Gen<Int>, content: Arb<UByte>): Arb<UByteArray> =49 toPrimitiveArray(length, content, Collection<UByte>::toUByteArray)...

Full Screen

Full Screen

InputStreamDecompressingBodyHandlerIntegrationTest.kt

Source:InputStreamDecompressingBodyHandlerIntegrationTest.kt Github

copy

Full Screen

1/*2 * Copyright (C) 2022 Edgar Asatryan3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16@file:Suppress("ArrayInDataClass")17package io.github.nstdio.http.ext18import io.kotest.matchers.shouldBe19import io.kotest.property.Arb20import io.kotest.property.arbitrary.map21import io.kotest.property.arbitrary.next22import io.kotest.property.arbitrary.string23import mockwebserver3.MockResponse24import mockwebserver3.MockWebServer25import mockwebserver3.junit5.internal.MockWebServerExtension26import org.junit.jupiter.api.extension.ExtendWith27import org.junit.jupiter.params.ParameterizedTest28import org.junit.jupiter.params.provider.MethodSource29import java.net.http.HttpClient30import java.net.http.HttpRequest31import java.nio.charset.StandardCharsets32@ExtendWith(MockWebServerExtension::class)33internal class InputStreamDecompressingBodyHandlerIntegrationTest(private val mockWebServer: MockWebServer) {34 private val httpClient = HttpClient.newHttpClient()35 @ParameterizedTest36 @MethodSource("compressedData")37 fun shouldCreate(compressedString: CompressedString) {38 //given39 val uri = mockWebServer.url("/data").toUri()40 mockWebServer.enqueue(41 MockResponse()42 .setResponseCode(200)43 .addHeader("Content-Encoding", compressedString.compression)44 .setBody(compressedString.compressed)45 )46 val request = HttpRequest.newBuilder(uri)47 .build()48 //when49 val body = httpClient.send(request, BodyHandlers.ofDecompressing()).body()50 val actual = body.readAllBytes().toString(StandardCharsets.UTF_8)51 //then52 actual shouldBe compressedString.original53 }54 companion object {55 @JvmStatic56 fun compressedData(): List<CompressedString> {57 val stringArb = Arb.string(32..64)58 return listOf(59 stringArb.map { CompressedString(it, "gzip", Compression.gzip(it)) }.next(),60 stringArb.map { CompressedString(it, "deflate", Compression.deflate(it)) }.next()61 )62 }63 }64 internal data class CompressedString(val original: String, val compression: String, val compressed: ByteArray)65}...

Full Screen

Full Screen

Arb.Companion.byte

Using AI Code Generation

copy

Full Screen

1import io.kotest.property.arbitrary.Arb2import io.kotest.property.arbitrary.bytes3import io.kotest.property.arbitrary.int4import io.kotest.property.arbitrary.list5import io.kotest.property.checkAll6fun main() {7 checkAll(Arb.list(Arb.int())) { list ->8 println(list)9 }10 checkAll(Arb.list(Arb.bytes())) { list ->11 println(list)12 }13 checkAll(Arb.list(Arb.byte())) { list ->14 println(list)15 }16 checkAll(Arb.list(Arb.byte(10,20))) { list ->17 println(list)18 }19}

Full Screen

Full Screen

Arb.Companion.byte

Using AI Code Generation

copy

Full Screen

1import io.kotest.property.arbitrary.*2val byteArb = Arb.byte()3import io.kotest.property.arbitrary.*4val byteArb = Arb.byte()5import io.kotest.property.arbitrary.*6val byteArb = Arb.byte()7import io.kotest.property.arbitrary.*8val byteArb = Arb.byte()9import io.kotest.property.arbitrary.*10val byteArb = Arb.byte()11import io.kotest.property.arbitrary.*12val byteArb = Arb.byte()13import io.kotest.property.arbitrary.*14val byteArb = Arb.byte()15import io.kotest.property.arbitrary.*16val byteArb = Arb.byte()17import io.kotest.property.arbitrary.*18val byteArb = Arb.byte()19import io.kotest.property.arbitrary.*20val byteArb = Arb.byte()21import io.kotest.property.arbitrary.*22val byteArb = Arb.byte()23import io.kotest.property.arbitrary.*24val byteArb = Arb.byte()25import io.kotest.property.arbitrary.*26val byteArb = Arb.byte()27import io.kotest.property.arbitrary.*28val byteArb = Arb.byte()

Full Screen

Full Screen

Arb.Companion.byte

Using AI Code Generation

copy

Full Screen

1import io.kotest.core.spec.style.FunSpec2import io.kotest.matchers.shouldBe3import io.kotest.property.arbitrary.bytes4class ByteFunSpec : FunSpec({5 test("byte") {6 val byte = bytes().random().first()7 }8})

Full Screen

Full Screen

Arb.Companion.byte

Using AI Code Generation

copy

Full Screen

1import io.kotest.property.arbitrary.*2val byteArray = byte(100).toList()3import io.kotest.property.arbitrary.*4val byteArray = byte(100).toList()5import io.kotest.property.arbitrary.*6val byteArray = byte(100).toList()7import io.kotest.property.arbitrary.*8val byteArray = byte(100).toList()9import io.kotest.property.arbitrary.*10val byteArray = byte(100).toList()11import io.kotest.property.arbitrary.*12val byteArray = byte(100).toList()13import io.kotest.property.arbitrary.*14val byteArray = byte(100).toList()15import io.kotest.property.arbitrary.*16val byteArray = byte(100).toList()17import io.kotest.property.arbitrary.*18val byteArray = byte(100).toList()19import io.kotest.property.arbitrary.*20val byteArray = byte(100).toList()21import io.kotest.property.arbitrary.*22val byteArray = byte(100).toList()23import io.kotest.property.arbitrary.*24val byteArray = byte(100).toList()25import io.kotest.property.arbitrary.*26val byteArray = byte(100).toList()27import io.kotest.property.arbitrary.*28val byteArray = byte(100).toList

Full Screen

Full Screen

Arb.Companion.byte

Using AI Code Generation

copy

Full Screen

1 val byte = Arb.byte(1, 10)2 val byteArray = Arb.bytes(1, 10)3 val byteArray = Arb.bytes(1, 10)4 val byte = Arb.byte(1, 10)5 val byteArray = Arb.bytes(1, 10)6 val byte = Arb.byte(1, 10)7 val byteArray = Arb.bytes(1, 10)8 val byte = Arb.byte(1, 10)9 val byteArray = Arb.bytes(1, 10)10 val byte = Arb.byte(1, 10)11 val byteArray = Arb.bytes(1, 10)12 val byte = Arb.byte(1, 10)13 val byteArray = Arb.bytes(1, 10)14 val byte = Arb.byte(1, 10)15 val byteArray = Arb.bytes(1, 10)16 val byte = Arb.byte(1, 10)17 val byteArray = Arb.bytes(1, 10)18 val byte = Arb.byte(1, 10)19 val byteArray = Arb.bytes(1, 10)20 val byte = Arb.byte(1, 10)21 val byteArray = Arb.bytes(1, 10)22 val byte = Arb.byte(1, 10)23 val byteArray = Arb.bytes(1, 10)

Full Screen

Full Screen

Arb.Companion.byte

Using AI Code Generation

copy

Full Screen

1+val byteNotSpace = Arb.Companion.byte(32, 127)2+val string = Arb.Companion.string()3+val stringNoSpace = Arb.Companion.string(32, 127)4+val stringNoSpace10 = Arb.Companion.string(32, 127, 10)5+val stringNoSpace10 = Arb.Companion.string(32, 127, 10)6+val stringNoSpace10 = Arb.Companion.string(32, 127, 10)7+val stringNoSpace10 = Arb.Companion.string(32, 127, 10)8+val stringNoSpace10 = Arb.Companion.string(32, 127, 10)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Kotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful