// // Math.NET Numerics, part of the Math.NET Project // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // // Copyright (c) 2009-2016 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // using System; using System.IO; using System.Runtime.Serialization; using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra.Storage; using NUnit.Framework; namespace MathNet.Numerics.UnitTests.LinearAlgebraTests { #if NOSYSNUMERICS using Complex64 = Numerics.Complex; #else using Complex64 = System.Numerics.Complex; #endif [TestFixture] public class MatrixStorageSerializationTests { static readonly Type[] KnownTypes = { typeof(DenseColumnMajorMatrixStorage), typeof(SparseCompressedRowMatrixStorage), typeof(DiagonalMatrixStorage), typeof(DenseColumnMajorMatrixStorage), typeof(SparseCompressedRowMatrixStorage), typeof(DiagonalMatrixStorage), typeof(DenseColumnMajorMatrixStorage), typeof(SparseCompressedRowMatrixStorage), typeof(DiagonalMatrixStorage), typeof(DenseColumnMajorMatrixStorage), typeof(SparseCompressedRowMatrixStorage), typeof(DiagonalMatrixStorage) }; [Test] public void DenseColumnMajorMatrixStorageOfDoubleDataContractSerializationTest() { MatrixStorage expected = Matrix.Build.Dense(2, 2, new[] { 1.0d, 2.0d, 0.0d, 3.0d }).Storage; var serializer = new DataContractSerializer(typeof(MatrixStorage), KnownTypes); var stream = new MemoryStream(); serializer.WriteObject(stream, expected); stream.Position = 0; var actual = (MatrixStorage)serializer.ReadObject(stream); Assert.That(actual.RowCount, Is.EqualTo(expected.RowCount)); Assert.That(actual.ColumnCount, Is.EqualTo(expected.ColumnCount)); Assert.That(actual, Is.TypeOf(typeof(DenseColumnMajorMatrixStorage))); Assert.That(actual, Is.EqualTo(expected)); Assert.That(actual, Is.Not.SameAs(expected)); } [Test] public void DenseColumnMajorMatrixStorageOfSingleDataContractSerializationTest() { MatrixStorage expected = Matrix.Build.Dense(2, 2, new[] { 1.0f, 2.0f, 0.0f, 3.0f }).Storage; var serializer = new DataContractSerializer(typeof(MatrixStorage), KnownTypes); var stream = new MemoryStream(); serializer.WriteObject(stream, expected); stream.Position = 0; var actual = (MatrixStorage)serializer.ReadObject(stream); Assert.That(actual.RowCount, Is.EqualTo(expected.RowCount)); Assert.That(actual.ColumnCount, Is.EqualTo(expected.ColumnCount)); Assert.That(actual, Is.TypeOf(typeof(DenseColumnMajorMatrixStorage))); Assert.That(actual, Is.EqualTo(expected)); Assert.That(actual, Is.Not.SameAs(expected)); } [Test] public void DenseColumnMajorMatrixStorageOfComplex64DataContractSerializationTest() { var expectedMatrix = Matrix .Build .Dense(2, 2, new[] { new Complex64(1.0, -1.0), 2.0, 0.0, 3.0 }); MatrixStorage expected = expectedMatrix.Storage; var serializer = new DataContractSerializer(typeof(MatrixStorage), KnownTypes); var stream = new MemoryStream(); serializer.WriteObject(stream, expected); stream.Position = 0; var actual = (MatrixStorage)serializer.ReadObject(stream); var actualMatrix = Matrix .Build .Dense(actual as DenseColumnMajorMatrixStorage); Assert.That(actual.RowCount, Is.EqualTo(expected.RowCount)); Assert.That(actual.ColumnCount, Is.EqualTo(expected.ColumnCount)); Assert.That(actual, Is.TypeOf(typeof(DenseColumnMajorMatrixStorage))); Assert.That(actualMatrix, Is.EqualTo(expectedMatrix)); Assert.That(actualMatrix, Is.Not.SameAs(expectedMatrix)); } [Test] public void DenseColumnMajorMatrixStorageOfComplex32DataContractSerializationTest() { MatrixStorage expected = Matrix.Build.Dense(2, 2, new[] { new Numerics.Complex32(1.0f, -1.0f), 2.0f, 0.0f, 3.0f }).Storage; var serializer = new DataContractSerializer(typeof(MatrixStorage), KnownTypes); var stream = new MemoryStream(); serializer.WriteObject(stream, expected); stream.Position = 0; var actual = (MatrixStorage)serializer.ReadObject(stream); Assert.That(actual.RowCount, Is.EqualTo(expected.RowCount)); Assert.That(actual.ColumnCount, Is.EqualTo(expected.ColumnCount)); Assert.That(actual, Is.TypeOf(typeof(DenseColumnMajorMatrixStorage))); Assert.That(actual, Is.EqualTo(expected)); Assert.That(actual, Is.Not.SameAs(expected)); } [Test] public void SparseCompressedRowMatrixStorageOfDoubleDataContractSerializationTest() { MatrixStorage expected = Matrix.Build.SparseOfArray(new[,] { {1.0d, 2.0d}, {0.0d, 3.0d} }).Storage; var serializer = new DataContractSerializer(typeof(MatrixStorage), KnownTypes); var stream = new MemoryStream(); serializer.WriteObject(stream, expected); stream.Position = 0; var actual = (MatrixStorage)serializer.ReadObject(stream); Assert.That(actual.RowCount, Is.EqualTo(expected.RowCount)); Assert.That(actual.ColumnCount, Is.EqualTo(expected.ColumnCount)); Assert.That(actual, Is.TypeOf(typeof(SparseCompressedRowMatrixStorage))); Assert.That(actual, Is.EqualTo(expected)); Assert.That(actual, Is.Not.SameAs(expected)); } [Test] public void DiagonalMatrixStorageOfDoubleDataContractSerializationTest() { MatrixStorage expected = Matrix.Build.Diagonal(new[] { 1.0d, 2.0d, 0.0d, 3.0d }).Storage; var serializer = new DataContractSerializer(typeof(MatrixStorage), KnownTypes); var stream = new MemoryStream(); serializer.WriteObject(stream, expected); stream.Position = 0; var actual = (MatrixStorage)serializer.ReadObject(stream); Assert.That(actual.RowCount, Is.EqualTo(expected.RowCount)); Assert.That(actual.ColumnCount, Is.EqualTo(expected.ColumnCount)); Assert.That(actual, Is.TypeOf(typeof(DiagonalMatrixStorage))); Assert.That(actual, Is.EqualTo(expected)); Assert.That(actual, Is.Not.SameAs(expected)); } } }