|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Linq; |
3 | 4 | using System.Text; |
4 | 5 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
5 | 6 |
|
| 7 | +using FluentAssertions; |
| 8 | + |
6 | 9 | namespace NumSharp.UnitTest.Maths |
7 | 10 | { |
8 | 11 | [TestClass] |
9 | 12 | public class NDArrayCumsumTest : TestClass |
10 | 13 | { |
11 | 14 | [TestMethod] |
12 | | - public void CumsumStaticTest() |
| 15 | + public void CumsumStaticFunctionTest() |
13 | 16 | { |
14 | 17 | NDArray arr = new double[] { 0, 1, 4, 2, 5, 6, 2 }; |
15 | 18 | NDArray expected = new double[] { 0, 1, 5, 7, 12, 18, 20 }; |
16 | 19 |
|
17 | 20 | NDArray actual = np.cumsum(arr); |
18 | 21 |
|
19 | | - for (int i = 0; i < expected.shape[0]; i++) |
20 | | - { |
21 | | - Assert.AreEqual((double)expected[i], (double)actual[i]); |
22 | | - } |
| 22 | + actual.Array.Should().BeEquivalentTo(expected.Array); |
23 | 23 | } |
24 | 24 |
|
25 | 25 | [TestMethod] |
26 | | - public void CumsumMemberTest() |
| 26 | + public void CumsumMemberFunctionTest() |
27 | 27 | { |
28 | 28 | NDArray arr = new double[] { 0, 1, 4, 2, 5, 6, 2 }; |
29 | 29 | NDArray expected = new double[] { 0, 1, 5, 7, 12, 18, 20 }; |
30 | 30 |
|
31 | 31 | NDArray actual = arr.cumsum(); |
32 | 32 |
|
33 | | - for (int i = 0; i < expected.shape[0]; i++) |
| 33 | + actual.Array.Should().BeEquivalentTo(expected.Array); |
| 34 | + } |
| 35 | + |
| 36 | + [TestMethod] |
| 37 | + public void Cumsum2dTest() |
| 38 | + { |
| 39 | + NDArray arr = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } }; |
| 40 | + NDArray expected = new int[] { 1, 3, 6, 10, 15, 21 }; |
| 41 | + |
| 42 | + NDArray actual = np.cumsum(arr); |
| 43 | + |
| 44 | + actual.Array.Should().BeEquivalentTo(expected.Array); |
| 45 | + } |
| 46 | + |
| 47 | + [TestMethod] |
| 48 | + public void Cumsum2dDtypeTest() |
| 49 | + { |
| 50 | + NDArray arr = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } }; |
| 51 | + NDArray expected = new float[] { 1, 3, 6, 10, 15, 21 }; |
| 52 | + |
| 53 | + NDArray actual = np.cumsum(arr, dtype: typeof(float)); |
| 54 | + |
| 55 | + actual.Array.Should().BeEquivalentTo(expected.Array); |
| 56 | + } |
| 57 | + |
| 58 | + [TestMethod] |
| 59 | + public void Cumsum2dAxisRowsTest() |
| 60 | + { |
| 61 | + NDArray arr = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } }; |
| 62 | + NDArray expected = new int[,] { { 1, 2, 3 }, { 5, 7, 9 } }; |
| 63 | + |
| 64 | + NDArray actual = np.cumsum(arr, axis: 0); |
| 65 | + |
| 66 | + for (int i = 0; i < actual.shape[0]; i++) |
| 67 | + { |
| 68 | + for (int j = 0; j < actual.shape[1]; j++) |
| 69 | + { |
| 70 | + Assert.AreEqual((int)expected[i, j], (int)actual[i, j]); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + [TestMethod] |
| 76 | + public void Cumsum2dAxisColsTest() |
| 77 | + { |
| 78 | + NDArray arr = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } }; |
| 79 | + NDArray expected = new int[,] { { 1, 3, 6 }, { 4, 9, 15 } }; |
| 80 | + |
| 81 | + NDArray actual = np.cumsum(arr, axis: 1); |
| 82 | + |
| 83 | + for (int i = 0; i < actual.shape[0]; i++) |
34 | 84 | { |
35 | | - Assert.AreEqual((double)expected[i], (double)actual[i]); |
| 85 | + for (int j = 0; j < actual.shape[1]; j++) |
| 86 | + { |
| 87 | + Assert.AreEqual((int)expected[i, j], (int)actual[i, j]); |
| 88 | + } |
36 | 89 | } |
37 | 90 | } |
38 | 91 | } |
|
0 commit comments