< Summary

Class:KernelCommunication.UnmanagedMemoryReader
Assembly:KernelCommunication.BinaryReader
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/KernelCommunication/BinaryMessage/BinaryReader/UnmanagedMemoryReader.cs
Covered lines:30
Uncovered lines:9
Coverable lines:39
Total lines:76
Line coverage:76.9% (30 of 39)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UnmanagedMemoryReader(...)0%110100%
get_offset()0%2100%
CanRead()0%110100%
ReadInt32()0%4.434070%
ReadInt64()0%4.434070%
ReadBytes(...)0%3.13077.78%
Skip(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/KernelCommunication/BinaryMessage/BinaryReader/UnmanagedMemoryReader.cs

#LineLine coverage
 1using System;
 2using System.Runtime.InteropServices;
 3
 4namespace KernelCommunication
 5{
 6    public class UnmanagedMemoryReader : IBinaryReader
 7    {
 8        private unsafe byte* ptr;
 9        private int currentOffset;
 10
 11        private readonly int dataLenght;
 12
 613        public unsafe UnmanagedMemoryReader(IntPtr intPtr, int dataLenght)
 14        {
 615            this.ptr = (byte*)intPtr.ToPointer();
 616            this.currentOffset = 0;
 617            this.dataLenght = dataLenght;
 618        }
 19
 020        int IBinaryReader.offset => currentOffset;
 21
 1622        bool IBinaryReader.CanRead() => currentOffset < dataLenght;
 23
 24        unsafe int IBinaryReader.ReadInt32()
 25        {
 3726            if (ptr == null)
 027                throw new NullReferenceException("pointer == null");
 3728            if ((long)(uint)currentOffset >= (long)dataLenght)
 029                throw new IndexOutOfRangeException("offset is bigger than data lenght");
 3730            if (currentOffset > dataLenght - 4)
 031                throw new IndexOutOfRangeException("data lenght is not large enough to contain a valid Int32");
 32
 3733            byte* numPtr = ptr;
 3734            currentOffset += 4;
 3735            ptr += 4;
 36
 3737            return ByteUtils.PointerToInt32(numPtr);
 38        }
 39
 40        unsafe long IBinaryReader.ReadInt64()
 41        {
 742            if (ptr == null)
 043                throw new NullReferenceException("pointer == null");
 744            if ((long)(uint)currentOffset >= (long)dataLenght)
 045                throw new IndexOutOfRangeException("offset is bigger than data lenght");
 746            if (currentOffset > dataLenght - 8)
 047                throw new IndexOutOfRangeException("data lenght is not large enough to contain a valid Int64");
 48
 749            byte* numPtr = ptr;
 750            currentOffset += 8;
 751            ptr += 8;
 52
 753            return ByteUtils.PointerToInt64(numPtr);
 54        }
 55
 56        unsafe byte[] IBinaryReader.ReadBytes(int length)
 57        {
 658            if (ptr == null)
 059                throw new NullReferenceException("pointer == null");
 660            if ((long)(uint)currentOffset > (long)dataLenght - length)
 061                throw new IndexOutOfRangeException($"data lenght is not large enough to read {length} bytes");
 62
 663            byte[] data = new byte[length];
 664            Marshal.Copy(new IntPtr(ptr), data, 0, length);
 665            currentOffset += length;
 666            ptr += length;
 667            return data;
 68        }
 69
 70        unsafe void IBinaryReader.Skip(int bytesCount)
 71        {
 472            currentOffset += bytesCount;
 473            ptr += bytesCount;
 474        }
 75    }
 76}