From 1822e75fd883a0d7ac1c25ea76bf3296fc8ebc37 Mon Sep 17 00:00:00 2001 From: Alexander Luzgarev Date: Sat, 12 Apr 2025 09:07:16 +0200 Subject: [PATCH] Make tests less oopy --- .editorconfig | 2 + .../AbstractTestDataFactory.cs | 68 --------- .../ChecksumCalculatingStreamTests.cs | 62 ++++---- .../ExtensionTestFilenameConvention.cs | 43 ------ .../ITestFilenameConvention.cs | 24 --- MatFileHandler.Tests/MatFileReaderTests.cs | 144 ++++++++++-------- MatFileHandler.Tests/MatFileReadingMethod.cs | 29 ++++ MatFileHandler.Tests/MatFileReadingMethods.cs | 38 +++++ .../MatFileWriterOptionsForTests.cs | 18 +++ MatFileHandler.Tests/MatFileWriterTests.cs | 26 ++-- MatFileHandler.Tests/MatFileWritingMethod.cs | 33 +++- MatFileHandler.Tests/MatTestDataFactory.cs | 35 ----- .../PartialReadMatTestDataFactory.cs | 35 ----- .../UnalignedMatTestDataFactory.cs | 37 ----- 14 files changed, 236 insertions(+), 358 deletions(-) delete mode 100755 MatFileHandler.Tests/AbstractTestDataFactory.cs delete mode 100755 MatFileHandler.Tests/ExtensionTestFilenameConvention.cs delete mode 100755 MatFileHandler.Tests/ITestFilenameConvention.cs create mode 100644 MatFileHandler.Tests/MatFileReadingMethod.cs create mode 100644 MatFileHandler.Tests/MatFileReadingMethods.cs delete mode 100755 MatFileHandler.Tests/MatTestDataFactory.cs delete mode 100644 MatFileHandler.Tests/PartialReadMatTestDataFactory.cs delete mode 100644 MatFileHandler.Tests/UnalignedMatTestDataFactory.cs diff --git a/.editorconfig b/.editorconfig index 63e0c7a..b8470e6 100644 --- a/.editorconfig +++ b/.editorconfig @@ -18,6 +18,8 @@ dotnet_analyzer_diagnostic.category-Usage.severity = warning dotnet_diagnostic.IDE0010.severity = none dotnet_diagnostic.IDE0072.severity = none +dotnet_diagnostic.CA1707.severity = none +dotnet_diagnostic.CA1861.severity = none #### Core EditorConfig Options #### diff --git a/MatFileHandler.Tests/AbstractTestDataFactory.cs b/MatFileHandler.Tests/AbstractTestDataFactory.cs deleted file mode 100755 index a7717d1..0000000 --- a/MatFileHandler.Tests/AbstractTestDataFactory.cs +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright 2017-2018 Alexander Luzgarev - -using System.Collections.Generic; -using System.IO; -using System.Linq; - -namespace MatFileHandler.Tests -{ - /// - /// Abstract factory of test data. - /// - /// Type of test data. - public abstract class AbstractTestDataFactory - { - /// - /// Initializes a new instance of the class. - /// - /// Directory with test files. - /// A convention used to filter test files. - protected AbstractTestDataFactory(string dataDirectory, ITestFilenameConvention testFilenameConvention) - { - DataDirectory = dataDirectory; - TestFilenameConvention = testFilenameConvention; - } - - private string DataDirectory { get; } - - private ITestFilenameConvention TestFilenameConvention { get; } - - /// - /// Get test data set by name. - /// - /// Name of the data set. - /// Test data. - public TTestData this[string dataSet] => - ReadTestData(FixPath(TestFilenameConvention.ConvertTestNameToFilename(dataSet))); - - /// - /// Get a sequence of all test data sets in the factory. - /// - /// A sequence of data sets. - public IEnumerable GetAllTestData() - { - var files = Directory.EnumerateFiles(DataDirectory).Where(TestFilenameConvention.FilterFile); - foreach (var filename in files) - { - yield return ReadTestData(filename); - } - } - - /// - /// Read test data from a stream. - /// - /// Input stream. - /// Test data. - protected abstract TTestData ReadDataFromStream(Stream stream); - - private string FixPath(string filename) => Path.Combine(DataDirectory, filename); - - private TTestData ReadTestData(string filename) - { - using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) - { - return ReadDataFromStream(stream); - } - } - } -} \ No newline at end of file diff --git a/MatFileHandler.Tests/ChecksumCalculatingStreamTests.cs b/MatFileHandler.Tests/ChecksumCalculatingStreamTests.cs index cdf86ab..65d15bb 100644 --- a/MatFileHandler.Tests/ChecksumCalculatingStreamTests.cs +++ b/MatFileHandler.Tests/ChecksumCalculatingStreamTests.cs @@ -17,50 +17,48 @@ namespace MatFileHandler.Tests /// /// Test writing various things. /// - /// + /// Bytes to write. [Theory] [MemberData(nameof(TestData))] - public void Test(Action action) + public void Test(byte[] bytes) { using var stream = new MemoryStream(); var sut = new ChecksumCalculatingStream(stream); - action(sut); + sut.Write(bytes, 0, bytes.Length); var actual = sut.GetCrc(); - var expected = ReferenceCalculation(action); + + var expected = ReferenceCalculation(bytes); } /// /// Test data for . /// /// Test data. - public static IEnumerable TestData() + public static TheoryData TestData() { - foreach (var data in TestData_Typed()) + var empty = new byte[1234]; + var nonEmpty = new byte[12345]; + for (var i = 0; i < 1234; i++) { - yield return new object[] { data }; + nonEmpty[i] = (byte)((i * i) % 256); } - } - - private static IEnumerable> TestData_Typed() - { - yield return BinaryWriterAction(w => w.Write(true)); - yield return BinaryWriterAction(w => w.Write(false)); - yield return BinaryWriterAction(w => w.Write(byte.MinValue)); - yield return BinaryWriterAction(w => w.Write(byte.MaxValue)); - yield return BinaryWriterAction(w => w.Write(short.MinValue)); - yield return BinaryWriterAction(w => w.Write(short.MaxValue)); - yield return BinaryWriterAction(w => w.Write(int.MinValue)); - yield return BinaryWriterAction(w => w.Write(int.MaxValue)); - yield return BinaryWriterAction(w => w.Write(long.MinValue)); - yield return BinaryWriterAction(w => w.Write(long.MaxValue)); - yield return BinaryWriterAction(w => w.Write(decimal.MinValue)); - yield return BinaryWriterAction(w => w.Write(decimal.MaxValue)); - yield return BinaryWriterAction(w => w.Write(double.MinValue)); - yield return BinaryWriterAction(w => w.Write(double.MaxValue)); - yield return BinaryWriterAction(w => w.Write(double.PositiveInfinity)); - yield return BinaryWriterAction(w => w.Write(double.NaN)); - yield return BinaryWriterAction(w => w.Write(new byte[] { 1, 2, 3, 4, 5, 6, 7 })); - yield return BinaryWriterAction(w => w.Write(Enumerable.Range(0, 255).SelectMany(x => Enumerable.Range(0, 255)).Select(x => (byte)x).ToArray())); + return new TheoryData() + { + new byte[] { 0x00 }, + new byte[] { 0x01 }, + new byte[] { 0xff }, + new byte[] { 0xff, 0xff }, + new byte[] { 0xff, 0xff, 0xff }, + new byte[] { 0xff, 0xff, 0xff, 0xff }, + new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff }, + new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, + new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, + new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, + new byte[] { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, + new byte[] { 0x02, 0x03, 0x05, 0x07, 0x0b, 0x0d, 0x11, 0x13, 0x17, 0x1d }, + empty, + nonEmpty, + }; } private static Action BinaryWriterAction(Action action) @@ -72,15 +70,15 @@ namespace MatFileHandler.Tests }; } - private uint ReferenceCalculation(Action action) + private static uint ReferenceCalculation(byte[] bytes) { using var stream = new MemoryStream(); - action(stream); + stream.Write(bytes, 0, bytes.Length); stream.Position = 0; return CalculateAdler32Checksum(stream); } - private static uint CalculateAdler32Checksum(Stream stream) + private static uint CalculateAdler32Checksum(MemoryStream stream) { uint s1 = 1; uint s2 = 0; diff --git a/MatFileHandler.Tests/ExtensionTestFilenameConvention.cs b/MatFileHandler.Tests/ExtensionTestFilenameConvention.cs deleted file mode 100755 index 4ec2591..0000000 --- a/MatFileHandler.Tests/ExtensionTestFilenameConvention.cs +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2017-2018 Alexander Luzgarev - -using System.IO; - -namespace MatFileHandler.Tests -{ - /// - /// A filename convention based on file extensions. - /// - internal sealed class ExtensionTestFilenameConvention : ITestFilenameConvention - { - /// - /// Initializes a new instance of the class. - /// - /// File extension. - public ExtensionTestFilenameConvention(string extension) - { - Extension = extension; - } - - private string Extension { get; } - - /// - /// Convert test name to filename by adding the extension. - /// - /// Test name. - /// The corresponding filename. - public string ConvertTestNameToFilename(string testName) - { - return Path.ChangeExtension(testName, Extension); - } - - /// - /// Compare file's extension to the one specified during initialization. - /// - /// Filename. - /// True iff the file has the extension stored in the class. - public bool FilterFile(string filename) - { - return Path.GetExtension(filename) == "." + Extension; - } - } -} diff --git a/MatFileHandler.Tests/ITestFilenameConvention.cs b/MatFileHandler.Tests/ITestFilenameConvention.cs deleted file mode 100755 index 5f31cbe..0000000 --- a/MatFileHandler.Tests/ITestFilenameConvention.cs +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2017-2018 Alexander Luzgarev - -namespace MatFileHandler.Tests -{ - /// - /// Interface for handling filtering tests based on filenames. - /// - public interface ITestFilenameConvention - { - /// - /// Convert test name to a filename (e.g., by adding an appropriate extension). - /// - /// Name of a test. - /// Filename. - string ConvertTestNameToFilename(string testName); - - /// - /// Decide if a file contains a test based on the filename. - /// - /// A filename. - /// True iff the file should contain a test. - bool FilterFile(string filename); - } -} \ No newline at end of file diff --git a/MatFileHandler.Tests/MatFileReaderTests.cs b/MatFileHandler.Tests/MatFileReaderTests.cs index 91d597c..bf48b86 100755 --- a/MatFileHandler.Tests/MatFileReaderTests.cs +++ b/MatFileHandler.Tests/MatFileReaderTests.cs @@ -1,6 +1,7 @@ // Copyright 2017-2018 Alexander Luzgarev using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Numerics; @@ -19,9 +20,9 @@ namespace MatFileHandler.Tests /// Test reading all files in a given test set. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestReader(AbstractTestDataFactory testFactory) + public void TestReader(MatFileReadingMethod method) { - foreach (var matFile in testFactory.GetAllTestData()) + foreach (var matFile in ReadAllTestFiles(method)) { Assert.NotEmpty(matFile.Variables); } @@ -31,9 +32,9 @@ namespace MatFileHandler.Tests /// Test reading lower and upper limits of integer data types. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestLimits(AbstractTestDataFactory testFactory) + public void TestLimits(MatFileReadingMethod method) { - var matFile = testFactory["limits"]; + var matFile = ReadTestFile("limits", method); IArray array; array = matFile["int8_"].Value; CheckLimits(array as IArrayOf, CommonData.Int8Limits); @@ -65,9 +66,9 @@ namespace MatFileHandler.Tests /// Test writing lower and upper limits of integer-based complex data types. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestComplexLimits(AbstractTestDataFactory testFactory) + public void TestComplexLimits(MatFileReadingMethod method) { - var matFile = testFactory["limits_complex"]; + var matFile = ReadTestFile("limits_complex", method); IArray array; array = matFile["int8_complex"].Value; CheckComplexLimits(array as IArrayOf>, CommonData.Int8Limits); @@ -101,9 +102,9 @@ namespace MatFileHandler.Tests /// Test reading an ASCII-encoded string. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestAscii(AbstractTestDataFactory testFactory) + public void TestAscii(MatFileReadingMethod method) { - var matFile = testFactory["ascii"]; + var matFile = ReadTestFile("ascii", method); var arrayAscii = matFile["s"].Value as ICharArray; Assert.NotNull(arrayAscii); Assert.Equal(new[] { 1, 3 }, arrayAscii.Dimensions); @@ -115,9 +116,9 @@ namespace MatFileHandler.Tests /// Test reading a Unicode string. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestUnicode(AbstractTestDataFactory testFactory) + public void TestUnicode(MatFileReadingMethod method) { - var matFile = testFactory["unicode"]; + var matFile = ReadTestFile("unicode", method); var arrayUnicode = matFile["s"].Value as ICharArray; Assert.NotNull(arrayUnicode); Assert.Equal(new[] { 1, 2 }, arrayUnicode.Dimensions); @@ -130,9 +131,9 @@ namespace MatFileHandler.Tests /// Test reading a wide Unicode string. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestUnicodeWide(AbstractTestDataFactory testFactory) + public void TestUnicodeWide(MatFileReadingMethod method) { - var matFile = testFactory["unicode-wide"]; + var matFile = ReadTestFile("unicode-wide", method); var arrayUnicodeWide = matFile["s"].Value as ICharArray; Assert.NotNull(arrayUnicodeWide); Assert.Equal(new[] { 1, 2 }, arrayUnicodeWide.Dimensions); @@ -143,9 +144,9 @@ namespace MatFileHandler.Tests /// Test converting a structure array to a Double array. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestConvertToDoubleArray(AbstractTestDataFactory testFactory) + public void TestConvertToDoubleArray(MatFileReadingMethod method) { - var matFile = testFactory["struct"]; + var matFile = ReadTestFile("struct", method); var array = matFile.Variables[0].Value; Assert.Null(array.ConvertToDoubleArray()); } @@ -155,9 +156,9 @@ namespace MatFileHandler.Tests /// /// Should return null. [Theory, MemberData(nameof(TestDataFactories))] - public void TestConvertToComplexArray(AbstractTestDataFactory testFactory) + public void TestConvertToComplexArray(MatFileReadingMethod method) { - var matFile = testFactory["struct"]; + var matFile = ReadTestFile("struct", method); var array = matFile.Variables[0].Value; Assert.Null(array.ConvertToComplexArray()); } @@ -166,9 +167,9 @@ namespace MatFileHandler.Tests /// Test reading an enumeration. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestEnum(AbstractTestDataFactory testFactory) + public void TestEnum(MatFileReadingMethod method) { - var matFile = testFactory["enum"]; + var matFile = ReadTestFile("enum", method); var days = matFile["days"].Value; var enumeration = new EnumAdapter(days); Assert.Equal(5, enumeration.Values.Count); @@ -183,9 +184,9 @@ namespace MatFileHandler.Tests /// Test reading a structure array. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestStruct(AbstractTestDataFactory testFactory) + public void TestStruct(MatFileReadingMethod method) { - var matFile = testFactory["struct"]; + var matFile = ReadTestFile("struct", method); var structure = matFile["struct_"].Value as IStructureArray; Assert.NotNull(structure); Assert.Equal(new[] { "x", "y" }, structure.FieldNames); @@ -237,9 +238,9 @@ namespace MatFileHandler.Tests /// Test reading a sparse array. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestSparse(AbstractTestDataFactory testFactory) + public void TestSparse(MatFileReadingMethod method) { - var matFile = testFactory["sparse"]; + var matFile = ReadTestFile("sparse", method); var sparseArray = matFile["sparse_"].Value as ISparseArrayOf; Assert.NotNull(sparseArray); Assert.Equal(new[] { 4, 5 }, sparseArray.Dimensions); @@ -267,9 +268,9 @@ namespace MatFileHandler.Tests /// Test reading a logical array. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestLogical(AbstractTestDataFactory testFactory) + public void TestLogical(MatFileReadingMethod method) { - var matFile = testFactory["logical"]; + var matFile = ReadTestFile("logical", method); var array = matFile["logical_"].Value; var logicalArray = array as IArrayOf; Assert.NotNull(logicalArray); @@ -285,9 +286,9 @@ namespace MatFileHandler.Tests /// Test reading a sparse logical array. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestSparseLogical(AbstractTestDataFactory testFactory) + public void TestSparseLogical(MatFileReadingMethod method) { - var matFile = testFactory["sparse_logical"]; + var matFile = ReadTestFile("sparse_logical", method); var array = matFile["sparse_logical"].Value; var sparseArray = array as ISparseArrayOf; Assert.NotNull (sparseArray); @@ -304,9 +305,9 @@ namespace MatFileHandler.Tests /// Test reading a global variable. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestGlobal(AbstractTestDataFactory testFactory) + public void TestGlobal(MatFileReadingMethod method) { - var matFile = testFactory["global"]; + var matFile = ReadTestFile("global", method); var variable = matFile.Variables.First(); Assert.True(variable.IsGlobal); } @@ -315,9 +316,9 @@ namespace MatFileHandler.Tests /// Test reading a sparse complex array. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TextSparseComplex(AbstractTestDataFactory testFactory) + public void TextSparseComplex(MatFileReadingMethod method) { - var matFile = testFactory["sparse_complex"]; + var matFile = ReadTestFile("sparse_complex", method); var array = matFile["sparse_complex"].Value; var sparseArray = array as ISparseArrayOf; Assert.NotNull(sparseArray); @@ -331,9 +332,9 @@ namespace MatFileHandler.Tests /// Test reading an object. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestObject(AbstractTestDataFactory testFactory) + public void TestObject(MatFileReadingMethod method) { - var matFile = testFactory["object"]; + var matFile = ReadTestFile("object", method); var obj = matFile["object_"].Value as IMatObject; Assert.NotNull(obj); Assert.Equal("Point", obj.ClassName); @@ -348,9 +349,9 @@ namespace MatFileHandler.Tests /// Test reading another object. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestObject2(AbstractTestDataFactory testFactory) + public void TestObject2(MatFileReadingMethod method) { - var matFile = testFactory["object2"]; + var matFile = ReadTestFile("object2", method); var obj = matFile["object2"].Value as IMatObject; Assert.NotNull(obj); Assert.Equal("Point", obj.ClassName); @@ -371,9 +372,9 @@ namespace MatFileHandler.Tests /// Test reading a table. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestTable(AbstractTestDataFactory testFactory) + public void TestTable(MatFileReadingMethod method) { - var matFile = testFactory["table"]; + var matFile = ReadTestFile("table", method); var obj = matFile["table_"].Value as IMatObject; var table = new TableAdapter(obj); Assert.Equal(3, table.NumberOfRows); @@ -392,9 +393,9 @@ namespace MatFileHandler.Tests /// Test reading a deeply nested table. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestDeepTable(AbstractTestDataFactory testFactory) + public void TestDeepTable(MatFileReadingMethod method) { - var matFile = testFactory["table-deep"]; + var matFile = ReadTestFile("table-deep", method); var obj = matFile["t"].Value as IMatObject; var table = new TableAdapter(obj); Assert.Equal(1, table.NumberOfRows); @@ -416,9 +417,9 @@ namespace MatFileHandler.Tests /// Test reading a table with strings /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestTableWithStrings(AbstractTestDataFactory testFactory) + public void TestTableWithStrings(MatFileReadingMethod method) { - var matFile = testFactory["table-with-strings"]; + var matFile = ReadTestFile("table-with-strings", method); var obj = matFile["t"].Value as IMatObject; var table = new TableAdapter(obj); Assert.Equal(5, table.NumberOfRows); @@ -441,9 +442,9 @@ namespace MatFileHandler.Tests /// Test subobjects within objects. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestSubobjects(AbstractTestDataFactory testFactory) + public void TestSubobjects(MatFileReadingMethod method) { - var matFile = testFactory["pointWithSubpoints"]; + var matFile = ReadTestFile("pointWithSubpoints", method); var p = matFile["p"].Value as IMatObject; Assert.Equal("Point", p.ClassName); var x = p["x"] as IMatObject; @@ -464,9 +465,9 @@ namespace MatFileHandler.Tests /// Test nested objects. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestNestedObjects(AbstractTestDataFactory testFactory) + public void TestNestedObjects(MatFileReadingMethod method) { - var matFile = testFactory["subsubPoint"]; + var matFile = ReadTestFile("subsubPoint", method); var p = matFile["p"].Value as IMatObject; Assert.Equal("Point", p.ClassName); Assert.Equal(new[] { 1.0 }, p["x"].ConvertToDoubleArray()); @@ -482,9 +483,9 @@ namespace MatFileHandler.Tests /// Test datetime objects. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestDatetime(AbstractTestDataFactory testFactory) + public void TestDatetime(MatFileReadingMethod method) { - var matFile = testFactory["datetime"]; + var matFile = ReadTestFile("datetime", method); var d = matFile["d"].Value as IMatObject; var datetime = new DatetimeAdapter(d); Assert.Equal(new[] { 1, 2 }, datetime.Dimensions); @@ -496,9 +497,9 @@ namespace MatFileHandler.Tests /// Another test for datetime objects. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestDatetime2(AbstractTestDataFactory testFactory) + public void TestDatetime2(MatFileReadingMethod method) { - var matFile = testFactory["datetime2"]; + var matFile = ReadTestFile("datetime2", method); var d = matFile["d"].Value as IMatObject; var datetime = new DatetimeAdapter(d); Assert.Equal(new[] { 1, 1 }, datetime.Dimensions); @@ -511,9 +512,9 @@ namespace MatFileHandler.Tests /// Test string objects. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestString(AbstractTestDataFactory testFactory) + public void TestString(MatFileReadingMethod method) { - var matFile = testFactory["string"]; + var matFile = ReadTestFile("string", method); var s = matFile["s"].Value as IMatObject; var str = new StringAdapter(s); Assert.Equal(new[] { 4, 1 }, str.Dimensions); @@ -527,9 +528,9 @@ namespace MatFileHandler.Tests /// Test duration objects. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestDuration(AbstractTestDataFactory testFactory) + public void TestDuration(MatFileReadingMethod method) { - var matFile = testFactory["duration"]; + var matFile = ReadTestFile("duration", method); var d = matFile["d"].Value as IMatObject; var duration = new DurationAdapter(d); Assert.Equal(new[] { 1, 3 }, duration.Dimensions); @@ -542,9 +543,9 @@ namespace MatFileHandler.Tests /// Test unrepresentable datetime. /// [Theory, MemberData(nameof(TestDataFactories))] - public void TestDatetime_Unrepresentable(AbstractTestDataFactory testFactory) + public void TestDatetime_Unrepresentable(MatFileReadingMethod method) { - var matFile = testFactory["datetime-unrepresentable"]; + var matFile = ReadTestFile("datetime-unrepresentable", method); var obj = matFile["d"].Value as IMatObject; var datetime = new DatetimeAdapter(obj); var d0 = datetime[0]; @@ -555,9 +556,9 @@ namespace MatFileHandler.Tests /// Test 3-dimensional arrays. /// [Theory, MemberData(nameof(TestDataFactories))] - public void Test_3DArrays(AbstractTestDataFactory testFactory) + public void Test_3DArrays(MatFileReadingMethod method) { - var matFile = testFactory["issue20.mat"]; + var matFile = ReadTestFile("issue20", method); var obj = matFile["a3d"].Value; var values = obj.ConvertToDoubleArray(); Assert.Equal(Enumerable.Range(1, 24).Select(x => (double)x).ToArray(), values); @@ -590,9 +591,9 @@ namespace MatFileHandler.Tests /// Test four-dimensional arrays. /// [Theory, MemberData(nameof(TestDataFactories))] - public void Test4DArrays(AbstractTestDataFactory testFactory) + public void Test4DArrays(MatFileReadingMethod method) { - var matFile = testFactory["issue20.mat"]; + var matFile = ReadTestFile("issue20", method); var obj = matFile["a4d"].Value; Assert.Equal(Enumerable.Range(1, 120).Select(x => (double)x).ToArray(), obj.ConvertToDoubleArray()); Assert.Null(obj.ConvertTo2dDoubleArray()); @@ -601,19 +602,36 @@ namespace MatFileHandler.Tests /// /// Returns the factories that provide test data in various configurations. /// - public static TheoryData> TestDataFactories + public static TheoryData TestDataFactories { get { - return new TheoryData> + return new TheoryData { - new MatTestDataFactory(Path.Combine(TestDirectory, "good")), - new PartialReadMatTestDataFactory(Path.Combine(TestDirectory, "good")), - new UnalignedMatTestDataFactory(Path.Combine(TestDirectory, "good")), + MatFileReadingMethod.NormalStream, + MatFileReadingMethod.PartialStream, + MatFileReadingMethod.UnalignedStream, }; } } + private static IMatFile ReadTestFile(string fileName, MatFileReadingMethod method) + { + var fullFileName = Path.Combine("test-data", "good", $"{fileName}.mat"); + return MatFileReadingMethods.ReadMatFile(method, fullFileName); + } + + private static IEnumerable ReadAllTestFiles(MatFileReadingMethod method) + { + foreach (var fileName in Directory.EnumerateFiles( + Path.Combine("test-data", "good"), + "*.mat")) + { + var fullFileName = fileName; + yield return MatFileReadingMethods.ReadMatFile(method, fullFileName); + } + } + private static void CheckLimits(IArrayOf array, T[] limits) where T : struct { diff --git a/MatFileHandler.Tests/MatFileReadingMethod.cs b/MatFileHandler.Tests/MatFileReadingMethod.cs new file mode 100644 index 0000000..8b2e711 --- /dev/null +++ b/MatFileHandler.Tests/MatFileReadingMethod.cs @@ -0,0 +1,29 @@ +// Copyright 2017-2018 Alexander Luzgarev + +namespace MatFileHandler.Tests; + +/// +/// Method of reading .mat files for testing. +/// +public enum MatFileReadingMethod +{ + /// + /// Undefined. + /// + Undefined = 0, + + /// + /// Normal stream (like memory or file stream). + /// + NormalStream, + + /// + /// Partial stream (only is capable of reading one byte at a time). + /// + PartialStream, + + /// + /// Unaligned stream (what happens if the data don't start at the beginning?). + /// + UnalignedStream, +} diff --git a/MatFileHandler.Tests/MatFileReadingMethods.cs b/MatFileHandler.Tests/MatFileReadingMethods.cs new file mode 100644 index 0000000..759d447 --- /dev/null +++ b/MatFileHandler.Tests/MatFileReadingMethods.cs @@ -0,0 +1,38 @@ +using System; +using System.IO; + +namespace MatFileHandler.Tests; + +internal static class MatFileReadingMethods +{ + public static IMatFile ReadMatFile(MatFileReadingMethod method, string fullFileName) + { + using var stream = File.OpenRead(fullFileName); + switch (method) + { + case MatFileReadingMethod.NormalStream: + return ReadFromStream(stream); + case MatFileReadingMethod.PartialStream: + { + using var wrapper = new PartialUnseekableReadStream(stream); + return ReadFromStream(wrapper); + } + case MatFileReadingMethod.UnalignedStream: + { + using var ms = new MemoryStream(); + ms.Seek(3, SeekOrigin.Begin); + stream.CopyTo(ms); + ms.Seek(3, SeekOrigin.Begin); + return ReadFromStream(ms); + } + default: + throw new NotImplementedException(); + } + } + + private static IMatFile ReadFromStream(Stream stream) + { + var reader = new MatFileReader(stream); + return reader.Read(); + } +} diff --git a/MatFileHandler.Tests/MatFileWriterOptionsForTests.cs b/MatFileHandler.Tests/MatFileWriterOptionsForTests.cs index 3d8a158..b3b6a74 100644 --- a/MatFileHandler.Tests/MatFileWriterOptionsForTests.cs +++ b/MatFileHandler.Tests/MatFileWriterOptionsForTests.cs @@ -1,9 +1,27 @@ namespace MatFileHandler.Tests; +/// +/// Options to give to MatFileWriter constructor for testing it. +/// public enum MatFileWriterOptionsForTests { + /// + /// Undefined. + /// Undefined = 0, + + /// + /// No options. + /// None, + + /// + /// Option to always use compression. + /// Always, + + /// + /// Option to never use compression. + /// Never, } diff --git a/MatFileHandler.Tests/MatFileWriterTests.cs b/MatFileHandler.Tests/MatFileWriterTests.cs index c3c2aae..4cadc14 100755 --- a/MatFileHandler.Tests/MatFileWriterTests.cs +++ b/MatFileHandler.Tests/MatFileWriterTests.cs @@ -247,10 +247,7 @@ namespace MatFileHandler.Tests } } - private static AbstractTestDataFactory GetMatTestData(string factoryName) => - new MatTestDataFactory(Path.Combine(TestDirectory, factoryName)); - - private void CompareSparseArrays(ISparseArrayOf expected, ISparseArrayOf actual) + private static void CompareSparseArrays(ISparseArrayOf expected, ISparseArrayOf actual) where T : struct { Assert.NotNull(actual); @@ -258,7 +255,7 @@ namespace MatFileHandler.Tests Assert.Equal(expected.Data, actual.Data); } - private void CompareStructureArrays(IStructureArray expected, IStructureArray actual) + private static void CompareStructureArrays(IStructureArray expected, IStructureArray actual) { Assert.NotNull(actual); Assert.Equal(expected.Dimensions, actual.Dimensions); @@ -272,7 +269,7 @@ namespace MatFileHandler.Tests } } - private void CompareCellArrays(ICellArray expected, ICellArray actual) + private static void CompareCellArrays(ICellArray expected, ICellArray actual) { Assert.NotNull(actual); Assert.Equal(expected.Dimensions, actual.Dimensions); @@ -282,21 +279,21 @@ namespace MatFileHandler.Tests } } - private void CompareNumericalArrays(IArrayOf expected, IArrayOf actual) + private static void CompareNumericalArrays(IArrayOf expected, IArrayOf actual) { Assert.NotNull(actual); Assert.Equal(expected.Dimensions, actual.Dimensions); Assert.Equal(expected.Data, actual.Data); } - private void CompareCharArrays(ICharArray expected, ICharArray actual) + private static void CompareCharArrays(ICharArray expected, ICharArray actual) { Assert.NotNull(actual); Assert.Equal(expected.Dimensions, actual.Dimensions); Assert.Equal(expected.String, actual.String); } - private void CompareMatArrays(IArray expected, IArray actual) + private static void CompareMatArrays(IArray expected, IArray actual) { switch (expected) { @@ -387,7 +384,7 @@ namespace MatFileHandler.Tests throw new NotSupportedException(); } - private void CompareMatFiles(IMatFile expected, IMatFile actual) + private static void CompareMatFiles(IMatFile expected, IMatFile actual) { Assert.Equal(expected.Variables.Length, actual.Variables.Length); for (var i = 0; i < expected.Variables.Length; i++) @@ -400,14 +397,17 @@ namespace MatFileHandler.Tests } } - private void MatCompareWithTestData( + private static void MatCompareWithTestData( string factoryName, string testName, IMatFile actual, MatFileWritingMethod method, MatFileWriterOptionsForTests options) { - var expected = GetMatTestData(factoryName)[testName]; + var fullFileName = Path.Combine("test-data", "good", $"{testName}.mat"); + var expected = MatFileReadingMethods.ReadMatFile( + MatFileReadingMethod.NormalStream, + fullFileName); var buffer = MatFileWritingMethods.WriteMatFile(method, options, actual); using var stream = new MemoryStream(buffer); var reader = new MatFileReader(stream); @@ -415,7 +415,7 @@ namespace MatFileHandler.Tests CompareMatFiles(expected, actualRead); } - private ComplexOf[] CreateComplexLimits(T[] limits) + private static ComplexOf[] CreateComplexLimits(T[] limits) where T : struct { return new[] { new ComplexOf(limits[0], limits[1]), new ComplexOf(limits[1], limits[0]) }; diff --git a/MatFileHandler.Tests/MatFileWritingMethod.cs b/MatFileHandler.Tests/MatFileWritingMethod.cs index 225a550..b9f3888 100644 --- a/MatFileHandler.Tests/MatFileWritingMethod.cs +++ b/MatFileHandler.Tests/MatFileWritingMethod.cs @@ -1,12 +1,29 @@ // Copyright 2017-2018 Alexander Luzgarev -namespace MatFileHandler.Tests +namespace MatFileHandler.Tests; + +/// +/// Method of writing .mat files for testing. +/// +public enum MatFileWritingMethod { - public enum MatFileWritingMethod - { - Undefined, - NormalStream, - UnseekableStream, - UnalignedStream, - } + /// + /// Undefined. + /// + Undefined = 0, + + /// + /// Normal stream (like memory or file stream). + /// + NormalStream, + + /// + /// A stream that cannot be seeked (like a deflate stream). + /// + UnseekableStream, + + /// + /// Unaligned stream (what happens if the data don't start at the beginning?). + /// + UnalignedStream, } diff --git a/MatFileHandler.Tests/MatTestDataFactory.cs b/MatFileHandler.Tests/MatTestDataFactory.cs deleted file mode 100755 index ac6935d..0000000 --- a/MatFileHandler.Tests/MatTestDataFactory.cs +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2017-2018 Alexander Luzgarev - -using System.IO; - -namespace MatFileHandler.Tests -{ - /// - /// Factory providing the parsed contents of .mat files. - /// - public class MatTestDataFactory : AbstractTestDataFactory - { - /// - /// Initializes a new instance of the class. - /// - /// Directory containing test files. - public MatTestDataFactory(string testDirectory) - : base( - testDirectory, - new ExtensionTestFilenameConvention("mat")) - { - } - - /// - /// Read and parse data from a .mat file. - /// - /// Input stream. - /// Parsed contents of the file. - protected override IMatFile ReadDataFromStream(Stream stream) - { - var matFileReader = new MatFileReader(stream); - var matFile = matFileReader.Read(); - return matFile; - } - } -} \ No newline at end of file diff --git a/MatFileHandler.Tests/PartialReadMatTestDataFactory.cs b/MatFileHandler.Tests/PartialReadMatTestDataFactory.cs deleted file mode 100644 index 1c277b6..0000000 --- a/MatFileHandler.Tests/PartialReadMatTestDataFactory.cs +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2017-2018 Alexander Luzgarev - -using System.IO; - -namespace MatFileHandler.Tests -{ - /// - /// Factory providing the parsed contents of .mat files, - /// wrapped in a . - /// - public class PartialReadMatTestDataFactory : MatTestDataFactory - { - /// - /// Initializes a new instance of the class. - /// - /// Directory containing test files. - public PartialReadMatTestDataFactory(string testDirectory) - : base(testDirectory) - { - } - - /// - /// Read and parse data from a .mat file. - /// - /// Input stream. - /// Parsed contents of the file. - protected override IMatFile ReadDataFromStream(Stream stream) - { - using (var wrapper = new PartialUnseekableReadStream(stream)) - { - return base.ReadDataFromStream(wrapper); - } - } - } -} diff --git a/MatFileHandler.Tests/UnalignedMatTestDataFactory.cs b/MatFileHandler.Tests/UnalignedMatTestDataFactory.cs deleted file mode 100644 index 8cf2f48..0000000 --- a/MatFileHandler.Tests/UnalignedMatTestDataFactory.cs +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2017-2018 Alexander Luzgarev - -using System.IO; - -namespace MatFileHandler.Tests -{ - /// - /// Factory providing the parsed contents of .mat files, - /// which start at a non-8 byte-aligned offset in the stream. - /// - public class UnalignedMatTestDataFactory : MatTestDataFactory - { - /// - /// Initializes a new instance of the class. - /// - /// Directory containing test files. - public UnalignedMatTestDataFactory(string testDirectory) - : base(testDirectory) - { - } - - /// - protected override IMatFile ReadDataFromStream(Stream stream) - { - using (var ms = new MemoryStream()) - { - ms.Seek(3, SeekOrigin.Begin); - - stream.CopyTo(ms); - - ms.Seek(3, SeekOrigin.Begin); - - return base.ReadDataFromStream(ms); - } - } - } -}