Skip to content

Commit

Permalink
Add further tests for BytesUtil and BinaryLengthLength fixes #538 (#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgd authored Jul 24, 2023
1 parent 9a1fa35 commit 05494fc
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/test/java/net/openhft/chronicle/bytes/BytesUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<byte[]> a = Bytes.from("a");
Bytes<byte[]> 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<byte[]> bytes = Bytes.from("test");
char[] charArray = BytesUtil.toCharArray(bytes);
for (char c : charArray) {
assertEquals(bytes.readChar(), c);
}
}

@Test
public void reverse() {
Bytes<byte[]> test = Bytes.from("test");
BytesUtil.reverse(test, 0);
assertEquals(Bytes.from("tset"), test);
}

@Test
public void combineDoubleNewline() {
doTestCombineDoubleNewline("\n", "\n");
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object[]> 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<ByteBuffer> bytes = Bytes.elasticByteBuffer(128);
binaryLengthLength.initialise(bytes);
byte readCode = (byte) bytes.readUnsignedByte();
assertEquals((byte) binaryWireCode, readCode);
}

}

0 comments on commit 05494fc

Please sign in to comment.