public static void OnStartsWithOfEqualSpansMakeSureEveryElementIsCompared() { for (int length = 0; length < 100; length++) { TIntLog log = new TIntLog(); TInt[] first = new TInt[length]; TInt[] second = new TInt[length]; for (int i = 0; i < length; i++) { first[i] = second[i] = new TInt(10 * (i + 1), log); } Span <TInt> firstSpan = new Span <TInt>(first); ReadOnlySpan <TInt> secondSpan = new ReadOnlySpan <TInt>(second); bool b = firstSpan.StartsWith(secondSpan); Assert.True(b); // Make sure each element of the array was compared once. (Strictly speaking, it would not be illegal for // StartsWith to compare an element more than once but that would be a non-optimal implementation and // a red flag. So we'll stick with the stricter test.) Assert.Equal(first.Length, log.Count); foreach (TInt elem in first) { int numCompares = log.CountCompares(elem.Value, elem.Value); Assert.True(numCompares == 1); } } }
public static void StartsWithNoMatch() { for (int length = 1; length < 32; length++) { for (int mismatchIndex = 0; mismatchIndex < length; mismatchIndex++) { TIntLog log = new TIntLog(); TInt[] first = new TInt[length]; TInt[] second = new TInt[length]; for (int i = 0; i < length; i++) { first[i] = second[i] = new TInt(10 * (i + 1), log); } second[mismatchIndex] = new TInt(second[mismatchIndex].Value + 1, log); Span <TInt> firstSpan = new Span <TInt>(first); ReadOnlySpan <TInt> secondSpan = new ReadOnlySpan <TInt>(second); bool b = firstSpan.StartsWith(secondSpan); Assert.False(b); Assert.Equal(1, log.CountCompares(first[mismatchIndex].Value, second[mismatchIndex].Value)); } } }
public static void SequenceCompareToSingleMismatch() { for (int length = 1; length < 32; length++) { for (int mismatchIndex = 0; mismatchIndex < length; mismatchIndex++) { TIntLog log = new TIntLog(); TInt[] first = new TInt[length]; TInt[] second = new TInt[length]; for (int i = 0; i < length; i++) { first[i] = second[i] = new TInt(10 * (i + 1), log); } second[mismatchIndex] = new TInt(second[mismatchIndex].Value + 1, log); Span <TInt> firstSpan = new Span <TInt>(first); ReadOnlySpan <TInt> secondSpan = new ReadOnlySpan <TInt>(second); int result = firstSpan.SequenceCompareTo(secondSpan); Assert.True(result < 0); Assert.Equal(1, log.CountCompares(first[mismatchIndex].Value, second[mismatchIndex].Value)); result = secondSpan.SequenceCompareTo(firstSpan); // adds to log.CountCompares Assert.True(result > 0); Assert.Equal(2, log.CountCompares(first[mismatchIndex].Value, second[mismatchIndex].Value)); } } }
public static void SequenceCompareToNoMatch() { for (int length = 1; length < 32; length++) { TIntLog log = new TIntLog(); TInt[] first = new TInt[length]; TInt[] second = new TInt[length]; for (int i = 0; i < length; i++) { first[i] = new TInt(i + 1, log); second[i] = new TInt(length + i + 1, log); } Span <TInt> firstSpan = new Span <TInt>(first); ReadOnlySpan <TInt> secondSpan = new ReadOnlySpan <TInt>(second); int result = firstSpan.SequenceCompareTo(secondSpan); Assert.True(result < 0); Assert.Equal(1, log.CountCompares(firstSpan[0].Value, secondSpan[0].Value)); result = secondSpan.SequenceCompareTo(firstSpan); // adds to log.CountCompares Assert.True(result > 0); Assert.Equal(2, log.CountCompares(firstSpan[0].Value, secondSpan[0].Value)); } }
public static void OnNoMatchMakeSureEveryElementIsCompared() { for (int length = 0; length < 100; length++) { TIntLog log = new TIntLog(); TInt[] a = new TInt[length]; for (int i = 0; i < length; i++) { a[i] = new TInt(10 * (i + 1), log); } ReadOnlySpan <TInt> span = new ReadOnlySpan <TInt>(a); int idx = span.IndexOf(new TInt(9999, log)); Assert.Equal(-1, idx); // Since we asked for a non-existent value, make sure each element of the array was compared once. // (Strictly speaking, it would not be illegal for IndexOf to compare an element more than once but // that would be a non-optimal implementation and a red flag. So we'll stick with the stricter test.) Assert.Equal(a.Length, log.Count); foreach (TInt elem in a) { int numCompares = log.CountCompares(elem.Value, 9999); Assert.True(numCompares == 1, $"Expected {numCompares} == 1 for element {elem.Value}."); } } }
public static void OnSequenceCompareToOfEqualSpansMakeSureEveryElementIsCompared() { for (int length = 0; length < 100; length++) { var log = new TIntLog(); var first = new TInt[length]; var second = new TInt[length]; for (int i = 0; i < length; i++) { first[i] = second[i] = new TInt(10 * (i + 1), log); } var firstSpan = new ReadOnlySpan <TInt>(first); var secondSpan = new ReadOnlySpan <TInt>(second); int result = firstSpan.SequenceCompareTo(secondSpan); Assert.Equal(0, result); // Make sure each element of the array was compared once. (Strictly speaking, it would not be illegal for // SequenceCompareTo to compare an element more than once but that would be a non-optimal implementation and // a red flag. So we'll stick with the stricter test.) Assert.Equal(first.Length, log.Count); foreach (TInt elem in first) { int numCompares = log.CountCompares(elem.Value, elem.Value); Assert.True(numCompares == 1, $"Expected {numCompares} == 1 for element {elem.Value}."); } } }
public static void OnSequenceEqualOfEqualSpansMakeSureEveryElementIsCompared(int mode) { for (int length = 0; length < 100; length++) { TIntLog log = new TIntLog(); TInt[] first = new TInt[length]; TInt[] second = new TInt[length]; for (int i = 0; i < length; i++) { first[i] = second[i] = new TInt(10 * (i + 1), log); } ReadOnlySpan <TInt> firstSpan = new ReadOnlySpan <TInt>(first); ReadOnlySpan <TInt> secondSpan = new ReadOnlySpan <TInt>(second); Assert.True(mode switch { 0 => firstSpan.SequenceEqual(secondSpan), 1 => firstSpan.SequenceEqual(secondSpan, null), _ => firstSpan.SequenceEqual(secondSpan, EqualityComparer <TInt> .Default) });