#pragma warning disable CS0618 // Type or member is obsolete private static void TestSerializationToPath( TestableDoubleMatrix testableMatrix, string partialPath) { // dense { var expected = testableMatrix.AsDense; var path = "dense-" + partialPath; CsvMatrixSerializer.Serialize(path, expected); var actual = CsvMatrixSerializer.Deserialize(path); DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy); } // sparse { var expected = testableMatrix.AsSparse; var path = "sparse-" + partialPath; CsvMatrixSerializer.Serialize(path, expected); var actual = CsvMatrixSerializer.Deserialize(path); DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy); } // read-only dense { var expected = testableMatrix.AsDense; var path = "read-only-dense-" + partialPath; CsvMatrixSerializer.Serialize(path, expected.AsReadOnly()); var actual = CsvMatrixSerializer.Deserialize(path); DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy); } // read-only sparse { var expected = testableMatrix.AsSparse; var path = "read-only-sparse-" + partialPath; CsvMatrixSerializer.Serialize(path, expected.AsReadOnly()); var actual = CsvMatrixSerializer.Deserialize(path); DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy); } }
public void SerializationToStreamTest() { // reader or writer is null { ArgumentExceptionAssert.Throw( () => { CsvMatrixSerializer.Serialize((TextWriter)null, DoubleMatrix.Dense(2, 2)); }, expectedType: typeof(ArgumentNullException), expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage, expectedParameterName: "writer"); ArgumentExceptionAssert.Throw( () => { CsvMatrixSerializer.Serialize((TextWriter)null, DoubleMatrix.Dense(2, 2).AsReadOnly()); }, expectedType: typeof(ArgumentNullException), expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage, expectedParameterName: "writer"); ArgumentExceptionAssert.Throw( () => { CsvMatrixSerializer.Deserialize((TextReader)null); }, expectedType: typeof(ArgumentNullException), expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage, expectedParameterName: "reader"); } // stream does not contain a valid dense matrix representation { string[] data = new string[4] { "Dense,2, 3, MatrixName", ",column0,,column2", "row0,1.0,4.0,5.0", "row1,2.0,6.0" }; // Create a stream containing the CSV content. MemoryStream stream = new(); StreamWriter writer = new(stream); for (int i = 0; i < data.Length; i++) { writer.WriteLine(data[i].ToCharArray()); writer.Flush(); } stream.Position = 0; // Create a reader for the stream. StreamReader reader = new(stream); ExceptionAssert.Throw( () => { CsvMatrixSerializer.Deserialize(reader); }, expectedType: typeof(InvalidOperationException), expectedMessage: ImplementationServices.GetResourceString( "STR_EXCEPT_REP_UNABLE_TO_DESERIALIZE")); } // stream does not contain a valid sparse matrix representation { string[] data = new string[4] { "Sparse,2, 3, MatrixName", "2,3,5.0", "0,Column0", "0,Row0" }; // Create a stream containing the CSV content. MemoryStream stream = new(); StreamWriter writer = new(stream); for (int i = 0; i < data.Length; i++) { writer.WriteLine(data[i].ToCharArray()); writer.Flush(); } stream.Position = 0; // Create a reader for the stream. StreamReader reader = new(stream); ExceptionAssert.Throw( () => { CsvMatrixSerializer.Deserialize(reader); }, expectedType: typeof(InvalidOperationException), expectedMessage: ImplementationServices.GetResourceString( "STR_EXCEPT_REP_UNABLE_TO_DESERIALIZE")); } // matrix is null { MemoryStream stream = new(); StreamWriter writer = new(stream); ArgumentExceptionAssert.Throw( () => { CsvMatrixSerializer.Serialize(writer, (DoubleMatrix)null); }, expectedType: typeof(ArgumentNullException), expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage, expectedParameterName: "matrix"); ArgumentExceptionAssert.Throw( () => { CsvMatrixSerializer.Serialize(writer, (ReadOnlyDoubleMatrix)null); }, expectedType: typeof(ArgumentNullException), expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage, expectedParameterName: "matrix"); } // valid input { TestableDoubleMatrix testableMatrix; testableMatrix = GetTestableMatrix(true, true, true, out string partialPath); TestSerializationToStream(testableMatrix); testableMatrix = GetTestableMatrix(true, true, false, out partialPath); TestSerializationToStream(testableMatrix); testableMatrix = GetTestableMatrix(true, false, true, out partialPath); TestSerializationToStream(testableMatrix); testableMatrix = GetTestableMatrix(true, false, false, out partialPath); TestSerializationToStream(testableMatrix); testableMatrix = GetTestableMatrix(false, true, true, out partialPath); TestSerializationToStream(testableMatrix); testableMatrix = GetTestableMatrix(false, true, false, out partialPath); TestSerializationToStream(testableMatrix); testableMatrix = GetTestableMatrix(false, false, true, out partialPath); TestSerializationToStream(testableMatrix); testableMatrix = GetTestableMatrix(false, false, false, out partialPath); TestSerializationToStream(testableMatrix); } }
public void SerializationToPathTest() { // path is null { ArgumentExceptionAssert.Throw( () => { CsvMatrixSerializer.Serialize((string)null, DoubleMatrix.Dense(2, 2)); }, expectedType: typeof(ArgumentNullException), expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage, expectedParameterName: "path"); ArgumentExceptionAssert.Throw( () => { CsvMatrixSerializer.Serialize((string)null, DoubleMatrix.Dense(2, 2).AsReadOnly()); }, expectedType: typeof(ArgumentNullException), expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage, expectedParameterName: "path"); ArgumentExceptionAssert.Throw( () => { CsvMatrixSerializer.Deserialize((string)null); }, expectedType: typeof(ArgumentNullException), expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage, expectedParameterName: "path"); } // path is not valid { ExceptionAssert.Throw( () => { CsvMatrixSerializer.Serialize("n/:.csv", DoubleMatrix.Dense(2, 2)); }, expectedType: typeof(InvalidOperationException), expectedMessage: ImplementationServices.GetResourceString( "STR_EXCEPT_REP_UNABLE_TO_SERIALIZE")); ExceptionAssert.Throw( () => { CsvMatrixSerializer.Serialize("n/:.csv", DoubleMatrix.Dense(2, 2).AsReadOnly()); }, expectedType: typeof(InvalidOperationException), expectedMessage: ImplementationServices.GetResourceString( "STR_EXCEPT_REP_UNABLE_TO_SERIALIZE")); } // path of invalid representation { ExceptionAssert.Throw( () => { CsvMatrixSerializer.Deserialize("deserialize-invalid-sparse.csv"); }, expectedType: typeof(InvalidOperationException), expectedMessage: ImplementationServices.GetResourceString( "STR_EXCEPT_REP_UNABLE_TO_DESERIALIZE")); ExceptionAssert.Throw( () => { CsvMatrixSerializer.Deserialize("deserialize-invalid-dense.csv"); }, expectedType: typeof(InvalidOperationException), expectedMessage: ImplementationServices.GetResourceString( "STR_EXCEPT_REP_UNABLE_TO_DESERIALIZE")); } // matrix is null { ArgumentExceptionAssert.Throw( () => { CsvMatrixSerializer.Serialize("file.csv", (DoubleMatrix)null); }, expectedType: typeof(ArgumentNullException), expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage, expectedParameterName: "matrix"); ArgumentExceptionAssert.Throw( () => { CsvMatrixSerializer.Serialize("file.csv", (ReadOnlyDoubleMatrix)null); }, expectedType: typeof(ArgumentNullException), expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage, expectedParameterName: "matrix"); } // valid input { TestableDoubleMatrix testableMatrix; testableMatrix = GetTestableMatrix(true, true, true, out string partialPath); TestSerializationToPath(testableMatrix, partialPath); testableMatrix = GetTestableMatrix(true, true, false, out partialPath); TestSerializationToPath(testableMatrix, partialPath); testableMatrix = GetTestableMatrix(true, false, true, out partialPath); TestSerializationToPath(testableMatrix, partialPath); testableMatrix = GetTestableMatrix(true, false, false, out partialPath); TestSerializationToPath(testableMatrix, partialPath); testableMatrix = GetTestableMatrix(false, true, true, out partialPath); TestSerializationToPath(testableMatrix, partialPath); testableMatrix = GetTestableMatrix(false, true, false, out partialPath); TestSerializationToPath(testableMatrix, partialPath); testableMatrix = GetTestableMatrix(false, false, true, out partialPath); TestSerializationToPath(testableMatrix, partialPath); testableMatrix = GetTestableMatrix(false, false, false, out partialPath); TestSerializationToPath(testableMatrix, partialPath); } }
private static void TestSerializationToStream( TestableDoubleMatrix testableMatrix) { // dense { var expected = testableMatrix.AsDense; MemoryStream stream = new(); var textWriter = new StreamWriter(stream); CsvMatrixSerializer.Serialize(textWriter, expected); stream.Position = 0; var textReader = new StreamReader(stream); var actual = CsvMatrixSerializer.Deserialize(textReader); DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy); } // sparse { var expected = testableMatrix.AsSparse; MemoryStream stream = new(); var textWriter = new StreamWriter(stream); CsvMatrixSerializer.Serialize(textWriter, expected); stream.Position = 0; var textReader = new StreamReader(stream); var actual = CsvMatrixSerializer.Deserialize(textReader); DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy); } // read-only dense { var expected = testableMatrix.AsDense; MemoryStream stream = new(); var textWriter = new StreamWriter(stream); CsvMatrixSerializer.Serialize(textWriter, expected.AsReadOnly()); stream.Position = 0; var textReader = new StreamReader(stream); var actual = CsvMatrixSerializer.Deserialize(textReader); DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy); } // read-only sparse { var expected = testableMatrix.AsSparse; MemoryStream stream = new(); var textWriter = new StreamWriter(stream); CsvMatrixSerializer.Serialize(textWriter, expected.AsReadOnly()); stream.Position = 0; var textReader = new StreamReader(stream); var actual = CsvMatrixSerializer.Deserialize(textReader); DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy); } }