|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Matthew Nelson |
| 3 | + * |
| 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 at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * 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 and |
| 14 | + * limitations under the License. |
| 15 | + **/ |
| 16 | +package io.matthewnelson.encoding.core |
| 17 | + |
| 18 | +import io.matthewnelson.encoding.core.Decoder.Companion.decodeBuffered |
| 19 | +import io.matthewnelson.encoding.core.EncoderDecoder.Companion.DEFAULT_BUFFER_SIZE |
| 20 | +import io.matthewnelson.encoding.core.helpers.TestConfig |
| 21 | +import io.matthewnelson.encoding.core.helpers.TestEncoderDecoder |
| 22 | +import kotlin.test.Test |
| 23 | +import kotlin.test.assertEquals |
| 24 | +import kotlin.test.assertFailsWith |
| 25 | +import kotlin.test.fail |
| 26 | + |
| 27 | +class DecodeBufferedUnitTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + fun givenBufferSize_whenLessThanOrEqualToConfigMaxDecodeEmit_thenThrowsIllegalArgumentException() { |
| 31 | + val decoder = TestEncoderDecoder(TestConfig( |
| 32 | + maxDecodeEmit = 255, |
| 33 | + decodeInputReturn = { error("Should not make it here") } |
| 34 | + )) |
| 35 | + |
| 36 | + intArrayOf(254, 255).forEach { testSize -> |
| 37 | + assertFailsWith<IllegalArgumentException> { |
| 38 | + "a".decodeBuffered( |
| 39 | + decoder, |
| 40 | + throwOnOverflow = true, |
| 41 | + maxBufSize = testSize, |
| 42 | + action = { _, _, _ -> error("Should not make it here") }, |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + assertFailsWith<IllegalArgumentException> { |
| 47 | + "a".decodeBuffered( |
| 48 | + decoder, |
| 49 | + throwOnOverflow = true, |
| 50 | + buf = ByteArray(testSize), |
| 51 | + action = { _, _, _ -> error("Should not make it here") }, |
| 52 | + ) |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun givenDecodeInputNonSizeException_whenConfigThrows_thenIsNeverIgnored() { |
| 59 | + val expected = "Config implementation has some sort of checksum at end of encoding and it did not pass" |
| 60 | + val decoder = TestEncoderDecoder(TestConfig( |
| 61 | + decodeInputReturn = { throw EncodingException(expected) } |
| 62 | + )) |
| 63 | + try { |
| 64 | + "a".decodeBuffered(decoder, throwOnOverflow = true) { _, _, _ -> error("Should not make it here") } |
| 65 | + fail() |
| 66 | + } catch (e: EncodingException) { |
| 67 | + assertEquals(expected, e.message) |
| 68 | + } |
| 69 | + try { |
| 70 | + "a".decodeBuffered(decoder, throwOnOverflow = false) { _, _, _ -> error("Should not make it here") } |
| 71 | + fail() |
| 72 | + } catch (e: EncodingException) { |
| 73 | + assertEquals(expected, e.message) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun givenDecodeInputSizeException_whenThrowOnOverflowTrue_thenEncodingSizeExceptionIsRethrown() { |
| 79 | + val decoder = TestEncoderDecoder(TestConfig()) |
| 80 | + assertFailsWith<EncodingSizeException> { |
| 81 | + "a".decodeBuffered( |
| 82 | + decoder, |
| 83 | + throwOnOverflow = true, |
| 84 | + action = { _, _, _ -> error("Should not make it here") } |
| 85 | + ) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun givenDecodeInputSizeException_whenThrowOnOverflowFalse_thenEncodingSizeExceptionIsIgnored() { |
| 91 | + var invocationInput = 0 |
| 92 | + val decoder = TestEncoderDecoder(TestConfig( |
| 93 | + decodeInputReturn = { invocationInput++; -1 }, |
| 94 | + )) |
| 95 | + |
| 96 | + var invocationAction = 0 |
| 97 | + object : CharSequence { |
| 98 | + override val length: Int = DEFAULT_BUFFER_SIZE + 50 |
| 99 | + override fun get(index: Int): Char = 'a' |
| 100 | + override fun subSequence(startIndex: Int, endIndex: Int): CharSequence { error("unused") } |
| 101 | + }.decodeBuffered( |
| 102 | + decoder, |
| 103 | + throwOnOverflow = false, |
| 104 | + action = { buf, _, _ -> |
| 105 | + invocationAction++ |
| 106 | + assertEquals(DEFAULT_BUFFER_SIZE, buf.size) |
| 107 | + } |
| 108 | + ) |
| 109 | + assertEquals(1, invocationInput) |
| 110 | + // Confirms that it went into stream decoding b/c was flushed 2 times |
| 111 | + assertEquals(2, invocationAction) |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + fun givenDecodeInputSize_whenLessThanBufferSize_thenOneShotDecodesWithSmallerSize() { |
| 116 | + val expectedSize = 2 |
| 117 | + val expectedInputSize = DEFAULT_BUFFER_SIZE + 50 |
| 118 | + var invocationConsume = 0 |
| 119 | + val decoder = TestEncoderDecoder( |
| 120 | + config = TestConfig( |
| 121 | + decodeInputReturn = { expectedSize }, |
| 122 | + ), |
| 123 | + decoderConsume = { invocationConsume++ }, |
| 124 | + decoderDoFinal = { (it as TestEncoderDecoder.DecoderFeed).getOut().output(1) }, |
| 125 | + ) |
| 126 | + |
| 127 | + var invocationAction = 0 |
| 128 | + val result = object : CharSequence { |
| 129 | + override val length: Int = expectedInputSize |
| 130 | + override fun get(index: Int): Char = 'a' |
| 131 | + override fun subSequence(startIndex: Int, endIndex: Int): CharSequence = error("unused") |
| 132 | + }.decodeBuffered( |
| 133 | + decoder, |
| 134 | + throwOnOverflow = true, |
| 135 | + maxBufSize = expectedSize * 10, |
| 136 | + action = { buf, _, len -> |
| 137 | + invocationAction++ |
| 138 | + assertEquals(expectedSize, buf.size) |
| 139 | + assertEquals(1, len) |
| 140 | + } |
| 141 | + ) |
| 142 | + assertEquals(1, invocationAction) |
| 143 | + assertEquals(invocationConsume, expectedInputSize) |
| 144 | + assertEquals(1L, result) |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + fun givenConfigMaxDecodeEmit_whenDecodingBuffered_thenFlushesIfLastIndexWouldBeExceeded() { |
| 149 | + val decoder = TestEncoderDecoder(TestConfig(maxDecodeEmit = 50)) |
| 150 | + |
| 151 | + var invocationAction = 0 |
| 152 | + var invocationActionAssertion = 0 |
| 153 | + var actualLen = 0 |
| 154 | + val result = object : CharSequence { |
| 155 | + override val length: Int = DEFAULT_BUFFER_SIZE |
| 156 | + override fun get(index: Int): Char = 'a' |
| 157 | + override fun subSequence(startIndex: Int, endIndex: Int): CharSequence = error("unused") |
| 158 | + }.decodeBuffered( |
| 159 | + decoder, |
| 160 | + throwOnOverflow = false, |
| 161 | + maxBufSize = DEFAULT_BUFFER_SIZE, |
| 162 | + action = { buf, _, len -> |
| 163 | + invocationAction++ |
| 164 | + actualLen += len |
| 165 | + assertEquals(DEFAULT_BUFFER_SIZE, buf.size) |
| 166 | + if (invocationAction == 1) { |
| 167 | + invocationActionAssertion++ |
| 168 | + assertEquals(DEFAULT_BUFFER_SIZE - decoder.config.maxDecodeEmit + 1, len, "invocationAction[$invocationAction]") |
| 169 | + } |
| 170 | + if (invocationAction == 2) { |
| 171 | + invocationActionAssertion++ |
| 172 | + assertEquals(decoder.config.maxDecodeEmit, len) |
| 173 | + } |
| 174 | + } |
| 175 | + ) |
| 176 | + assertEquals(2, invocationAction) |
| 177 | + assertEquals(2, invocationActionAssertion) |
| 178 | + // TestEncoderDecoder.doFinal default value will output one 1 byte |
| 179 | + assertEquals((DEFAULT_BUFFER_SIZE + 1).toLong(), result) |
| 180 | + assertEquals(result, actualLen.toLong()) |
| 181 | + } |
| 182 | +} |
0 commit comments