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 authored and peter-lawrey committed Jan 5, 2024
1 parent 333eda5 commit d41da0d
Show file tree
Hide file tree
Showing 226 changed files with 5,436 additions and 400 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@

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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,54 @@
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));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,51 +28,76 @@

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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,80 +28,120 @@

public class Base85LongConverterTest extends WireTestCommon {

// A sample string to test the parsing functionality.
private static final CharSequence TEST_STRING = "world";

// Validate parsing and stringification of various string inputs
@Test
public void parse() {
// Obtain the singleton instance of Base85LongConverter
LongConverter c = Base85LongConverter.INSTANCE;
// System.out.println(c.asString(-1L));
// Iterate through predefined strings, validate parsing and string reconstruction
for (String s : ",a,ab,abc,abcd,ab.de,123=56,1234567,12345678,zzzzzzzzz,+ko2&)z.0".split(",")) {
// Parse the string into a long value
long v = c.parse(s);
// Convert the parsed long value back into a string and validate against the original string
StringBuilder sb = new StringBuilder();
c.append(sb, v);
assertEquals(s, sb.toString());
}
}

// Validate string conversion of randomly generated long numbers
@Test
public void asString() {
// Obtain the singleton instance of Base85LongConverter
LongConverter c = Base85LongConverter.INSTANCE;

// Validate the conversion of 100,000 randomly generated long numbers
Random rand = new Random();
for (int i = 0; i < 100000; i++) {
long l = (long) Math.random() * Base85LongConverter.MAX_LENGTH;
// Convert the long number to a Base85 encoded string
String s = c.asString(l);
// Assert conversion consistency by parsing it back and comparing with the original long number
Assert.assertEquals(s, l, c.parse(s));
}
}

// Validate the append operation for a known input string
@Test
public void testAppend() {
// Create an elastic byte buffer
final Bytes<?> b = Bytes.elasticByteBuffer();
try {
// Obtain the singleton instance of Base85LongConverter
final Base85LongConverter idLongConverter = Base85LongConverter.INSTANCE;
// Parse the TEST_STRING into a long value
final long helloWorld = idLongConverter.parse(TEST_STRING);
// Append the parsed value back into a byte buffer
idLongConverter.append(b, helloWorld);
// Validate the byte buffer content against the TEST_STRING
assertEquals(TEST_STRING, b.toString());
} finally {
// Release the allocated buffer
b.releaseLast();
}
}

// Validate appending data with pre-existing content in the buffer
@Test
public void testAppendWithExistingData() {
// Create an elastic byte buffer and append "hello" to it
final Bytes<?> b = Bytes.elasticByteBuffer().append("hello");
try {
// Obtain the singleton instance of Base85LongConverter
final Base85LongConverter idLongConverter = Base85LongConverter.INSTANCE;
// Parse the TEST_STRING into a long value
final long helloWorld = idLongConverter.parse(TEST_STRING);
// Append the parsed value to the already partially filled byte buffer
idLongConverter.append(b, helloWorld);
// Validate the byte buffer content against the concatenated string "hello" + TEST_STRING
assertEquals("hello" + TEST_STRING, b.toString());
} finally {
// Release the allocated buffer
b.releaseLast();
}
}

// 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);
}

// Validate all safe characters within the provided Wire instance
private void allSafeChars(Wire wire) {
// Obtain the singleton instance of Base85LongConverter
final Base85LongConverter converter = Base85LongConverter.INSTANCE;
// Iterate through long numbers, validating their conversion and sequencing in the Wire
for (long i = 0; i <= 85 * 85; 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,4 @@ public void end() {
}
}
}

}
22 changes: 16 additions & 6 deletions src/test/java/net/openhft/chronicle/wire/DMNestedClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,28 @@

package net.openhft.chronicle.wire;

// Class representing a nested data structure that is self-describing.
/**
* This class acts as a nested data structure
* which is self-describing, enabling simplified marshalling and unmarshalling
* while interacting with byte streams or while serialization.
*/
class DMNestedClass extends SelfDescribingMarshallable {

// String representation of the nested class.
// A string attribute to hold textual data within the instance of DMNestedClass.
String str;

// Numerical value associated with the nested class.
// An integer attribute to hold numerical data within the instance of DMNestedClass.
int num;

// Constructor to initialize the nested class with given values.
/**
* Parameterized constructor to initialize an instance of DMNestedClass
* with specified values.
*
* @param str a String, representing the textual data to be held by the instance.
* @param num an int, representing the numerical data to be held by the instance.
*/
public DMNestedClass(String str, int num) {
this.str = str;
this.num = num;
this.str = str; // Assign the provided string to the instance variable 'str'.
this.num = num; // Assign the provided integer to the instance variable 'num'.
}
}
Loading

0 comments on commit d41da0d

Please sign in to comment.