Skip to content

Commit

Permalink
Implement nullable long encoding/decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret committed Mar 22, 2024
1 parent 6645d4e commit 596ad2a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/ArtemisNetCoreClient/ByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,28 @@ public long ReadLong()
_ = _memoryStream.Read(buffer);
return BinaryPrimitives.ReadInt64BigEndian(buffer);
}

public void WriteNullableLong(long? value)
{
WriteBool(value.HasValue);
if (value.HasValue)
{
WriteLong(value.Value);
}
}

public long? ReadNullableLong()
{
var isNotNull = ReadBool();
if (isNotNull)
{
return ReadLong();
}
else
{
return null;
}
}

public void WriteNullableString(string? value)
{
Expand Down
28 changes: 28 additions & 0 deletions test/ArtemisNetCoreClient.Tests/ByteBufferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,34 @@ public void should_decode_long()
Assert.That(value, Is.EqualTo(long.MaxValue));
}

[TestCase(280, new byte[] { unchecked((byte)-1), 0, 0, 0, 0, 0, 0, 1, 24 })]
[TestCase(null, new byte[] { 0 })]
public void should_encode_nullable_long(long? value, byte[] encoded)
{
// Arrange
var byteBuffer = new ByteBuffer();

// Act
byteBuffer.WriteNullableLong(value);

// Assert
CollectionAssert.AreEqual(encoded, byteBuffer.GetBuffer().ToArray());
}

[TestCase(new byte[] { unchecked((byte)-1), 0, 0, 0, 0, 0, 0, 1, 24 }, 280)]
[TestCase(new byte[] { 0 }, null)]
public void should_decode_nullable_long(byte[] encoded, long? expected)
{
// Arrange
var byteBuffer = new ByteBuffer(encoded);

// Act
var value = byteBuffer.ReadNullableLong();

// Assert
Assert.That(expected, Is.EqualTo(value));
}

[Test]
public void should_encode_short_string()
{
Expand Down

0 comments on commit 596ad2a

Please sign in to comment.