Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
import net.openhft.chronicle.wire.*;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* Tests for validating the behavior of field converters with long data types.
Expand Down Expand Up @@ -47,30 +53,27 @@ static class Base16DTO extends SelfDescribingMarshallable {
}
}

/**
* Test for verifying the Base16 encoding functionality.
*/
@Test
public void base16() {
// Validate Base16 encoding with a range of positive values
doTest(new Base16DTO((byte) 1, '2', (short) 3, 4, 5), "" +
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$Base16DTO {\n" +
" b: 1,\n" +
" ch: 32,\n" +
" s: 3,\n" +
" i: 4,\n" +
" l: 5\n" +
"}\n");
// Validate Base16 encoding with maximum negative values
// Note: shorter types yield shorter strings, not all ffffffffffffffff
doTest(new Base16DTO((byte) -1, (char) -1, (short) -1, -1, -1), "" +
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$Base16DTO {\n" +
" b: ff,\n" +
" ch: ffff,\n" +
" s: ffff,\n" +
" i: ffffffff,\n" +
" l: ffffffffffffffff\n" +
"}\n");
Map<String, String> expected1 = new HashMap<>();
expected1.put("b", "1");
expected1.put("ch", "32");
expected1.put("s", "3");
expected1.put("i", "4");
expected1.put("l", "5");
doTest(new Base16DTO((byte) 1, '2', (short) 3, 4, 5),
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$Base16DTO",
expected1);

Map<String, String> expected2 = new HashMap<>();
expected2.put("b", "ff");
expected2.put("ch", "ffff");
expected2.put("s", "ffff");
expected2.put("i", "ffffffff");
expected2.put("l", "ffffffffffffffff");
doTest(new Base16DTO((byte) -1, (char) -1, (short) -1, -1, -1),
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$Base16DTO",
expected2);
}

/**
Expand Down Expand Up @@ -106,30 +109,27 @@ static class Base64DTO extends SelfDescribingMarshallable {
}
}

/**
* Test for verifying the Base64 encoding functionality.
*/
@Test
public void base64() {
// Validate Base64 encoding with a range of positive values
doTest(new Base64DTO((byte) 1, '2', (short) 3, 4, 5), "" +
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$Base64DTO {\n" +
" b: A,\n" +
" ch: x,\n" +
" s: C,\n" +
" i: D,\n" +
" l: E\n" +
"}\n");
// Validate Base64 encoding with maximum negative values
// Note: shorter types yield shorter strings, not all ffffffffffffffff
doTest(new Base64DTO((byte) -1, (char) -1, (short) -1, -1, -1), "" +
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$Base64DTO {\n" +
" b: C_,\n" +
" ch: O__,\n" +
" s: O__,\n" +
" i: C_____,\n" +
" l: O__________\n" +
"}\n");
Map<String, String> expected1 = new HashMap<>();
expected1.put("b", "A");
expected1.put("ch", "x");
expected1.put("s", "C");
expected1.put("i", "D");
expected1.put("l", "E");
doTest(new Base64DTO((byte) 1, '2', (short) 3, 4, 5),
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$Base64DTO",
expected1);

Map<String, String> expected2 = new HashMap<>();
expected2.put("b", "C_");
expected2.put("ch", "O__");
expected2.put("s", "O__");
expected2.put("i", "C_____");
expected2.put("l", "O__________");
doTest(new Base64DTO((byte) -1, (char) -1, (short) -1, -1, -1),
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$Base64DTO",
expected2);
}

/**
Expand Down Expand Up @@ -165,30 +165,27 @@ static class Base85DTO extends SelfDescribingMarshallable {
}
}

/**
* Test for verifying the Base85 encoding functionality.
*/
@Test
public void base85() {
// Validate Base85 encoding with a range of positive values
doTest(new Base85DTO((byte) 1, '2', (short) 3, 4, 5), "" +
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$Base85DTO {\n" +
" b: 1,\n" +
" ch: g,\n" +
" s: 3,\n" +
" i: 4,\n" +
" l: 5\n" +
"}\n");
// Validate Base85 encoding with maximum negative values
// Note: the encoded values for negative numbers are not straightforward like Base16 and Base64
doTest(new Base85DTO((byte) -1, (char) -1, (short) -1, -1, -1), "" +
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$Base85DTO {\n" +
" b: 30,\n" +
" ch: 960,\n" +
" s: 960,\n" +
" i: .Gk<0,\n" +
" l: +ko2&)z.H0\n" +
"}\n");
Map<String, String> expected1 = new HashMap<>();
expected1.put("b", "1");
expected1.put("ch", "g");
expected1.put("s", "3");
expected1.put("i", "4");
expected1.put("l", "5");
doTest(new Base85DTO((byte) 1, '2', (short) 3, 4, 5),
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$Base85DTO",
expected1);

Map<String, String> expected2 = new HashMap<>();
expected2.put("b", "30");
expected2.put("ch", "960");
expected2.put("s", "960");
expected2.put("i", ".Gk<0");
expected2.put("l", "+ko2&)z.H0");
doTest(new Base85DTO((byte) -1, (char) -1, (short) -1, -1, -1),
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$Base85DTO",
expected2);
}

@Test
Expand Down Expand Up @@ -230,23 +227,25 @@ static class ShortTextDTO extends SelfDescribingMarshallable {

@Test
public void shortText() {
doTest(new ShortTextDTO((byte) 1, '2', (short) 3, 4, 5), "" +
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$ShortTextDTO {\n" +
" b: 1,\n" +
" ch: g,\n" +
" s: 3,\n" +
" i: 4,\n" +
" l: 5\n" +
"}\n");
// note shorter types are shorter strings and not all ffffffffffffffff
doTest(new ShortTextDTO((byte) -1, (char) -1, (short) -1, -1, -1), "" +
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$ShortTextDTO {\n" +
" b: \"3 \",\n" +
" ch: \"96 \",\n" +
" s: \"96 \",\n" +
" i: \".Gk< \",\n" +
" l: \"+ko2&)z.H \"\n" +
"}\n");
Map<String, String> expected1 = new HashMap<>();
expected1.put("b", "1");
expected1.put("ch", "g");
expected1.put("s", "3");
expected1.put("i", "4");
expected1.put("l", "5");
doTest(new ShortTextDTO((byte) 1, '2', (short) 3, 4, 5),
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$ShortTextDTO",
expected1);

Map<String, String> expected2 = new HashMap<>();
expected2.put("b", "\"3 \"");
expected2.put("ch", "\"96 \"");
expected2.put("s", "\"96 \"");
expected2.put("i", "\".Gk< \"");
expected2.put("l", "\"+ko2&)z.H \"");
doTest(new ShortTextDTO((byte) -1, (char) -1, (short) -1, -1, -1),
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$ShortTextDTO",
expected2);
}

@Test
Expand Down Expand Up @@ -295,46 +294,53 @@ static class WordsDTO extends SelfDescribingMarshallable {
}
}

/**
* Test method for verifying the Words encoding functionality.
*/
@Test
public void words() {
// Validate Words encoding with a range of positive values.
// The expected results are arbitrary word mappings for demonstration.
doTest(new WordsDTO((byte) 1, '2', (short) 3, 4, 5), "" +
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$WordsDTO {\n" +
" b: aid,\n" +
" ch: joy,\n" +
" s: air,\n" +
" i: all,\n" +
" l: and\n" +
"}\n");
Map<String, String> expected1 = new HashMap<>();
expected1.put("b", "aid");
expected1.put("ch", "joy");
expected1.put("s", "air");
expected1.put("i", "all");
expected1.put("l", "and");
doTest(new WordsDTO((byte) 1, '2', (short) 3, 4, 5),
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$WordsDTO",
expected1);

// Validate Words encoding with maximum negative values.
// Note: shorter types yield different word combinations based on the negative value.
doTest(new WordsDTO((byte) -1, (char) -1, (short) -1, -1, -1), "" +
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$WordsDTO {\n" +
" b: corn,\n" +
" ch: writer.eight,\n" +
" s: writer.eight,\n" +
" i: writer.writer.among,\n" +
" l: writer.writer.writer.writer.writer.leg\n" +
"}\n");
Map<String, String> expected2 = new HashMap<>();
expected2.put("b", "corn");
expected2.put("ch", "writer.eight");
expected2.put("s", "writer.eight");
expected2.put("i", "writer.writer.among");
expected2.put("l", "writer.writer.writer.writer.writer.leg");
doTest(new WordsDTO((byte) -1, (char) -1, (short) -1, -1, -1),
"!net.openhft.chronicle.wire.converter.LongConvertorFieldsTest$WordsDTO",
expected2);
}

/**
* Helper method to serialize a DTO object using the YamlWire format,
* and validate the result against an expected string representation.
*
* @param dto The object to be serialized.
* @param expected The expected string representation of the serialized object.
*/
private void doTest(Marshallable dto, String expected) {
private void doTest(Marshallable dto, String expectedType, Map<String, String> expectedFields) {
Wire wire = new YamlWire();
wire.getValueOut().object(dto);
assertEquals(expected, wire.toString());
String actual = wire.toString();

assertTrue(actual.startsWith(expectedType));

Map<String, String> actualFields = extractFields(actual);
assertEquals(expectedFields.size(), actualFields.size());
for (Map.Entry<String, String> entry : expectedFields.entrySet()) {
assertEquals(entry.getValue(), actualFields.get(entry.getKey()));
}

Object object = wire.getValueIn().object();
assertEquals(dto, object);
}

private Map<String, String> extractFields(String yaml) {
Map<String, String> fields = new HashMap<>();
Pattern pattern = Pattern.compile("^\\s+(\\w+):\\s*(.+?)(?:,|\\s*)$", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(yaml);
while (matcher.find()) {
fields.put(matcher.group(1), matcher.group(2).trim().replaceAll(",$", ""));
}
return fields;
}
}