private static void TestSerializationToPath(
            TestableComplexMatrix testableMatrix,
            string partialPath)
        {
            // dense
            {
                var expected = testableMatrix.AsDense;

                var path = "dense-" + partialPath;

                CsvComplexMatrixSerializer.Serialize(path, expected);

                var actual = CsvComplexMatrixSerializer.Deserialize(path);

                ComplexMatrixAssert.AreEqual(expected, actual, ComplexMatrixTest.Accuracy);
            }

            // sparse
            {
                var expected = testableMatrix.AsSparse;

                var path = "sparse-" + partialPath;

                CsvComplexMatrixSerializer.Serialize(path, expected);

                var actual = CsvComplexMatrixSerializer.Deserialize(path);

                ComplexMatrixAssert.AreEqual(expected, actual, ComplexMatrixTest.Accuracy);
            }

            // read-only dense
            {
                var expected = testableMatrix.AsDense;

                var path = "read-only-dense-" + partialPath;

                CsvComplexMatrixSerializer.Serialize(path, expected.AsReadOnly());

                var actual = CsvComplexMatrixSerializer.Deserialize(path);

                ComplexMatrixAssert.AreEqual(expected, actual, ComplexMatrixTest.Accuracy);
            }

            // read-only sparse
            {
                var expected = testableMatrix.AsSparse;

                var path = "read-only-sparse-" + partialPath;

                CsvComplexMatrixSerializer.Serialize(path, expected.AsReadOnly());

                var actual = CsvComplexMatrixSerializer.Deserialize(path);

                ComplexMatrixAssert.AreEqual(expected, actual, ComplexMatrixTest.Accuracy);
            }
        }
 public void DeserializationUnexpectedEntryTypeTest()
 {
     ExceptionAssert.Throw(
         () =>
     {
         CsvComplexMatrixSerializer.Deserialize(
             Path.Combine("Data", "deserialize-valid-expected-complex-declared-double.csv"));
     },
         expectedType: typeof(InvalidOperationException),
         expectedMessage: ImplementationServices.GetResourceString(
             "STR_EXCEPT_REP_UNABLE_TO_DESERIALIZE"),
         expectedInnerType: typeof(InvalidOperationException),
         expectedInnerMessage: String.Format(
             ImplementationServices.GetResourceString(
                 "STR_EXCEPT_REP_DESERIALIZE_UNEXPECTED_ENTRY_TYPE"),
             "Double",
             "Complex"));
 }
        public void Main()
        {
            // Create the CSV representation of a dense matrix.
            string[] data = new string[5] {
                "Dense|Complex,3,2,MatrixName",
                ",column0,column1",
                "row0,(1.0 -2.0),(NaN NaN)",
                "row1,(2.0 -3.0),(4.0 -1.0)",
                "row2,(0 0),(5.0 7.0)"
            };

            // Show the matrix CSV representation.
            Console.WriteLine("CSV representation of the matrix:");
            for (int i = 0; i < data.Length; i++)
            {
                Console.WriteLine(data[i]);
            }
            Console.WriteLine();

            // 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);

            // Deserialize the CSV document contained in the stream.
            ComplexMatrix matrix =
                CsvComplexMatrixSerializer.Deserialize(reader);

            // Show the matrix.
            Console.WriteLine("Deserialized matrix:");
            Console.WriteLine(matrix);
        }
        public void SerializationToStreamTest()
        {
            // reader or writer is null
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    CsvComplexMatrixSerializer.Serialize((TextWriter)null, ComplexMatrix.Dense(2, 2));
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "writer");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    CsvComplexMatrixSerializer.Serialize((TextWriter)null, ComplexMatrix.Dense(2, 2).AsReadOnly());
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "writer");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    CsvComplexMatrixSerializer.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(
                    () =>
                {
                    CsvComplexMatrixSerializer.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(
                    () =>
                {
                    CsvComplexMatrixSerializer.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(
                    () =>
                {
                    CsvComplexMatrixSerializer.Serialize(writer, (ComplexMatrix)null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "matrix");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    CsvComplexMatrixSerializer.Serialize(writer, (ReadOnlyComplexMatrix)null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "matrix");
            }

            // valid input
            {
                TestableComplexMatrix 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(
                    () =>
                {
                    CsvComplexMatrixSerializer.Serialize((string)null, ComplexMatrix.Dense(2, 2));
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "path");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    CsvComplexMatrixSerializer.Serialize((string)null, ComplexMatrix.Dense(2, 2).AsReadOnly());
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "path");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    CsvComplexMatrixSerializer.Deserialize((string)null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "path");
            }

            // path is not valid
            {
                ExceptionAssert.Throw(
                    () =>
                {
                    CsvComplexMatrixSerializer.Serialize("n/:.csv", ComplexMatrix.Dense(2, 2));
                },
                    expectedType: typeof(InvalidOperationException),
                    expectedMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_REP_UNABLE_TO_SERIALIZE"));

                ExceptionAssert.Throw(
                    () =>
                {
                    CsvComplexMatrixSerializer.Serialize("n/:.csv", ComplexMatrix.Dense(2, 2).AsReadOnly());
                },
                    expectedType: typeof(InvalidOperationException),
                    expectedMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_REP_UNABLE_TO_SERIALIZE"));
            }

            // path of invalid representation
            {
                ExceptionAssert.Throw(
                    () =>
                {
                    CsvComplexMatrixSerializer.Deserialize(
                        Path.Combine("Data", $"deserialize-invalid-sparse-{EntryType}.csv"));
                },
                    expectedType: typeof(InvalidOperationException),
                    expectedMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_REP_UNABLE_TO_DESERIALIZE"));

                ExceptionAssert.Throw(
                    () =>
                {
                    CsvComplexMatrixSerializer.Deserialize(
                        Path.Combine("Data", $"deserialize-invalid-dense-{EntryType}.csv"));
                },
                    expectedType: typeof(InvalidOperationException),
                    expectedMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_REP_UNABLE_TO_DESERIALIZE"));
            }

            // matrix is null
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    CsvComplexMatrixSerializer.Serialize("file.csv", (ComplexMatrix)null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "matrix");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    CsvComplexMatrixSerializer.Serialize("file.csv", (ReadOnlyComplexMatrix)null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "matrix");
            }

            // valid input
            {
                TestableComplexMatrix 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(
            TestableComplexMatrix testableMatrix)
        {
            // dense
            {
                var expected = testableMatrix.AsDense;

                MemoryStream stream = new();

                var textWriter = new StreamWriter(stream);

                CsvComplexMatrixSerializer.Serialize(textWriter, expected);

                stream.Position = 0;

                var textReader = new StreamReader(stream);

                var actual = CsvComplexMatrixSerializer.Deserialize(textReader);

                ComplexMatrixAssert.AreEqual(expected, actual, ComplexMatrixTest.Accuracy);
            }

            // sparse
            {
                var expected = testableMatrix.AsSparse;

                MemoryStream stream = new();

                var textWriter = new StreamWriter(stream);

                CsvComplexMatrixSerializer.Serialize(textWriter, expected);

                stream.Position = 0;

                var textReader = new StreamReader(stream);

                var actual = CsvComplexMatrixSerializer.Deserialize(textReader);

                ComplexMatrixAssert.AreEqual(expected, actual, ComplexMatrixTest.Accuracy);
            }

            // read-only dense
            {
                var expected = testableMatrix.AsDense;

                MemoryStream stream = new();

                var textWriter = new StreamWriter(stream);

                CsvComplexMatrixSerializer.Serialize(textWriter, expected.AsReadOnly());

                stream.Position = 0;

                var textReader = new StreamReader(stream);

                var actual = CsvComplexMatrixSerializer.Deserialize(textReader);

                ComplexMatrixAssert.AreEqual(expected, actual, ComplexMatrixTest.Accuracy);
            }

            // read-only sparse
            {
                var expected = testableMatrix.AsSparse;

                MemoryStream stream = new();

                var textWriter = new StreamWriter(stream);

                CsvComplexMatrixSerializer.Serialize(textWriter, expected.AsReadOnly());

                stream.Position = 0;

                var textReader = new StreamReader(stream);

                var actual = CsvComplexMatrixSerializer.Deserialize(textReader);

                ComplexMatrixAssert.AreEqual(expected, actual, ComplexMatrixTest.Accuracy);
            }
        }