Skip to content

Commit 1e7ec8b

Browse files
committed
Fix permutation method recursive calls and add unit tests.
1 parent 32a37ac commit 1e7ec8b

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

CSparse.Tests/PermutationTest.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+

2+
namespace CSparse.Tests
3+
{
4+
using NUnit.Framework;
5+
6+
public class PermutationTest
7+
{
8+
[Test]
9+
public void TestCreate()
10+
{
11+
var expected = new[] { 0, 1, 2 };
12+
var actual = Permutation.Create(3, 0);
13+
14+
CollectionAssert.AreEqual(expected, actual);
15+
16+
expected = new[] { 2, 1, 0 };
17+
actual = Permutation.Create(3, -1);
18+
19+
CollectionAssert.AreEqual(expected, actual);
20+
}
21+
22+
[Test]
23+
public void TestApply()
24+
{
25+
var vector = new double[] { 1d, 2d, 3d };
26+
var p = new[] { 2, 0, 1 };
27+
28+
var expected = new double[] { 3d, 1d, 2d };
29+
var actual = new double[3];
30+
31+
Permutation.Apply(p, vector, actual, 3);
32+
33+
CollectionAssert.AreEqual(expected, actual);
34+
}
35+
36+
[Test]
37+
public void TestApplyInverse()
38+
{
39+
var vector = new double[] { 1d, 2d, 3d };
40+
var p = new[] { 2, 0, 1 };
41+
42+
var expected = new double[] { 2d, 3d, 1d };
43+
var actual = new double[3];
44+
45+
Permutation.ApplyInverse(p, vector, actual, 3);
46+
47+
CollectionAssert.AreEqual(expected, actual);
48+
}
49+
50+
[Test]
51+
public void TestInvert()
52+
{
53+
var p = new[] { 2, 0, 1 };
54+
55+
var expected = new[] { 1, 2, 0 };
56+
var actual = Permutation.Invert(p);
57+
58+
CollectionAssert.AreEqual(expected, actual);
59+
}
60+
61+
[Test]
62+
public void TestIsValid()
63+
{
64+
var p_valid = new[] { 2, 0, 1 };
65+
66+
Assert.IsTrue(Permutation.IsValid(p_valid));
67+
68+
var p_invalid = new[] { 2, 1, 1 };
69+
70+
Assert.IsFalse(Permutation.IsValid(p_invalid));
71+
}
72+
}
73+
}

CSparse/Permutation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static class Permutation
1919
/// </remarks>
2020
public static void Apply<T>(int[] p, T[] b, T[] x, int n)
2121
{
22-
Apply(p, b, x, n);
22+
Apply(p, b.AsSpan(), x.AsSpan(), n);
2323
}
2424

2525
/// <summary>
@@ -59,7 +59,7 @@ public static void Apply<T>(int[] p, ReadOnlySpan<T> b, Span<T> x, int n)
5959
/// </remarks>
6060
public static void ApplyInverse<T>(int[] p, T[] b, T[] x, int n)
6161
{
62-
ApplyInverse(p, b, x, n);
62+
ApplyInverse(p, b.AsSpan(), x.AsSpan(), n);
6363
}
6464

6565
/// <summary>

0 commit comments

Comments
 (0)