using System; using System.Linq; using Developpez.Dotnet.Collections; using NUnit.Framework; namespace Developpez.Dotnet.Tests.Collections { [TestFixture] public class ArrayExtensionsTests { [Test] public void Test_GetSegment() { var array = new int[] { 5, 8, 15, 16, 23, 42 }; var segment = array.GetSegment(); Assert.AreEqual(array, segment.Array); Assert.AreEqual(0, segment.Offset); Assert.AreEqual(array.Length, segment.Count); segment = array.GetSegment(2); Assert.AreEqual(array, segment.Array); Assert.AreEqual(2, segment.Offset); Assert.AreEqual(array.Length - 2, segment.Count); segment = array.GetSegment(2, 3); Assert.AreEqual(array, segment.Array); Assert.AreEqual(2, segment.Offset); Assert.AreEqual(3, segment.Count); } [Test] public void Test_Segment_AsEnumerable() { var array = new int[] { 5, 8, 15, 16, 23, 42 }; var segment = array.GetSegment(2, 3); var expected = array.Skip(2).Take(3); var actual = segment.AsEnumerable(); CollectionAssert.AreEqual(expected, actual); } [Test] public void Test_Segment_ToArray() { var array = new int[] { 5, 8, 15, 16, 23, 42 }; var segment = array.GetSegment(2, 3); var expected = array.Skip(2).Take(3).ToArray(); var actual = segment.ToArray(); CollectionAssert.AreEqual(expected, actual); } [Test] public void Test_ShiftLeft() { int[] array = new[] { 0, 4, 8, 15, 16, 23 }; int[] expected = new[] { 4, 8, 15, 16, 23, 42 }; int[] actual = array.ShiftLeft(42); CollectionAssert.AreEqual(expected, actual); } [Test] public void Test_ShiftRight() { int[] array = new[] { 8, 15, 16, 23, 42, 0 }; int[] expected = new[] { 4, 8, 15, 16, 23, 42 }; int[] actual = array.ShiftRight(4); CollectionAssert.AreEqual(expected, actual); } [Test] public void Test_RotateLeft() { int[] array = new[] { 42, 4, 8, 15, 16, 23 }; int[] expected = new[] { 4, 8, 15, 16, 23, 42 }; int[] actual = array.RotateLeft(); CollectionAssert.AreEqual(expected, actual); } [Test] public void Test_RotateRight() { int[] array = new[] { 8, 15, 16, 23, 42, 4 }; int[] expected = new[] { 4, 8, 15, 16, 23, 42 }; int[] actual = array.RotateRight(); CollectionAssert.AreEqual(expected, actual); } [Test] public void Test_GetRows() { int[,] array = new[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, }; var rows = array.GetRows(); int[][] expected = new[] { new[] { 1, 2 }, new[] { 3, 4 }, new[] { 5, 6 }, }; int[][] actual = rows.ToArray(); Assert.AreEqual(expected, actual); } [Test] public void Test_ToJaggedArray() { int[,] array = new[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, }; int[][] expected = new[] { new[] { 1, 2 }, new[] { 3, 4 }, new[] { 5, 6 }, }; int[][] actual = array.ToJaggedArray(); Assert.IsTrue(AreEqual(expected, actual)); } [Test] public void Test_ToBidimensionalArray() { int[][] jaggedArray = new[] { new[] { 1, 2 }, new[] { 3, 4 }, new[] { 5, 6, 42 }, }; int[,] expected = new[,] { { 1, 2, 0 }, { 3, 4, 0 }, { 5, 6, 42 }, }; int[,] actual = jaggedArray.ToBidimensionalArray(); Assert.IsTrue(AreEqual(expected, actual)); } [Test] public void Test_ToBidimensionalArrayThrowsIfRowTooLong() { int[][] jaggedArray = new[] { new[] { 1, 2 }, new[] { 3, 4 }, new[] { 5, 6, 42 }, }; Assert.Throws(() => jaggedArray.ToBidimensionalArray(true)); } #region Helpers private static bool AreEqual(T[][] x, T[][] y) { if (x.Length != y.Length) return false; for (int i = 0; i < x.Length; i++) { T[] xRow = x[i]; T[] yRow = y[i]; if (xRow.Length != yRow.Length) return false; for (int j = 0; j < xRow.Length; j++) { if (!Equals(xRow[j], yRow[j])) return false; } } return true; } private static bool AreEqual(T[,] x, T[,] y) { if (x.GetLength(0) != y.GetLength(0)) return false; if (x.GetLength(1) != y.GetLength(1)) return false; int rows = x.GetLength(0); int columns = x.GetLength(1); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { if (!Equals(x[i, j], y[i, j])) return false; } } return true; } #endregion } }