diff --git a/src/test/java/net/openhft/chronicle/bytes/BytesUtilTest.java b/src/test/java/net/openhft/chronicle/bytes/BytesUtilTest.java index e70091a41c6..41a60183824 100644 --- a/src/test/java/net/openhft/chronicle/bytes/BytesUtilTest.java +++ b/src/test/java/net/openhft/chronicle/bytes/BytesUtilTest.java @@ -18,6 +18,7 @@ package net.openhft.chronicle.bytes; import net.openhft.chronicle.core.Jvm; +import org.jetbrains.annotations.NotNull; import org.junit.Test; import java.io.File; @@ -135,6 +136,40 @@ public void contentsEqual() { } } + @Test + public void equals_reference() { + String a = "a"; + assertTrue(BytesUtil.equals(a, a)); + } + @Test + public void equals_equivalentCharSequences() { + Bytes a = Bytes.from("a"); + Bytes aa = Bytes.from("a"); + assertTrue(BytesUtil.equals(a, aa)); + } + + @Test + public void equals_equivalentObjects() { + // Intentional boxing to create two equivalent but distinct objects + assertTrue(BytesUtil.equals(new Integer(1), new Integer(1))); + } + + @Test + public void toCharArray() { + Bytes bytes = Bytes.from("test"); + char[] charArray = BytesUtil.toCharArray(bytes); + for (char c : charArray) { + assertEquals(bytes.readChar(), c); + } + } + + @Test + public void reverse() { + Bytes test = Bytes.from("test"); + BytesUtil.reverse(test, 0); + assertEquals(Bytes.from("tset"), test); + } + @Test public void combineDoubleNewline() { doTestCombineDoubleNewline("\n", "\n"); diff --git a/src/test/java/net/openhft/chronicle/bytes/util/BinaryLengthLengthTest.java b/src/test/java/net/openhft/chronicle/bytes/util/BinaryLengthLengthTest.java new file mode 100644 index 00000000000..53c4b15014f --- /dev/null +++ b/src/test/java/net/openhft/chronicle/bytes/util/BinaryLengthLengthTest.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2016-2022 chronicle.software + * + * https://chronicle.software + * + * 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 net.openhft.chronicle.bytes.util; + +import net.openhft.chronicle.bytes.*; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Collection; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@RunWith(Parameterized.class) +public class BinaryLengthLengthTest { + + private final BinaryLengthLength binaryLengthLength; + private final int binaryWireCode; + + public BinaryLengthLengthTest(BinaryLengthLength binaryLengthLength, int binaryWireCode) { + this.binaryLengthLength = binaryLengthLength; + this.binaryWireCode = binaryWireCode; + } + + @Parameterized.Parameters(name = "binaryLengthLength {0} binaryWireCode {1}") + public static Collection data() { + return Arrays.asList(new Object[][]{ + {BinaryLengthLength.LENGTH_8BIT, BinaryWireCode.BYTES_LENGTH8}, + {BinaryLengthLength.LENGTH_16BIT, BinaryWireCode.BYTES_LENGTH16}, + {BinaryLengthLength.LENGTH_32BIT, BinaryWireCode.BYTES_LENGTH32} + }); + } + + @Test + public void checkCodeMatches() { + assertEquals(binaryWireCode, binaryLengthLength.code()); + } + + @Test + public void checkCodeIsWritten() { + Bytes bytes = Bytes.elasticByteBuffer(128); + binaryLengthLength.initialise(bytes); + byte readCode = (byte) bytes.readUnsignedByte(); + assertEquals((byte) binaryWireCode, readCode); + } + +} \ No newline at end of file