public void Increment() { var a = new MemoryGroupIndex(10, 3, 3); a += 1; Assert.Equal(new MemoryGroupIndex(10, 3, 4), a); }
public void SmallerElementIndex() { var a = new MemoryGroupIndex(10, 3, 3); var b = new MemoryGroupIndex(10, 3, 9); Assert.False(a == b); Assert.True(a != b); Assert.True(a < b); Assert.False(a > b); }
public void SmallerBufferIndex() { var a = new MemoryGroupIndex(10, 3, 3); var b = new MemoryGroupIndex(10, 5, 3); Assert.False(a == b); Assert.True(a != b); Assert.True(a < b); Assert.False(a > b); }
public void Increment_OverflowBuffer() { var a = new MemoryGroupIndex(10, 5, 3); var b = new MemoryGroupIndex(10, 5, 9); a += 8; b += 1; Assert.Equal(new MemoryGroupIndex(10, 6, 1), a); Assert.Equal(new MemoryGroupIndex(10, 6, 0), b); }
public void Equal() { var a = new MemoryGroupIndex(10, 1, 3); var b = new MemoryGroupIndex(10, 1, 3); Assert.True(a.Equals(b)); Assert.True(a == b); Assert.False(a != b); Assert.False(a < b); Assert.False(a > b); }
public void TransformInplace(int totalLength, int bufferLength) { using MemoryGroup <int> src = this.CreateTestGroup(10, 20, true); src.TransformInplace(s => MultiplyAllBy2(s, s)); int cnt = 1; for (MemoryGroupIndex i = src.MinIndex(); i < src.MaxIndex(); i += 1) { int val = src.GetElementAt(i); Assert.Equal(expected: cnt * 2, val); cnt++; } }
public void WhenSourceBufferIsShorterOrEqual(int srcTotal, int srcBufLen, int trgTotal, int trgBufLen) { using MemoryGroup <int> src = this.CreateTestGroup(srcTotal, srcBufLen, true); using MemoryGroup <int> trg = this.CreateTestGroup(trgTotal, trgBufLen, false); src.CopyTo(trg); int pos = 0; MemoryGroupIndex i = src.MinIndex(); MemoryGroupIndex j = trg.MinIndex(); for (; i < src.MaxIndex(); i += 1, j += 1, pos++) { int a = src.GetElementAt(i); int b = trg.GetElementAt(j); Assert.True(a == b, $"Mismatch @ {pos} Expected: {a} Actual: {b}"); } }
/// <summary> /// Create a group, either uninitialized or filled with incrementing numbers starting with 1. /// </summary> internal MemoryGroup <int> CreateTestGroup(long totalLength, int bufferLength, bool fillSequence = false) { this.MemoryAllocator.BufferCapacityInBytes = bufferLength * sizeof(int); var g = MemoryGroup <int> .Allocate(this.MemoryAllocator, totalLength, bufferLength); if (!fillSequence) { return(g); } int j = 1; for (MemoryGroupIndex i = g.MinIndex(); i < g.MaxIndex(); i += 1) { g.SetElementAt(i, j); j++; } return(g); }
public void SpanToGroup_Success(long totalLength, int bufferLength, int spanLength) { var src = new int[spanLength]; for (int i = 0; i < src.Length; i++) { src[i] = i + 1; } using MemoryGroup <int> trg = this.CreateTestGroup(totalLength, bufferLength); src.AsSpan().CopyTo(trg); int position = 0; for (MemoryGroupIndex i = trg.MinIndex(); position < spanLength; i += 1, position++) { int expected = position + 1; Assert.Equal(expected, trg.GetElementAt(i)); } }
public static void SetElementAt <T>(this IMemoryGroup <T> group, MemoryGroupIndex idx, T value) where T : struct { group[idx.BufferIndex].Span[idx.ElementIndex] = value; }
public static T GetElementAt <T>(this IMemoryGroup <T> group, MemoryGroupIndex idx) where T : struct { return(group[idx.BufferIndex].Span[idx.ElementIndex]); }