Skip to content

Commit

Permalink
Added javadoc and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
keiran-lawrey committed Nov 15, 2023
1 parent 7b45456 commit 07f031c
Show file tree
Hide file tree
Showing 265 changed files with 7,410 additions and 777 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@

import static org.junit.jupiter.api.Assertions.assertFalse;

/**
* A test class for validating the behavior of AbstractCommonMarshallable.
* This class extends the base test functionalities provided by WireTestCommon.
*/
class AbstractCommonMarshallableTest extends net.openhft.chronicle.wire.WireTestCommon {

/**
* Tests the default behavior of AbstractCommonMarshallable
* to ensure it doesn't use self-describing messages by default.
*/
@Test
void doesNotUseSelfDescribingMessagesByDefault() {
// Assert that a new instance of AbstractCommonMarshallable
// doesn't use self-describing messages by default
assertFalse(new AbstractCommonMarshallable() {
}.usesSelfDescribingMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,69 @@

public class Base128LongConverterTest extends WireTestCommon {

// Testing the parsing of strings to long and re-creating the strings from parsed long values
@Test
public void parse() {
// Getting an instance of Base128LongConverter
LongConverter c = Base128LongConverter.INSTANCE;
// System.out.println(c.asString(-1L));
// Loop through predefined strings
for (String s : ",a,ab,abc,abcd,abcde,123456,1234567,12345678,123456789,~~~~~~~~~".split(",")) {
// Convert string s to a long value v
long v = c.parse(s);
StringBuilder sb = new StringBuilder();
// Append the parsed value v back into a StringBuilder sb
c.append(sb, v);
// Assert that the original string s and the newly created string are equal
assertEquals(s, sb.toString());
}
}

// Testing the conversion of random long numbers to Base128 encoded strings and back to long numbers
@Test
public void asString() {
// Getting an instance of Base128LongConverter
LongConverter c = Base128LongConverter.INSTANCE;
Random rand = new Random();
// Looping to perform the test with 100000 random numbers
for (int i = 0; i < 100000; i++) {
// Generate a random long number l and test its string conversion and re-parsing
long l = (long) Math.random() * Base128LongConverter.MAX_LENGTH;
String s = c.asString(l);
Assert.assertEquals(s, l, c.parse(s));
}
}

// Ensure safe character conversion using TextWire
@Test
public void allSafeCharsTextWire() {
// Create a TextWire instance and perform a general character safety test
Wire wire = new TextWire(Bytes.allocateElasticOnHeap()).useTextDocuments();
allSafeChars(wire);
}

// Ensure safe character conversion using YamlWire
@Test
public void allSafeCharsYamlWire() {
// Create a YamlWire instance and perform a general character safety test
Wire wire = new YamlWire(Bytes.allocateElasticOnHeap()).useTextDocuments();
allSafeChars(wire);
}

// Generalized method for validating safe character handling
private void allSafeChars(Wire wire) {
// Getting an instance of Base128LongConverter
final LongConverter converter = Base128LongConverter.INSTANCE;
// Looping through numbers [0, 128*128] and validate their wire representation
for (long i = 0; i <= 128 * 128; i++) {
// Clear the wire, write the number, and verify the written data
wire.clear();
wire.write("a").writeLong(converter, i);
wire.write("b").sequence(i, (i2, v) -> {
v.writeLong(converter, i2);
v.writeLong(converter, i2);
});
// Assert that the wire content matches the expected values
assertEquals(wire.toString(),
i, wire.read("a").readLong(converter));
wire.read("b").sequence(i, (i2, v) -> {
Expand All @@ -80,5 +99,4 @@ private void allSafeChars(Wire wire) {
});
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Base256IntConverterTest extends WireTestCommon {

@Test
public void parse() {
// Various assertions to test the parse functionality of the Base256IntConverter
assertEquals(0, Base256IntConverter.INSTANCE.parse(""));
assertEquals(1, Base256IntConverter.INSTANCE.parse("\u0001"));
assertEquals(255, Base256IntConverter.INSTANCE.parse("\u00FF"));
Expand All @@ -36,6 +37,7 @@ public void parse() {

@Test
public void append() {
// Various assertions to test the asString functionality of the Base256IntConverter
assertEquals("", Base256IntConverter.INSTANCE.asString(0));
assertEquals("A", Base256IntConverter.INSTANCE.asString('A'));
assertEquals("U51", Base256IntConverter.INSTANCE.asString(('U' << 16) + ('5' << 8) + '1'));
Expand All @@ -49,4 +51,4 @@ public void append() {
assertEquals(4, Base256IntConverter.INSTANCE.asString((1 << 24)).length());
assertEquals(4, Base256IntConverter.INSTANCE.asString(~0).length());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,57 @@
import static org.junit.Assert.assertEquals;

public class Base32LongConverterTest extends WireTestCommon {

// A test to check the parsing functionality of Base32LongConverter.
@Test
public void parse() {
LongConverter bic = new Base32LongConverter();

// Iterate through predefined string values to check the conversion consistency
// in both original and lower case forms.
for (String s : ",O,A,L,ZZ,QQ,ABCDEGHIJKLM,5OPQRSTVWXYZ,JZZZZZZZZZZZ".split(",")) {
assertEquals(s, bic.asString(bic.parse(s)));
assertEquals(s, bic.asString(bic.parse(s.toLowerCase())));
assertEquals(s, bic.asString(bic.parse(s))); // Check if parsing and then converting back to string remains consistent with the original string
assertEquals(s, bic.asString(bic.parse(s.toLowerCase()))); // Do the same for the lower case version
}
}

// A test to check the character safety in TextWire.
@Test
public void allSafeCharsTextWire() {
Wire wire = new TextWire(Bytes.allocateElasticOnHeap()).useTextDocuments();
allSafeChars(wire);
}

// A test to check the character safety in YamlWire.
@Test
public void allSafeCharsYamlWire() {
Wire wire = new YamlWire(Bytes.allocateElasticOnHeap()).useTextDocuments();
allSafeChars(wire);
}

// A method that performs a check on all safe characters in a given wire format.
private void allSafeChars(Wire wire) {
// Retrieve an instance of Base32LongConverter
final LongConverter converter = Base32LongConverter.INSTANCE;

// Iterating over a set of long numbers, to validate the consistency
// of writing a long to the wire and reading it back.
for (long i = 0; i <= 32 * 32; i++) {
wire.clear();
wire.write("a").writeLong(converter, i);
wire.clear(); // Clear the wire content
wire.write("a").writeLong(converter, i); // Write a long value using the converter
wire.write("b").sequence(i, (i2, v) -> {
// Write a sequence of long values using the converter
v.writeLong(converter, i2);
v.writeLong(converter, i2);
});
// Validate that the read value matches the written value.
assertEquals(wire.toString(),
i, wire.read("a").readLong(converter));
wire.read("b").sequence(i, (i2, v) -> {
// Validate that the sequence read values match the written values.
assertEquals((long) i2, v.readLong(converter));
assertEquals((long) i2, v.readLong(converter));
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,67 +24,98 @@
import static org.junit.Assert.assertEquals;

public class Base40LongConverterTest extends WireTestCommon {

// Validate the parsing of Base40 upper case characters and digits
@Test
public void parse() {
// Use UPPER instance of Base40LongConverter
Base40LongConverter bic = Base40LongConverter.UPPER;

// Iterating through predefined strings and validating
// conversion from string to long and back to string
for (String s : ",A,0,ZZ,99,ABCDEF,012345,ZZZZZZZZZZZZ,999999999999".split(",")) {
assertEquals(s, bic.asString(bic.parse(s)));
}
}

// Validate the parsing of Base40 lower case characters and digits
@Test
public void parseLower() {
// Use LOWER instance of Base40LongConverter
Base40LongConverter bic = Base40LongConverter.LOWER;

// Iterating through predefined strings and validating
// conversion from string to long and back to string
for (String s : ",a,0,zz,99,abcdef,012345,zzzzzzzzzzzz,999999999999".split(",")) {
assertEquals(s, bic.asString(bic.parse(s)));
}
}

// Validate long conversion in a Sample class instance
@Test
public void longConversion() {
// Initialize a sample object
Sample sample = new Sample();
// Parse a string "TEST" into a long and set it as strategyId
sample.strategyId = Base40LongConverter.INSTANCE.parse("TEST");

// Define the expected string representation of the sample object
final String expectedToString = "!net.openhft.chronicle.wire.Base40LongConverterTest$Sample {\n" +
" strategyId: TEST\n" +
"}\n";

// Assert that the string representation of the sample object matches the expected string
assertEquals(expectedToString, sample.toString());
}

// Internal class used in the longConversion test
private static class Sample extends SelfDescribingMarshallable {
// StrategyId field utilizing Base40LongConverter for long conversion
@LongConversion(Base40LongConverter.class)
public long strategyId;
}

// Ensure safe character conversion using TextWire
@Test
public void allSafeCharsTextWire() {
// Create a TextWire instance with elastic on heap bytes and configure it to use text documents
Wire wire = new TextWire(Bytes.allocateElasticOnHeap()).useTextDocuments();
// Run the generic safe character check
allSafeChars(wire);
}

// Ensure safe character conversion using YamlWire
@Test
public void allSafeCharsYamlWire() {
// Create a YamlWire instance with elastic on heap bytes and configure it to use text documents
Wire wire = new YamlWire(Bytes.allocateElasticOnHeap()).useTextDocuments();
// Run the generic safe character check
allSafeChars(wire);
}

// Utility method: Check all safe characters within the provided Wire instance
private void allSafeChars(Wire wire) {
// Utilize the instance of Base40LongConverter
final LongConverter converter = Base40LongConverter.INSTANCE;
// Iterating through long numbers and validating their conversion and sequencing in the Wire
for (long i = 0; i <= 40 * 40; i++) {
// Clear the wire data
wire.clear();
// Write the long number i into the wire with a tag "a" using the converter
wire.write("a").writeLong(converter, i);
// Write a sequence of the same number tagged as "b"
wire.write("b").sequence(i, (i2, v) -> {
v.writeLong(converter, i2);
v.writeLong(converter, i2);
});
// Validate that the wire representation is accurate
assertEquals(wire.toString(),
i, wire.read("a").readLong(converter));
// Check the sequence integrity and correctness in the wire
wire.read("b").sequence(i, (i2, v) -> {
assertEquals((long) i2, v.readLong(converter));
assertEquals((long) i2, v.readLong(converter));
});
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,55 +28,80 @@

public class Base64LongConverterTest extends WireTestCommon {

// Validate the parsing of Base64 encoded strings to long and vice versa
@Test
public void parse() {
// Obtain the singleton instance of Base64LongConverter
LongConverter c = Base64LongConverter.INSTANCE;
// System.out.println(c.asString(-1L));
// Iterate through predefined strings, validate conversion from string to long and back to string
for (String s : ",a,ab,abc,abcd,ab.de,123_56,1234567,12345678,123456789,z23456789,z234567890,O_________".split(",")) {
long v = c.parse(s);
assertEquals(s, c.asString(v));
}
}

// Validate string conversion of randomly generated long numbers
@Test
public void asString() {
// Obtain the singleton instance of Base64LongConverter
LongConverter c = Base64LongConverter.INSTANCE;
// Initialize a random number generator
Random rand = new Random();

// Validate the conversion of 128 randomly generated long numbers
for (int i = 0; i < 128; i++) {
// Ensure random consistency by seeding with the loop variable
rand.setSeed(i);
long l = rand.nextLong();
// Convert the long number to a Base64 encoded string
String s = c.asString(l);
// Assert conversion consistency by parsing it back and comparing with the original long number
Assert.assertEquals("i: " + i + ", s: " + s, l, c.parse(s));
}
}

// Ensure safe character conversion using TextWire
@Test
public void allSafeCharsTextWire() {
// Create a TextWire instance with elastic on heap bytes and configure it to use text documents
Wire wire = new TextWire(Bytes.allocateElasticOnHeap()).useTextDocuments();
// Execute the generic safe character check
allSafeChars(wire);
}

// Ensure safe character conversion using YamlWire
@Test
public void allSafeCharsYamlWire() {
// Create a YamlWire instance with elastic on heap bytes and configure it to use text documents
Wire wire = new YamlWire(Bytes.allocateElasticOnHeap()).useTextDocuments();
// Execute the generic safe character check
allSafeChars(wire);
}

// Utility method: Validate all safe characters within the provided Wire instance
private void allSafeChars(Wire wire) {
// Obtain the singleton instance of Base64LongConverter
final LongConverter converter = Base64LongConverter.INSTANCE;
// Iterate through long numbers, validating their conversion and sequencing in the Wire
for (long i = 0; i <= 64 * 64; i++) {
// Clear the wire data
wire.clear();
// Write the long number i into the wire with a tag "a" using the converter
wire.write("a").writeLong(converter, i);
// Write a sequence of the same number tagged as "b"
wire.write("b").sequence(i, (i2, v) -> {
v.writeLong(converter, i2);
v.writeLong(converter, i2);
});
// Validate that the wire representation is accurate
assertEquals(wire.toString(),
i, wire.read("a").readLong(converter));
// Check the sequence integrity and correctness in the wire
wire.read("b").sequence(i, (i2, v) -> {
assertEquals((long) i2, v.readLong(converter));
assertEquals((long) i2, v.readLong(converter));
});
}
}
}
}
Loading

0 comments on commit 07f031c

Please sign in to comment.