|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 6 | + |
| 7 | +using FluentAssertions; |
| 8 | + |
| 9 | +namespace NumSharp.UnitTest.Maths |
| 10 | +{ |
| 11 | + [TestClass] |
| 12 | + public class NDArrayCumsumTest : TestClass |
| 13 | + { |
| 14 | + [TestMethod] |
| 15 | + public void CumsumStaticFunctionTest() |
| 16 | + { |
| 17 | + NDArray arr = new double[] { 0, 1, 4, 2, 5, 6, 2 }; |
| 18 | + NDArray expected = new double[] { 0, 1, 5, 7, 12, 18, 20 }; |
| 19 | + |
| 20 | + NDArray actual = np.cumsum(arr); |
| 21 | + |
| 22 | + actual.Array.Should().BeEquivalentTo(expected.Array); |
| 23 | + } |
| 24 | + |
| 25 | + [TestMethod] |
| 26 | + public void CumsumMemberFunctionTest() |
| 27 | + { |
| 28 | + NDArray arr = new double[] { 0, 1, 4, 2, 5, 6, 2 }; |
| 29 | + NDArray expected = new double[] { 0, 1, 5, 7, 12, 18, 20 }; |
| 30 | + |
| 31 | + NDArray actual = arr.cumsum(); |
| 32 | + |
| 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++) |
| 84 | + { |
| 85 | + for (int j = 0; j < actual.shape[1]; j++) |
| 86 | + { |
| 87 | + Assert.AreEqual((int)expected[i, j], (int)actual[i, j]); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments