< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Serialization.ValueObjects.BigSizeTypeSerializer
Assembly: NLightning.Infrastructure.Serialization
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Serialization/ValueObjects/BigSizeTypeSerializer.cs
Tag: 30_15166811759
Line coverage
92%
Covered lines: 36
Uncovered lines: 3
Coverable lines: 39
Total lines: 111
Line coverage: 92.3%
Branch coverage
95%
Covered branches: 21
Total branches: 22
Branch coverage: 95.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SerializeAsync()87.5%8.03892.31%
DeserializeAsync()100%1414100%
NLightning-Domain-Serialization-ValueObjects-IValueObjectTypeSerializer-DeserializeAsync()100%210%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Serialization/ValueObjects/BigSizeTypeSerializer.cs

#LineLine coverage
 1using System.Buffers;
 2
 3namespace NLightning.Infrastructure.Serialization.ValueObjects;
 4
 5using Converters;
 6using Domain.Serialization.ValueObjects;
 7using Domain.ValueObjects;
 8using Domain.ValueObjects.Interfaces;
 9
 10public class BigSizeTypeSerializer : IValueObjectTypeSerializer<BigSize>
 11{
 12    /// <summary>
 13    /// Serializes a BigSize value into a stream.
 14    /// </summary>
 15    /// <param name="valueObject">The BigSize value to serialize.</param>
 16    /// <param name="stream">The stream where the serialized value will be written.</param>
 17    /// <returns>A task that represents the asynchronous serialization operation.</returns>
 18    /// <exception cref="ArgumentNullException">Thrown when the stream is null.</exception>
 19    /// <exception cref="IOException">Thrown when an I/O error occurs during the write operation.</exception>
 20    public async Task SerializeAsync(IValueObject valueObject, Stream stream)
 21    {
 17622        if (valueObject is not BigSize bigSize)
 023            throw new ArgumentException("Value object must be of type BigSize.", nameof(valueObject));
 24
 17625        if (bigSize < 0xfd)
 26        {
 15227            await stream.WriteAsync(new[] { (byte)bigSize });
 28        }
 2429        else if (bigSize < 0x10000)
 30        {
 831            await stream.WriteAsync(new byte[] { 0xfd });
 832            await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian((ushort)bigSize));
 33        }
 1634        else if (bigSize < 0x100000000)
 35        {
 836            await stream.WriteAsync(new byte[] { 0xfe });
 837            await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian((uint)bigSize));
 38        }
 39        else
 40        {
 841            await stream.WriteAsync(new byte[] { 0xff });
 842            await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian(bigSize.Value));
 43        }
 17644    }
 45
 46    /// <summary>
 47    /// Deserializes a BigSize value from a stream.
 48    /// </summary>
 49    /// <param name="stream">The stream from which the BigSize value will be deserialized.</param>
 50    /// <returns>A task that represents the asynchronous deserialization operation, containing the deserialized BigSize 
 51    /// <exception cref="ArgumentException">Thrown when the stream is empty or contains insufficient data for deserializ
 52    /// <exception cref="IOException">Thrown when an I/O error occurs during the read operation.</exception>
 53    public async Task<BigSize> DeserializeAsync(Stream stream)
 54    {
 28455        if (stream.Position == stream.Length)
 856            throw new ArgumentException("BigSize cannot be read from an empty stream.");
 57
 27658        var buffer = ArrayPool<byte>.Shared.Rent(sizeof(ulong));
 59
 60        try
 61        {
 27662            await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(byte)]);
 63            ulong value;
 64
 27665            switch (buffer[0])
 66            {
 67                case < 0xfd:
 22868                    value = buffer[0];
 22869                    break;
 70                // Check if there are enough bytes to read
 1671                case 0xfd when stream.Position + 2 > stream.Length:
 872                    throw new ArgumentException("BigSize cannot be read from a stream with insufficient data.");
 73                case 0xfd:
 74                    {
 875                        await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(ushort)]);
 876                        value = EndianBitConverter.ToUInt16BigEndian(buffer[..sizeof(ushort)]);
 877                        break;
 78                    }
 1679                case 0xfe when stream.Position + 4 > stream.Length:
 880                    throw new ArgumentException("BigSize cannot be read from a stream with insufficient data.");
 81                case 0xfe:
 82                    {
 883                        await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(uint)]);
 884                        value = EndianBitConverter.ToUInt32BigEndian(buffer[..sizeof(uint)]);
 885                        break;
 86                    }
 87                default:
 88                    {
 1689                        if (stream.Position + 8 > stream.Length)
 90                        {
 891                            throw new ArgumentException("BigSize cannot be read from a stream with insufficient data.");
 92                        }
 93
 894                        await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(ulong)]);
 895                        value = EndianBitConverter.ToUInt64BigEndian(buffer[..sizeof(ulong)]);
 96                        break;
 97                    }
 98            }
 99
 252100            return new BigSize(value);
 101        }
 102        finally
 103        {
 276104            ArrayPool<byte>.Shared.Return(buffer);
 105        }
 252106    }
 107    async Task<IValueObject> IValueObjectTypeSerializer.DeserializeAsync(Stream stream)
 108    {
 0109        return await DeserializeAsync(stream);
 0110    }
 111}