lkuchars commented on code in PR #10105: URL: https://github.com/apache/nifi/pull/10105#discussion_r2254116046
########## nifi-extension-bundles/nifi-confluent-platform-bundle/nifi-confluent-protobuf-message-name-resolver/src/test/java/org/apache/nifi/confluent/schemaregistry/VarintUtilsTest.java: ########## @@ -0,0 +1,245 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.nifi.confluent.schemaregistry; + +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +import static org.apache.nifi.confluent.schemaregistry.VarintUtils.decodeZigZag; +import static org.apache.nifi.confluent.schemaregistry.VarintUtils.readVarintFromStream; +import static org.apache.nifi.confluent.schemaregistry.VarintUtils.readVarintFromStreamAfterFirstByteConsumed; +import static org.apache.nifi.confluent.schemaregistry.VarintUtils.writeZigZagVarint; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class VarintUtilsTest { + + @Test + public void testReadVarintFromStream_SingleByte() throws IOException { + byte[] data = {0x08}; // 8 in varint format (0x08 = 00001000) + InputStream inputStream = new ByteArrayInputStream(data); + + int result = readVarintFromStream(inputStream); + assertEquals(8, result); + } + + @Test + public void testReadVarintFromStream_MultiByte() throws IOException { + byte[] data = {(byte) 0x96, 0x01}; // 150 in varint format (10010110 00000001) + InputStream inputStream = new ByteArrayInputStream(data); + + int result = readVarintFromStream(inputStream); + assertEquals(150, result); + } + + @Test + public void testReadVarintFromStream_MaxValue() throws IOException { + // Maximum 32-bit value in varint format (11111111 11111111 11111111 11111111 00001111) + byte[] data = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x0F}; + InputStream inputStream = new ByteArrayInputStream(data); + + int result = readVarintFromStream(inputStream); + assertEquals(0xFFFFFFFF, result); + } + + @Test + public void testReadVarintFromStream_WithFirstByte() throws IOException { + byte[] data = {0x08}; // 8 in varint format (0x08 = 00001000) + InputStream inputStream = new ByteArrayInputStream(data); + + int result = readVarintFromStreamAfterFirstByteConsumed(inputStream, 0x08); + assertEquals(8, result); + } + + @Test + public void testReadVarintFromStream_Zero() throws IOException { + byte[] data = {0x00}; // 0 in varint format (0x00 = 00000000) + InputStream inputStream = new ByteArrayInputStream(data); + + int result = readVarintFromStream(inputStream); + assertEquals(0, result); + } + + @Test + public void testReadVarintFromStream_LargeValue() throws IOException { + // 16384 in varint format (10000000 10000000 00000001) + byte[] data = {(byte) 0x80, (byte) 0x80, 0x01}; + InputStream inputStream = new ByteArrayInputStream(data); + + int result = readVarintFromStream(inputStream); + assertEquals(16384, result); + } + + @Test + public void testReadVarintFromStream_EmptyStream() { + InputStream inputStream = new ByteArrayInputStream(new byte[0]); + + IOException exception = assertThrows(IOException.class, () -> readVarintFromStream(inputStream)); + assertTrue(exception.getMessage().contains("Unexpected end of stream while reading varint")); + } + + @Test + public void testReadVarintFromStream_TruncatedStream() { + // Start of a multi-byte varint but missing continuation bytes (0x80 = 10000000) + byte[] data = {(byte) 0x80}; // Indicates more bytes to follow but none provided + InputStream inputStream = new ByteArrayInputStream(data); + + IOException exception = assertThrows(IOException.class, () -> readVarintFromStream(inputStream)); + assertTrue(exception.getMessage().contains("Unexpected end of stream while reading varint")); + } + + @Test + public void testReadVarintFromStream_TooLong() { + // Varint with more than 32 bits (5 bytes with continuation bit set) + // (10000000 10000000 10000000 10000000 10000000 00000001) + byte[] data = {(byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, 0x01}; + InputStream inputStream = new ByteArrayInputStream(data); + + IOException exception = assertThrows(IOException.class, () -> readVarintFromStream(inputStream)); + assertTrue(exception.getMessage().contains("Varint too long (more than 32 bits)")); + } + + @Test + public void testDecodeZigZag_PositiveValues() { + // Test positive values + assertEquals(0, decodeZigZag(0)); // 0 -> 0 + assertEquals(1, decodeZigZag(2)); // 1 -> 2 + assertEquals(2, decodeZigZag(4)); // 2 -> 4 + assertEquals(3, decodeZigZag(6)); // 3 -> 6 + assertEquals(150, decodeZigZag(300)); // 150 -> 300 + } + + @Test + public void testDecodeZigZag_NegativeValues() { + // Test negative values + assertEquals(-1, decodeZigZag(1)); // -1 -> 1 + assertEquals(-2, decodeZigZag(3)); // -2 -> 3 + assertEquals(-3, decodeZigZag(5)); // -3 -> 5 + assertEquals(-150, decodeZigZag(299)); // -150 -> 299 + } + + @Test + public void testDecodeZigZag_ExtremeValues() { + // Test extreme values + assertEquals(Integer.MAX_VALUE, decodeZigZag(0xFFFFFFFE)); + assertEquals(Integer.MIN_VALUE, decodeZigZag(0xFFFFFFFF)); + } + + @Test + public void testDecodeZigZag_LargeValues() { + // Test some larger values to verify the algorithm + assertEquals(1000, decodeZigZag(2000)); // 1000 -> 2000 + assertEquals(-1000, decodeZigZag(1999)); // -1000 -> 1999 + } Review Comment: Let's leave them like this please. It's more readable with comments and all -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
