diff --git a/src/main/java/com/exactpro/epfast/decoder/IDecodeContext.java b/src/main/java/com/exactpro/epfast/decoder/IDecodeContext.java deleted file mode 100644 index 22c7a5d9..00000000 --- a/src/main/java/com/exactpro/epfast/decoder/IDecodeContext.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2019-2020 Exactpro (Exactpro Systems Limited) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.exactpro.epfast.decoder; - -import io.netty.buffer.ByteBuf; - -public interface IDecodeContext { - - int CLEAR_STOP_BIT_MASK = 0b01111111; - - void decode(ByteBuf buf); - - void continueDecode(ByteBuf buf); - - boolean isReady(); - - boolean isOverlong(); -} diff --git a/src/main/java/com/exactpro/epfast/decoder/StreamDecoderCommand.java b/src/main/java/com/exactpro/epfast/decoder/StreamDecoderCommand.java new file mode 100644 index 00000000..16a2a523 --- /dev/null +++ b/src/main/java/com/exactpro/epfast/decoder/StreamDecoderCommand.java @@ -0,0 +1,42 @@ +/* + * Copyright 2019-2020 Exactpro (Exactpro Systems Limited) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.exactpro.epfast.decoder; + +import com.exactpro.epfast.decoder.message.DecoderCommand; +import com.exactpro.epfast.decoder.message.DecoderState; +import com.exactpro.epfast.decoder.message.UnionRegister; +import io.netty.buffer.ByteBuf; + +public abstract class StreamDecoderCommand implements DecoderCommand { + + public static final int FINISHED = 1; + + public static final int MORE_DATA_NEEDED = 0; + + protected static final int CLEAR_STOP_BIT_MASK = 0b01111111; + + protected abstract int decode(ByteBuf buf, UnionRegister register); + + @Override + public int executeOn(DecoderState decoderState) { + if (!decoderState.inputBuffer.isReadable()) { + decoderState.canProceed = false; + return MORE_DATA_NEEDED; + } + return decode(decoderState.inputBuffer, decoderState.register); + } +} diff --git a/src/main/java/com/exactpro/epfast/decoder/ascii/DecodeAsciiString.java b/src/main/java/com/exactpro/epfast/decoder/ascii/DecodeAsciiString.java index 01bafaaf..781badba 100644 --- a/src/main/java/com/exactpro/epfast/decoder/ascii/DecodeAsciiString.java +++ b/src/main/java/com/exactpro/epfast/decoder/ascii/DecodeAsciiString.java @@ -16,17 +16,17 @@ package com.exactpro.epfast.decoder.ascii; -import com.exactpro.epfast.decoder.IDecodeContext; -import com.exactpro.epfast.decoder.OverflowException; +import com.exactpro.epfast.decoder.StreamDecoderCommand; +import com.exactpro.epfast.decoder.message.UnionRegister; import io.netty.buffer.ByteBuf; -public abstract class DecodeAsciiString implements IDecodeContext { +public abstract class DecodeAsciiString extends StreamDecoderCommand { final StringBuilder stringBuilder = new StringBuilder(); private boolean ready; - boolean zeroPreamble; + boolean decodingPreamble = true; final boolean checkOverlong; @@ -38,56 +38,57 @@ public abstract class DecodeAsciiString implements IDecodeContext { this.checkOverlong = checkOverlong; } - public void decode(ByteBuf buf) { - reset(); + @Override + public int decode(ByteBuf buf, UnionRegister register) { int readerIndex = buf.readerIndex(); int readLimit = buf.writerIndex(); - if (buf.getByte(readerIndex) == 0) { - zeroPreamble = true; + while (decodingPreamble && !ready && (readerIndex < readLimit)) { + accumulateValue(getPreambleByte(readerIndex++, buf)); } - accumulateValue(buf.getByte(readerIndex++)); - while ((readerIndex < readLimit) && !ready) { - accumulateValue(buf.getByte(readerIndex++)); + while (!ready && (readerIndex < readLimit)) { + accumulateValue(getByte(readerIndex++, buf)); } buf.readerIndex(readerIndex); - } - - public void continueDecode(ByteBuf buf) { - int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - while ((readerIndex < readLimit) && !ready) { - accumulateValue(buf.getByte(readerIndex++)); + if (ready) { + setResult(register); + reset(); + return FINISHED; + } else { + return MORE_DATA_NEEDED; } - buf.readerIndex(readerIndex); } - public abstract String getValue() throws OverflowException; + public abstract void setResult(UnionRegister unionRegister); - public boolean isReady() { - return ready; - } - - public boolean isOverlong() { - return (zeroPreamble && (zeroCount < stringBuilder.length())); + private int getPreambleByte(int index, ByteBuf buf) { + int aByte = getByte(index, buf); + if (aByte == 0) { + ++zeroCount; + } else { + decodingPreamble = false; + } + return aByte; } - private void accumulateValue(int oneByte) { - if (oneByte < 0) { // if stop bit is set - oneByte &= CLEAR_STOP_BIT_MASK; + private int getByte(int index, ByteBuf buf) { + int aByte = buf.getByte(index); + if (aByte < 0) { // if stop bit is set + aByte &= CLEAR_STOP_BIT_MASK; ready = true; } - if (oneByte == 0) { - zeroCount++; - } + return aByte; + } + + private void accumulateValue(int aByte) { if (stringBuilder.length() < MAX_ALLOWED_LENGTH) { - stringBuilder.append((char) oneByte); + stringBuilder.append((char) aByte); } } public final void reset() { stringBuilder.setLength(0); ready = false; + decodingPreamble = true; zeroCount = 0; - zeroPreamble = false; } } diff --git a/src/main/java/com/exactpro/epfast/decoder/ascii/DecodeMandatoryAsciiString.java b/src/main/java/com/exactpro/epfast/decoder/ascii/DecodeMandatoryAsciiString.java index bcda7d57..7bad1445 100644 --- a/src/main/java/com/exactpro/epfast/decoder/ascii/DecodeMandatoryAsciiString.java +++ b/src/main/java/com/exactpro/epfast/decoder/ascii/DecodeMandatoryAsciiString.java @@ -16,33 +16,44 @@ package com.exactpro.epfast.decoder.ascii; -import com.exactpro.epfast.decoder.OverflowException; +import com.exactpro.epfast.decoder.message.UnionRegister; public final class DecodeMandatoryAsciiString extends DecodeAsciiString { - public DecodeMandatoryAsciiString() { + public DecodeMandatoryAsciiString() { this(false); } - public DecodeMandatoryAsciiString(boolean checkOverlong) { + public DecodeMandatoryAsciiString(boolean checkOverlong) { super(checkOverlong); } - public String getValue() throws OverflowException { + @Override + public void setResult(UnionRegister register) { if (stringBuilder.length() >= MAX_ALLOWED_LENGTH) { - throw new OverflowException("String is longer than allowed"); - } - if (zeroCount < stringBuilder.length()) { - if (zeroPreamble && checkOverlong) { - throw new OverflowException("String with zero preamble can't contain any value except 0"); + register.isOverflow = true; + register.isOverlong = false; + register.isNull = false; + register.infoMessage = "String is longer than allowed"; + } else if (zeroCount < stringBuilder.length()) { + register.isNull = false; + register.isOverflow = false; + register.isOverlong = false; + if ((zeroCount > 1) && checkOverlong) { + register.stringValue = stringBuilder.substring(1); + } else if (zeroCount == 1) { + register.isOverlong = true; + register.infoMessage = "String is overlong if first 7 bits after zero preamble are not \\0"; + register.stringValue = stringBuilder.substring(1); } else { - return stringBuilder.toString(); + register.stringValue = stringBuilder.toString(); } - } else if (zeroCount == 1) { - return ""; } else { stringBuilder.setLength(zeroCount - 1); - return stringBuilder.toString(); + register.isOverflow = false; + register.isOverlong = false; + register.isNull = false; + register.stringValue = stringBuilder.toString(); } } } diff --git a/src/main/java/com/exactpro/epfast/decoder/ascii/DecodeNullableAsciiString.java b/src/main/java/com/exactpro/epfast/decoder/ascii/DecodeNullableAsciiString.java index cbb34ffb..059c4b97 100644 --- a/src/main/java/com/exactpro/epfast/decoder/ascii/DecodeNullableAsciiString.java +++ b/src/main/java/com/exactpro/epfast/decoder/ascii/DecodeNullableAsciiString.java @@ -16,7 +16,7 @@ package com.exactpro.epfast.decoder.ascii; -import com.exactpro.epfast.decoder.OverflowException; +import com.exactpro.epfast.decoder.message.UnionRegister; public final class DecodeNullableAsciiString extends DecodeAsciiString { @@ -28,23 +28,41 @@ public DecodeNullableAsciiString(boolean checkOverlong) { super(checkOverlong); } - public String getValue() throws OverflowException { + @Override + public void setResult(UnionRegister register) { if (stringBuilder.length() >= MAX_ALLOWED_LENGTH) { - throw new OverflowException("String is longer than allowed"); - } - if (zeroCount < stringBuilder.length()) { - if (zeroPreamble && checkOverlong) { - throw new OverflowException("String with zero preamble can't contain any value except 0"); + register.isOverflow = true; + register.isOverlong = false; + register.isNull = false; + register.infoMessage = "String is longer than allowed"; + } else if (zeroCount < stringBuilder.length()) { + register.isNull = false; + register.isOverflow = false; + register.isOverlong = false; + if ((zeroCount > 2) && checkOverlong) { + register.stringValue = stringBuilder.substring(2); + } else if ((zeroCount == 1) && checkOverlong) { + register.isOverlong = true; + register.infoMessage = "String is overlong if first 7 bits after zero preamble are not \\0"; + register.stringValue = stringBuilder.substring(1); + } else if ((zeroCount == 2) && checkOverlong) { + register.isOverlong = true; + register.infoMessage = "String is overlong if first 7 bits after zero preamble are not \\0"; + register.stringValue = stringBuilder.substring(2); } else { - return stringBuilder.toString(); + register.stringValue = stringBuilder.toString(); } } else if (zeroCount == 1) { - return null; - } else if (zeroCount == 2) { - return ""; + register.isOverflow = false; + register.isNull = true; + register.isOverlong = false; + register.stringValue = null; } else { stringBuilder.setLength(zeroCount - 2); - return stringBuilder.toString(); + register.isOverflow = false; + register.isOverlong = false; + register.isNull = false; + register.stringValue = stringBuilder.toString(); } } } diff --git a/src/main/java/com/exactpro/epfast/decoder/decimal/DecodeDecimal.java b/src/main/java/com/exactpro/epfast/decoder/decimal/DecodeDecimal.java index 04c5bccc..7891a503 100644 --- a/src/main/java/com/exactpro/epfast/decoder/decimal/DecodeDecimal.java +++ b/src/main/java/com/exactpro/epfast/decoder/decimal/DecodeDecimal.java @@ -16,14 +16,11 @@ package com.exactpro.epfast.decoder.decimal; -import com.exactpro.epfast.decoder.IDecodeContext; -import com.exactpro.epfast.decoder.OverflowException; +import com.exactpro.epfast.decoder.StreamDecoderCommand; import com.exactpro.epfast.decoder.integer.DecodeMandatoryInt64; -import io.netty.buffer.ByteBuf; +import com.exactpro.epfast.decoder.message.UnionRegister; -import java.math.BigDecimal; - -public abstract class DecodeDecimal implements IDecodeContext { +public abstract class DecodeDecimal extends StreamDecoderCommand { DecodeMandatoryInt64 mantissaDecoder = new DecodeMandatoryInt64(); @@ -31,31 +28,24 @@ public abstract class DecodeDecimal implements IDecodeContext { boolean exponentReady; - boolean startedMantissa; - boolean ready; boolean exponentOverflow; boolean mantissaOverflow; - public abstract void decode(ByteBuf buf); + boolean exponentOverlong; - public abstract void continueDecode(ByteBuf buf); + boolean mantissaOverlong; public final void reset() { exponentReady = false; - startedMantissa = false; ready = false; exponentOverflow = false; mantissaOverflow = false; + exponentOverlong = false; + mantissaOverlong = false; } - public abstract BigDecimal getValue() throws OverflowException; - - public boolean isReady() { - return ready; - } - - public abstract boolean isOverlong(); + public abstract void setResult(UnionRegister register); } diff --git a/src/main/java/com/exactpro/epfast/decoder/decimal/DecodeMandatoryDecimal.java b/src/main/java/com/exactpro/epfast/decoder/decimal/DecodeMandatoryDecimal.java index abc76b56..2d230256 100644 --- a/src/main/java/com/exactpro/epfast/decoder/decimal/DecodeMandatoryDecimal.java +++ b/src/main/java/com/exactpro/epfast/decoder/decimal/DecodeMandatoryDecimal.java @@ -16,8 +16,8 @@ package com.exactpro.epfast.decoder.decimal; -import com.exactpro.epfast.decoder.OverflowException; import com.exactpro.epfast.decoder.integer.DecodeMandatoryInt32; +import com.exactpro.epfast.decoder.message.UnionRegister; import io.netty.buffer.ByteBuf; import java.math.BigDecimal; @@ -28,92 +28,64 @@ public final class DecodeMandatoryDecimal extends DecodeDecimal { private int exponent; - public void decode(ByteBuf buf) { - reset(); - exponentDecoder.decode(buf); - if (exponentDecoder.isReady()) { - exponentReady = true; - try { - exponent = exponentDecoder.getValue(); - } catch (OverflowException ex) { - exponentOverflow = true; - } - if (buf.isReadable()) { - mantissaDecoder.decode(buf); - startedMantissa = true; - if (mantissaDecoder.isReady()) { - ready = true; - try { - mantissa = mantissaDecoder.getValue(); - } catch (OverflowException ex) { - mantissaOverflow = true; - } - } - } - } - } - - public void continueDecode(ByteBuf buf) { - if (exponentReady && startedMantissa) { - mantissaDecoder.continueDecode(buf); - if (mantissaDecoder.isReady()) { - ready = true; - try { - mantissa = mantissaDecoder.getValue(); - } catch (OverflowException ex) { - mantissaOverflow = true; - } - } - } else if (exponentReady) { - mantissaDecoder.decode(buf); - startedMantissa = true; - if (mantissaDecoder.isReady()) { - ready = true; - try { - mantissa = mantissaDecoder.getValue(); - } catch (OverflowException ex) { - mantissaOverflow = true; - } - } - } else { - exponentDecoder.continueDecode(buf); - if (exponentDecoder.isReady()) { + @Override + public int decode(ByteBuf buf, UnionRegister register) { + if (!exponentReady) { + if (exponentDecoder.decode(buf, register) == FINISHED) { exponentReady = true; - try { - exponent = exponentDecoder.getValue(); - } catch (OverflowException ex) { + if (register.isOverlong) { + exponentOverlong = true; + } + if (register.isOverflow) { exponentOverflow = true; + } else { + exponent = register.int32Value; } if (buf.isReadable()) { - mantissaDecoder.decode(buf); - startedMantissa = true; - if (mantissaDecoder.isReady()) { + if (mantissaDecoder.decode(buf, register) == FINISHED) { ready = true; - try { - mantissa = mantissaDecoder.getValue(); - } catch (OverflowException ex) { + if (register.isOverlong) { + mantissaOverlong = true; + } + if (register.isOverflow) { mantissaOverflow = true; + } else { + mantissa = register.int64Value; } + setResult(register); + return FINISHED; } } } + } else if (mantissaDecoder.decode(buf, register) == FINISHED) { + ready = true; + if (register.isOverlong) { + mantissaOverlong = true; + } + if (register.isOverflow) { + mantissaOverflow = true; + } else { + mantissa = register.int64Value; + } + setResult(register); + return FINISHED; } + return MORE_DATA_NEEDED; } - public BigDecimal getValue() throws OverflowException { + public void setResult(UnionRegister register) { + register.isOverlong = exponentOverlong || mantissaOverlong; + register.isOverflow = exponentOverflow || mantissaOverflow; if (exponentOverflow) { - throw new OverflowException("exponent value range is int32"); + register.infoMessage = "exponent value range is int32"; } else if (mantissaOverflow) { - throw new OverflowException("mantissa value range is int64"); + register.infoMessage = "mantissa value range is int64"; } else if (exponent >= -63 && exponent <= 63) { - return new BigDecimal(mantissa).movePointRight(exponent); + register.decimalValue = new BigDecimal(mantissa).movePointRight(exponent); } else { - throw new OverflowException("exponent value allowed range is -63 ... 63"); + register.isOverflow = true; + register.infoMessage = "exponent value allowed range is -63 ... 63"; } - } - - @Override - public boolean isOverlong() { - return exponentDecoder.isOverlong() || mantissaDecoder.isOverlong(); + reset(); } } diff --git a/src/main/java/com/exactpro/epfast/decoder/decimal/DecodeNullableDecimal.java b/src/main/java/com/exactpro/epfast/decoder/decimal/DecodeNullableDecimal.java index 75e567c6..66f4fbae 100644 --- a/src/main/java/com/exactpro/epfast/decoder/decimal/DecodeNullableDecimal.java +++ b/src/main/java/com/exactpro/epfast/decoder/decimal/DecodeNullableDecimal.java @@ -16,8 +16,8 @@ package com.exactpro.epfast.decoder.decimal; -import com.exactpro.epfast.decoder.OverflowException; import com.exactpro.epfast.decoder.integer.DecodeNullableInt32; +import com.exactpro.epfast.decoder.message.UnionRegister; import io.netty.buffer.ByteBuf; import java.math.BigDecimal; @@ -26,104 +26,71 @@ public final class DecodeNullableDecimal extends DecodeDecimal { private DecodeNullableInt32 exponentDecoder = new DecodeNullableInt32(); - private Integer exponent; + private int exponent; - private boolean nullValue; - - public void decode(ByteBuf buf) { - reset(); - exponentDecoder.decode(buf); - if (exponentDecoder.isReady()) { - exponentReady = true; - try { - exponent = exponentDecoder.getValue(); - } catch (OverflowException ex) { - exponentOverflow = true; - } - if (exponent != null && buf.isReadable()) { - mantissaDecoder.decode(buf); - startedMantissa = true; - if (mantissaDecoder.isReady()) { - ready = true; - try { - mantissa = mantissaDecoder.getValue(); - } catch (OverflowException ex) { - mantissaOverflow = true; - } - } - } else if (exponent == null) { - nullValue = true; - ready = true; - } - } - } - - public void continueDecode(ByteBuf buf) { - if (exponentReady && startedMantissa) { - mantissaDecoder.continueDecode(buf); - if (mantissaDecoder.isReady()) { - ready = true; - try { - mantissa = mantissaDecoder.getValue(); - } catch (OverflowException ex) { - mantissaOverflow = true; - } - } - } else if (exponentReady) { - startedMantissa = true; - mantissaDecoder.decode(buf); - if (mantissaDecoder.isReady()) { - ready = true; - try { - mantissa = mantissaDecoder.getValue(); - } catch (OverflowException ex) { - mantissaOverflow = true; - } - } - } else { - exponentDecoder.continueDecode(buf); - if (exponentDecoder.isReady()) { + @Override + public int decode(ByteBuf buf, UnionRegister register) { + if (!exponentReady) { + if (exponentDecoder.decode(buf, register) == FINISHED) { exponentReady = true; - try { - exponent = exponentDecoder.getValue(); - } catch (OverflowException ex) { + if (register.isOverlong) { + exponentOverlong = true; + } + if (register.isOverflow) { exponentOverflow = true; + } else { + exponent = register.int32Value; } - if (exponent != null && buf.isReadable()) { - mantissaDecoder.decode(buf); - startedMantissa = true; - if (mantissaDecoder.isReady()) { + if (!register.isNull && buf.isReadable()) { + if (mantissaDecoder.decode(buf, register) == FINISHED) { ready = true; - try { - mantissa = mantissaDecoder.getValue(); - } catch (OverflowException ex) { + if (register.isOverlong) { + mantissaOverlong = true; + } + if (register.isOverflow) { mantissaOverflow = true; + } else { + mantissa = register.int64Value; } + setResult(register); + return FINISHED; } - } else if (exponent == null) { - nullValue = true; + } else if (register.isNull) { ready = true; + setResult(register); + return FINISHED; } } + } else if (mantissaDecoder.decode(buf, register) == FINISHED) { + ready = true; + if (register.isOverlong) { + mantissaOverlong = true; + } + if (register.isOverflow) { + mantissaOverflow = true; + } else { + mantissa = register.int64Value; + } + setResult(register); + return FINISHED; } + return MORE_DATA_NEEDED; } - public BigDecimal getValue() throws OverflowException { + @Override + public void setResult(UnionRegister register) { + register.isOverlong = exponentOverlong || mantissaOverlong; + register.isOverflow = exponentOverflow || mantissaOverflow; if (exponentOverflow) { - throw new OverflowException("exponent value range is int32"); + register.infoMessage = "exponent value range is int32"; } else if (mantissaOverflow) { - throw new OverflowException("mantissa value range is int64"); - } else if (nullValue) { - return null; + register.infoMessage = "mantissa value range is int64"; } else if (exponent >= -63 && exponent <= 63) { - return new BigDecimal(mantissa).movePointRight(exponent); + register.decimalValue = new BigDecimal(mantissa).movePointRight(exponent); } else { - throw new OverflowException("exponent value allowed range is -63 ... 63"); + register.isOverflow = true; + register.infoMessage = "exponent value allowed range is -63 ... 63"; } - } - - @Override - public boolean isOverlong() { - return exponentDecoder.isOverlong() || mantissaDecoder.isOverlong(); + reset(); } } diff --git a/src/main/java/com/exactpro/epfast/decoder/integer/DecodeInteger.java b/src/main/java/com/exactpro/epfast/decoder/integer/DecodeInteger.java index b58bf253..642d0b26 100644 --- a/src/main/java/com/exactpro/epfast/decoder/integer/DecodeInteger.java +++ b/src/main/java/com/exactpro/epfast/decoder/integer/DecodeInteger.java @@ -16,41 +16,40 @@ package com.exactpro.epfast.decoder.integer; -import com.exactpro.epfast.decoder.IDecodeContext; +import com.exactpro.epfast.decoder.StreamDecoderCommand; import io.netty.buffer.ByteBuf; -public abstract class DecodeInteger implements IDecodeContext { +public abstract class DecodeInteger extends StreamDecoderCommand { - static final int SIGN_BIT_MASK = 0b01000000; + protected static final int SIGN_BIT_MASK = 0b01000000; protected boolean ready; protected boolean overflow; - boolean overlong; + protected boolean overlong; - boolean checkForSignExtension = false; - - public abstract void decode(ByteBuf buf); - - public abstract void continueDecode(ByteBuf buf); - - public boolean isReady() { - return ready; - } - - public boolean isOverlong() { - return overlong; - } + protected int bytesRead; protected final void reset() { + bytesRead = 0; ready = false; overflow = false; overlong = false; - checkForSignExtension = false; } - static void longToBytes(long value, byte[] bytes) { + protected int getByte(ByteBuf buf, int index) { + int oneByte = buf.getByte(index); + ++bytesRead; + if (oneByte < 0) { // if stop bit is set + ready = true; + return oneByte & CLEAR_STOP_BIT_MASK; + } else { + return oneByte; + } + } + + protected static void longToBytes(long value, byte[] bytes) { for (int i = 7; i >= 0; --i) { bytes[i] = (byte) value; value >>>= 8; diff --git a/src/main/java/com/exactpro/epfast/decoder/integer/DecodeMandatoryInt32.java b/src/main/java/com/exactpro/epfast/decoder/integer/DecodeMandatoryInt32.java index b6a9e5f5..e0c9324d 100644 --- a/src/main/java/com/exactpro/epfast/decoder/integer/DecodeMandatoryInt32.java +++ b/src/main/java/com/exactpro/epfast/decoder/integer/DecodeMandatoryInt32.java @@ -16,7 +16,7 @@ package com.exactpro.epfast.decoder.integer; -import com.exactpro.epfast.decoder.OverflowException; +import com.exactpro.epfast.decoder.message.UnionRegister; import io.netty.buffer.ByteBuf; public final class DecodeMandatoryInt32 extends DecodeInteger { @@ -27,105 +27,97 @@ public final class DecodeMandatoryInt32 extends DecodeInteger { private int value; - public void decode(ByteBuf buf) { - reset(); + @Override + public int decode(ByteBuf buf, UnionRegister register) { int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - int oneByte = buf.getByte(readerIndex++); - if ((oneByte & SIGN_BIT_MASK) == 0) { - value = 0; - accumulatePositive(oneByte); - if (oneByte < 0) { - buf.readerIndex(readerIndex); - return; - } - if (readerIndex < readLimit) { - checkOverlongPositive(buf.getByte(readerIndex)); //check second byte - do { - accumulatePositive(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); + int readerLimit = buf.writerIndex(); + if (bytesRead == 0) { + int oneByte = getByte(buf, readerIndex++); + if ((oneByte & SIGN_BIT_MASK) == 0) { + value = 0; + accumulatePositive(oneByte); + if (!ready && (readerIndex < readerLimit)) { + readerIndex = continuePositive(buf, readerIndex, readerLimit); + } } else { - checkForSignExtension = true; + value = -1; + accumulateNegative(oneByte); + if (!ready && (readerIndex < readerLimit)) { + readerIndex = continueNegative(buf, readerIndex, readerLimit); + } } } else { - value = -1; - accumulateNegative(oneByte); - if (oneByte < 0) { - buf.readerIndex(readerIndex); - return; - } - if (readerIndex < readLimit) { - checkOverlongNegative(buf.getByte(readerIndex)); //check second byte - do { - accumulateNegative(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); + if (value >= 0) { + readerIndex = continuePositive(buf, readerIndex, readerLimit); } else { - checkForSignExtension = true; + readerIndex = continueNegative(buf, readerIndex, readerLimit); } } buf.readerIndex(readerIndex); + if (ready) { + setResult(register); + return FINISHED; + } else { + return MORE_DATA_NEEDED; + } } - public void continueDecode(ByteBuf buf) { - int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - if (value >= 0) { - if (checkForSignExtension) { - checkOverlongPositive(buf.getByte(readerIndex)); //continue checking - checkForSignExtension = false; - } - do { - accumulatePositive(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); - } else { - if (checkForSignExtension) { - checkOverlongNegative(buf.getByte(readerIndex)); //check first and second bytes - checkForSignExtension = false; + private int continuePositive(ByteBuf buf, int readerIndex, int readerLimit) { + do { + int oneByte = getByte(buf, readerIndex++); + if (bytesRead == 2) { + checkOverlongPositive(oneByte); } - do { - accumulateNegative(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); - } - buf.readerIndex(readerIndex); + accumulatePositive(oneByte); + } while (!ready && (readerIndex < readerLimit)); + return readerIndex; } - public int getValue() throws OverflowException { - if (overflow) { - throw new OverflowException("Int32 Overflow"); - } else { - return value; - } + private int continueNegative(ByteBuf buf, int readerIndex, int readerLimit) { + do { + int oneByte = getByte(buf, readerIndex++); + if (bytesRead == 2) { + checkOverlongNegative(oneByte); + } + accumulateNegative(oneByte); + } while ((!ready && (readerIndex < readerLimit))); + return readerIndex; } private void accumulatePositive(int oneByte) { - if (oneByte < 0) { // if stop bit is set - oneByte &= CLEAR_STOP_BIT_MASK; - ready = true; - } if (value <= POSITIVE_LIMIT) { - value = (value << 7) | oneByte; + accumulate(oneByte); } else { overflow = true; } } private void accumulateNegative(int oneByte) { - if (oneByte < 0) { // if stop bit is set - oneByte &= CLEAR_STOP_BIT_MASK; - ready = true; - } if (value >= NEGATIVE_LIMIT) { - value = (value << 7) | oneByte; + accumulate(oneByte); } else { overflow = true; } } + private void accumulate(int oneByte) { + value = (value << 7) | oneByte; + } + private void checkOverlongPositive(int secondByte) { - overlong = value == 0 && ((secondByte & SIGN_BIT_MASK) == 0); + overlong = (value == 0) && ((secondByte & SIGN_BIT_MASK) == 0); } private void checkOverlongNegative(int secondByte) { - overlong = value == -1 && ((secondByte & SIGN_BIT_MASK) != 0); + overlong = (value == -1) && ((secondByte & SIGN_BIT_MASK) != 0); + } + + private void setResult(UnionRegister register) { + register.int32Value = value; + register.isNull = false; + register.isOverlong = overlong; + register.isOverflow = overflow; + register.infoMessage = "Int32 Overflow"; + reset(); } } diff --git a/src/main/java/com/exactpro/epfast/decoder/integer/DecodeMandatoryInt64.java b/src/main/java/com/exactpro/epfast/decoder/integer/DecodeMandatoryInt64.java index 706e3cf1..c6177a6e 100644 --- a/src/main/java/com/exactpro/epfast/decoder/integer/DecodeMandatoryInt64.java +++ b/src/main/java/com/exactpro/epfast/decoder/integer/DecodeMandatoryInt64.java @@ -16,7 +16,7 @@ package com.exactpro.epfast.decoder.integer; -import com.exactpro.epfast.decoder.OverflowException; +import com.exactpro.epfast.decoder.message.UnionRegister; import io.netty.buffer.ByteBuf; public final class DecodeMandatoryInt64 extends DecodeInteger { @@ -27,100 +27,92 @@ public final class DecodeMandatoryInt64 extends DecodeInteger { private long value; - public void decode(ByteBuf buf) { - reset(); + @Override + public int decode(ByteBuf buf, UnionRegister register) { int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - int oneByte = buf.getByte(readerIndex++); - if ((oneByte & SIGN_BIT_MASK) == 0) { - value = 0; - accumulatePositive(oneByte); - if (oneByte < 0) { - buf.readerIndex(readerIndex); - return; - } - if (readerIndex < readLimit) { - checkOverlongPositive(buf.getByte(readerIndex)); //check second byte - do { - accumulatePositive(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); + int readerLimit = buf.writerIndex(); + if (bytesRead == 0) { + int oneByte = getByte(buf, readerIndex++); + if ((oneByte & SIGN_BIT_MASK) == 0) { + value = 0; + accumulatePositive(oneByte); + if (!ready && (readerIndex < readerLimit)) { + readerIndex = continuePositive(buf, readerIndex, readerLimit); + } } else { - checkForSignExtension = true; + value = -1; + accumulateNegative(oneByte); + if (!ready && (readerIndex < readerLimit)) { + readerIndex = continueNegative(buf, readerIndex, readerLimit); + } } } else { - value = -1; - accumulateNegative(oneByte); - if (oneByte < 0) { - buf.readerIndex(readerIndex); - return; - } - if (readerIndex < readLimit) { - checkOverlongNegative(buf.getByte(readerIndex)); //check second byte - do { - accumulateNegative(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); + if (value >= 0) { + readerIndex = continuePositive(buf, readerIndex, readerLimit); } else { - checkForSignExtension = true; + readerIndex = continueNegative(buf, readerIndex, readerLimit); } } buf.readerIndex(readerIndex); + if (ready) { + setResult(register); + return FINISHED; + } else { + return MORE_DATA_NEEDED; + } } - public void continueDecode(ByteBuf buf) { - int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - if (value >= 0) { - if (checkForSignExtension) { - checkOverlongPositive(buf.getByte(readerIndex)); //continue checking - checkForSignExtension = false; - } - do { - accumulatePositive(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); - } else { - if (checkForSignExtension) { - checkOverlongNegative(buf.getByte(readerIndex)); //check first and second bytes - checkForSignExtension = false; + private int continuePositive(ByteBuf buf, int readerIndex, int readerLimit) { + do { + int oneByte = getByte(buf, readerIndex++); + if (bytesRead == 2) { + checkOverlongPositive(oneByte); } - do { - accumulateNegative(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); - } - buf.readerIndex(readerIndex); + accumulatePositive(oneByte); + } while (!ready && (readerIndex < readerLimit)); + return readerIndex; } - public long getValue() throws OverflowException { - if (overflow) { - throw new OverflowException("Int64 Overflow"); - } else { - return value; - } + private int continueNegative(ByteBuf buf, int readerIndex, int readerLimit) { + do { + int oneByte = getByte(buf, readerIndex++); + if (bytesRead == 2) { + checkOverlongNegative(oneByte); + } + accumulateNegative(oneByte); + } while ((!ready && (readerIndex < readerLimit))); + return readerIndex; } private void accumulatePositive(int oneByte) { - if (oneByte < 0) { // if stop bit is set - oneByte &= CLEAR_STOP_BIT_MASK; - ready = true; - } if (value <= POSITIVE_LIMIT) { - value = (value << 7) | oneByte; + accumulate(oneByte); } else { overflow = true; } } private void accumulateNegative(int oneByte) { - if (oneByte < 0) { // if stop bit is set - oneByte &= CLEAR_STOP_BIT_MASK; - ready = true; - } if (value >= NEGATIVE_LIMIT) { - value = (value << 7) | oneByte; + accumulate(oneByte); } else { overflow = true; } } + private void accumulate(int oneByte) { + value = (value << 7) | oneByte; + } + + private void setResult(UnionRegister register) { + register.int64Value = value; + register.isNull = false; + register.isOverlong = overlong; + register.isOverflow = overflow; + register.infoMessage = "Int64 Overflow"; + reset(); + } + private void checkOverlongPositive(int secondByte) { overlong = value == 0 && ((secondByte & SIGN_BIT_MASK) == 0); } diff --git a/src/main/java/com/exactpro/epfast/decoder/integer/DecodeMandatoryUInt32.java b/src/main/java/com/exactpro/epfast/decoder/integer/DecodeMandatoryUInt32.java index c0434341..4ba84024 100644 --- a/src/main/java/com/exactpro/epfast/decoder/integer/DecodeMandatoryUInt32.java +++ b/src/main/java/com/exactpro/epfast/decoder/integer/DecodeMandatoryUInt32.java @@ -16,7 +16,7 @@ package com.exactpro.epfast.decoder.integer; -import com.exactpro.epfast.decoder.OverflowException; +import com.exactpro.epfast.decoder.message.UnionRegister; import io.netty.buffer.ByteBuf; public final class DecodeMandatoryUInt32 extends DecodeInteger { @@ -25,54 +25,41 @@ public final class DecodeMandatoryUInt32 extends DecodeInteger { private int value; - public void decode(ByteBuf buf) { - reset(); - value = 0; + @Override + public int decode(ByteBuf buf, UnionRegister register) { int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - int oneByte = buf.getByte(readerIndex++); - accumulate(oneByte); - if (oneByte < 0) { - buf.readerIndex(readerIndex); - return; - } - if (readerIndex < readLimit) { - checkOverlong(buf.getByte(readerIndex)); //check second byte - do { - accumulate(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); + int readerLimit = buf.writerIndex(); + if (bytesRead == 0) { + int oneByte = getByte(buf, readerIndex++); + value = 0; + accumulate(oneByte); + if (!ready && (readerIndex < readerLimit)) { + readerIndex = continuePositive(buf, readerIndex, readerLimit); + } } else { - checkForSignExtension = true; + readerIndex = continuePositive(buf, readerIndex, readerLimit); } buf.readerIndex(readerIndex); - } - - public void continueDecode(ByteBuf buf) { - int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - if (checkForSignExtension) { - checkOverlong(buf.getByte(readerIndex)); //continue checking - checkForSignExtension = false; + if (ready) { + setResult(register); + return FINISHED; + } else { + return MORE_DATA_NEEDED; } - do { - accumulate(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); - buf.readerIndex(readerIndex); } - public long getValue() throws OverflowException { - if (overflow) { - throw new OverflowException("UInt32 Overflow"); - } else { - return value & 0x0_FFFFFFFFL; - } + private int continuePositive(ByteBuf buf, int readerIndex, int readerLimit) { + do { + int oneByte = getByte(buf, readerIndex++); + if (bytesRead == 2) { + checkOverlong(oneByte); + } + accumulate(oneByte); + } while (!ready && (readerIndex < readerLimit)); + return readerIndex; } private void accumulate(int oneByte) { - if (oneByte < 0) { // if stop bit is set - oneByte &= CLEAR_STOP_BIT_MASK; - ready = true; - } if ((value & OVERFLOW_MASK) == 0) { value = (value << 7) | oneByte; } else { @@ -80,6 +67,15 @@ private void accumulate(int oneByte) { } } + private void setResult(UnionRegister register) { + register.uInt32Value = value & 0x0_FFFFFFFFL; + register.isNull = false; + register.isOverlong = overlong; + register.isOverflow = overflow; + register.infoMessage = "UInt32 Overflow"; + reset(); + } + private void checkOverlong(int secondByte) { overlong = value == 0 && ((secondByte & SIGN_BIT_MASK) == 0); } diff --git a/src/main/java/com/exactpro/epfast/decoder/integer/DecodeMandatoryUInt64.java b/src/main/java/com/exactpro/epfast/decoder/integer/DecodeMandatoryUInt64.java index 79eb9b8f..ec65166c 100644 --- a/src/main/java/com/exactpro/epfast/decoder/integer/DecodeMandatoryUInt64.java +++ b/src/main/java/com/exactpro/epfast/decoder/integer/DecodeMandatoryUInt64.java @@ -16,7 +16,7 @@ package com.exactpro.epfast.decoder.integer; -import com.exactpro.epfast.decoder.OverflowException; +import com.exactpro.epfast.decoder.message.UnionRegister; import io.netty.buffer.ByteBuf; import java.math.BigInteger; @@ -29,55 +29,41 @@ public final class DecodeMandatoryUInt64 extends DecodeInteger { private long value; - public void decode(ByteBuf buf) { - reset(); - value = 0; + @Override + public int decode(ByteBuf buf, UnionRegister register) { int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - int oneByte = buf.getByte(readerIndex++); - accumulate(oneByte); - if (oneByte < 0) { - buf.readerIndex(readerIndex); - return; - } - if (readerIndex < readLimit) { - checkOverlong(buf.getByte(readerIndex)); //check second byte - do { - accumulate(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); + int readerLimit = buf.writerIndex(); + if (bytesRead == 0) { + int oneByte = getByte(buf, readerIndex++); + value = 0; + accumulate(oneByte); + if (!ready && (readerIndex < readerLimit)) { + readerIndex = continuePositive(buf, readerIndex, readerLimit); + } } else { - checkForSignExtension = true; + readerIndex = continuePositive(buf, readerIndex, readerLimit); } buf.readerIndex(readerIndex); - } - - public void continueDecode(ByteBuf buf) { - int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - if (checkForSignExtension) { - checkOverlong(buf.getByte(readerIndex)); //continue checking - checkForSignExtension = false; + if (ready) { + setResult(register); + return FINISHED; + } else { + return MORE_DATA_NEEDED; } - do { - accumulate(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); - buf.readerIndex(readerIndex); } - public BigInteger getValue() throws OverflowException { - if (overflow) { - throw new OverflowException("UInt32 Overflow"); - } else { - longToBytes(value, bytes); - return new BigInteger(1, bytes); - } + private int continuePositive(ByteBuf buf, int readerIndex, int readerLimit) { + do { + int oneByte = getByte(buf, readerIndex++); + if (bytesRead == 2) { + checkOverlong(oneByte); + } + accumulate(oneByte); + } while (!ready && (readerIndex < readerLimit)); + return readerIndex; } private void accumulate(int oneByte) { - if (oneByte < 0) { // if stop bit is set - oneByte &= CLEAR_STOP_BIT_MASK; - ready = true; - } if ((value & OVERFLOW_MASK) == 0) { value = (value << 7) | oneByte; } else { @@ -85,8 +71,17 @@ private void accumulate(int oneByte) { } } + private void setResult(UnionRegister register) { + longToBytes(value, bytes); + register.uInt64Value = new BigInteger(1, bytes); + register.isNull = false; + register.isOverlong = overlong; + register.isOverflow = overflow; + register.infoMessage = "UInt64 Overflow"; + reset(); + } + private void checkOverlong(int secondByte) { overlong = value == 0 && ((secondByte & SIGN_BIT_MASK) == 0); } - } diff --git a/src/main/java/com/exactpro/epfast/decoder/integer/DecodeNullableInt32.java b/src/main/java/com/exactpro/epfast/decoder/integer/DecodeNullableInt32.java index 2620f426..7c5583bd 100644 --- a/src/main/java/com/exactpro/epfast/decoder/integer/DecodeNullableInt32.java +++ b/src/main/java/com/exactpro/epfast/decoder/integer/DecodeNullableInt32.java @@ -16,7 +16,7 @@ package com.exactpro.epfast.decoder.integer; -import com.exactpro.epfast.decoder.OverflowException; +import com.exactpro.epfast.decoder.message.UnionRegister; import io.netty.buffer.ByteBuf; public final class DecodeNullableInt32 extends DecodeInteger { @@ -29,85 +29,68 @@ public final class DecodeNullableInt32 extends DecodeInteger { private int value; - public void decode(ByteBuf buf) { - reset(); + @Override + public int decode(ByteBuf buf, UnionRegister register) { int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - int oneByte = buf.getByte(readerIndex++); - if ((oneByte & SIGN_BIT_MASK) == 0) { - positive = true; - value = 0; - accumulatePositive(oneByte); - if (oneByte < 0) { - buf.readerIndex(readerIndex); - return; - } - if (readerIndex < readLimit) { - checkOverlongPositive(buf.getByte(readerIndex)); //check second byte - do { - accumulatePositive(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); + int readerLimit = buf.writerIndex(); + if (bytesRead == 0) { + int oneByte = getByte(buf, readerIndex++); + if ((oneByte & SIGN_BIT_MASK) == 0) { + positive = true; + value = 0; + accumulatePositive(oneByte); + if (!ready && (readerIndex < readerLimit)) { + readerIndex = continuePositive(buf, readerIndex, readerLimit); + } } else { - checkForSignExtension = true; + positive = false; + value = -1; + accumulateNegative(oneByte); + if (!ready && (readerIndex < readerLimit)) { + readerIndex = continueNegative(buf, readerIndex, readerLimit); + } } } else { - positive = false; - value = -1; - accumulateNegative(oneByte); - if (oneByte < 0) { - buf.readerIndex(readerIndex); - return; - } - if (readerIndex < readLimit) { - checkOverlongNegative(buf.getByte(readerIndex)); //check second byte - do { - accumulateNegative(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); + if (value >= 0) { + readerIndex = continuePositive(buf, readerIndex, readerLimit); } else { - checkForSignExtension = true; + readerIndex = continueNegative(buf, readerIndex, readerLimit); } } buf.readerIndex(readerIndex); + if (ready) { + setResult(register); + return FINISHED; + } else { + return MORE_DATA_NEEDED; + } } - public void continueDecode(ByteBuf buf) { - int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - if (value >= 0) { - if (checkForSignExtension) { - checkOverlongPositive(buf.getByte(readerIndex)); //continue checking - checkForSignExtension = false; - } - do { - accumulatePositive(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); - } else { - if (checkForSignExtension) { - checkOverlongNegative(buf.getByte(readerIndex)); //check first and second bytes - checkForSignExtension = false; + private int continuePositive(ByteBuf buf, int readerIndex, int readerLimit) { + do { + int oneByte = getByte(buf, readerIndex++); + if (bytesRead == 2) { + checkOverlongPositive(oneByte); } - do { - accumulateNegative(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); - } - buf.readerIndex(readerIndex); + accumulatePositive(oneByte); + } while (!ready && (readerIndex < readerLimit)); + return readerIndex; } - public Integer getValue() throws OverflowException { - if (overflow) { - throw new OverflowException("Int32 Overflow"); - } else { - return value == 0 ? null : positive ? value - 1 : value; - } + private int continueNegative(ByteBuf buf, int readerIndex, int readerLimit) { + do { + int oneByte = getByte(buf, readerIndex++); + if (bytesRead == 2) { + checkOverlongNegative(oneByte); + } + accumulateNegative(oneByte); + } while ((!ready && (readerIndex < readerLimit))); + return readerIndex; } private void accumulatePositive(int oneByte) { - if (oneByte < 0) { // if stop bit is set - oneByte &= CLEAR_STOP_BIT_MASK; - ready = true; - } if (value < POSITIVE_LIMIT) { - value = (value << 7) | oneByte; + accumulate(oneByte); } else if (value == POSITIVE_LIMIT && oneByte == 0 && ready) { value = (value << 7); } else { @@ -116,17 +99,26 @@ private void accumulatePositive(int oneByte) { } private void accumulateNegative(int oneByte) { - if (oneByte < 0) { // if stop bit is set - oneByte &= CLEAR_STOP_BIT_MASK; - ready = true; - } if (value >= NEGATIVE_LIMIT) { - value = (value << 7) | oneByte; + accumulate(oneByte); } else { overflow = true; } } + private void accumulate(int oneByte) { + value = (value << 7) | oneByte; + } + + private void setResult(UnionRegister register) { + register.int32Value = positive ? value - 1 : value; + register.isNull = value == 0; + register.isOverlong = overlong; + register.isOverflow = overflow; + register.infoMessage = "Int32 Overflow"; + reset(); + } + private void checkOverlongPositive(int secondByte) { overlong = value == 0 && ((secondByte & SIGN_BIT_MASK) == 0); } diff --git a/src/main/java/com/exactpro/epfast/decoder/integer/DecodeNullableInt64.java b/src/main/java/com/exactpro/epfast/decoder/integer/DecodeNullableInt64.java index 7aa33915..d2fd5241 100644 --- a/src/main/java/com/exactpro/epfast/decoder/integer/DecodeNullableInt64.java +++ b/src/main/java/com/exactpro/epfast/decoder/integer/DecodeNullableInt64.java @@ -16,7 +16,7 @@ package com.exactpro.epfast.decoder.integer; -import com.exactpro.epfast.decoder.OverflowException; +import com.exactpro.epfast.decoder.message.UnionRegister; import io.netty.buffer.ByteBuf; public final class DecodeNullableInt64 extends DecodeInteger { @@ -29,85 +29,68 @@ public final class DecodeNullableInt64 extends DecodeInteger { private long value; - public void decode(ByteBuf buf) { - reset(); + @Override + public int decode(ByteBuf buf, UnionRegister register) { int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - int oneByte = buf.getByte(readerIndex++); - if ((oneByte & SIGN_BIT_MASK) == 0) { - positive = true; - value = 0; - accumulatePositive(oneByte); - if (oneByte < 0) { - buf.readerIndex(readerIndex); - return; - } - if (readerIndex < readLimit) { - checkOverlongPositive(buf.getByte(readerIndex)); //check second byte - do { - accumulatePositive(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); + int readerLimit = buf.writerIndex(); + if (bytesRead == 0) { + int oneByte = getByte(buf, readerIndex++); + if ((oneByte & SIGN_BIT_MASK) == 0) { + positive = true; + value = 0; + accumulatePositive(oneByte); + if (!ready && (readerIndex < readerLimit)) { + readerIndex = continuePositive(buf, readerIndex, readerLimit); + } } else { - checkForSignExtension = true; + positive = false; + value = -1; + accumulateNegative(oneByte); + if (!ready && (readerIndex < readerLimit)) { + readerIndex = continueNegative(buf, readerIndex, readerLimit); + } } } else { - positive = false; - value = -1; - accumulateNegative(oneByte); - if (oneByte < 0) { - buf.readerIndex(readerIndex); - return; - } - if (readerIndex < readLimit) { - checkOverlongNegative(buf.getByte(readerIndex)); //check second byte - do { - accumulateNegative(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); + if (value >= 0) { + readerIndex = continuePositive(buf, readerIndex, readerLimit); } else { - checkForSignExtension = true; + readerIndex = continueNegative(buf, readerIndex, readerLimit); } } buf.readerIndex(readerIndex); + if (ready) { + setResult(register); + return FINISHED; + } else { + return MORE_DATA_NEEDED; + } } - public void continueDecode(ByteBuf buf) { - int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - if (value >= 0) { - if (checkForSignExtension) { - checkOverlongPositive(buf.getByte(readerIndex)); //continue checking - checkForSignExtension = false; - } - do { - accumulatePositive(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); - } else { - if (checkForSignExtension) { - checkOverlongNegative(buf.getByte(readerIndex)); //check first and second bytes - checkForSignExtension = false; + private int continuePositive(ByteBuf buf, int readerIndex, int readerLimit) { + do { + int oneByte = getByte(buf, readerIndex++); + if (bytesRead == 2) { + checkOverlongPositive(oneByte); } - do { - accumulateNegative(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); - } - buf.readerIndex(readerIndex); + accumulatePositive(oneByte); + } while (!ready && (readerIndex < readerLimit)); + return readerIndex; } - public Long getValue() throws OverflowException { - if (overflow) { - throw new OverflowException("Int64 Overflow"); - } else { - return value == 0 ? null : positive ? value - 1 : value; - } + private int continueNegative(ByteBuf buf, int readerIndex, int readerLimit) { + do { + int oneByte = getByte(buf, readerIndex++); + if (bytesRead == 2) { + checkOverlongNegative(oneByte); + } + accumulateNegative(oneByte); + } while ((!ready && (readerIndex < readerLimit))); + return readerIndex; } private void accumulatePositive(int oneByte) { - if (oneByte < 0) { // if stop bit is set - oneByte &= CLEAR_STOP_BIT_MASK; - ready = true; - } if (value < POSITIVE_LIMIT) { - value = (value << 7) | oneByte; + accumulate(oneByte); } else if (value == POSITIVE_LIMIT && oneByte == 0 && ready) { value = (value << 7); } else { @@ -116,17 +99,26 @@ private void accumulatePositive(int oneByte) { } private void accumulateNegative(int oneByte) { - if (oneByte < 0) { // if stop bit is set - oneByte &= CLEAR_STOP_BIT_MASK; - ready = true; - } if (value >= NEGATIVE_LIMIT) { - value = (value << 7) | oneByte; + accumulate(oneByte); } else { overflow = true; } } + private void accumulate(int oneByte) { + value = (value << 7) | oneByte; + } + + private void setResult(UnionRegister register) { + register.int64Value = positive ? value - 1 : value; + register.isNull = value == 0; + register.isOverlong = overlong; + register.isOverflow = overflow; + register.infoMessage = "Int64 Overflow"; + reset(); + } + private void checkOverlongPositive(int secondByte) { overlong = value == 0 && ((secondByte & SIGN_BIT_MASK) == 0); } diff --git a/src/main/java/com/exactpro/epfast/decoder/integer/DecodeNullableUInt32.java b/src/main/java/com/exactpro/epfast/decoder/integer/DecodeNullableUInt32.java index f71f7e52..c772444f 100644 --- a/src/main/java/com/exactpro/epfast/decoder/integer/DecodeNullableUInt32.java +++ b/src/main/java/com/exactpro/epfast/decoder/integer/DecodeNullableUInt32.java @@ -16,7 +16,7 @@ package com.exactpro.epfast.decoder.integer; -import com.exactpro.epfast.decoder.OverflowException; +import com.exactpro.epfast.decoder.message.UnionRegister; import io.netty.buffer.ByteBuf; public final class DecodeNullableUInt32 extends DecodeInteger { @@ -27,57 +27,42 @@ public final class DecodeNullableUInt32 extends DecodeInteger { private int value; - public void decode(ByteBuf buf) { - reset(); - value = 0; - isUInt32Limit = false; + @Override + public int decode(ByteBuf buf, UnionRegister register) { int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - int oneByte = buf.getByte(readerIndex++); - accumulate(oneByte); - if (oneByte < 0) { - buf.readerIndex(readerIndex); - return; - } - if (readerIndex < readLimit) { - checkOverlong(buf.getByte(readerIndex)); //check second byte - do { - accumulate(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); + int readerLimit = buf.writerIndex(); + if (bytesRead == 0) { + int oneByte = getByte(buf, readerIndex++); + value = 0; + isUInt32Limit = false; + accumulate(oneByte); + if (!ready && (readerIndex < readerLimit)) { + readerIndex = continuePositive(buf, readerIndex, readerLimit); + } } else { - checkForSignExtension = true; + readerIndex = continuePositive(buf, readerIndex, readerLimit); } buf.readerIndex(readerIndex); - } - - public void continueDecode(ByteBuf buf) { - int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - if (checkForSignExtension) { - checkOverlong(buf.getByte(readerIndex)); //continue checking - checkForSignExtension = false; + if (ready) { + setResult(register); + return FINISHED; + } else { + return MORE_DATA_NEEDED; } - do { - accumulate(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); - buf.readerIndex(readerIndex); } - public Long getValue() throws OverflowException { - if (overflow) { - throw new OverflowException("UInt32 Overflow"); - } else if (value == 0) { - return null; - } else { - return isUInt32Limit ? 0x0_FFFFFFFFL : value - 1 & 0x0_FFFFFFFFL; - } + private int continuePositive(ByteBuf buf, int readerIndex, int readerLimit) { + do { + int oneByte = getByte(buf, readerIndex++); + if (bytesRead == 2) { + checkOverlong(oneByte); + } + accumulate(oneByte); + } while (!ready && (readerIndex < readerLimit)); + return readerIndex; } private void accumulate(int oneByte) { - if (oneByte < 0) { // if stop bit is set - oneByte &= CLEAR_STOP_BIT_MASK; - ready = true; - } if (value < POSITIVE_LIMIT) { value = (value << 7) | oneByte; } else if (value == POSITIVE_LIMIT && oneByte == 0 && ready) { @@ -87,6 +72,15 @@ private void accumulate(int oneByte) { } } + private void setResult(UnionRegister register) { + register.uInt32Value = isUInt32Limit ? 0x0_FFFFFFFFL : value - 1 & 0x0_FFFFFFFFL; + register.isNull = value == 0; + register.isOverlong = overlong; + register.isOverflow = overflow; + register.infoMessage = "UInt32 Overflow"; + reset(); + } + private void checkOverlong(int secondByte) { overlong = value == 0 && ((secondByte & SIGN_BIT_MASK) == 0); } diff --git a/src/main/java/com/exactpro/epfast/decoder/integer/DecodeNullableUInt64.java b/src/main/java/com/exactpro/epfast/decoder/integer/DecodeNullableUInt64.java index 51f2a63c..0e2c7a91 100644 --- a/src/main/java/com/exactpro/epfast/decoder/integer/DecodeNullableUInt64.java +++ b/src/main/java/com/exactpro/epfast/decoder/integer/DecodeNullableUInt64.java @@ -16,7 +16,7 @@ package com.exactpro.epfast.decoder.integer; -import com.exactpro.epfast.decoder.OverflowException; +import com.exactpro.epfast.decoder.message.UnionRegister; import io.netty.buffer.ByteBuf; import java.math.BigInteger; @@ -31,63 +31,42 @@ public final class DecodeNullableUInt64 extends DecodeInteger { private long value; - public void decode(ByteBuf buf) { - reset(); - value = 0; - isUInt64Limit = false; + @Override + public int decode(ByteBuf buf, UnionRegister register) { int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - int oneByte = buf.getByte(readerIndex++); - accumulate(oneByte); - if (oneByte < 0) { - buf.readerIndex(readerIndex); - return; - } - if (readerIndex < readLimit) { - checkOverlong(buf.getByte(readerIndex)); //check second byte - do { - accumulate(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); + int readerLimit = buf.writerIndex(); + if (bytesRead == 0) { + int oneByte = getByte(buf, readerIndex++); + value = 0; + isUInt64Limit = false; + accumulate(oneByte); + if (!ready && (readerIndex < readerLimit)) { + readerIndex = continuePositive(buf, readerIndex, readerLimit); + } } else { - checkForSignExtension = true; + readerIndex = continuePositive(buf, readerIndex, readerLimit); } buf.readerIndex(readerIndex); - } - - public void continueDecode(ByteBuf buf) { - int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - if (checkForSignExtension) { - checkOverlong(buf.getByte(readerIndex)); //continue checking - checkForSignExtension = false; + if (ready) { + setResult(register); + return FINISHED; + } else { + return MORE_DATA_NEEDED; } - do { - accumulate(buf.getByte(readerIndex++)); - } while (!ready && readerIndex < readLimit); - buf.readerIndex(readerIndex); } - public BigInteger getValue() throws OverflowException { - if (overflow) { - throw new OverflowException("UInt64 Overflow"); - } else if (value == 0) { - return null; - } else { - if (isUInt64Limit) { - longToBytes(-1L, bytes); - return new BigInteger(1, bytes); - } else { - longToBytes(value - 1, bytes); - return new BigInteger(1, bytes); + private int continuePositive(ByteBuf buf, int readerIndex, int readerLimit) { + do { + int oneByte = getByte(buf, readerIndex++); + if (bytesRead == 2) { + checkOverlong(oneByte); } - } + accumulate(oneByte); + } while (!ready && (readerIndex < readerLimit)); + return readerIndex; } private void accumulate(int oneByte) { - if (oneByte < 0) { // if stop bit is set - oneByte &= CLEAR_STOP_BIT_MASK; - ready = true; - } if (value < POSITIVE_LIMIT) { value = (value << 7) | oneByte; } else if (value == POSITIVE_LIMIT && oneByte == 0 && ready) { @@ -97,6 +76,20 @@ private void accumulate(int oneByte) { } } + private void setResult(UnionRegister register) { + if (isUInt64Limit) { + longToBytes(-1L, bytes); + } else { + longToBytes(value - 1, bytes); + } + register.uInt64Value = new BigInteger(1, bytes); + register.isNull = value == 0; + register.isOverlong = overlong; + register.isOverflow = overflow; + register.infoMessage = "UInt64 Overflow"; + reset(); + } + private void checkOverlong(int secondByte) { overlong = value == 0 && ((secondByte & SIGN_BIT_MASK) == 0); } diff --git a/src/main/java/com/exactpro/epfast/decoder/message/DecoderState.java b/src/main/java/com/exactpro/epfast/decoder/message/DecoderState.java index f4ef9422..70fbd740 100644 --- a/src/main/java/com/exactpro/epfast/decoder/message/DecoderState.java +++ b/src/main/java/com/exactpro/epfast/decoder/message/DecoderState.java @@ -21,8 +21,6 @@ import com.exactpro.epfast.template.*; import io.netty.buffer.ByteBuf; -import java.math.BigDecimal; -import java.math.BigInteger; import java.util.*; public class DecoderState { @@ -79,32 +77,6 @@ public Collection readMessages() { return messages; } - // Logically it's one register. It's up to application to ensure that the right field is used for read / write. - public static class UnionRegister { - - // public boolean isNull; - - public int mandatoryInt32Value; - - public Integer optionalInt32Value; - - public Long optionalUInt32Value; - - public long mandatoryUInt32Value; - - public String stringValue; - - public long mandatoryInt64Value; - - public Long optionalInt64Value; - - public BigInteger unsignedInt64Value; - - public BigDecimal decimalValue; - - public Object applicationValue; - } - public static class CallStackFrame { private final PresenceMap presenceMap; diff --git a/src/main/java/com/exactpro/epfast/decoder/message/FastCompiler.java b/src/main/java/com/exactpro/epfast/decoder/message/FastCompiler.java index bf9ac08d..e8f05875 100644 --- a/src/main/java/com/exactpro/epfast/decoder/message/FastCompiler.java +++ b/src/main/java/com/exactpro/epfast/decoder/message/FastCompiler.java @@ -16,10 +16,14 @@ package com.exactpro.epfast.decoder.message; +import com.exactpro.epfast.decoder.ascii.DecodeMandatoryAsciiString; +import com.exactpro.epfast.decoder.ascii.DecodeNullableAsciiString; +import com.exactpro.epfast.decoder.integer.DecodeMandatoryInt32; +import com.exactpro.epfast.decoder.integer.DecodeMandatoryUInt32; +import com.exactpro.epfast.decoder.integer.DecodeNullableInt32; +import com.exactpro.epfast.decoder.integer.DecodeNullableUInt32; import com.exactpro.epfast.decoder.message.commands.*; import com.exactpro.epfast.decoder.message.commands.InitIndexedProperty; -import com.exactpro.epfast.decoder.message.commands.ascii.ReadMandatoryAsciiString; -import com.exactpro.epfast.decoder.message.commands.ascii.ReadNullableAsciiString; import com.exactpro.epfast.decoder.message.commands.ascii.SetString; import com.exactpro.epfast.decoder.message.commands.integer.*; import com.exactpro.epfast.decoder.message.commands.operators.AllOtherOperatorsMissingValue; @@ -27,7 +31,8 @@ import com.exactpro.epfast.decoder.message.commands.operators.DefaultMissingValue; import com.exactpro.epfast.decoder.message.commands.operators.DefaultPresentValue; import com.exactpro.epfast.decoder.message.commands.presencemap.CheckPresenceBit; -import com.exactpro.epfast.decoder.message.commands.presencemap.ReadPresenceMap; +import com.exactpro.epfast.decoder.message.commands.presencemap.SetPresenceMap; +import com.exactpro.epfast.decoder.presencemap.DecodePresenceMap; import com.exactpro.epfast.template.*; import java.util.*; @@ -63,7 +68,8 @@ private void compile( commandSet.add(new InitApplicationType(typeRef)); } if (requiresPresenceMap(instructions)) { - commandSet.add(new ReadPresenceMap()); + commandSet.add(new DecodePresenceMap()); + commandSet.add(new SetPresenceMap()); } for (Instruction instruction : instructions) { if (instruction instanceof Group) { @@ -92,10 +98,10 @@ private void compileGroup(Group group) { private void compileSequence(Sequence sequence) { if (sequence.isOptional()) { - commandSet.add(new ReadNullableUInt32()); + commandSet.add(new DecodeNullableUInt32()); commandSet.add(new SetNullableLengthField()); } else { - commandSet.add(new ReadMandatoryUInt32()); + commandSet.add(new DecodeMandatoryUInt32()); commandSet.add(new SetMandatoryLengthField()); } commandSet.add(new InitIndexedProperty(sequence.getFieldId())); @@ -116,11 +122,11 @@ private void compileFieldInstruction(FieldInstruction instruction) { presenceBitIndex++; } if (instruction.isOptional()) { - commandSet.add(new ReadNullableInt32()); + commandSet.add(new DecodeNullableInt32()); addOperator(operator); commandSet.add(new SetNullableInt32(instruction.getFieldId())); } else { - commandSet.add(new ReadMandatoryInt32()); + commandSet.add(new DecodeMandatoryInt32()); addOperator(operator); commandSet.add(new SetMandatoryInt32(instruction.getFieldId())); } @@ -132,9 +138,9 @@ private void compileFieldInstruction(FieldInstruction instruction) { presenceBitIndex++; } if (instruction.isOptional()) { - commandSet.add(new ReadNullableAsciiString()); + commandSet.add(new DecodeNullableAsciiString()); } else { - commandSet.add(new ReadMandatoryAsciiString()); + commandSet.add(new DecodeMandatoryAsciiString()); } addOperator(operator); commandSet.add(new SetString(instruction.getFieldId())); diff --git a/src/main/java/com/exactpro/epfast/decoder/message/InstructionWithDecoder.java b/src/main/java/com/exactpro/epfast/decoder/message/InstructionWithDecoder.java deleted file mode 100644 index 7760db8b..00000000 --- a/src/main/java/com/exactpro/epfast/decoder/message/InstructionWithDecoder.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2020 Exactpro (Exactpro Systems Limited) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.exactpro.epfast.decoder.message; - -import com.exactpro.epfast.decoder.IDecodeContext; -import com.exactpro.epfast.decoder.OverflowException; - -import java.util.Objects; - -public abstract class InstructionWithDecoder implements DecoderCommand { - - protected T fieldDecoder; - - protected boolean decoderStarted; - - protected InstructionWithDecoder(T fieldDecoder) { - this.fieldDecoder = Objects.requireNonNull(fieldDecoder); - } - - protected abstract void decode(DecoderState decoderState) throws OverflowException; - - protected abstract void continueDecode(DecoderState decoderState) throws OverflowException; - - public boolean isReady() { - return fieldDecoder.isReady(); - } - - @Override - public int executeOn(DecoderState decoderState) throws OverflowException { - if (!decoderState.inputBuffer.isReadable()) { - decoderState.canProceed = false; - return 0; - } - if (!decoderStarted) { - decode(decoderState); - } else { - continueDecode(decoderState); - } - if (isReady()) { - return 1; - } else { - decoderState.canProceed = false; - return 0; - } - } -} diff --git a/src/main/java/com/exactpro/epfast/decoder/message/PrimitiveInstruction.java b/src/main/java/com/exactpro/epfast/decoder/message/PrimitiveInstruction.java deleted file mode 100644 index ed4d63d1..00000000 --- a/src/main/java/com/exactpro/epfast/decoder/message/PrimitiveInstruction.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2019-2020 Exactpro (Exactpro Systems Limited) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.exactpro.epfast.decoder.message; - -import com.exactpro.epfast.decoder.IDecodeContext; -import com.exactpro.epfast.decoder.OverflowException; - -public abstract class PrimitiveInstruction extends InstructionWithDecoder { - - protected PrimitiveInstruction(T fieldDecoder) { - super(fieldDecoder); - } - - protected void decode(DecoderState decoderState) throws OverflowException { - decoderStarted = true; - fieldDecoder.decode(decoderState.inputBuffer); - if (isReady()) { - setRegisterValue(decoderState); - decoderStarted = false; - } - } - - protected void continueDecode(DecoderState decoderState) throws OverflowException { - fieldDecoder.continueDecode(decoderState.inputBuffer); - if (isReady()) { - setRegisterValue(decoderState); - decoderStarted = false; - } - } - - public abstract void setRegisterValue(DecoderState decoderState) throws OverflowException; -} diff --git a/src/main/java/com/exactpro/epfast/decoder/message/UnionRegister.java b/src/main/java/com/exactpro/epfast/decoder/message/UnionRegister.java new file mode 100644 index 00000000..3070a19f --- /dev/null +++ b/src/main/java/com/exactpro/epfast/decoder/message/UnionRegister.java @@ -0,0 +1,53 @@ +/* + * Copyright 2020 Exactpro (Exactpro Systems Limited) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.exactpro.epfast.decoder.message; + +import com.exactpro.epfast.decoder.presencemap.PresenceMap; + +import java.math.BigDecimal; +import java.math.BigInteger; + +// Logically it's one register. It's up to application to ensure that the right field is used for read / write. +public class UnionRegister { + + public boolean isNull; + + public boolean isOverflow; + + public boolean isOverlong; + + public int int32Value; + + public long uInt32Value; + + public String stringValue; + + public long int64Value; + + public BigInteger uInt64Value; + + public BigDecimal decimalValue; + + public byte[] byteVectorValue; + + public Object applicationValue; + + public String infoMessage; + + public PresenceMap presenceMap; +} + diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/SetMandatoryLengthField.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/SetMandatoryLengthField.java index 4f6ac10a..5460b16c 100644 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/SetMandatoryLengthField.java +++ b/src/main/java/com/exactpro/epfast/decoder/message/commands/SetMandatoryLengthField.java @@ -22,7 +22,7 @@ public class SetMandatoryLengthField implements DecoderCommand { @Override public int executeOn(DecoderState decoderState) { - decoderState.loopLimit = decoderState.register.mandatoryUInt32Value; + decoderState.loopLimit = decoderState.register.uInt32Value; return 1; } } diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/SetNullableLengthField.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/SetNullableLengthField.java index bd5c4f4c..ebfb230b 100644 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/SetNullableLengthField.java +++ b/src/main/java/com/exactpro/epfast/decoder/message/commands/SetNullableLengthField.java @@ -23,9 +23,8 @@ public class SetNullableLengthField implements DecoderCommand { @Override public int executeOn(DecoderState decoderState) { - Long lengthValue = decoderState.register.optionalUInt32Value; - if (lengthValue != null) { - decoderState.loopLimit = lengthValue; + if (!decoderState.register.isNull) { + decoderState.loopLimit = decoderState.register.uInt32Value; } else { decoderState.loopLimit = -1; } diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/ascii/ReadMandatoryAsciiString.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/ascii/ReadMandatoryAsciiString.java deleted file mode 100644 index ceabcd8d..00000000 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/ascii/ReadMandatoryAsciiString.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2019-2020 Exactpro (Exactpro Systems Limited) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.exactpro.epfast.decoder.message.commands.ascii; - -import com.exactpro.epfast.decoder.OverflowException; -import com.exactpro.epfast.decoder.ascii.DecodeMandatoryAsciiString; -import com.exactpro.epfast.decoder.message.DecoderState; -import com.exactpro.epfast.decoder.message.PrimitiveInstruction; - -public class ReadMandatoryAsciiString extends PrimitiveInstruction { - - public ReadMandatoryAsciiString() { - super(new DecodeMandatoryAsciiString()); - } - - public void setRegisterValue(DecoderState decoderState) throws OverflowException { - decoderState.register.stringValue = fieldDecoder.getValue(); - } -} diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/ascii/ReadNullableAsciiString.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/ascii/ReadNullableAsciiString.java deleted file mode 100644 index 72277d41..00000000 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/ascii/ReadNullableAsciiString.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2019-2020 Exactpro (Exactpro Systems Limited) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.exactpro.epfast.decoder.message.commands.ascii; - -import com.exactpro.epfast.decoder.OverflowException; -import com.exactpro.epfast.decoder.ascii.DecodeNullableAsciiString; -import com.exactpro.epfast.decoder.message.DecoderState; -import com.exactpro.epfast.decoder.message.PrimitiveInstruction; - -public class ReadNullableAsciiString extends PrimitiveInstruction { - - public ReadNullableAsciiString() { - super(new DecodeNullableAsciiString()); - } - - public void setRegisterValue(DecoderState decoderState) throws OverflowException { - decoderState.register.stringValue = fieldDecoder.getValue(); - } -} diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/decimal/ReadMandatoryCompoundDecimal.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/decimal/ReadMandatoryCompoundDecimal.java deleted file mode 100644 index 378cec7d..00000000 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/decimal/ReadMandatoryCompoundDecimal.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2020 Exactpro (Exactpro Systems Limited) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.exactpro.epfast.decoder.message.commands.decimal; - -import com.exactpro.epfast.decoder.OverflowException; -import com.exactpro.epfast.decoder.decimal.DecodeMandatoryDecimal; -import com.exactpro.epfast.decoder.message.DecoderState; -import com.exactpro.epfast.decoder.message.PrimitiveInstruction; - -public class ReadMandatoryCompoundDecimal extends PrimitiveInstruction { - - public ReadMandatoryCompoundDecimal() { - super(new DecodeMandatoryDecimal()); } - - @Override - public void setRegisterValue(DecoderState decoderState) throws OverflowException { - throw new UnsupportedOperationException(); - } -} diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/decimal/ReadMandatorySimpleDecimal.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/decimal/ReadMandatorySimpleDecimal.java deleted file mode 100644 index 2e2aba37..00000000 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/decimal/ReadMandatorySimpleDecimal.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2020 Exactpro (Exactpro Systems Limited) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.exactpro.epfast.decoder.message.commands.decimal; - -import com.exactpro.epfast.decoder.OverflowException; -import com.exactpro.epfast.decoder.decimal.DecodeMandatoryDecimal; -import com.exactpro.epfast.decoder.message.DecoderState; -import com.exactpro.epfast.decoder.message.PrimitiveInstruction; - -public class ReadMandatorySimpleDecimal extends PrimitiveInstruction { - public ReadMandatorySimpleDecimal() { - super(new DecodeMandatoryDecimal()); - } - - @Override - public void setRegisterValue(DecoderState decoderState) throws OverflowException { - throw new UnsupportedOperationException(); - } -} diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/decimal/ReadNullableCompoundDecimal.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/decimal/ReadNullableCompoundDecimal.java deleted file mode 100644 index cc1025ef..00000000 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/decimal/ReadNullableCompoundDecimal.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2020 Exactpro (Exactpro Systems Limited) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.exactpro.epfast.decoder.message.commands.decimal; - -import com.exactpro.epfast.decoder.OverflowException; -import com.exactpro.epfast.decoder.decimal.DecodeNullableDecimal; -import com.exactpro.epfast.decoder.message.DecoderState; -import com.exactpro.epfast.decoder.message.PrimitiveInstruction; - -public class ReadNullableCompoundDecimal extends PrimitiveInstruction { - - public ReadNullableCompoundDecimal() { - super(new DecodeNullableDecimal()); - } - - @Override - public void setRegisterValue(DecoderState decoderState) throws OverflowException { - throw new UnsupportedOperationException(); - } -} diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/decimal/ReadNullableSimpleDecimal.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/decimal/ReadNullableSimpleDecimal.java deleted file mode 100644 index e038c7cf..00000000 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/decimal/ReadNullableSimpleDecimal.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2020 Exactpro (Exactpro Systems Limited) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.exactpro.epfast.decoder.message.commands.decimal; - -import com.exactpro.epfast.decoder.OverflowException; -import com.exactpro.epfast.decoder.decimal.DecodeNullableDecimal; -import com.exactpro.epfast.decoder.message.DecoderState; -import com.exactpro.epfast.decoder.message.PrimitiveInstruction; - -public class ReadNullableSimpleDecimal extends PrimitiveInstruction { - - public ReadNullableSimpleDecimal() { - super(new DecodeNullableDecimal()); - } - - @Override - public void setRegisterValue(DecoderState decoderState) throws OverflowException { - throw new UnsupportedOperationException(); - } -} diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadMandatoryInt32.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadMandatoryInt32.java deleted file mode 100644 index 2eb3409d..00000000 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadMandatoryInt32.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2019-2020 Exactpro (Exactpro Systems Limited) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.exactpro.epfast.decoder.message.commands.integer; - -import com.exactpro.epfast.decoder.OverflowException; -import com.exactpro.epfast.decoder.integer.DecodeMandatoryInt32; -import com.exactpro.epfast.decoder.message.DecoderState; -import com.exactpro.epfast.decoder.message.PrimitiveInstruction; - -public class ReadMandatoryInt32 extends PrimitiveInstruction { - - public ReadMandatoryInt32() { - super(new DecodeMandatoryInt32()); - } - - @Override - public void setRegisterValue(DecoderState decoderState) throws OverflowException { - decoderState.register.mandatoryInt32Value = fieldDecoder.getValue(); - } -} diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadMandatoryInt64.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadMandatoryInt64.java deleted file mode 100644 index 2c4dad9e..00000000 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadMandatoryInt64.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2020 Exactpro (Exactpro Systems Limited) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.exactpro.epfast.decoder.message.commands.integer; - -import com.exactpro.epfast.decoder.OverflowException; -import com.exactpro.epfast.decoder.integer.DecodeMandatoryInt64; -import com.exactpro.epfast.decoder.message.DecoderState; -import com.exactpro.epfast.decoder.message.PrimitiveInstruction; - -public class ReadMandatoryInt64 extends PrimitiveInstruction { - public ReadMandatoryInt64() { - super(new DecodeMandatoryInt64()); - } - - @Override - public void setRegisterValue(DecoderState decoderState) throws OverflowException { - throw new UnsupportedOperationException(); - } -} diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadMandatoryUInt32.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadMandatoryUInt32.java deleted file mode 100644 index a3dcaecf..00000000 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadMandatoryUInt32.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2020 Exactpro (Exactpro Systems Limited) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.exactpro.epfast.decoder.message.commands.integer; - -import com.exactpro.epfast.decoder.OverflowException; -import com.exactpro.epfast.decoder.integer.DecodeMandatoryUInt32; -import com.exactpro.epfast.decoder.message.DecoderState; -import com.exactpro.epfast.decoder.message.PrimitiveInstruction; - -public class ReadMandatoryUInt32 extends PrimitiveInstruction { - - public ReadMandatoryUInt32() { - super(new DecodeMandatoryUInt32()); - } - - @Override - public void setRegisterValue(DecoderState decoderState) throws OverflowException { - decoderState.register.mandatoryUInt32Value = fieldDecoder.getValue(); - } -} diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadMandatoryUInt64.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadMandatoryUInt64.java deleted file mode 100644 index 30b0cf16..00000000 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadMandatoryUInt64.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2020 Exactpro (Exactpro Systems Limited) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.exactpro.epfast.decoder.message.commands.integer; - -import com.exactpro.epfast.decoder.OverflowException; -import com.exactpro.epfast.decoder.integer.DecodeMandatoryUInt64; -import com.exactpro.epfast.decoder.message.DecoderState; -import com.exactpro.epfast.decoder.message.PrimitiveInstruction; - -public class ReadMandatoryUInt64 extends PrimitiveInstruction { - - public ReadMandatoryUInt64() { - super(new DecodeMandatoryUInt64()); - } - - @Override - public void setRegisterValue(DecoderState decoderState) throws OverflowException { - throw new UnsupportedOperationException(); - } -} diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadNullableInt32.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadNullableInt32.java deleted file mode 100644 index d2825fa9..00000000 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadNullableInt32.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2019-2020 Exactpro (Exactpro Systems Limited) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.exactpro.epfast.decoder.message.commands.integer; - -import com.exactpro.epfast.decoder.integer.DecodeNullableInt32; -import com.exactpro.epfast.decoder.OverflowException; -import com.exactpro.epfast.decoder.message.DecoderState; -import com.exactpro.epfast.decoder.message.PrimitiveInstruction; - -public class ReadNullableInt32 extends PrimitiveInstruction { - - public ReadNullableInt32() { - super(new DecodeNullableInt32()); - } - - public void setRegisterValue(DecoderState decoderState) throws OverflowException { - decoderState.register.optionalInt32Value = fieldDecoder.getValue(); - } -} diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadNullableInt64.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadNullableInt64.java deleted file mode 100644 index f3dde71e..00000000 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadNullableInt64.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2020 Exactpro (Exactpro Systems Limited) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.exactpro.epfast.decoder.message.commands.integer; - -import com.exactpro.epfast.decoder.OverflowException; -import com.exactpro.epfast.decoder.integer.DecodeNullableInt64; -import com.exactpro.epfast.decoder.message.DecoderState; -import com.exactpro.epfast.decoder.message.PrimitiveInstruction; - -public class ReadNullableInt64 extends PrimitiveInstruction { - - public ReadNullableInt64() { - super(new DecodeNullableInt64()); - } - - @Override - public void setRegisterValue(DecoderState decoderState) throws OverflowException { - throw new UnsupportedOperationException(); - } -} diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadNullableUInt32.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadNullableUInt32.java deleted file mode 100644 index b5fdb59d..00000000 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadNullableUInt32.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2020 Exactpro (Exactpro Systems Limited) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.exactpro.epfast.decoder.message.commands.integer; - -import com.exactpro.epfast.decoder.OverflowException; -import com.exactpro.epfast.decoder.integer.DecodeNullableUInt32; -import com.exactpro.epfast.decoder.message.DecoderState; -import com.exactpro.epfast.decoder.message.PrimitiveInstruction; - -public class ReadNullableUInt32 extends PrimitiveInstruction { - public ReadNullableUInt32() { - super(new DecodeNullableUInt32()); - } - - public void setRegisterValue(DecoderState decoderState) throws OverflowException { - decoderState.register.optionalUInt32Value = fieldDecoder.getValue(); - } -} diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadNullableUInt64.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadNullableUInt64.java deleted file mode 100644 index a1d31f2f..00000000 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/ReadNullableUInt64.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2020 Exactpro (Exactpro Systems Limited) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.exactpro.epfast.decoder.message.commands.integer; - -import com.exactpro.epfast.decoder.OverflowException; -import com.exactpro.epfast.decoder.integer.DecodeNullableUInt64; -import com.exactpro.epfast.decoder.message.DecoderState; -import com.exactpro.epfast.decoder.message.PrimitiveInstruction; - -public class ReadNullableUInt64 extends PrimitiveInstruction { - public ReadNullableUInt64() { - super(new DecodeNullableUInt64()); - } - - @Override - public void setRegisterValue(DecoderState decoderState) throws OverflowException { - throw new UnsupportedOperationException(); - } -} diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/SetMandatoryInt32.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/SetMandatoryInt32.java index f3a36858..f8c52061 100644 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/SetMandatoryInt32.java +++ b/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/SetMandatoryInt32.java @@ -30,7 +30,7 @@ public SetMandatoryInt32(Reference propertyId) { @Override public int executeOn(DecoderState decoderState) { - decoderState.activeMessage.setField(propertyId, decoderState.register.mandatoryInt32Value); + decoderState.activeMessage.setField(propertyId, decoderState.register.int32Value); return 1; } } diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/SetNullableInt32.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/SetNullableInt32.java index a4ca8dd1..e1c61abc 100644 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/SetNullableInt32.java +++ b/src/main/java/com/exactpro/epfast/decoder/message/commands/integer/SetNullableInt32.java @@ -30,7 +30,11 @@ public SetNullableInt32(Reference propertyId) { @Override public int executeOn(DecoderState decoderState) { - decoderState.activeMessage.setField(propertyId, decoderState.register.optionalInt32Value); + if (decoderState.register.isNull) { + decoderState.activeMessage.setField(propertyId, null); + } else { + decoderState.activeMessage.setField(propertyId, decoderState.register.int32Value); + } return 1; } } diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/operators/AllOtherOperatorsMissingValue.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/operators/AllOtherOperatorsMissingValue.java index bd8fdd12..04299c84 100644 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/operators/AllOtherOperatorsMissingValue.java +++ b/src/main/java/com/exactpro/epfast/decoder/message/commands/operators/AllOtherOperatorsMissingValue.java @@ -27,8 +27,7 @@ public int executeOn(DecoderState decoderState) { } private void tempOperatorLogic(DecoderState decoderState) { - decoderState.register.mandatoryInt32Value = -1; - decoderState.register.optionalInt32Value = -1; + decoderState.register.int32Value = -1; decoderState.register.stringValue = null; } } diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/operators/DefaultMissingValue.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/operators/DefaultMissingValue.java index ef76fdcc..92caedeb 100644 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/operators/DefaultMissingValue.java +++ b/src/main/java/com/exactpro/epfast/decoder/message/commands/operators/DefaultMissingValue.java @@ -29,8 +29,7 @@ public int executeOn(DecoderState decoderState) { } private void tempDefaultOperatorLogic(DecoderState decoderState) { - decoderState.register.mandatoryInt32Value = 1; - decoderState.register.optionalInt32Value = 1; + decoderState.register.int32Value = 1; decoderState.register.stringValue = "Default"; } diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/presencemap/ReadPresenceMap.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/presencemap/SetPresenceMap.java similarity index 65% rename from src/main/java/com/exactpro/epfast/decoder/message/commands/presencemap/ReadPresenceMap.java rename to src/main/java/com/exactpro/epfast/decoder/message/commands/presencemap/SetPresenceMap.java index 1f0a3255..e17c1189 100644 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/presencemap/ReadPresenceMap.java +++ b/src/main/java/com/exactpro/epfast/decoder/message/commands/presencemap/SetPresenceMap.java @@ -16,18 +16,13 @@ package com.exactpro.epfast.decoder.message.commands.presencemap; +import com.exactpro.epfast.decoder.message.DecoderCommand; import com.exactpro.epfast.decoder.message.DecoderState; -import com.exactpro.epfast.decoder.message.PrimitiveInstruction; -import com.exactpro.epfast.decoder.presencemap.DecodePresenceMap; - -public class ReadPresenceMap extends PrimitiveInstruction { - - public ReadPresenceMap() { - super(new DecodePresenceMap()); - } +public class SetPresenceMap implements DecoderCommand { @Override - public void setRegisterValue(DecoderState decoderState) { - decoderState.presenceMap = fieldDecoder.getValue(); + public int executeOn(DecoderState decoderState) { + decoderState.presenceMap = decoderState.register.presenceMap; + return 1; } } diff --git a/src/main/java/com/exactpro/epfast/decoder/message/commands/unicode/ReadMandatoryByteVector.java b/src/main/java/com/exactpro/epfast/decoder/message/commands/unicode/ReadMandatoryByteVector.java deleted file mode 100644 index 1a8ffc6e..00000000 --- a/src/main/java/com/exactpro/epfast/decoder/message/commands/unicode/ReadMandatoryByteVector.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2020 Exactpro (Exactpro Systems Limited) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.exactpro.epfast.decoder.message.commands.unicode; - -import com.exactpro.epfast.decoder.OverflowException; -import com.exactpro.epfast.decoder.message.DecoderState; -import com.exactpro.epfast.decoder.message.PrimitiveInstruction; -import com.exactpro.epfast.decoder.unicode.DecodeMandatoryByteVector; - -public class ReadMandatoryByteVector extends PrimitiveInstruction { - - public ReadMandatoryByteVector() { - super(new DecodeMandatoryByteVector()); - } - - @Override - public void setRegisterValue(DecoderState decoderState) throws OverflowException { - throw new UnsupportedOperationException(); - } -} diff --git a/src/main/java/com/exactpro/epfast/decoder/presencemap/DecodePresenceMap.java b/src/main/java/com/exactpro/epfast/decoder/presencemap/DecodePresenceMap.java index 00378cd6..95acf01c 100644 --- a/src/main/java/com/exactpro/epfast/decoder/presencemap/DecodePresenceMap.java +++ b/src/main/java/com/exactpro/epfast/decoder/presencemap/DecodePresenceMap.java @@ -16,12 +16,13 @@ package com.exactpro.epfast.decoder.presencemap; -import com.exactpro.epfast.decoder.IDecodeContext; +import com.exactpro.epfast.decoder.StreamDecoderCommand; +import com.exactpro.epfast.decoder.message.UnionRegister; import io.netty.buffer.ByteBuf; import java.util.BitSet; -public class DecodePresenceMap implements IDecodeContext { +public class DecodePresenceMap extends StreamDecoderCommand { private BitSet value = new BitSet(); @@ -31,30 +32,27 @@ public class DecodePresenceMap implements IDecodeContext { private boolean ready; - public void decode(ByteBuf buf) { - reset(); - continueDecode(buf); - } - - public void continueDecode(ByteBuf buf) { + @Override + public int decode(ByteBuf buf, UnionRegister register) { int readerIndex = buf.readerIndex(); int readLimit = buf.writerIndex(); while ((readerIndex < readLimit) && !ready) { accumulateValue(buf.getByte(readerIndex++)); } buf.readerIndex(readerIndex); + if (ready) { + setResult(register); + reset(); + return FINISHED; + } else { + return MORE_DATA_NEEDED; + } } - public PresenceMap getValue() { - return new PresenceMap((BitSet) value.clone()); - } - - public boolean isReady() { - return ready; - } - - public boolean isOverlong() { - return setIndex > lastNonZeroIndex; + public void setResult(UnionRegister register) { + register.isOverlong = setIndex > lastNonZeroIndex; + register.isNull = false; + register.presenceMap = new PresenceMap((BitSet) value.clone()); } private void accumulateValue(int oneByte) { @@ -76,6 +74,5 @@ public final void reset() { setIndex = 0; lastNonZeroIndex = 0; } - } diff --git a/src/main/java/com/exactpro/epfast/decoder/unicode/DecodeByteVector.java b/src/main/java/com/exactpro/epfast/decoder/unicode/DecodeByteVector.java index c0f22f18..fe3e180b 100644 --- a/src/main/java/com/exactpro/epfast/decoder/unicode/DecodeByteVector.java +++ b/src/main/java/com/exactpro/epfast/decoder/unicode/DecodeByteVector.java @@ -16,16 +16,15 @@ package com.exactpro.epfast.decoder.unicode; -import com.exactpro.epfast.decoder.IDecodeContext; -import com.exactpro.epfast.decoder.OverflowException; -import io.netty.buffer.ByteBuf; +import com.exactpro.epfast.decoder.StreamDecoderCommand; +import com.exactpro.epfast.decoder.message.UnionRegister; import java.util.ArrayList; import java.util.List; -public abstract class DecodeByteVector implements IDecodeContext { +public abstract class DecodeByteVector extends StreamDecoderCommand { - List value; + List value = new ArrayList<>(); int counter; @@ -35,24 +34,7 @@ public abstract class DecodeByteVector implements IDecodeContext { boolean overflow; - public abstract void decode(ByteBuf buf); - - public abstract void continueDecode(ByteBuf buf); - - public abstract byte[] getValue() throws OverflowException; - - public boolean isReady() { - return ready; - } - - public boolean isOverflow() { - return overflow; - } - - @Override - public boolean isOverlong() { - return false; - } + public abstract void setResult(UnionRegister register); public final void reset() { lengthReady = false; @@ -61,6 +43,7 @@ public final void reset() { counter = 0; value = new ArrayList<>(); } + } diff --git a/src/main/java/com/exactpro/epfast/decoder/unicode/DecodeMandatoryByteVector.java b/src/main/java/com/exactpro/epfast/decoder/unicode/DecodeMandatoryByteVector.java index 9c7a5f53..62952f5f 100644 --- a/src/main/java/com/exactpro/epfast/decoder/unicode/DecodeMandatoryByteVector.java +++ b/src/main/java/com/exactpro/epfast/decoder/unicode/DecodeMandatoryByteVector.java @@ -16,8 +16,8 @@ package com.exactpro.epfast.decoder.unicode; -import com.exactpro.epfast.decoder.OverflowException; import com.exactpro.epfast.decoder.integer.DecodeMandatoryUInt32; +import com.exactpro.epfast.decoder.message.UnionRegister; import io.netty.buffer.ByteBuf; public final class DecodeMandatoryByteVector extends DecodeByteVector { @@ -26,57 +26,15 @@ public final class DecodeMandatoryByteVector extends DecodeByteVector { private long messageLength; - public void decode(ByteBuf buf) { - reset(); - lengthDecoder.decode(buf); - if (lengthDecoder.isReady()) { - lengthReady = true; - try { - messageLength = lengthDecoder.getValue(); - } catch (OverflowException ex) { - overflow = true; - } - if (messageLength > 0) { - int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - while ((readerIndex < readLimit) && !ready) { - if (counter < messageLength) { - value.add(buf.getByte(readerIndex++)); - counter++; - } - if (counter == messageLength) { - ready = true; - } - } - buf.readerIndex(readerIndex); - } else { - ready = true; - } - } - } - - public void continueDecode(ByteBuf buf) { - if (lengthReady) { - int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - while ((readerIndex < readLimit) && !ready) { - if (counter < messageLength) { - value.add(buf.getByte(readerIndex++)); - counter++; - } - if (counter == messageLength) { - ready = true; - } - } - buf.readerIndex(readerIndex); - } else { - lengthDecoder.continueDecode(buf); - if (lengthDecoder.isReady()) { + @Override + public int decode(ByteBuf buf, UnionRegister register) { + if (!lengthReady) { + if (lengthDecoder.decode(buf, register) == FINISHED) { lengthReady = true; - try { - messageLength = lengthDecoder.getValue(); - } catch (OverflowException ex) { + if (register.isOverflow) { overflow = true; + } else { + messageLength = register.uInt32Value; } if (messageLength > 0) { int readerIndex = buf.readerIndex(); @@ -88,25 +46,47 @@ public void continueDecode(ByteBuf buf) { } if (counter == messageLength) { ready = true; + buf.readerIndex(readerIndex); + setResult(register); + return FINISHED; } } buf.readerIndex(readerIndex); } else { ready = true; + setResult(register); + return FINISHED; + } + } + } else { + int readerIndex = buf.readerIndex(); + int readLimit = buf.writerIndex(); + while ((readerIndex < readLimit) && !ready) { + if (counter < messageLength) { + value.add(buf.getByte(readerIndex++)); + counter++; + } + if (counter == messageLength) { + ready = true; + buf.readerIndex(readerIndex); + setResult(register); + return FINISHED; } } + buf.readerIndex(readerIndex); } + return MORE_DATA_NEEDED; } - public byte[] getValue() throws OverflowException { - if (overflow) { - throw new OverflowException("length value range is uint32"); - } else { - byte[] finalVal = new byte[value.size()]; - for (int i = 0; i < value.size(); i++) { - finalVal[i] = value.get(i); - } - return finalVal; + @Override + public void setResult(UnionRegister register) { + register.isOverflow = overflow; + register.infoMessage = "length value range is uint32"; + byte[] finalVal = new byte[value.size()]; + for (int i = 0; i < value.size(); i++) { + finalVal[i] = value.get(i); } + register.byteVectorValue = finalVal; + reset(); } } diff --git a/src/main/java/com/exactpro/epfast/decoder/unicode/DecodeNullableByteVector.java b/src/main/java/com/exactpro/epfast/decoder/unicode/DecodeNullableByteVector.java index 8e3c54e5..ba666792 100644 --- a/src/main/java/com/exactpro/epfast/decoder/unicode/DecodeNullableByteVector.java +++ b/src/main/java/com/exactpro/epfast/decoder/unicode/DecodeNullableByteVector.java @@ -16,69 +16,27 @@ package com.exactpro.epfast.decoder.unicode; -import com.exactpro.epfast.decoder.OverflowException; import com.exactpro.epfast.decoder.integer.DecodeNullableUInt32; +import com.exactpro.epfast.decoder.message.UnionRegister; import io.netty.buffer.ByteBuf; public final class DecodeNullableByteVector extends DecodeByteVector { private DecodeNullableUInt32 lengthDecoder = new DecodeNullableUInt32(); - private Long messageLength; + private long messageLength; - public void decode(ByteBuf buf) { - reset(); - lengthDecoder.decode(buf); - if (lengthDecoder.isReady()) { - lengthReady = true; - try { - messageLength = lengthDecoder.getValue(); - } catch (OverflowException ex) { - overflow = true; - } - if (messageLength != null && messageLength > 0) { - int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - while ((readerIndex < readLimit) && !ready) { - if (counter < messageLength) { - value.add(buf.getByte(readerIndex++)); - counter++; - } - if (counter == messageLength) { - ready = true; - } - } - buf.readerIndex(readerIndex); - } else { - ready = true; - } - } - } - - public void continueDecode(ByteBuf buf) { - if (lengthReady) { - int readerIndex = buf.readerIndex(); - int readLimit = buf.writerIndex(); - while ((readerIndex < readLimit) && !ready) { - if (counter < messageLength) { - value.add(buf.getByte(readerIndex++)); - counter++; - } - if (counter == messageLength) { - ready = true; - } - } - buf.readerIndex(readerIndex); - } else { - lengthDecoder.continueDecode(buf); - if (lengthDecoder.isReady()) { + @Override + public int decode(ByteBuf buf, UnionRegister register) { + if (!lengthReady) { + if (lengthDecoder.decode(buf, register) == FINISHED) { lengthReady = true; - try { - messageLength = lengthDecoder.getValue(); - } catch (OverflowException ex) { + if (register.isOverflow) { overflow = true; + } else { + messageLength = register.uInt32Value; } - if (messageLength != null && messageLength > 0) { + if (!register.isNull && messageLength > 0) { int readerIndex = buf.readerIndex(); int readLimit = buf.writerIndex(); while ((readerIndex < readLimit) && !ready) { @@ -88,25 +46,47 @@ public void continueDecode(ByteBuf buf) { } if (counter == messageLength) { ready = true; + buf.readerIndex(readerIndex); + setResult(register); + return FINISHED; } } buf.readerIndex(readerIndex); } else { ready = true; + setResult(register); + return FINISHED; + } + } + } else { + int readerIndex = buf.readerIndex(); + int readLimit = buf.writerIndex(); + while ((readerIndex < readLimit) && !ready) { + if (counter < messageLength) { + value.add(buf.getByte(readerIndex++)); + counter++; + } + if (counter == messageLength) { + ready = true; + buf.readerIndex(readerIndex); + setResult(register); + return FINISHED; } } + buf.readerIndex(readerIndex); } + return MORE_DATA_NEEDED; } - public byte[] getValue() throws OverflowException { - if (overflow) { - throw new OverflowException("length value range is uint32"); - } else { - byte[] finalVal = new byte[value.size()]; - for (int i = 0; i < value.size(); i++) { - finalVal[i] = value.get(i); - } - return messageLength == null ? null : finalVal; + @Override + public void setResult(UnionRegister register) { + register.isOverflow = overflow; + register.infoMessage = "length value range is uint32"; + byte[] finalVal = new byte[value.size()]; + for (int i = 0; i < value.size(); i++) { + finalVal[i] = value.get(i); } + register.byteVectorValue = finalVal; + reset(); } } diff --git a/src/test/java/com/exactpro/epfast/DecoderUtils.java b/src/test/java/com/exactpro/epfast/decoder/DecoderUtils.java similarity index 77% rename from src/test/java/com/exactpro/epfast/DecoderUtils.java rename to src/test/java/com/exactpro/epfast/decoder/DecoderUtils.java index f5efaed1..055cb411 100644 --- a/src/test/java/com/exactpro/epfast/DecoderUtils.java +++ b/src/test/java/com/exactpro/epfast/decoder/DecoderUtils.java @@ -14,20 +14,18 @@ * limitations under the License. */ -package com.exactpro.epfast; +package com.exactpro.epfast.decoder; -import com.exactpro.epfast.decoder.IDecodeContext; +import com.exactpro.epfast.decoder.message.UnionRegister; import io.netty.buffer.ByteBuf; import java.util.Iterator; public class DecoderUtils { - public static void decode(IDecodeContext decoder, Iterable buffers) { + public static void decode(StreamDecoderCommand decoder, Iterable buffers, UnionRegister register) { Iterator it = buffers.iterator(); - decoder.decode(nextNonEmptyBuffer(it)); - while (!decoder.isReady()) { - decoder.continueDecode(nextNonEmptyBuffer(it)); + while (decoder.decode(nextNonEmptyBuffer(it), register) == StreamDecoderCommand.MORE_DATA_NEEDED) { } } diff --git a/src/test/java/com/exactpro/epfast/decoder/ascii/TestDecodeAsciiString.java b/src/test/java/com/exactpro/epfast/decoder/ascii/TestDecodeAsciiString.java index 41701498..c6d12dcb 100644 --- a/src/test/java/com/exactpro/epfast/decoder/ascii/TestDecodeAsciiString.java +++ b/src/test/java/com/exactpro/epfast/decoder/ascii/TestDecodeAsciiString.java @@ -16,22 +16,24 @@ package com.exactpro.epfast.decoder.ascii; -import com.exactpro.epfast.decoder.OverflowException; +import com.exactpro.epfast.decoder.message.UnionRegister; import com.exactpro.junit5.WithByteBuf; import io.netty.buffer.ByteBuf; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import java.io.IOException; import java.security.InvalidParameterException; import java.util.Collection; import static org.junit.jupiter.api.Assertions.*; import static com.exactpro.junit5.ByteBufUtils.*; -import static com.exactpro.epfast.DecoderUtils.*; +import static com.exactpro.epfast.decoder.DecoderUtils.*; class TestDecodeAsciiString { + private final UnionRegister decodeResult = new UnionRegister(); + static String fastAsciiStringOf(char character, int length) { if (character > 0 && character < 128) { StringBuilder stringBuilder = new StringBuilder(length); @@ -45,190 +47,244 @@ static String fastAsciiStringOf(char character, int length) { } @Nested - class TestNullable { - - private DecodeNullableAsciiString decoder = new DecodeNullableAsciiString(); + class TestOptional { + private final DecodeNullableAsciiString decoder = new DecodeNullableAsciiString(true); @WithByteBuf("80") - void testNull(Collection buffers) throws IOException { - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertNull(decoder.getValue()); - assertFalse(decoder.isOverlong()); + void testNull(Collection buffers) { + decodeResult.isNull = false; + decodeResult.stringValue = ""; + + decode(decoder, buffers, decodeResult); + assertNull(decodeResult.stringValue); + assertTrue(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("00 80") - void testOptionalEmptyString(Collection buffers) throws IOException { - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertEquals("", decoder.getValue()); - assertFalse(decoder.isOverlong()); + void testOptionalEmptyString(Collection buffers) { + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals("", decodeResult.stringValue); } @WithByteBuf("41 42 c3") - void testSimpleString(Collection buffers) throws IOException { - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertEquals("ABC", decoder.getValue()); - assertFalse(decoder.isOverlong()); + void testOptionalSimpleString(Collection buffers) { + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals("ABC", decodeResult.stringValue); } - @WithByteBuf("00 00 80") - void testZeroByteStringNullable1(Collection buffers) throws IOException { - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertEquals("\0", decoder.getValue()); - assertFalse(decoder.isOverlong()); + @WithByteBuf("00 C1") + void testOptionalOverlongString1(Collection buffers) { + decodeResult.isOverlong = false; + + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertTrue(decodeResult.isOverlong); + assertEquals("A", decodeResult.stringValue); } - @WithByteBuf("00 00 00 00 80") - void testZeroByteStringNullable2(Collection buffers) throws IOException { - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertEquals("\0\0\0", decoder.getValue()); - assertFalse(decoder.isOverlong()); + @WithByteBuf("00 00 C1") + void testOptionalOverlongString2(Collection buffers) { + decodeResult.isOverlong = false; + + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertTrue(decodeResult.isOverlong); + assertEquals("A", decodeResult.stringValue); } - @WithByteBuf("41 42 c3 42 42 c3 41 44 c3") - void testNullableReuse(Collection buffers) throws IOException { - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertFalse(decoder.isOverlong()); - assertEquals("ABC", decoder.getValue()); - decode(decoder, buffers); - assertEquals("BBC", decoder.getValue()); - decode(decoder, buffers); - assertEquals("ADC", decoder.getValue()); + @WithByteBuf("00 00 00 00 80") + void testOptionalStringAllZeros(Collection buffers) { + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals("\0\0\0", decodeResult.stringValue); } - @WithByteBuf("41 42 c3") - void testSimpleStringGetValueTwice(Collection buffers) throws IOException { - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertFalse(decoder.isOverlong()); - assertEquals("ABC", decoder.getValue()); - assertEquals("ABC", decoder.getValue()); + @WithByteBuf("41 42 c3 42 42 c3 41 44 c3") + void testOptionalStringDecoderReuse(Collection buffers) { + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals("ABC", decodeResult.stringValue); + + resetRegisterFlags(); + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals("BBC", decodeResult.stringValue); + + resetRegisterFlags(); + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals("ADC", decodeResult.stringValue); } - @WithByteBuf("00 00 00 81") - void testNullableOverlongNoException(Collection buffers) { - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertTrue(decoder.isOverlong()); + @WithByteBuf("00 00 00 C1") + void testOptionalStringStartedWithZero(Collection buffers) { + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals("\0A", decodeResult.stringValue); } @Nested class TestOverflowException { - - private DecodeNullableAsciiString decoder = new DecodeNullableAsciiString(true); - - @WithByteBuf("00 00 00 81") - void testNullableOverlong1(Collection buffers) { - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertTrue(decoder.isOverlong()); - assertThrows(OverflowException.class, () -> decoder.getValue()); + private final DecodeNullableAsciiString decoder = new DecodeNullableAsciiString(true); + + @WithByteBuf("00 00 00 C1") + void testOptionalOverlong1(Collection buffers) { + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals("\0A", decodeResult.stringValue); } @Test - void testNullableOverlong2() { - decoder.decode(fromHex(fastAsciiStringOf(')', 2 * DecodeAsciiString.MAX_ALLOWED_LENGTH))); - assertTrue(decoder.isReady()); - assertThrows(OverflowException.class, () -> decoder.getValue()); + void testOptionalOverlong2() { + decodeResult.isOverflow = false; + + decoder.decode( + fromHex(fastAsciiStringOf(')', 2 * DecodeAsciiString.MAX_ALLOWED_LENGTH)), + decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } } @WithByteBuf("00 00 80 00 00 00 00 80") - void testZeroByteStringNullableTwoValuesInRow(Collection buffers) throws IOException { - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertFalse(decoder.isOverlong()); - assertEquals("\0", decoder.getValue()); - - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertFalse(decoder.isOverlong()); - assertEquals("\0\0\0", decoder.getValue()); + void testOptionalStringTwoValuesInRow(Collection buffers) { + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals("\0", decodeResult.stringValue); + + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals("\0\0\0", decodeResult.stringValue); } } @Nested class TestMandatory { - - private DecodeMandatoryAsciiString decoder = new DecodeMandatoryAsciiString(); + private final DecodeMandatoryAsciiString decoder = new DecodeMandatoryAsciiString(true); @WithByteBuf("80") - void testMandatoryEmptyString(Collection buffers) throws IOException { - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertFalse(decoder.isOverlong()); - assertEquals("", decoder.getValue()); + void testMandatoryEmptyString(Collection buffers) { + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals("", decodeResult.stringValue); } @WithByteBuf("00 00 80") - void testZeroByteStringMandatory1(Collection buffers) throws IOException { - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertFalse(decoder.isOverlong()); - assertEquals("\0\0", decoder.getValue()); + void testMandatoryStringAllZeros1(Collection buffers) { + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals("\0\0", decodeResult.stringValue); } @WithByteBuf("00 00 00 00 80") - void testZeroByteStringMandatory2(Collection buffers) throws IOException { - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertFalse(decoder.isOverlong()); - assertEquals("\0\0\0\0", decoder.getValue()); + void testMandatoryStringAllZeros2(Collection buffers) { + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals("\0\0\0\0", decodeResult.stringValue); } - @WithByteBuf("80") - void testMandatoryEmptyStringGetValueTwice(Collection buffers) throws IOException { - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertFalse(decoder.isOverlong()); - assertEquals("", decoder.getValue()); - assertEquals("", decoder.getValue()); + @WithByteBuf("00 C1") + void testMandatoryOverlongString(Collection buffers) { + decodeResult.isOverlong = false; + + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertTrue(decodeResult.isOverlong); + assertEquals("A", decodeResult.stringValue); } - @WithByteBuf("00 00 00 81") - void testMandatoryOverlongNoException(Collection buffers) { - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertTrue(decoder.isOverlong()); + @WithByteBuf("00 00 C1") + void testMandatoryStringStartedWithZero(Collection buffers) { + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals("\0A", decodeResult.stringValue); } @Nested class TestOverflowException { + private final DecodeMandatoryAsciiString decoder = new DecodeMandatoryAsciiString(true); - private DecodeMandatoryAsciiString decoder = new DecodeMandatoryAsciiString(true); - - @WithByteBuf("00 81") + @WithByteBuf("00 C1") void testMandatoryOverlong1(Collection buffers) { - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertTrue(decoder.isOverlong()); - assertThrows(OverflowException.class, () -> decoder.getValue()); + decodeResult.isOverlong = false; + + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertTrue(decodeResult.isOverlong); + assertEquals("A", decodeResult.stringValue); } @Test void testMandatoryOverlong2() { - decoder.decode(fromHex(fastAsciiStringOf('*', 3 * DecodeAsciiString.MAX_ALLOWED_LENGTH))); - assertTrue(decoder.isReady()); - assertThrows(OverflowException.class, () -> decoder.getValue()); + decodeResult.isOverflow = false; + + decoder.decode( + fromHex(fastAsciiStringOf('*', 3 * DecodeAsciiString.MAX_ALLOWED_LENGTH)), + decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } } @WithByteBuf("00 00 80 00 00 00 00 80") - void testZeroByteStringMandatoryTwoValuesInRow(Collection buffers) throws IOException { - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertFalse(decoder.isOverlong()); - assertEquals("\0\0", decoder.getValue()); - - decode(decoder, buffers); - assertTrue(decoder.isReady()); - assertFalse(decoder.isOverlong()); - assertEquals("\0\0\0\0", decoder.getValue()); + void testMandatoryStringTwoValuesInRow(Collection buffers) { + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals("\0\0", decodeResult.stringValue); + + decode(decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals("\0\0\0\0", decodeResult.stringValue); } } + + @BeforeEach + void resetRegisterFlags() { + decodeResult.isOverlong = true; + decodeResult.isNull = true; + decodeResult.isOverflow = true; + decodeResult.stringValue = null; + } } diff --git a/src/test/java/com/exactpro/epfast/decoder/decimal/TestDecodeDecimal.java b/src/test/java/com/exactpro/epfast/decoder/decimal/TestDecodeDecimal.java index b078fd88..8bd0630f 100644 --- a/src/test/java/com/exactpro/epfast/decoder/decimal/TestDecodeDecimal.java +++ b/src/test/java/com/exactpro/epfast/decoder/decimal/TestDecodeDecimal.java @@ -16,15 +16,17 @@ package com.exactpro.epfast.decoder.decimal; -import java.io.IOException; import java.math.BigDecimal; import java.util.Collection; +import com.exactpro.epfast.decoder.message.UnionRegister; import com.exactpro.junit5.WithByteBuf; import io.netty.buffer.ByteBuf; +import org.junit.jupiter.api.BeforeEach; import static org.junit.jupiter.api.Assertions.*; -import static com.exactpro.epfast.DecoderUtils.*; +import static com.exactpro.epfast.decoder.DecoderUtils.*; +import static org.junit.jupiter.api.Assertions.assertFalse; class TestDecodeDecimal { @@ -32,247 +34,324 @@ class TestDecodeDecimal { private DecodeMandatoryDecimal mandatoryDecimalDecoder = new DecodeMandatoryDecimal(); + private UnionRegister decodeResult = new UnionRegister(); + @WithByteBuf("80") - void testNull(Collection buffers) throws IOException { - decode(nullableDecimalDecoder, buffers); - assertTrue(nullableDecimalDecoder.isReady()); - assertFalse(nullableDecimalDecoder.isOverlong()); - assertNull(nullableDecimalDecoder.getValue()); + void testNull(Collection buffers) { + decodeResult.isNull = false; + decodeResult.decimalValue = new BigDecimal(1); + + decode(nullableDecimalDecoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertTrue(decodeResult.isNull); } @WithByteBuf("83 39 45 a3") - void testNullablePositive1(Collection buffers) throws IOException { - decode(nullableDecimalDecoder, buffers); - assertTrue(nullableDecimalDecoder.isReady()); - assertFalse(nullableDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("94275500"), nullableDecimalDecoder.getValue()); + void testNullablePositive1(Collection buffers) { + decode(nullableDecimalDecoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("94275500"), decodeResult.decimalValue); } @WithByteBuf("82 04 3f 34 de") - void testNullablePositive(Collection buffers) throws IOException { - decode(nullableDecimalDecoder, buffers); - assertTrue(nullableDecimalDecoder.isReady()); - assertFalse(nullableDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("94275500"), nullableDecimalDecoder.getValue()); + void testNullablePositive(Collection buffers) { + decode(nullableDecimalDecoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("94275500"), decodeResult.decimalValue); } @WithByteBuf("82 39 45 a3") - void testMandatoryPositive(Collection buffers) throws IOException { - decode(mandatoryDecimalDecoder, buffers); - assertTrue(mandatoryDecimalDecoder.isReady()); - assertFalse(mandatoryDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("94275500"), mandatoryDecimalDecoder.getValue()); + void testMandatoryPositive(Collection buffers) { + decode(mandatoryDecimalDecoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("94275500"), decodeResult.decimalValue); } @WithByteBuf("81 04 3f 34 de") - void testMandatoryPositive2(Collection buffers) throws IOException { - decode(mandatoryDecimalDecoder, buffers); - assertTrue(mandatoryDecimalDecoder.isReady()); - assertFalse(mandatoryDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("94275500"), mandatoryDecimalDecoder.getValue()); + void testMandatoryPositive2(Collection buffers) { + decode(mandatoryDecimalDecoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("94275500"), decodeResult.decimalValue); } @WithByteBuf("fe 39 45 a3") - void testMandatoryPositive3(Collection buffers) throws IOException { - decode(mandatoryDecimalDecoder, buffers); - assertTrue(mandatoryDecimalDecoder.isReady()); - assertFalse(mandatoryDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("9427.55"), mandatoryDecimalDecoder.getValue()); + void testMandatoryPositive3(Collection buffers) { + decode(mandatoryDecimalDecoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("9427.55"), decodeResult.decimalValue); } @WithByteBuf("fe 39 45 a3") - void testNullablePositive3(Collection buffers) throws IOException { - decode(nullableDecimalDecoder, buffers); - assertTrue(nullableDecimalDecoder.isReady()); - assertFalse(nullableDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("9427.55"), nullableDecimalDecoder.getValue()); + void testNullablePositive3(Collection buffers) { + decode(nullableDecimalDecoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("9427.55"), decodeResult.decimalValue); } @WithByteBuf("fe 46 3a dd") - void testNullableNegative(Collection buffers) throws IOException { - decode(nullableDecimalDecoder, buffers); - assertTrue(nullableDecimalDecoder.isReady()); - assertFalse(nullableDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("-9427.55"), nullableDecimalDecoder.getValue()); + void testNullableNegative(Collection buffers) { + decode(nullableDecimalDecoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("-9427.55"), decodeResult.decimalValue); } @WithByteBuf("fd 7f 3f ff") - void testNullableNegativeSignExtension(Collection buffers) throws IOException { - decode(nullableDecimalDecoder, buffers); - assertTrue(nullableDecimalDecoder.isReady()); - assertFalse(nullableDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("-8.193"), nullableDecimalDecoder.getValue()); + void testNullableNegativeSignExtension(Collection buffers) { + decode(nullableDecimalDecoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("-8.193"), decodeResult.decimalValue); } @WithByteBuf("39 45 a4 7f 3f ff") void testExponentIOException(Collection buffers) { - decode(nullableDecimalDecoder, buffers); - assertTrue(nullableDecimalDecoder.isReady()); - assertFalse(nullableDecimalDecoder.isOverlong()); - assertThrows(IOException.class, () -> nullableDecimalDecoder.getValue()); + decodeResult.isOverflow = false; + + decode(nullableDecimalDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); } @WithByteBuf("82 04 3f 34 de") - void testNullablePositiveGetValueTwice(Collection buffers) throws IOException { - decode(nullableDecimalDecoder, buffers); - assertTrue(nullableDecimalDecoder.isReady()); - assertFalse(nullableDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("94275500"), nullableDecimalDecoder.getValue()); - assertEquals(new BigDecimal("94275500"), nullableDecimalDecoder.getValue()); + void testNullablePositiveGetValueTwice(Collection buffers) { + decode(nullableDecimalDecoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("94275500"), decodeResult.decimalValue); + assertEquals(new BigDecimal("94275500"), decodeResult.decimalValue); } @WithByteBuf("82 39 45 a3") - void testMandatoryPositiveGetValueTwice(Collection buffers) throws IOException { - decode(mandatoryDecimalDecoder, buffers); - assertTrue(mandatoryDecimalDecoder.isReady()); - assertFalse(mandatoryDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("94275500"), mandatoryDecimalDecoder.getValue()); - assertEquals(new BigDecimal("94275500"), mandatoryDecimalDecoder.getValue()); + void testMandatoryPositiveGetValueTwice(Collection buffers) { + decode(mandatoryDecimalDecoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("94275500"), decodeResult.decimalValue); + assertEquals(new BigDecimal("94275500"), decodeResult.decimalValue); } @WithByteBuf("83 39 45 a3 82 04 3f 34 de") - void testNullablePositiveTwoValuesInRow(Collection buffers) throws IOException { - decode(nullableDecimalDecoder, buffers); - assertTrue(nullableDecimalDecoder.isReady()); - assertFalse(nullableDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("94275500"), nullableDecimalDecoder.getValue()); - - decode(nullableDecimalDecoder, buffers); - assertTrue(nullableDecimalDecoder.isReady()); - assertFalse(nullableDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("94275500"), nullableDecimalDecoder.getValue()); + void testNullablePositiveTwoValuesInRow(Collection buffers) { + decode(nullableDecimalDecoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("94275500"), decodeResult.decimalValue); + + decode(nullableDecimalDecoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("94275500"), decodeResult.decimalValue); } @WithByteBuf("82 39 45 a3 81 04 3f 34 de") - void testMandatoryPositiveTwoValuesInRow(Collection buffers) throws IOException { - decode(mandatoryDecimalDecoder, buffers); - assertTrue(mandatoryDecimalDecoder.isReady()); - assertFalse(mandatoryDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("94275500"), mandatoryDecimalDecoder.getValue()); - - decode(mandatoryDecimalDecoder, buffers); - assertTrue(mandatoryDecimalDecoder.isReady()); - assertFalse(mandatoryDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("94275500"), mandatoryDecimalDecoder.getValue()); + void testMandatoryPositiveTwoValuesInRow(Collection buffers) { + decode(mandatoryDecimalDecoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("94275500"), decodeResult.decimalValue); + + decode(mandatoryDecimalDecoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("94275500"), decodeResult.decimalValue); } @WithByteBuf("08 00 00 00 81 39 45 a3") void testNullableExponentOverflow1(Collection buffers) { - decode(nullableDecimalDecoder, buffers); - assertTrue(nullableDecimalDecoder.isReady()); - assertThrows(IOException.class, () -> nullableDecimalDecoder.getValue()); + decodeResult.isOverflow = false; + + decode(nullableDecimalDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); } @WithByteBuf("08 00 00 00 80 39 45 a3") void testMandatoryExponentOverflow1(Collection buffers) { - decode(mandatoryDecimalDecoder, buffers); - assertTrue(mandatoryDecimalDecoder.isReady()); - assertThrows(IOException.class, () -> mandatoryDecimalDecoder.getValue()); + decodeResult.isOverflow = false; + + decode(mandatoryDecimalDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); } @WithByteBuf("08 00 00 00 00 81 39 45 a3") void testNullableExponentOverflow2(Collection buffers) { - decode(nullableDecimalDecoder, buffers); - assertTrue(nullableDecimalDecoder.isReady()); - assertThrows(IOException.class, () -> nullableDecimalDecoder.getValue()); + decodeResult.isOverflow = false; + + decode(nullableDecimalDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); } @WithByteBuf("07 7f 00 7f 7f 7f ff 39 45 a3") void testMandatoryExponentOverflow2(Collection buffers) { - decode(mandatoryDecimalDecoder, buffers); - assertTrue(mandatoryDecimalDecoder.isReady()); - assertThrows(IOException.class, () -> mandatoryDecimalDecoder.getValue()); + decodeResult.isOverflow = false; + + decode(mandatoryDecimalDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); } @WithByteBuf("86 01 00 00 00 00 00 00 00 00 80") void testNullableMantissaOverflow1(Collection buffers) { - decode(nullableDecimalDecoder, buffers); - assertTrue(nullableDecimalDecoder.isReady()); - assertThrows(IOException.class, () -> nullableDecimalDecoder.getValue()); + decodeResult.isOverflow = false; + + decode(nullableDecimalDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); } @WithByteBuf("86 01 00 00 00 00 00 00 00 00 80") void testMandatoryMantissaOverflow1(Collection buffers) { - decode(mandatoryDecimalDecoder, buffers); - assertTrue(mandatoryDecimalDecoder.isReady()); - assertThrows(IOException.class, () -> mandatoryDecimalDecoder.getValue()); + decodeResult.isOverflow = false; + + decode(mandatoryDecimalDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); } @WithByteBuf("86 00 7f 00 7f 7f 7f 7f 7f 7f 7f ff") void testNullableMantissaOverflow2(Collection buffers) { - decode(nullableDecimalDecoder, buffers); - assertTrue(nullableDecimalDecoder.isReady()); - assertThrows(IOException.class, () -> nullableDecimalDecoder.getValue()); + decodeResult.isOverflow = true; + + decode(nullableDecimalDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); } @WithByteBuf("86 00 7f 00 7f 7f 7f 7f 7f 7f 7f ff") void testMandatoryMantissaOverflow2(Collection buffers) { - decode(mandatoryDecimalDecoder, buffers); - assertTrue(mandatoryDecimalDecoder.isReady()); - assertThrows(IOException.class, () -> mandatoryDecimalDecoder.getValue()); + decodeResult.isOverflow = false; + + decode(mandatoryDecimalDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); } @WithByteBuf("00 83 39 45 a3") - void testNullableExponentOverlong(Collection buffers) throws IOException { - decode(nullableDecimalDecoder, buffers); - assertTrue(nullableDecimalDecoder.isReady()); - assertTrue(nullableDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("94275500"), nullableDecimalDecoder.getValue()); + void testNullableExponentOverlong(Collection buffers) { + decodeResult.isOverlong = false; + + decode(nullableDecimalDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("94275500"), decodeResult.decimalValue); } @WithByteBuf("7f fe 46 3a dd") - void testNullableNegativeExponentOverlong(Collection buffers) throws IOException { - decode(nullableDecimalDecoder, buffers); - assertTrue(nullableDecimalDecoder.isReady()); - assertTrue(nullableDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("-9427.55"), nullableDecimalDecoder.getValue()); + void testNullableNegativeExponentOverlong(Collection buffers) { + decodeResult.isOverlong = false; + + decode(nullableDecimalDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("-9427.55"), decodeResult.decimalValue); } @WithByteBuf("00 82 39 45 a3") - void testMandatoryExponentOverlong(Collection buffers) throws IOException { - decode(mandatoryDecimalDecoder, buffers); - assertTrue(mandatoryDecimalDecoder.isReady()); - assertTrue(mandatoryDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("94275500"), mandatoryDecimalDecoder.getValue()); + void testMandatoryExponentOverlong(Collection buffers) { + decodeResult.isOverlong = false; + + decode(mandatoryDecimalDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("94275500"), decodeResult.decimalValue); } @WithByteBuf("7f fe 46 3a dd") - void testMandatoryNegativeExponentOverlong(Collection buffers) throws IOException { - decode(mandatoryDecimalDecoder, buffers); - assertTrue(mandatoryDecimalDecoder.isReady()); - assertTrue(mandatoryDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("-9427.55"), mandatoryDecimalDecoder.getValue()); + void testMandatoryNegativeExponentOverlong(Collection buffers) { + decodeResult.isOverlong = false; + + decode(mandatoryDecimalDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("-9427.55"), decodeResult.decimalValue); } @WithByteBuf("fe 7f 46 3a dd") - void testNullableNegativeMantissaOverlong(Collection buffers) throws IOException { - decode(nullableDecimalDecoder, buffers); - assertTrue(nullableDecimalDecoder.isReady()); - assertTrue(nullableDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("-9427.55"), nullableDecimalDecoder.getValue()); + void testNullableNegativeMantissaOverlong(Collection buffers) { + decodeResult.isOverlong = false; + + decode(nullableDecimalDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("-9427.55"), decodeResult.decimalValue); } @WithByteBuf("83 00 39 45 a3") - void testNullableMantissaOverlong(Collection buffers) throws IOException { - decode(nullableDecimalDecoder, buffers); - assertTrue(nullableDecimalDecoder.isReady()); - assertTrue(nullableDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("94275500"), nullableDecimalDecoder.getValue()); + void testNullableMantissaOverlong(Collection buffers) { + decodeResult.isOverlong = false; + + decode(nullableDecimalDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("94275500"), decodeResult.decimalValue); } @WithByteBuf("82 00 39 45 a3") - void testMandatoryMantissaOverlong(Collection buffers) throws IOException { - decode(mandatoryDecimalDecoder, buffers); - assertTrue(mandatoryDecimalDecoder.isReady()); - assertTrue(mandatoryDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("94275500"), mandatoryDecimalDecoder.getValue()); + void testMandatoryMantissaOverlong(Collection buffers) { + decodeResult.isOverlong = false; + + decode(mandatoryDecimalDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("94275500"), decodeResult.decimalValue); } @WithByteBuf("fe 7f 46 3a dd") - void testMandatoryNegativeMantissaOverlong(Collection buffers) throws IOException { - decode(mandatoryDecimalDecoder, buffers); - assertTrue(mandatoryDecimalDecoder.isReady()); - assertTrue(mandatoryDecimalDecoder.isOverlong()); - assertEquals(new BigDecimal("-9427.55"), mandatoryDecimalDecoder.getValue()); + void testMandatoryNegativeMantissaOverlong(Collection buffers) { + decodeResult.isOverlong = false; + + decode(mandatoryDecimalDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(new BigDecimal("-9427.55"), decodeResult.decimalValue); + } + + @BeforeEach + void resetRegisterFlags() { + decodeResult.isOverlong = true; + decodeResult.isNull = true; + decodeResult.isOverflow = true; + decodeResult.decimalValue = null; } } diff --git a/src/test/java/com/exactpro/epfast/decoder/integer/TestInt32.java b/src/test/java/com/exactpro/epfast/decoder/integer/TestInt32.java index 14d40d20..752c9e94 100644 --- a/src/test/java/com/exactpro/epfast/decoder/integer/TestInt32.java +++ b/src/test/java/com/exactpro/epfast/decoder/integer/TestInt32.java @@ -16,15 +16,16 @@ package com.exactpro.epfast.decoder.integer; +import com.exactpro.epfast.decoder.message.UnionRegister; import io.netty.buffer.ByteBuf; import com.exactpro.junit5.WithByteBuf; +import org.junit.jupiter.api.BeforeEach; -import java.io.IOException; import java.util.Collection; import static org.junit.jupiter.api.Assertions.*; -import static com.exactpro.epfast.DecoderUtils.*; +import static com.exactpro.epfast.decoder.DecoderUtils.*; class TestInt32 { @@ -32,287 +33,405 @@ class TestInt32 { private DecodeMandatoryInt32 mandatoryInt32Decoder = new DecodeMandatoryInt32(); + private UnionRegister decodeResult = new UnionRegister(); + @WithByteBuf("80") - void testNull(Collection buffers) throws IOException { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertNull(nullableInt32Decoder.getValue()); + void testNull(Collection buffers) { + decodeResult.isNull = false; + + decode(nullableInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("81") - void optionalZero(Collection buffers) throws IOException { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertEquals(0, nullableInt32Decoder.getValue()); + void optionalZero(Collection buffers) { + decode(nullableInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(0, decodeResult.int32Value); + } @WithByteBuf("80") - void mandatoryZero(Collection buffers) throws IOException { - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertEquals(0, mandatoryInt32Decoder.getValue()); + void mandatoryZero(Collection buffers) { + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(0, decodeResult.int32Value); } @WithByteBuf("08 00 00 00 80") - void testMaxNullable(Collection buffers) throws IOException { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertEquals(Integer.MAX_VALUE, nullableInt32Decoder.getValue()); + void testMaxNullable(Collection buffers) { + decode(nullableInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(Integer.MAX_VALUE, decodeResult.int32Value); } @WithByteBuf("07 7f 7f 7f ff") - void testMaxMandatory(Collection buffers) throws IOException { - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertEquals(Integer.MAX_VALUE, mandatoryInt32Decoder.getValue()); + void testMaxMandatory(Collection buffers) { + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(Integer.MAX_VALUE, decodeResult.int32Value); } @WithByteBuf("78 00 00 00 80") - void testMinNullable(Collection buffers) throws IOException { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertEquals(Integer.MIN_VALUE, nullableInt32Decoder.getValue()); + void testMinNullable(Collection buffers) { + decode(nullableInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(Integer.MIN_VALUE, decodeResult.int32Value); } @WithByteBuf("78 00 00 00 80") - void testMinMandatory(Collection buffers) throws IOException { - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertEquals(Integer.MIN_VALUE, mandatoryInt32Decoder.getValue()); + void testMinMandatory(Collection buffers) { + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(Integer.MIN_VALUE, decodeResult.int32Value); } @WithByteBuf("08 00 00 00 81") void testMaxOverflowNullable1(Collection buffers) { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertThrows(IOException.class, () -> nullableInt32Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(nullableInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertTrue(decodeResult.isOverflow); } @WithByteBuf("08 00 00 00 00 00 00 80") void testMaxOverflowNullable2(Collection buffers) { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertThrows(IOException.class, () -> nullableInt32Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(nullableInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("08 00 00 00 80") void testMaxOverflowMandatory1(Collection buffers) { - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertThrows(IOException.class, () -> mandatoryInt32Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("07 7f 00 7f 7f 7f ff") void testMaxOverflowMandatory2(Collection buffers) { - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertThrows(IOException.class, () -> mandatoryInt32Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("77 7f 7f 7f ff") void testMinOverflowNullable1(Collection buffers) { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertThrows(IOException.class, () -> nullableInt32Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(nullableInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("78 00 00 00 00 80") void testMinOverflowNullable2(Collection buffers) { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertThrows(IOException.class, () -> nullableInt32Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(nullableInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("77 7f 7f 7f ff") void testMinOverflowMandatory1(Collection buffers) { - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertThrows(IOException.class, () -> mandatoryInt32Decoder.getValue()); + decodeResult.isOverflow = false; + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("78 00 00 00 00 80") void testMinOverflowMandatory2(Collection buffers) { - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertThrows(IOException.class, () -> mandatoryInt32Decoder.getValue()); + decodeResult.isOverflow = false; + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("39 45 a4") - void optionalPositive(Collection buffers) throws IOException { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertEquals(942755, nullableInt32Decoder.getValue()); + void optionalPositive(Collection buffers) { + decode(nullableInt32Decoder, buffers, decodeResult); + assertEquals(942755, decodeResult.int32Value); } @WithByteBuf("39 45 a3") - void mandatoryPositive(Collection buffers) throws IOException { - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertEquals(942755, mandatoryInt32Decoder.getValue()); + void mandatoryPositive(Collection buffers) { + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals(942755, decodeResult.int32Value); } @WithByteBuf("46 3a dd") - void optionalNegative(Collection buffers) throws IOException { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertEquals(-942755, nullableInt32Decoder.getValue()); + void optionalNegative(Collection buffers) { + decode(nullableInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals(-942755, decodeResult.int32Value); } @WithByteBuf("7c 1b 1b 9d") - void mandatoryNegative(Collection buffers) throws IOException { - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertEquals(-7942755, mandatoryInt32Decoder.getValue()); + void mandatoryNegative(Collection buffers) { + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals(-7942755, decodeResult.int32Value); } @WithByteBuf("ff") - void optionalMinusOne(Collection buffers) throws IOException { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertEquals(-1, nullableInt32Decoder.getValue()); + void optionalMinusOne(Collection buffers) { + decode(nullableInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals(-1, decodeResult.int32Value); } @WithByteBuf("ff") - void mandatoryMinusOne(Collection buffers) throws IOException { - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertEquals(-1, mandatoryInt32Decoder.getValue()); + void mandatoryMinusOne(Collection buffers) { + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals(-1, decodeResult.int32Value); } - @WithByteBuf("00 00 40 82") - void optionalSignExtensionPositive(Collection buffers) throws IOException { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertEquals(8193, nullableInt32Decoder.getValue()); + @WithByteBuf("00 40 82") + void optionalSignExtensionPositive(Collection buffers) { + decode(nullableInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals(8193, decodeResult.int32Value); } - @WithByteBuf("00 00 40 81") - void mandatorySignExtensionPositive(Collection buffers) throws IOException { - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertEquals(8193, mandatoryInt32Decoder.getValue()); - } + @WithByteBuf("00 00 40 82") + void optionalSignExtensionPositiveOverlong(Collection buffers) { + decodeResult.isOverlong = false; - @WithByteBuf("7f 3f ff") - void optionalSignExtensionNegative(Collection buffers) throws IOException { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertEquals(-8193, nullableInt32Decoder.getValue()); + decode(nullableInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertTrue(decodeResult.isOverlong); + assertEquals(8193, decodeResult.int32Value); } - @WithByteBuf("7f 3f ff") - void mandatorySignExtensionNegative(Collection buffers) throws IOException { - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertEquals(-8193, mandatoryInt32Decoder.getValue()); + @WithByteBuf("00 40 81") + void mandatorySignExtensionPositive(Collection buffers) { + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals(8193, decodeResult.int32Value); } - @WithByteBuf("7f 3f ff 7f 3f ff") - void mandatoryNegativeTwoValuesInRow(Collection buffers) throws IOException { - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertEquals(-8193, mandatoryInt32Decoder.getValue()); + @WithByteBuf("00 00 40 81") + void mandatorySignExtensionPositiveOverlong(Collection buffers) { + decodeResult.isOverlong = false; - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertEquals(-8193, mandatoryInt32Decoder.getValue()); + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertTrue(decodeResult.isOverlong); + assertEquals(8193, decodeResult.int32Value); } - @WithByteBuf("00 00 40 81 00 00 40 81") - void mandatoryPositiveTwoValuesInRow(Collection buffers) throws IOException { - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertEquals(8193, mandatoryInt32Decoder.getValue()); + @WithByteBuf("7f 3f ff") + void optionalSignExtensionNegative(Collection buffers) { + decode(nullableInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals(-8193, decodeResult.int32Value); + } - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertEquals(8193, mandatoryInt32Decoder.getValue()); + @WithByteBuf("7f 3f ff") + void mandatorySignExtensionNegative(Collection buffers) { + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals(-8193, decodeResult.int32Value); } @WithByteBuf("7f 3f ff 7f 3f ff") - void optionalNegativeTwoValuesInRow(Collection buffers) throws IOException { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertEquals(-8193, nullableInt32Decoder.getValue()); - - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertEquals(-8193, nullableInt32Decoder.getValue()); + void mandatoryNegativeTwoValuesInRow(Collection buffers) { + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals(-8193, decodeResult.int32Value); + + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals(-8193, decodeResult.int32Value); + } + + @WithByteBuf("00 40 81 00 40 81") + void mandatoryPositiveTwoValuesInRow(Collection buffers) { + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals(8193, decodeResult.int32Value); + + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals(8193, decodeResult.int32Value); } - @WithByteBuf("00 00 40 82 00 00 40 82") - void optionalPositiveTwoValuesInRow(Collection buffers) throws IOException { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertEquals(8193, nullableInt32Decoder.getValue()); - - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertEquals(8193, nullableInt32Decoder.getValue()); + @WithByteBuf("7f 3f ff 7f 3f ff") + void optionalNegativeTwoValuesInRow(Collection buffers) { + decode(nullableInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals(-8193, decodeResult.int32Value); + + decode(nullableInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals(-8193, decodeResult.int32Value); + } + + @WithByteBuf("00 40 82 00 40 82") + void optionalPositiveTwoValuesInRow(Collection buffers) { + decode(nullableInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals(8193, decodeResult.int32Value); + + decode(nullableInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); + assertEquals(8193, decodeResult.int32Value); } @WithByteBuf("00 39 45 a4") - void mandatoryOverlong(Collection buffers) throws IOException { - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertTrue(mandatoryInt32Decoder.isOverlong()); - assertEquals(942756, mandatoryInt32Decoder.getValue()); + void mandatoryOverlong(Collection buffers) { + decodeResult.isOverlong = false; + + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(942756, decodeResult.int32Value); } @WithByteBuf("00 40 81") - void mandatoryNotOverlong(Collection buffers) throws IOException { - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertFalse(mandatoryInt32Decoder.isOverlong()); - assertEquals(8193, mandatoryInt32Decoder.getValue()); + void mandatoryNotOverlong(Collection buffers) { + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(8193, decodeResult.int32Value); } @WithByteBuf("7f 7c 1b 1b 9d") - void mandatoryOverlongNegative(Collection buffers) throws IOException { - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertTrue(mandatoryInt32Decoder.isOverlong()); - assertEquals(-7942755, mandatoryInt32Decoder.getValue()); + void mandatoryOverlongNegative(Collection buffers) { + decodeResult.isOverlong = false; + + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(-7942755, decodeResult.int32Value); } @WithByteBuf("7f 3f ff") - void mandatoryNotOverlongNegative(Collection buffers) throws IOException { - decode(mandatoryInt32Decoder, buffers); - assertTrue(mandatoryInt32Decoder.isReady()); - assertFalse(mandatoryInt32Decoder.isOverlong()); - assertEquals(-8193, mandatoryInt32Decoder.getValue()); + void mandatoryNotOverlongNegative(Collection buffers) { + decode(mandatoryInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(-8193, decodeResult.int32Value); } @WithByteBuf("00 39 45 a4") - void nullableOverlong(Collection buffers) throws IOException { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertTrue(nullableInt32Decoder.isOverlong()); - assertEquals(942755, nullableInt32Decoder.getValue()); + void nullableOverlong(Collection buffers) { + decodeResult.isOverlong = false; + + decode(nullableInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(942755, decodeResult.int32Value); } @WithByteBuf("00 40 81") - void nullableNotOverlong(Collection buffers) throws IOException { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertFalse(nullableInt32Decoder.isOverlong()); - assertEquals(8192, nullableInt32Decoder.getValue()); + void nullableNotOverlong(Collection buffers) { + decode(nullableInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(8192, decodeResult.int32Value); } @WithByteBuf("7f 7c 1b 1b 9d") - void nullableOverlongNegative(Collection buffers) throws IOException { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertTrue(nullableInt32Decoder.isOverlong()); - assertEquals(-7942755, nullableInt32Decoder.getValue()); + void nullableOverlongNegative(Collection buffers) { + decodeResult.isOverlong = false; + decode(nullableInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(-7942755, decodeResult.int32Value); } @WithByteBuf("7f 3f ff") - void nullableNotOverlongNegative(Collection buffers) throws IOException { - decode(nullableInt32Decoder, buffers); - assertTrue(nullableInt32Decoder.isReady()); - assertFalse(nullableInt32Decoder.isOverlong()); - assertEquals(-8193, nullableInt32Decoder.getValue()); + void nullableNotOverlongNegative(Collection buffers) { + decode(nullableInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertEquals(-8193, decodeResult.int32Value); + } + + @BeforeEach + void resetRegisterFlags() { + decodeResult.isOverlong = true; + decodeResult.isNull = true; + decodeResult.isOverflow = true; + decodeResult.int32Value = -999; } } diff --git a/src/test/java/com/exactpro/epfast/decoder/integer/TestInt64.java b/src/test/java/com/exactpro/epfast/decoder/integer/TestInt64.java index 40563632..eb75fb8c 100644 --- a/src/test/java/com/exactpro/epfast/decoder/integer/TestInt64.java +++ b/src/test/java/com/exactpro/epfast/decoder/integer/TestInt64.java @@ -16,14 +16,15 @@ package com.exactpro.epfast.decoder.integer; +import com.exactpro.epfast.decoder.message.UnionRegister; import com.exactpro.junit5.WithByteBuf; import io.netty.buffer.ByteBuf; +import org.junit.jupiter.api.BeforeEach; -import java.io.IOException; import java.util.Collection; import static org.junit.jupiter.api.Assertions.*; -import static com.exactpro.epfast.DecoderUtils.*; +import static com.exactpro.epfast.decoder.DecoderUtils.*; class TestInt64 { @@ -31,286 +32,408 @@ class TestInt64 { private DecodeMandatoryInt64 mandatoryInt64Decoder = new DecodeMandatoryInt64(); + private UnionRegister decodeResult = new UnionRegister(); + @WithByteBuf("80") - void testNull(Collection buffers) throws IOException { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertNull(nullableInt64Decoder.getValue()); + void testNull(Collection buffers) { + decodeResult.isNull = false; + + decode(nullableInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("81") - void optionalZero(Collection buffers) throws IOException { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertEquals(0, nullableInt64Decoder.getValue()); + void optionalZero(Collection buffers) { + decode(nullableInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(0, decodeResult.int64Value); } @WithByteBuf("80") - void mandatoryZero(Collection buffers) throws IOException { - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertEquals(0, mandatoryInt64Decoder.getValue()); + void mandatoryZero(Collection buffers) { + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(0, decodeResult.int64Value); } @WithByteBuf("01 00 00 00 00 00 00 00 00 80") - void testMaxNullable(Collection buffers) throws IOException { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertEquals(Long.MAX_VALUE, nullableInt64Decoder.getValue()); + void testMaxNullable(Collection buffers) { + decode(nullableInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(Long.MAX_VALUE, decodeResult.int64Value); } @WithByteBuf("00 7f 7f 7f 7f 7f 7f 7f 7f ff") - void testMaxMandatory(Collection buffers) throws IOException { - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertEquals(Long.MAX_VALUE, mandatoryInt64Decoder.getValue()); + void testMaxMandatory(Collection buffers) { + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(Long.MAX_VALUE, decodeResult.int64Value); } @WithByteBuf("7f 00 00 00 00 00 00 00 00 80") - void testMinNullable(Collection buffers) throws IOException { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertEquals(Long.MIN_VALUE, nullableInt64Decoder.getValue()); + void testMinNullable(Collection buffers) { + decode(nullableInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(Long.MIN_VALUE, decodeResult.int64Value); } @WithByteBuf("7f 00 00 00 00 00 00 00 00 80") - void testMinMandatory(Collection buffers) throws IOException { - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertEquals(Long.MIN_VALUE, mandatoryInt64Decoder.getValue()); + void testMinMandatory(Collection buffers) { + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(Long.MIN_VALUE, decodeResult.int64Value); } @WithByteBuf("01 00 00 00 00 00 00 00 00 81") void testMaxOverflowNullable1(Collection buffers) { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertThrows(IOException.class, () -> nullableInt64Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(nullableInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("01 00 00 00 00 00 00 00 00 00 80") void testMaxOverflowNullable2(Collection buffers) { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertThrows(IOException.class, () -> nullableInt64Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(nullableInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("01 00 00 00 00 00 00 00 00 80") void testMaxOverflowMandatory1(Collection buffers) { - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertThrows(IOException.class, () -> mandatoryInt64Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("00 7f 00 7f 7f 7f 7f 7f 7f 7f ff") void testMaxOverflowMandatory2(Collection buffers) { - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertThrows(IOException.class, () -> mandatoryInt64Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("77 7f 7f 7f 7f 7f 7f 7f 7f ff") void testMinOverflowNullable1(Collection buffers) { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertThrows(IOException.class, () -> nullableInt64Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(nullableInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("7f 00 00 00 00 00 00 00 00 00 80") void testMinOverflowNullable2(Collection buffers) { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertThrows(IOException.class, () -> nullableInt64Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(nullableInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("77 7f 7f 7f 7f 7f 7f 7f 7f ff") void testMinOverflowMandatory1(Collection buffers) { - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertThrows(IOException.class, () -> mandatoryInt64Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("7f 00 00 00 00 00 00 00 00 00 80") void testMinOverflowMandatory2(Collection buffers) { - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertThrows(IOException.class, () -> mandatoryInt64Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("39 45 a4") - void optionalPositive(Collection buffers) throws IOException { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertEquals(942755, nullableInt64Decoder.getValue()); + void optionalPositive(Collection buffers) { + decode(nullableInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(942755, decodeResult.int64Value); } @WithByteBuf("39 45 a3") - void mandatoryPositive(Collection buffers) throws IOException { - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertEquals(942755, mandatoryInt64Decoder.getValue()); + void mandatoryPositive(Collection buffers) { + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(942755, decodeResult.int64Value); } @WithByteBuf("46 3a dd") - void optionalNegative(Collection buffers) throws IOException { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertEquals(-942755, nullableInt64Decoder.getValue()); + void optionalNegative(Collection buffers) { + decode(nullableInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(-942755, decodeResult.int64Value); } @WithByteBuf("7c 1b 1b 9d") - void mandatoryNegative(Collection buffers) throws IOException { - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertEquals(-7942755, mandatoryInt64Decoder.getValue()); + void mandatoryNegative(Collection buffers) { + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(-7942755, decodeResult.int64Value); } @WithByteBuf("ff") - void optionalMinusOne(Collection buffers) throws IOException { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertEquals(-1, nullableInt64Decoder.getValue()); + void optionalMinusOne(Collection buffers) { + decode(nullableInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(-1, decodeResult.int64Value); } @WithByteBuf("ff") - void mandatoryMinusOne(Collection buffers) throws IOException { - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertEquals(-1, mandatoryInt64Decoder.getValue()); + void mandatoryMinusOne(Collection buffers) { + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(-1, decodeResult.int64Value); } - @WithByteBuf("00 00 40 82") - void optionalSignExtensionPositive(Collection buffers) throws IOException { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertEquals(8193, nullableInt64Decoder.getValue()); + @WithByteBuf("00 40 82") + void optionalSignExtensionPositive(Collection buffers) { + decode(nullableInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(8193, decodeResult.int64Value); } - @WithByteBuf("00 00 40 81") - void mandatorySignExtensionPositive(Collection buffers) throws IOException { - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertEquals(8193, mandatoryInt64Decoder.getValue()); - } + @WithByteBuf("00 00 40 82") + void optionalSignExtensionPositiveOverlong(Collection buffers) { + decodeResult.isOverlong = false; - @WithByteBuf("7f 3f ff") - void optionalSignExtensionNegative(Collection buffers) throws IOException { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertEquals(-8193, nullableInt64Decoder.getValue()); + decode(nullableInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertTrue(decodeResult.isOverlong); + assertEquals(8193, decodeResult.int64Value); } - @WithByteBuf("7f 3f ff") - void mandatorySignExtensionNegative(Collection buffers) throws IOException { - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertEquals(-8193, mandatoryInt64Decoder.getValue()); + @WithByteBuf("00 40 81") + void mandatorySignExtensionPositive(Collection buffers) { + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(8193, decodeResult.int64Value); } - @WithByteBuf("7f 3f ff 7f 3f ff") - void mandatoryNegativeTwoValuesInRow(Collection buffers) throws IOException { - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertEquals(-8193, mandatoryInt64Decoder.getValue()); + @WithByteBuf("00 00 40 81") + void mandatorySignExtensionPositiveOverlong(Collection buffers) { + decodeResult.isOverlong = false; - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertEquals(-8193, mandatoryInt64Decoder.getValue()); + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertTrue(decodeResult.isOverlong); + assertEquals(8193, decodeResult.int64Value); } - @WithByteBuf("00 00 40 81 00 00 40 81") - void mandatoryPositiveTwoValuesInRow(Collection buffers) throws IOException { - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertEquals(8193, mandatoryInt64Decoder.getValue()); + @WithByteBuf("7f 3f ff") + void optionalSignExtensionNegative(Collection buffers) { + decode(nullableInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(-8193, decodeResult.int64Value); + } - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertEquals(8193, mandatoryInt64Decoder.getValue()); + @WithByteBuf("7f 3f ff") + void mandatorySignExtensionNegative(Collection buffers) { + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(-8193, decodeResult.int64Value); } @WithByteBuf("7f 3f ff 7f 3f ff") - void optionalNegativeTwoValuesInRow(Collection buffers) throws IOException { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertEquals(-8193, nullableInt64Decoder.getValue()); - - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertEquals(-8193, nullableInt64Decoder.getValue()); + void mandatoryNegativeTwoValuesInRow(Collection buffers) { + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(-8193, decodeResult.int64Value); + + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(-8193, decodeResult.int64Value); + } + + @WithByteBuf("00 40 81 00 40 81") + void mandatoryPositiveTwoValuesInRow(Collection buffers) { + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(8193, decodeResult.int64Value); + + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(8193, decodeResult.int64Value); } - @WithByteBuf("00 00 40 82 00 00 40 82") - void optionalPositiveTwoValuesInRow(Collection buffers) throws IOException { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertEquals(8193, nullableInt64Decoder.getValue()); - - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertEquals(8193, nullableInt64Decoder.getValue()); + @WithByteBuf("7f 3f ff 7f 3f ff") + void optionalNegativeTwoValuesInRow(Collection buffers) { + decode(nullableInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(-8193, decodeResult.int64Value); + + decode(nullableInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(-8193, decodeResult.int64Value); + } + + @WithByteBuf("00 40 82 00 40 82") + void optionalPositiveTwoValuesInRow(Collection buffers) { + decode(nullableInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(8193, decodeResult.int64Value); + + decode(nullableInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(8193, decodeResult.int64Value); } @WithByteBuf("00 39 45 a4") - void mandatoryOverlong(Collection buffers) throws IOException { - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertTrue(mandatoryInt64Decoder.isOverlong()); - assertEquals(942756, mandatoryInt64Decoder.getValue()); + void mandatoryOverlong(Collection buffers) { + decodeResult.isOverlong = false; + + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals(942756, decodeResult.int64Value); } @WithByteBuf("00 40 81") - void mandatoryNotOverlong(Collection buffers) throws IOException { - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertFalse(mandatoryInt64Decoder.isOverlong()); - assertEquals(8193, mandatoryInt64Decoder.getValue()); + void mandatoryNotOverlong(Collection buffers) { + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals(8193, decodeResult.int64Value); } @WithByteBuf("7f 7c 1b 1b 9d") - void mandatoryOverlongNegative(Collection buffers) throws IOException { - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertTrue(mandatoryInt64Decoder.isOverlong()); - assertEquals(-7942755, mandatoryInt64Decoder.getValue()); + void mandatoryOverlongNegative(Collection buffers) { + decodeResult.isOverlong = false; + + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals(-7942755, decodeResult.int64Value); } @WithByteBuf("7f 3f ff") - void mandatoryNotOverlongNegative(Collection buffers) throws IOException { - decode(mandatoryInt64Decoder, buffers); - assertTrue(mandatoryInt64Decoder.isReady()); - assertFalse(mandatoryInt64Decoder.isOverlong()); - assertEquals(-8193, mandatoryInt64Decoder.getValue()); + void mandatoryNotOverlongNegative(Collection buffers) { + decode(mandatoryInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals(-8193, decodeResult.int64Value); } @WithByteBuf("00 39 45 a4") - void nullableOverlong(Collection buffers) throws IOException { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertTrue(nullableInt64Decoder.isOverlong()); - assertEquals(942755, nullableInt64Decoder.getValue()); + void nullableOverlong(Collection buffers) { + decodeResult.isOverlong = false; + decode(nullableInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals(942755, decodeResult.int64Value); } @WithByteBuf("00 40 81") - void nullableNotOverlong(Collection buffers) throws IOException { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertFalse(nullableInt64Decoder.isOverlong()); - assertEquals(8192, nullableInt64Decoder.getValue()); + void nullableNotOverlong(Collection buffers) { + decode(nullableInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals(8192, decodeResult.int64Value); } @WithByteBuf("7f 7c 1b 1b 9d") - void nullableOverlongNegative(Collection buffers) throws IOException { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertTrue(nullableInt64Decoder.isOverlong()); - assertEquals(-7942755, nullableInt64Decoder.getValue()); + void nullableOverlongNegative(Collection buffers) { + decodeResult.isOverlong = false; + + decode(nullableInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals(-7942755, decodeResult.int64Value); } @WithByteBuf("7f 3f ff") - void nullableNotOverlongNegative(Collection buffers) throws IOException { - decode(nullableInt64Decoder, buffers); - assertTrue(nullableInt64Decoder.isReady()); - assertFalse(nullableInt64Decoder.isOverlong()); - assertEquals(-8193, nullableInt64Decoder.getValue()); + void nullableNotOverlongNegative(Collection buffers) { + decode(nullableInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals(-8193, decodeResult.int64Value); + } + + @BeforeEach + void resetRegisterFlags() { + decodeResult.isOverlong = true; + decodeResult.isNull = true; + decodeResult.isOverflow = true; + decodeResult.int64Value = -999; } } diff --git a/src/test/java/com/exactpro/epfast/decoder/integer/TestUInt32.java b/src/test/java/com/exactpro/epfast/decoder/integer/TestUInt32.java index 1c11f83f..88545dea 100644 --- a/src/test/java/com/exactpro/epfast/decoder/integer/TestUInt32.java +++ b/src/test/java/com/exactpro/epfast/decoder/integer/TestUInt32.java @@ -16,14 +16,15 @@ package com.exactpro.epfast.decoder.integer; +import com.exactpro.epfast.decoder.message.UnionRegister; import com.exactpro.junit5.WithByteBuf; import io.netty.buffer.ByteBuf; +import org.junit.jupiter.api.BeforeEach; -import java.io.IOException; import java.util.Collection; import static org.junit.jupiter.api.Assertions.*; -import static com.exactpro.epfast.DecoderUtils.*; +import static com.exactpro.epfast.decoder.DecoderUtils.*; class TestUInt32 { @@ -31,157 +32,216 @@ class TestUInt32 { private DecodeMandatoryUInt32 mandatoryUInt32Decoder = new DecodeMandatoryUInt32(); + private UnionRegister decodeResult = new UnionRegister(); + @WithByteBuf("80") - void testNull(Collection buffers) throws IOException { - decode(nullableUInt32Decoder, buffers); - assertTrue(nullableUInt32Decoder.isReady()); - assertNull(nullableUInt32Decoder.getValue()); + void testNull(Collection buffers) { + decodeResult.isNull = false; + + decode(nullableUInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("81") - void optionalZero(Collection buffers) throws IOException { - decode(nullableUInt32Decoder, buffers); - assertTrue(nullableUInt32Decoder.isReady()); - assertEquals(0, nullableUInt32Decoder.getValue()); + void optionalZero(Collection buffers) { + decode(nullableUInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(0, decodeResult.uInt32Value); } @WithByteBuf("80") - void mandatoryZero(Collection buffers) throws IOException { - decode(mandatoryUInt32Decoder, buffers); - assertTrue(mandatoryUInt32Decoder.isReady()); - assertEquals(0, mandatoryUInt32Decoder.getValue()); + void mandatoryZero(Collection buffers) { + decode(mandatoryUInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(0, decodeResult.uInt32Value); } @WithByteBuf("10 00 00 00 80") - void testMaxNullable(Collection buffers) throws IOException { - decode(nullableUInt32Decoder, buffers); - assertTrue(nullableUInt32Decoder.isReady()); - assertEquals(4294967295L, nullableUInt32Decoder.getValue()); + void testMaxNullable(Collection buffers) { + decode(nullableUInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(4294967295L, decodeResult.uInt32Value); } @WithByteBuf("0f 7f 7f 7f ff") - void testMaxMandatory(Collection buffers) throws IOException { - decode(mandatoryUInt32Decoder, buffers); - assertTrue(mandatoryUInt32Decoder.isReady()); - assertEquals(4294967295L, mandatoryUInt32Decoder.getValue()); + void testMaxMandatory(Collection buffers) { + decode(mandatoryUInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(4294967295L, decodeResult.uInt32Value); } @WithByteBuf("10 00 00 00 81") void testMaxOverflowNullable1(Collection buffers) { - decode(nullableUInt32Decoder, buffers); - assertTrue(nullableUInt32Decoder.isReady()); - assertThrows(IOException.class, () -> nullableUInt32Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(nullableUInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("10 00 00 00 00 00 80") void testMaxOverflowNullable2(Collection buffers) { - decode(nullableUInt32Decoder, buffers); - assertTrue(nullableUInt32Decoder.isReady()); - assertThrows(IOException.class, () -> nullableUInt32Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(nullableUInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("10 00 00 00 80") void testMaxOverflowMandatory1(Collection buffers) { - decode(mandatoryUInt32Decoder, buffers); - assertTrue(mandatoryUInt32Decoder.isReady()); - assertThrows(IOException.class, () -> mandatoryUInt32Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(mandatoryUInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("0f 7f 7f 7f 7f 00 ff") void testMaxOverflowMandatory2(Collection buffers) { - decode(mandatoryUInt32Decoder, buffers); - assertTrue(mandatoryUInt32Decoder.isReady()); - assertThrows(IOException.class, () -> mandatoryUInt32Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(mandatoryUInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("39 45 a4") - void optionalSimpleNumber(Collection buffers) throws IOException { - decode(nullableUInt32Decoder, buffers); - assertTrue(nullableUInt32Decoder.isReady()); - assertEquals(942755, nullableUInt32Decoder.getValue()); + void optionalSimpleNumber(Collection buffers) { + decode(nullableUInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(942755, decodeResult.uInt32Value); } @WithByteBuf("0f 7f 7f 7f ff") - void optionalSimpleNumber2(Collection buffers) throws IOException { - decode(nullableUInt32Decoder, buffers); - assertTrue(nullableUInt32Decoder.isReady()); - assertEquals(4294967294L, nullableUInt32Decoder.getValue()); + void optionalSimpleNumber2(Collection buffers) { + decode(nullableUInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(4294967294L, decodeResult.uInt32Value); } @WithByteBuf("39 45 a3") - void mandatorySimpleNumber(Collection buffers) throws IOException { - decode(mandatoryUInt32Decoder, buffers); - assertTrue(mandatoryUInt32Decoder.isReady()); - assertEquals(942755, mandatoryUInt32Decoder.getValue()); + void mandatorySimpleNumber(Collection buffers) { + decode(mandatoryUInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(942755, decodeResult.uInt32Value); } @WithByteBuf("39 45 a4") - void optionalSimpleNumberGetValueTwice(Collection buffers) throws IOException { - decode(nullableUInt32Decoder, buffers); - assertTrue(nullableUInt32Decoder.isReady()); - assertEquals(942755, nullableUInt32Decoder.getValue()); - assertEquals(942755, nullableUInt32Decoder.getValue()); + void optionalSimpleNumberGetValueTwice(Collection buffers) { + decode(nullableUInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(942755, decodeResult.uInt32Value); + assertEquals(942755, decodeResult.uInt32Value); } @WithByteBuf("39 45 a3") - void mandatorySimpleNumberGetValueTwice(Collection buffers) throws IOException { - decode(mandatoryUInt32Decoder, buffers); - assertTrue(mandatoryUInt32Decoder.isReady()); - assertEquals(942755, mandatoryUInt32Decoder.getValue()); - assertEquals(942755, mandatoryUInt32Decoder.getValue()); + void mandatorySimpleNumberGetValueTwice(Collection buffers) { + decode(mandatoryUInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(942755, decodeResult.uInt32Value); + assertEquals(942755, decodeResult.uInt32Value); } @WithByteBuf("39 45 a4 0f 7f 7f 7f ff") - void optionalSimpleNumbersTwoValuesInRow(Collection buffers) throws IOException { - decode(nullableUInt32Decoder, buffers); - assertTrue(nullableUInt32Decoder.isReady()); - assertEquals(942755, nullableUInt32Decoder.getValue()); + void optionalSimpleNumbersTwoValuesInRow(Collection buffers) { + decode(nullableUInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(942755, decodeResult.uInt32Value); - decode(nullableUInt32Decoder, buffers); - assertTrue(nullableUInt32Decoder.isReady()); - assertEquals(4294967294L, nullableUInt32Decoder.getValue()); + decode(nullableUInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(4294967294L, decodeResult.uInt32Value); } @WithByteBuf("39 45 a3 39 45 a3") - void mandatorySimpleNumbersTwoValuesInRow(Collection buffers) throws IOException { - decode(mandatoryUInt32Decoder, buffers); - assertTrue(mandatoryUInt32Decoder.isReady()); - assertEquals(942755, mandatoryUInt32Decoder.getValue()); + void mandatorySimpleNumbersTwoValuesInRow(Collection buffers) { + decode(mandatoryUInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(942755, decodeResult.uInt32Value); - decode(mandatoryUInt32Decoder, buffers); - assertTrue(mandatoryUInt32Decoder.isReady()); - assertEquals(942755, mandatoryUInt32Decoder.getValue()); + decode(mandatoryUInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(942755, decodeResult.uInt32Value); } @WithByteBuf("00 39 45 a4") - void mandatoryOverlong(Collection buffers) throws IOException { - decode(mandatoryUInt32Decoder, buffers); - assertTrue(mandatoryUInt32Decoder.isReady()); - assertTrue(mandatoryUInt32Decoder.isOverlong()); - assertEquals(942756, mandatoryUInt32Decoder.getValue()); + void mandatoryOverlong(Collection buffers) { + decodeResult.isOverlong = false; + + decode(mandatoryUInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals(942756, decodeResult.uInt32Value); } @WithByteBuf("00 40 81") - void mandatoryNotOverlong(Collection buffers) throws IOException { - decode(mandatoryUInt32Decoder, buffers); - assertTrue(mandatoryUInt32Decoder.isReady()); - assertFalse(mandatoryUInt32Decoder.isOverlong()); - assertEquals(8193, mandatoryUInt32Decoder.getValue()); + void mandatoryNotOverlong(Collection buffers) { + decode(mandatoryUInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals(8193, decodeResult.uInt32Value); } @WithByteBuf("00 39 45 a4") - void nullableOverlong(Collection buffers) throws IOException { - decode(nullableUInt32Decoder, buffers); - assertTrue(nullableUInt32Decoder.isReady()); - assertTrue(nullableUInt32Decoder.isOverlong()); - assertEquals(942755, nullableUInt32Decoder.getValue()); + void nullableOverlong(Collection buffers) { + decodeResult.isOverlong = false; + + decode(nullableUInt32Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals(942755, decodeResult.uInt32Value); } @WithByteBuf("00 40 81") - void nullableNotOverlong(Collection buffers) throws IOException { - decode(nullableUInt32Decoder, buffers); - assertTrue(nullableUInt32Decoder.isReady()); - assertFalse(nullableUInt32Decoder.isOverlong()); - assertEquals(8192, nullableUInt32Decoder.getValue()); + void nullableNotOverlong(Collection buffers) { + decode(nullableUInt32Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals(8192, decodeResult.uInt32Value); + } + + @BeforeEach + void resetRegisterFlags() { + decodeResult.isOverlong = true; + decodeResult.isNull = true; + decodeResult.isOverflow = true; + decodeResult.uInt32Value = -999; } } diff --git a/src/test/java/com/exactpro/epfast/decoder/integer/TestUInt64.java b/src/test/java/com/exactpro/epfast/decoder/integer/TestUInt64.java index 04f61ae2..b8bb01e4 100644 --- a/src/test/java/com/exactpro/epfast/decoder/integer/TestUInt64.java +++ b/src/test/java/com/exactpro/epfast/decoder/integer/TestUInt64.java @@ -16,15 +16,16 @@ package com.exactpro.epfast.decoder.integer; +import com.exactpro.epfast.decoder.message.UnionRegister; import com.exactpro.junit5.WithByteBuf; import io.netty.buffer.ByteBuf; +import org.junit.jupiter.api.BeforeEach; -import java.io.IOException; import java.math.BigInteger; import java.util.Collection; import static org.junit.jupiter.api.Assertions.*; -import static com.exactpro.epfast.DecoderUtils.*; +import static com.exactpro.epfast.decoder.DecoderUtils.*; class TestUInt64 { @@ -32,164 +33,225 @@ class TestUInt64 { private DecodeMandatoryUInt64 mandatoryUInt64Decoder = new DecodeMandatoryUInt64(); + private UnionRegister decodeResult = new UnionRegister(); + @WithByteBuf("80") - void testNull(Collection buffers) throws IOException { - decode(nullableUInt64Decoder, buffers); - assertTrue(nullableUInt64Decoder.isReady()); - assertNull(nullableUInt64Decoder.getValue()); + void testNull(Collection buffers) { + decodeResult.isNull = false; + decodeResult.uInt64Value = new BigInteger("1"); + + decode(nullableUInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("81") - void optionalZero(Collection buffers) throws IOException { - decode(nullableUInt64Decoder, buffers); - assertTrue(nullableUInt64Decoder.isReady()); - assertEquals(new BigInteger("0"), nullableUInt64Decoder.getValue()); + void optionalZero(Collection buffers) { + decode(nullableUInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(new BigInteger("0"), decodeResult.uInt64Value); } @WithByteBuf("80") - void mandatoryZero(Collection buffers) throws IOException { - decode(mandatoryUInt64Decoder, buffers); - assertTrue(mandatoryUInt64Decoder.isReady()); - assertEquals(new BigInteger("0"), mandatoryUInt64Decoder.getValue()); + void mandatoryZero(Collection buffers) { + decode(mandatoryUInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(new BigInteger("0"), decodeResult.uInt64Value); } @WithByteBuf("02 00 00 00 00 00 00 00 00 80") - void testMaxNullable(Collection buffers) throws IOException { - decode(nullableUInt64Decoder, buffers); - assertTrue(nullableUInt64Decoder.isReady()); - assertEquals(new BigInteger("18446744073709551615"), nullableUInt64Decoder.getValue()); + void testMaxNullable(Collection buffers) { + decode(nullableUInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(new BigInteger("18446744073709551615"), decodeResult.uInt64Value); } @WithByteBuf("01 7f 7f 7f 7f 7f 7f 7f 7f ff") - void testMaxMandatory(Collection buffers) throws IOException { - decode(mandatoryUInt64Decoder, buffers); - assertTrue(mandatoryUInt64Decoder.isReady()); - assertEquals(new BigInteger("18446744073709551615"), mandatoryUInt64Decoder.getValue()); + void testMaxMandatory(Collection buffers) { + decode(mandatoryUInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(new BigInteger("18446744073709551615"), decodeResult.uInt64Value); } @WithByteBuf("02 00 00 00 00 00 00 00 00 81") void testMaxOverflowNullable1(Collection buffers) { - decode(nullableUInt64Decoder, buffers); - assertTrue(nullableUInt64Decoder.isReady()); - assertThrows(IOException.class, () -> nullableUInt64Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(nullableUInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("02 00 00 00 00 00 00 00 00 00 80") void testMaxOverflowNullable2(Collection buffers) { - decode(nullableUInt64Decoder, buffers); - assertTrue(nullableUInt64Decoder.isReady()); - assertThrows(IOException.class, () -> nullableUInt64Decoder.getValue()); + decodeResult.isOverflow = false; + decode(nullableUInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("02 00 00 00 00 00 00 00 00 80") void testMaxOverflowMandatory1(Collection buffers) { - decode(mandatoryUInt64Decoder, buffers); - assertTrue(mandatoryUInt64Decoder.isReady()); - assertThrows(IOException.class, () -> mandatoryUInt64Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(mandatoryUInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("01 7f 7f 7f 7f 00 7f 7f 7f 7f ff") void testMaxOverflowMandatory2(Collection buffers) { - decode(mandatoryUInt64Decoder, buffers); - assertTrue(mandatoryUInt64Decoder.isReady()); - assertThrows(IOException.class, () -> mandatoryUInt64Decoder.getValue()); + decodeResult.isOverflow = false; + + decode(mandatoryUInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverlong); } @WithByteBuf("39 45 a4") - void optionalSimpleNumber1(Collection buffers) throws IOException { - decode(nullableUInt64Decoder, buffers); - assertTrue(nullableUInt64Decoder.isReady()); - assertEquals(new BigInteger("942755"), nullableUInt64Decoder.getValue()); + void optionalSimpleNumber1(Collection buffers) { + decode(nullableUInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(new BigInteger("942755"), decodeResult.uInt64Value); } @WithByteBuf("01 7f 7f 7f 7f 7f 7f 7f 7f ff") - void optionalSimpleNumber2(Collection buffers) throws IOException { - decode(nullableUInt64Decoder, buffers); - assertTrue(nullableUInt64Decoder.isReady()); - assertEquals(new BigInteger("18446744073709551614"), nullableUInt64Decoder.getValue()); + void optionalSimpleNumber2(Collection buffers) { + decode(nullableUInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(new BigInteger("18446744073709551614"), decodeResult.uInt64Value); } @WithByteBuf("39 45 a3") - void mandatorySimpleNumber1(Collection buffers) throws IOException { - decode(mandatoryUInt64Decoder, buffers); - assertTrue(mandatoryUInt64Decoder.isReady()); - assertEquals(new BigInteger("942755"), mandatoryUInt64Decoder.getValue()); + void mandatorySimpleNumber1(Collection buffers) { + decode(mandatoryUInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(new BigInteger("942755"), decodeResult.uInt64Value); } @WithByteBuf("01 10 78 20 76 62 2a 62 51 cf") - void mandatorySimpleNumber2(Collection buffers) throws IOException { - decode(mandatoryUInt64Decoder, buffers); - assertTrue(mandatoryUInt64Decoder.isReady()); - assertEquals(new BigInteger("10443992354206034127"), mandatoryUInt64Decoder.getValue()); + void mandatorySimpleNumber2(Collection buffers) { + decode(mandatoryUInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(new BigInteger("10443992354206034127"), decodeResult.uInt64Value); } @WithByteBuf("39 45 a4") - void optionalSimpleNumber1GetValueTwice(Collection buffers) throws IOException { - decode(nullableUInt64Decoder, buffers); - assertTrue(nullableUInt64Decoder.isReady()); - assertEquals(new BigInteger("942755"), nullableUInt64Decoder.getValue()); - assertEquals(new BigInteger("942755"), nullableUInt64Decoder.getValue()); + void optionalSimpleNumber1GetValueTwice(Collection buffers) { + decode(nullableUInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(new BigInteger("942755"), decodeResult.uInt64Value); + assertEquals(new BigInteger("942755"), decodeResult.uInt64Value); } @WithByteBuf("39 45 a3") - void mandatorySimpleNumber1GetValueTwice(Collection buffers) throws IOException { - decode(mandatoryUInt64Decoder, buffers); - assertTrue(mandatoryUInt64Decoder.isReady()); - assertEquals(new BigInteger("942755"), mandatoryUInt64Decoder.getValue()); - assertEquals(new BigInteger("942755"), mandatoryUInt64Decoder.getValue()); + void mandatorySimpleNumber1GetValueTwice(Collection buffers) { + decode(mandatoryUInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(new BigInteger("942755"), decodeResult.uInt64Value); + assertEquals(new BigInteger("942755"), decodeResult.uInt64Value); } @WithByteBuf("39 45 a4 01 7f 7f 7f 7f 7f 7f 7f 7f ff") - void optionalSimpleNumbersTwoValuesInRow(Collection buffers) throws IOException { - decode(nullableUInt64Decoder, buffers); - assertTrue(nullableUInt64Decoder.isReady()); - assertEquals(new BigInteger("942755"), nullableUInt64Decoder.getValue()); + void optionalSimpleNumbersTwoValuesInRow(Collection buffers) { + decode(nullableUInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(new BigInteger("942755"), decodeResult.uInt64Value); - decode(nullableUInt64Decoder, buffers); - assertTrue(nullableUInt64Decoder.isReady()); - assertEquals(new BigInteger("18446744073709551614"), nullableUInt64Decoder.getValue()); + decode(nullableUInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(new BigInteger("18446744073709551614"), decodeResult.uInt64Value); } @WithByteBuf("39 45 a3 01 10 78 20 76 62 2a 62 51 cf") - void mandatorySimpleNumbersTwoValuesInRow(Collection buffers) throws IOException { - decode(mandatoryUInt64Decoder, buffers); - assertTrue(mandatoryUInt64Decoder.isReady()); - assertEquals(new BigInteger("942755"), mandatoryUInt64Decoder.getValue()); + void mandatorySimpleNumbersTwoValuesInRow(Collection buffers) { + decode(mandatoryUInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(new BigInteger("942755"), decodeResult.uInt64Value); - decode(mandatoryUInt64Decoder, buffers); - assertTrue(mandatoryUInt64Decoder.isReady()); - assertEquals(new BigInteger("10443992354206034127"), mandatoryUInt64Decoder.getValue()); + decode(mandatoryUInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertFalse(decodeResult.isOverlong); + assertEquals(new BigInteger("10443992354206034127"), decodeResult.uInt64Value); } @WithByteBuf("00 39 45 a4") - void mandatoryOverlong(Collection buffers) throws IOException { - decode(mandatoryUInt64Decoder, buffers); - assertTrue(mandatoryUInt64Decoder.isReady()); - assertTrue(mandatoryUInt64Decoder.isOverlong()); - assertEquals(new BigInteger("942756"), mandatoryUInt64Decoder.getValue()); + void mandatoryOverlong(Collection buffers) { + decodeResult.isOverlong = false; + + decode(mandatoryUInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals(new BigInteger("942756"), decodeResult.uInt64Value); } @WithByteBuf("00 40 81") - void mandatoryNotOverlong(Collection buffers) throws IOException { - decode(mandatoryUInt64Decoder, buffers); - assertTrue(mandatoryUInt64Decoder.isReady()); - assertFalse(mandatoryUInt64Decoder.isOverlong()); - assertEquals(new BigInteger("8193"), mandatoryUInt64Decoder.getValue()); + void mandatoryNotOverlong(Collection buffers) { + decode(mandatoryUInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals(new BigInteger("8193"), decodeResult.uInt64Value); } @WithByteBuf("00 39 45 a4") - void nullableOverlong(Collection buffers) throws IOException { - decode(nullableUInt64Decoder, buffers); - assertTrue(nullableUInt64Decoder.isReady()); - assertTrue(nullableUInt64Decoder.isOverlong()); - assertEquals(new BigInteger("942755"), nullableUInt64Decoder.getValue()); + void nullableOverlong(Collection buffers) { + decodeResult.isOverlong = false; + + decode(nullableUInt64Decoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals(new BigInteger("942755"), decodeResult.uInt64Value); } @WithByteBuf("00 40 81") - void nullableNotOverlong(Collection buffers) throws IOException { - decode(nullableUInt64Decoder, buffers); - assertTrue(nullableUInt64Decoder.isReady()); - assertFalse(nullableUInt64Decoder.isOverlong()); - assertEquals(new BigInteger("8192"), nullableUInt64Decoder.getValue()); + void nullableNotOverlong(Collection buffers) { + decode(nullableUInt64Decoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals(new BigInteger("8192"), decodeResult.uInt64Value); + } + + @BeforeEach + void resetRegisterFlags() { + decodeResult.isOverlong = true; + decodeResult.isNull = true; + decodeResult.isOverflow = true; + decodeResult.uInt64Value = null; } } diff --git a/src/test/java/com/exactpro/epfast/decoder/presencemap/TestDecodePresenceMap.java b/src/test/java/com/exactpro/epfast/decoder/presencemap/TestDecodePresenceMap.java index 95ed82b2..a17a5d9b 100644 --- a/src/test/java/com/exactpro/epfast/decoder/presencemap/TestDecodePresenceMap.java +++ b/src/test/java/com/exactpro/epfast/decoder/presencemap/TestDecodePresenceMap.java @@ -16,58 +16,68 @@ package com.exactpro.epfast.decoder.presencemap; +import com.exactpro.epfast.decoder.message.UnionRegister; import com.exactpro.junit5.WithByteBuf; import io.netty.buffer.ByteBuf; +import org.junit.jupiter.api.BeforeEach; import java.util.Collection; -import static com.exactpro.epfast.DecoderUtils.decode; +import static com.exactpro.epfast.decoder.DecoderUtils.decode; import static org.junit.jupiter.api.Assertions.*; class TestDecodePresenceMap { private DecodePresenceMap presenceMapDecoder = new DecodePresenceMap(); + + private UnionRegister decodeResult = new UnionRegister(); @WithByteBuf("95") //0b10010101 void testSingleByte(Collection buffers) { - decode(presenceMapDecoder, buffers); - PresenceMap presenceMap = presenceMapDecoder.getValue(); - assertTrue(presenceMap.getValue(0)); - assertFalse(presenceMap.getValue(1)); - assertTrue(presenceMap.getValue(2)); - assertFalse(presenceMap.getValue(3)); - assertTrue(presenceMap.getValue(4)); - assertFalse(presenceMap.getValue(5)); - assertFalse(presenceMap.getValue(6)); + decode(presenceMapDecoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertTrue(decodeResult.presenceMap.getValue(0)); + assertFalse(decodeResult.presenceMap.getValue(1)); + assertTrue(decodeResult.presenceMap.getValue(2)); + assertFalse(decodeResult.presenceMap.getValue(3)); + assertTrue(decodeResult.presenceMap.getValue(4)); + assertFalse(decodeResult.presenceMap.getValue(5)); + assertFalse(decodeResult.presenceMap.getValue(6)); } @WithByteBuf("15 15 00 00 00 80") void testOverlong(Collection buffers) { - decode(presenceMapDecoder, buffers); - assertTrue(presenceMapDecoder.isReady()); - PresenceMap presenceMap = presenceMapDecoder.getValue(); - assertTrue(presenceMap.getValue(0)); - assertFalse(presenceMap.getValue(1)); - assertTrue(presenceMap.getValue(2)); - assertFalse(presenceMap.getValue(3)); - assertTrue(presenceMap.getValue(4)); - assertFalse(presenceMap.getValue(5)); - assertFalse(presenceMap.getValue(6)); - assertTrue(presenceMapDecoder.isOverlong()); + decode(presenceMapDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertTrue(decodeResult.presenceMap.getValue(0)); + assertFalse(decodeResult.presenceMap.getValue(1)); + assertTrue(decodeResult.presenceMap.getValue(2)); + assertFalse(decodeResult.presenceMap.getValue(3)); + assertTrue(decodeResult.presenceMap.getValue(4)); + assertFalse(decodeResult.presenceMap.getValue(5)); + assertFalse(decodeResult.presenceMap.getValue(6)); } @WithByteBuf("15 15 00 00 00 82") void testTruncateWhenNotOverlong(Collection buffers) { - decode(presenceMapDecoder, buffers); - assertTrue(presenceMapDecoder.isReady()); - PresenceMap presenceMap = presenceMapDecoder.getValue(); - assertTrue(presenceMap.getValue(0)); - assertFalse(presenceMap.getValue(1)); - assertTrue(presenceMap.getValue(2)); - assertFalse(presenceMap.getValue(3)); - assertTrue(presenceMap.getValue(4)); - assertFalse(presenceMap.getValue(5)); - assertFalse(presenceMap.getValue(6)); - assertFalse(presenceMapDecoder.isOverlong()); + decode(presenceMapDecoder, buffers, decodeResult); + assertFalse(decodeResult.isOverlong); + assertFalse(decodeResult.isNull); + assertTrue(decodeResult.presenceMap.getValue(0)); + assertFalse(decodeResult.presenceMap.getValue(1)); + assertTrue(decodeResult.presenceMap.getValue(2)); + assertFalse(decodeResult.presenceMap.getValue(3)); + assertTrue(decodeResult.presenceMap.getValue(4)); + assertFalse(decodeResult.presenceMap.getValue(5)); + assertFalse(decodeResult.presenceMap.getValue(6)); + } + + @BeforeEach + void resetRegisterFlags() { + decodeResult.isOverlong = true; + decodeResult.isNull = true; + decodeResult.presenceMap = null; } } diff --git a/src/test/java/com/exactpro/epfast/decoder/unicode/TestDecodeByteVector.java b/src/test/java/com/exactpro/epfast/decoder/unicode/TestDecodeByteVector.java index 8226bc5f..3b627fa5 100644 --- a/src/test/java/com/exactpro/epfast/decoder/unicode/TestDecodeByteVector.java +++ b/src/test/java/com/exactpro/epfast/decoder/unicode/TestDecodeByteVector.java @@ -16,15 +16,16 @@ package com.exactpro.epfast.decoder.unicode; +import com.exactpro.epfast.decoder.message.UnionRegister; import com.exactpro.junit5.WithByteBuf; import io.netty.buffer.ByteBuf; +import org.junit.jupiter.api.BeforeEach; -import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Collection; import static org.junit.jupiter.api.Assertions.*; -import static com.exactpro.epfast.DecoderUtils.*; +import static com.exactpro.epfast.decoder.DecoderUtils.*; class TestDecodeByteVector { @@ -32,104 +33,133 @@ class TestDecodeByteVector { private DecodeMandatoryByteVector mandatoryByteVectorDecoder = new DecodeMandatoryByteVector(); + private UnionRegister decodeResult = new UnionRegister(); + @WithByteBuf("80") - void testNull(Collection buffers) throws IOException { - decode(nullableByteVectorDecoder, buffers); - assertTrue(nullableByteVectorDecoder.isReady()); - assertNull(nullableByteVectorDecoder.getValue()); + void testNull(Collection buffers) { + decodeResult.isNull = false; + + decode(nullableByteVectorDecoder, buffers, decodeResult); + assertTrue(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); } @WithByteBuf("81") - void testNullableZeroLen(Collection buffers) throws IOException { - decode(nullableByteVectorDecoder, buffers); - assertTrue(nullableByteVectorDecoder.isReady()); - assertEquals("", new String(nullableByteVectorDecoder.getValue(), StandardCharsets.UTF_8)); + void testNullableZeroLen(Collection buffers) { + decode(nullableByteVectorDecoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals("", new String(decodeResult.byteVectorValue, StandardCharsets.UTF_8)); } @WithByteBuf("80") - void testMandatoryZeroLen(Collection buffers) throws IOException { - decode(mandatoryByteVectorDecoder, buffers); - assertTrue(mandatoryByteVectorDecoder.isReady()); - assertEquals("", new String(mandatoryByteVectorDecoder.getValue(), StandardCharsets.UTF_8)); + void testMandatoryZeroLen(Collection buffers) { + decode(mandatoryByteVectorDecoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals("", new String(decodeResult.byteVectorValue, StandardCharsets.UTF_8)); } @WithByteBuf("10 00 00 00 81 41 42 42 43 44 45") void testNullableLengthOverflow1(Collection buffers) { - decode(nullableByteVectorDecoder, buffers); - assertTrue(nullableByteVectorDecoder.isReady()); - assertThrows(IOException.class, () -> nullableByteVectorDecoder.getValue()); + decodeResult.isOverflow = false; + + decode(nullableByteVectorDecoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertTrue(decodeResult.isOverflow); } @WithByteBuf("10 00 00 00 00 00 80 41 42 42 43 44 45") void testNullableLengthOverflow2(Collection buffers) { - decode(nullableByteVectorDecoder, buffers); - assertTrue(nullableByteVectorDecoder.isReady()); - assertThrows(IOException.class, () -> nullableByteVectorDecoder.getValue()); + decodeResult.isOverflow = false; + + decode(nullableByteVectorDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); } @WithByteBuf("10 00 00 00 80 41 42 42 43 44 45") void testMandatoryLengthOverflow1(Collection buffers) { - decode(mandatoryByteVectorDecoder, buffers); - assertTrue(mandatoryByteVectorDecoder.isReady()); - assertThrows(IOException.class, () -> mandatoryByteVectorDecoder.getValue()); + decodeResult.isOverflow = false; + + decode(mandatoryByteVectorDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); } @WithByteBuf("0f 7f 7f 7f 7f 00 ff 41 42 42 43 44 45") void testMandatoryLengthOverflow2(Collection buffers) { - decode(mandatoryByteVectorDecoder, buffers); - assertTrue(mandatoryByteVectorDecoder.isReady()); - assertThrows(IOException.class, () -> mandatoryByteVectorDecoder.getValue()); + decodeResult.isOverflow = false; + + decode(mandatoryByteVectorDecoder, buffers, decodeResult); + assertTrue(decodeResult.isOverflow); + assertFalse(decodeResult.isNull); } @WithByteBuf("87 41 42 42 43 44 45") - void testSimpleNullableVector(Collection buffers) throws IOException { - decode(nullableByteVectorDecoder, buffers); - assertTrue(nullableByteVectorDecoder.isReady()); - assertEquals("ABBCDE", new String(nullableByteVectorDecoder.getValue(), StandardCharsets.UTF_8)); + void testSimpleNullableVector(Collection buffers) { + decode(nullableByteVectorDecoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals("ABBCDE", new String(decodeResult.byteVectorValue, StandardCharsets.UTF_8)); } @WithByteBuf("86 41 42 42 43 44 45") - void testSimpleMandatoryVector(Collection buffers) throws IOException { - decode(mandatoryByteVectorDecoder, buffers); - assertTrue(mandatoryByteVectorDecoder.isReady()); - assertEquals("ABBCDE", new String(mandatoryByteVectorDecoder.getValue(), StandardCharsets.UTF_8)); + void testSimpleMandatoryVector(Collection buffers) { + decode(mandatoryByteVectorDecoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals("ABBCDE", new String(decodeResult.byteVectorValue, StandardCharsets.UTF_8)); } @WithByteBuf("81") - void testNullableZeroLenGetValueTwice(Collection buffers) throws IOException { - decode(nullableByteVectorDecoder, buffers); - assertTrue(nullableByteVectorDecoder.isReady()); - assertEquals("", new String(nullableByteVectorDecoder.getValue(), StandardCharsets.UTF_8)); - assertEquals("", new String(nullableByteVectorDecoder.getValue(), StandardCharsets.UTF_8)); + void testNullableZeroLenGetValueTwice(Collection buffers) { + decode(nullableByteVectorDecoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals("", new String(decodeResult.byteVectorValue, StandardCharsets.UTF_8)); + assertEquals("", new String(decodeResult.byteVectorValue, StandardCharsets.UTF_8)); } @WithByteBuf("80") - void testMandatoryZeroLenGetValueTwice(Collection buffers) throws IOException { - decode(mandatoryByteVectorDecoder, buffers); - assertTrue(mandatoryByteVectorDecoder.isReady()); - assertEquals("", new String(mandatoryByteVectorDecoder.getValue(), StandardCharsets.UTF_8)); - assertEquals("", new String(mandatoryByteVectorDecoder.getValue(), StandardCharsets.UTF_8)); + void testMandatoryZeroLenGetValueTwice(Collection buffers) { + decode(mandatoryByteVectorDecoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals("", new String(decodeResult.byteVectorValue, StandardCharsets.UTF_8)); + assertEquals("", new String(decodeResult.byteVectorValue, StandardCharsets.UTF_8)); } @WithByteBuf("87 41 42 42 43 44 45 81") - void testSimpleNullableVectorTwoValuesInRow(Collection buffers) throws IOException { - decode(nullableByteVectorDecoder, buffers); - assertTrue(nullableByteVectorDecoder.isReady()); - assertEquals("ABBCDE", new String(nullableByteVectorDecoder.getValue(), StandardCharsets.UTF_8)); - - decode(nullableByteVectorDecoder, buffers); - assertTrue(nullableByteVectorDecoder.isReady()); - assertEquals("", new String(nullableByteVectorDecoder.getValue(), StandardCharsets.UTF_8)); + void testSimpleNullableVectorTwoValuesInRow(Collection buffers) { + decode(nullableByteVectorDecoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals("ABBCDE", new String(decodeResult.byteVectorValue, StandardCharsets.UTF_8)); + + decode(nullableByteVectorDecoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals("", new String(decodeResult.byteVectorValue, StandardCharsets.UTF_8)); } @WithByteBuf("86 41 42 42 43 44 45 80") - void testSimpleMandatoryVectorTwoValuesInRow(Collection buffers) throws IOException { - decode(mandatoryByteVectorDecoder, buffers); - assertTrue(mandatoryByteVectorDecoder.isReady()); - assertEquals("ABBCDE", new String(mandatoryByteVectorDecoder.getValue(), StandardCharsets.UTF_8)); - - decode(mandatoryByteVectorDecoder, buffers); - assertTrue(mandatoryByteVectorDecoder.isReady()); - assertEquals("", new String(mandatoryByteVectorDecoder.getValue(), StandardCharsets.UTF_8)); + void testSimpleMandatoryVectorTwoValuesInRow(Collection buffers) { + decode(mandatoryByteVectorDecoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals("ABBCDE", new String(decodeResult.byteVectorValue, StandardCharsets.UTF_8)); + + decode(mandatoryByteVectorDecoder, buffers, decodeResult); + assertFalse(decodeResult.isNull); + assertFalse(decodeResult.isOverflow); + assertEquals("", new String(decodeResult.byteVectorValue, StandardCharsets.UTF_8)); + } + + @BeforeEach + void resetRegisterFlags() { + decodeResult.isNull = true; + decodeResult.isOverflow = true; + decodeResult.presenceMap = null; } }