-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve block code coverage with more tests (#205)
- Loading branch information
Showing
9 changed files
with
242 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace nietras.SeparatedValues.Test; | ||
|
||
[TestClass] | ||
public class SepArrayPoolAccessIndexedTest | ||
{ | ||
readonly SepArrayPoolAccessIndexed _sut = new(); | ||
|
||
[TestMethod] | ||
public void SepArrayPoolAccessIndexedTest_Ctor() { } | ||
|
||
[TestMethod] | ||
public void SepArrayPoolAccessIndexedTest_RentUniqueArrayAsSpan() | ||
{ | ||
AssertUniqueSpans<int>(); | ||
AssertUniqueSpans<float>(); | ||
AssertUniqueSpans<double>(); | ||
AssertUniqueSpans<string>(); | ||
} | ||
|
||
[TestMethod] | ||
public void SepArrayPoolAccessIndexedTest_RentUniqueArrayAsSpan_Reset_RentUniqueArrayAsSpan_Same() | ||
{ | ||
var spanInt0 = _sut.RentUniqueArrayAsSpan<int>(length: 4); | ||
var spanFloat0 = _sut.RentUniqueArrayAsSpan<float>(length: 8); | ||
|
||
_sut.Reset(); | ||
|
||
var spanInt1 = _sut.RentUniqueArrayAsSpan<int>(length: 4); | ||
var spanFloat1 = _sut.RentUniqueArrayAsSpan<float>(length: 8); | ||
|
||
AssertRef(spanInt0, spanInt1, areSame: true); | ||
AssertRef(spanFloat0, spanFloat1, areSame: true); | ||
} | ||
|
||
[TestMethod] | ||
public void SepArrayPoolAccessIndexedTest_RentUniqueArrayAsSpan_Reset_RentUniqueArrayAsSpan_LargerNotSame() | ||
{ | ||
var spanInt0 = _sut.RentUniqueArrayAsSpan<int>(length: 4); | ||
|
||
_sut.Reset(); | ||
|
||
var spanInt1 = _sut.RentUniqueArrayAsSpan<int>(length: 128); | ||
|
||
Assert.AreEqual(128, spanInt1.Length); | ||
AssertRef(spanInt0, spanInt1, areSame: false); | ||
} | ||
|
||
[TestMethod] | ||
public void SepArrayPoolAccessIndexedTest_RentUniqueArrayAsSpan_Dispose() | ||
{ | ||
SepArrayPoolAccessIndexedTest_RentUniqueArrayAsSpan(); | ||
|
||
_sut.Dispose(); | ||
} | ||
|
||
void AssertUniqueSpans<T>() | ||
{ | ||
var span4_0 = _sut.RentUniqueArrayAsSpan<T>(length: 4); | ||
var span4_1 = _sut.RentUniqueArrayAsSpan<T>(length: 4); | ||
var span8_0 = _sut.RentUniqueArrayAsSpan<T>(length: 8); | ||
var span8_1 = _sut.RentUniqueArrayAsSpan<T>(length: 8); | ||
|
||
Assert.AreEqual(4, span4_0.Length); | ||
Assert.AreEqual(4, span4_1.Length); | ||
Assert.AreEqual(8, span8_0.Length); | ||
Assert.AreEqual(8, span8_1.Length); | ||
|
||
AssertRef(span4_0, span4_1, areSame: false); | ||
AssertRef(span4_0, span8_0, areSame: false); | ||
AssertRef(span8_0, span8_1, areSame: false); | ||
} | ||
|
||
static void AssertRef<T>(Span<T> a, Span<T> b, bool areSame) | ||
{ | ||
ref var refA = ref MemoryMarshal.GetReference(a); | ||
ref var refB = ref MemoryMarshal.GetReference(b); | ||
Assert.AreEqual(areSame, Unsafe.AreSame(ref refA, ref refB)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace nietras.SeparatedValues.Test; | ||
|
||
[TestClass] | ||
public class SepReaderExtensionsEnumerationTest | ||
{ | ||
const int PartRowCount = 1000; | ||
const int TotalRowCount = 2 * PartRowCount; | ||
const string ColNameInc = "Inc"; | ||
const string ColNameFill = "Fill"; | ||
const string ColNameDec = "Dec"; | ||
const string ShortRowFill = "A"; | ||
static readonly string s_longRowFill = new('B', 256); | ||
static readonly List<Seq> s_expected = Enumerable.Range(0, TotalRowCount) | ||
.Select(i => new Seq(i, TotalRowCount - i)).ToList(); | ||
|
||
record struct Seq(int Inc, int Dec); | ||
|
||
[TestMethod] | ||
public void SepReaderExtensionsEnumerationTest_Enumerate_RowFunc() | ||
{ | ||
using var reader = CreateReader(); | ||
var actual = reader.Enumerate(Parse).ToList(); | ||
CollectionAssert.AreEqual(s_expected, actual); | ||
} | ||
|
||
[TestMethod] | ||
public void SepReaderExtensionsEnumerationTest_Enumerate_RowTryFunc() | ||
{ | ||
using var reader = CreateReader(); | ||
var actual = reader.Enumerate<Seq>(TryParseEven).ToList(); | ||
var expected = s_expected.Where(s => s.Inc % 2 == 0).ToList(); | ||
CollectionAssert.AreEqual(expected, actual); | ||
} | ||
|
||
[TestMethod] | ||
public void SepReaderExtensionsEnumerationTest_ParallelEnumerate_RowFunc() | ||
{ | ||
using var reader = CreateReader(); | ||
var actual = reader.ParallelEnumerate(Parse).ToList(); | ||
CollectionAssert.AreEqual(s_expected, actual); | ||
} | ||
|
||
[TestMethod] | ||
public void SepReaderExtensionsEnumerationTest_ParallelEnumerate_RowTryFunc() | ||
{ | ||
using var reader = CreateReader(); | ||
var actual = reader.ParallelEnumerate<Seq>(TryParseEven).ToList(); | ||
var expected = s_expected.Where(s => s.Inc % 2 == 0).ToList(); | ||
CollectionAssert.AreEqual(expected, actual); | ||
} | ||
|
||
static SepReader CreateReader() | ||
{ | ||
var sb = new StringBuilder(1024 * 1024); | ||
using var stringWriter = new StringWriter(sb); | ||
using (var writer = Sep.Writer().To(stringWriter)) | ||
{ | ||
foreach (var (inc, dec) in s_expected) | ||
{ | ||
var fill = inc < PartRowCount ? ShortRowFill : s_longRowFill; | ||
using var row = writer.NewRow(); | ||
row[ColNameInc].Format(inc); | ||
row[ColNameFill].Set(fill); | ||
row[ColNameDec].Format(dec); | ||
} | ||
} | ||
var csv = sb.ToString(); | ||
// Force small initial buffer length even for Release, to force reader | ||
// state swapping and array swapping with increasing row length for | ||
// ParallelEnumerate. | ||
return Sep.Reader(o => o with { InitialBufferLength = 128 }).FromText(csv); | ||
} | ||
|
||
static bool TryParseEven(SepReader.Row row, out Seq seq) | ||
{ | ||
seq = Parse(row); | ||
return seq.Inc % 2 == 0; | ||
} | ||
|
||
static Seq Parse(SepReader.Row row) => | ||
new(row[ColNameInc].Parse<int>(), row[ColNameDec].Parse<int>()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters