Browse Source

Tests: no more need to use dynamic/DLR thanks to matrix builders

optimization-3
Christoph Ruegg 13 years ago
parent
commit
810cea5e1d
  1. 5
      src/UnitTests/LinearAlgebraTests/Complex/MatrixStructureTheory.cs
  2. 5
      src/UnitTests/LinearAlgebraTests/Complex32/MatrixStructureTheory.cs
  3. 5
      src/UnitTests/LinearAlgebraTests/Double/MatrixStructureTheory.cs
  4. 30
      src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs
  5. 5
      src/UnitTests/LinearAlgebraTests/Single/MatrixStructureTheory.cs
  6. 79
      src/UnitTests/LinearAlgebraTests/StaticDynamicWrapper.cs
  7. 1
      src/UnitTests/UnitTests.csproj

5
src/UnitTests/LinearAlgebraTests/Complex/MatrixStructureTheory.cs

@ -46,11 +46,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
[TestFixture]
public class MatrixStructureTheory : MatrixStructureTheory<Complex>
{
public MatrixStructureTheory()
: base(Complex.Zero, typeof(DenseMatrix), typeof(SparseMatrix), typeof(DiagonalMatrix), typeof(DenseVector), typeof(SparseVector))
{
}
[Datapoints]
Matrix<Complex>[] _matrices = new Matrix<Complex>[]
{

5
src/UnitTests/LinearAlgebraTests/Complex32/MatrixStructureTheory.cs

@ -42,11 +42,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
[TestFixture]
public class MatrixStructureTheory : MatrixStructureTheory<Complex32>
{
public MatrixStructureTheory()
: base(Complex32.Zero, typeof(DenseMatrix), typeof(SparseMatrix), typeof(DiagonalMatrix), typeof(DenseVector), typeof(SparseVector))
{
}
[Datapoints]
Matrix<Complex32>[] _matrices = new Matrix<Complex32>[]
{

5
src/UnitTests/LinearAlgebraTests/Double/MatrixStructureTheory.cs

@ -40,11 +40,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[TestFixture]
public class MatrixStructureTheory : MatrixStructureTheory<double>
{
public MatrixStructureTheory()
: base(0d, typeof (DenseMatrix), typeof (SparseMatrix), typeof (DiagonalMatrix), typeof (DenseVector), typeof (SparseVector))
{
}
[Datapoints]
Matrix<double>[] _matrices = new Matrix<double>[]
{

30
src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs

@ -45,20 +45,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
protected abstract Vector<T> CreateVectorRandom(int size, int seed);
protected readonly T Zero;
protected readonly dynamic Dense;
protected readonly dynamic Sparse;
protected readonly dynamic Diagonal;
protected readonly dynamic DenseVector;
protected readonly dynamic SparseVector;
protected MatrixStructureTheory(T zero, Type dense, Type sparse, Type diagonal, Type denseVector, Type sparseVector)
protected MatrixStructureTheory()
{
Zero = zero;
Dense = new StaticDynamicWrapper(dense);
Sparse = new StaticDynamicWrapper(sparse);
Diagonal = new StaticDynamicWrapper(diagonal);
DenseVector = new StaticDynamicWrapper(denseVector);
SparseVector = new StaticDynamicWrapper(sparseVector);
Zero = Matrix<T>.Build.Zero;
}
protected Matrix<T> CreateDenseFor(Matrix<T> m, int rows = -1, int columns = -1, int seed = 1)
@ -233,7 +223,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
public void CanCreateDenseFromMultiDimArray()
{
T[,] array = CreateDenseRandom(4, 3, 0).ToArray();
var matrix = Dense.OfArray(array);
var matrix = Matrix<T>.Build.DenseOfArray(array);
Assert.That(matrix.GetType().Name, Is.EqualTo("DenseMatrix"));
Assert.That(matrix.RowCount, Is.EqualTo(4));
Assert.That(matrix.ColumnCount, Is.EqualTo(3));
@ -246,7 +236,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
public void CanCreateSparseFromMultiDimArray()
{
T[,] array = CreateDenseRandom(4, 3, 0).ToArray();
var matrix = Sparse.OfArray(array);
var matrix = Matrix<T>.Build.SparseOfArray(array);
Assert.That(matrix.GetType().Name, Is.EqualTo("SparseMatrix"));
Assert.That(matrix.RowCount, Is.EqualTo(4));
Assert.That(matrix.ColumnCount, Is.EqualTo(3));
@ -264,7 +254,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
CreateVectorRandom(4, 1).ToArray(),
CreateVectorRandom(4, 3).ToArray()
};
var matrix = Dense.OfRows(3, 4, array);
var matrix = Matrix<T>.Build.DenseOfRows(3, 4, array);
Assert.That(matrix.GetType().Name, Is.EqualTo("DenseMatrix"));
Assert.That(matrix.RowCount, Is.EqualTo(3));
Assert.That(matrix.ColumnCount, Is.EqualTo(4));
@ -282,7 +272,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
CreateVectorRandom(4, 1).ToArray(),
CreateVectorRandom(4, 3).ToArray()
};
var matrix = Sparse.OfRows(3, 4, array);
var matrix = Matrix<T>.Build.SparseOfRows(3, 4, array);
Assert.That(matrix.GetType().Name, Is.EqualTo("SparseMatrix"));
Assert.That(matrix.RowCount, Is.EqualTo(3));
Assert.That(matrix.ColumnCount, Is.EqualTo(4));
@ -300,7 +290,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
CreateVectorRandom(4, 1),
CreateVectorRandom(4, 3)
};
var matrix = Dense.OfColumns(4, 3, columns);
var matrix = Matrix<T>.Build.DenseOfColumns(4, 3, columns);
Assert.That(matrix.GetType().Name, Is.EqualTo("DenseMatrix"));
Assert.That(matrix.RowCount, Is.EqualTo(4));
Assert.That(matrix.ColumnCount, Is.EqualTo(3));
@ -318,7 +308,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
CreateVectorRandom(4, 1),
CreateVectorRandom(4, 3)
};
var matrix = Sparse.OfColumns(4, 3, columns);
var matrix = Matrix<T>.Build.SparseOfColumns(4, 3, columns);
Assert.That(matrix.GetType().Name, Is.EqualTo("SparseMatrix"));
Assert.That(matrix.RowCount, Is.EqualTo(4));
Assert.That(matrix.ColumnCount, Is.EqualTo(3));
@ -336,7 +326,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
CreateVectorRandom(4, 1),
CreateVectorRandom(4, 3)
};
var matrix = Dense.OfRows(3, 4, rows);
var matrix = Matrix<T>.Build.DenseOfRows(3, 4, rows);
Assert.That(matrix.GetType().Name, Is.EqualTo("DenseMatrix"));
Assert.That(matrix.RowCount, Is.EqualTo(3));
Assert.That(matrix.ColumnCount, Is.EqualTo(4));
@ -354,7 +344,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
CreateVectorRandom(4, 1),
CreateVectorRandom(4, 3)
};
var matrix = Sparse.OfRows(3, 4, rows);
var matrix = Matrix<T>.Build.SparseOfRows(3, 4, rows);
Assert.That(matrix.GetType().Name, Is.EqualTo("SparseMatrix"));
Assert.That(matrix.RowCount, Is.EqualTo(3));
Assert.That(matrix.ColumnCount, Is.EqualTo(4));

5
src/UnitTests/LinearAlgebraTests/Single/MatrixStructureTheory.cs

@ -40,11 +40,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
[TestFixture]
public class MatrixStructureTheory : MatrixStructureTheory<float>
{
public MatrixStructureTheory()
: base(0f, typeof(DenseMatrix), typeof(SparseMatrix), typeof(DiagonalMatrix), typeof(DenseVector), typeof(SparseVector))
{
}
[Datapoints]
Matrix<float>[] _matrices = new Matrix<float>[]
{

79
src/UnitTests/LinearAlgebraTests/StaticDynamicWrapper.cs

@ -1,79 +0,0 @@
// <copyright file="StaticDynamicWrapper.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2013 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.
// </copyright>
using System;
using System.Dynamic;
using System.Linq;
using System.Reflection;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
{
/// <summary>
/// Helper to allow calling static members of dynamic types.
/// Usefull to test static methods of e.g. all dense matrix types.
/// </summary>
public class StaticDynamicWrapper : DynamicObject
{
readonly Type _type;
public StaticDynamicWrapper(Type type)
{
_type = type;
}
// Handle static properties
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
PropertyInfo prop = _type.GetProperty(binder.Name, BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public);
if (prop == null)
{
result = null;
return false;
}
result = prop.GetValue(null, null);
return true;
}
// Handle static methods
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
MethodInfo method = _type.GetMethod(binder.Name, args.Select(a => a.GetType()).ToArray());
if (method == null)
{
result = null;
return false;
}
result = method.Invoke(null, args);
return true;
}
}
}

1
src/UnitTests/UnitTests.csproj

@ -352,7 +352,6 @@
<Compile Include="LinearAlgebraTests\Single\VectorTests.cs" />
<Compile Include="LinearAlgebraTests\Single\VectorTests.Norm.cs" />
<Compile Include="LinearAlgebraTests\Double\VectorArithmeticTheory.cs" />
<Compile Include="LinearAlgebraTests\StaticDynamicWrapper.cs" />
<Compile Include="LinearAlgebraTests\VectorArithmeticTheory.cs" />
<Compile Include="MatrixHelpers.cs" />
<Compile Include="NumberTheoryTests\GcdRelatedTest.cs" />

Loading…
Cancel
Save