/LinqLibTests/Array/ArrayExtensionsTests.cs

# · C# · 1040 lines · 835 code · 205 blank · 0 comment · 91 complexity · d72831d539bb9f3778f57515d1e41719 MD5 · raw file

Large files are truncated click here to view the full file

  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using LinqLib.Array;
  4. using LinqLib.Sequence;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. namespace LinqLibTests.Array
  7. {
  8. [TestClass()]
  9. public class ArrayExtensionssTests
  10. {
  11. #region Slice and Fuse
  12. [TestMethod()]
  13. public void Slice2D()
  14. {
  15. const int Y = 10;
  16. const int X = 8;
  17. int i = 0;
  18. int[,] arr1 = new int[Y, X];
  19. List<int[]> expected = new List<int[]>();
  20. for (int y = 0; y < Y; y++)
  21. {
  22. expected.Add(new int[X]);
  23. for (int x = 0; x < X; x++)
  24. {
  25. arr1[y, x] = i;
  26. expected[y][x] = i;
  27. i++;
  28. }
  29. }
  30. List<int[]> actual = arr1.Slice().ToList();
  31. for (int y = 0; y < Y; y++)
  32. for (int x = 0; x < X; x++)
  33. Assert.IsTrue(expected[y][x] == actual[y][x]);
  34. var actual2 = actual.Fuse();
  35. Assert.IsTrue(arr1.ArrayEquals(actual2));
  36. }
  37. [TestMethod()]
  38. public void Slice3D()
  39. {
  40. const int Z = 5;
  41. const int Y = 10;
  42. const int X = 8;
  43. int i = 0;
  44. int[, ,] arr1 = new int[Z, Y, X];
  45. List<int[,]> expected = new List<int[,]>();
  46. for (int z = 0; z < Z; z++)
  47. {
  48. expected.Add(new int[Y, X]);
  49. for (int y = 0; y < Y; y++)
  50. for (int x = 0; x < X; x++)
  51. {
  52. arr1[z, y, x] = i;
  53. expected[z][y, x] = i;
  54. i++;
  55. }
  56. }
  57. List<int[,]> actual = arr1.Slice().ToList();
  58. for (int z = 0; z < Z; z++)
  59. for (int y = 0; y < Y; y++)
  60. for (int x = 0; x < X; x++)
  61. Assert.IsTrue(expected[z][y, x] == actual[z][y, x]);
  62. var actual2 = actual.Fuse();
  63. Assert.IsTrue(arr1.ArrayEquals(actual2));
  64. }
  65. [TestMethod()]
  66. public void Slice4D()
  67. {
  68. const int A = 7;
  69. const int Z = 5;
  70. const int Y = 10;
  71. const int X = 8;
  72. int i = 0;
  73. int[, , ,] arr1 = new int[A, Z, Y, X];
  74. List<int[, ,]> expected = new List<int[, ,]>();
  75. for (int a = 0; a < A; a++)
  76. {
  77. expected.Add(new int[Z, Y, X]);
  78. for (int z = 0; z < Z; z++)
  79. for (int y = 0; y < Y; y++)
  80. for (int x = 0; x < X; x++)
  81. {
  82. arr1[a, z, y, x] = i;
  83. expected[a][z, y, x] = i;
  84. i++;
  85. }
  86. }
  87. List<int[, ,]> actual = arr1.Slice().ToList();
  88. for (int a = 0; a < A; a++)
  89. for (int z = 0; z < Z; z++)
  90. for (int y = 0; y < Y; y++)
  91. for (int x = 0; x < X; x++)
  92. Assert.IsTrue(expected[a][z, y, x] == actual[a][z, y, x]);
  93. var actual2 = actual.Fuse();
  94. Assert.IsTrue(arr1.ArrayEquals(actual2));
  95. }
  96. #endregion
  97. #region Split (up to 4D - any type)
  98. [TestMethod()]
  99. public void Split1D()
  100. {
  101. double[] src = new double[] { 1, 2, 3, 4, 5, 6, 7, 8 };
  102. var actual1 = src.CircularShift(4).ToArray().Fuse();
  103. var actual2 = src.CircularShift(3, 2).ToArray().Fuse();
  104. var expected1 = ArrayReader.GetDoubleArray("Split1D-1");
  105. var expected2 = ArrayReader.GetDoubleArray("Split1D-2");
  106. Assert.IsTrue(src.Split(3).ToArray().Fuse().ArrayEquals(src.CircularShift(3, 3).ToArray().Fuse()));
  107. Assert.IsTrue(actual1.ArrayEquals(expected1));
  108. Assert.IsTrue(actual2.ArrayEquals(expected2));
  109. }
  110. [TestMethod()]
  111. public void Split2D()
  112. {
  113. double[,] src = Enumerator.Generate<double>(1, 1, 12).ToArray(3, 4);
  114. var actual1 = src.CircularShift(2, 2).ToArray().Fuse();
  115. var actual2 = src.CircularShift(1, 2, 2, 2).ToArray().Fuse();
  116. var expected1 = ArrayReader.GetDoubleArray("Split2D-1");
  117. var expected2 = ArrayReader.GetDoubleArray("Split2D-2");
  118. Assert.IsTrue(src.Split(2, 2).ToArray().Fuse().ArrayEquals(src.CircularShift(2, 2, 2, 2).ToArray().Fuse()));
  119. Assert.IsTrue(actual1.ArrayEquals(expected1));
  120. Assert.IsTrue(actual2.ArrayEquals(expected2));
  121. }
  122. [TestMethod()]
  123. public void Split3D()
  124. {
  125. double[, ,] src = Enumerator.Generate<double>(1, 1, 36).ToArray(3, 3, 4);
  126. var actual1 = src.CircularShift(2, 2, 2).ToArray().Fuse();
  127. var actual2 = src.CircularShift(1, 2, 2, 1, 2, 2).ToArray().Fuse();
  128. var expected1 = ArrayReader.GetDoubleArray("Split3D-1");
  129. var expected2 = ArrayReader.GetDoubleArray("Split3D-2");
  130. Assert.IsTrue(src.Split(2, 2, 2).ToArray().Fuse().ArrayEquals(src.CircularShift(2, 2, 2, 2, 2, 2).ToArray().Fuse()));
  131. Assert.IsTrue(actual1.ArrayEquals(expected1));
  132. Assert.IsTrue(actual2.ArrayEquals(expected2));
  133. }
  134. [TestMethod()]
  135. public void Split4D()
  136. {
  137. double[, , ,] src = Enumerator.Generate<double>(1, 1, 256).ToArray(3, 3, 3, 3);
  138. var actual1 = src.CircularShift(2, 2, 3, 2).ToArray();
  139. var actual2 = src.CircularShift(1, 2, 2, 1, 2, 1, 1, 2).ToArray();
  140. var actual3 = src.Split(2, 2, 2, 2).ToArray();
  141. var actual4 = src.CircularShift(2, 2, 2, 2, 2, 2, 2, 2).ToArray();
  142. for (int i = 0; i < actual1.Length; i++)
  143. Assert.IsTrue(actual1[i].ArrayEquals(ArrayReader.GetDoubleArray(string.Format("Split4D-1.{0}", i))));
  144. for (int i = 0; i < actual2.Length; i++)
  145. Assert.IsTrue(actual2[i].ArrayEquals(ArrayReader.GetDoubleArray(string.Format("Split4D-2.{0}", i))));
  146. Assert.IsTrue(actual3.Length == actual4.Length);
  147. for (int i = 0; i < actual3.Length; i++)
  148. Assert.IsTrue(actual3[i].ArrayEquals(actual4[i]));
  149. }
  150. #endregion
  151. #region ToArray and AsEnumerable
  152. [TestMethod()]
  153. public void ToArrayTest()
  154. {
  155. int[] expected1 = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0 };
  156. int[] actual1 = Enumerable.Range(0, 16).ToArray(20);
  157. Assert.IsTrue(expected1.ArrayEquals(actual1));
  158. int[,] expected2 = new int[,] { { 0, 1, 2, 3, 4, 5, 6, 7 }, { 8, 9, 10, 11, 12, 13, 0, 0 } };
  159. int[,] actual2 = Enumerable.Range(0, 14).ToArray(2, 8);
  160. Assert.IsTrue(expected2.ArrayEquals(actual2));
  161. int[, ,] expected3 = new int[,,] { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } }, { { 9, 10, 11 }, { 12, 13, 14 }, { 15, 16, 17 } }, { { 18, 19, 20 }, { 21, 22, 23 }, { 24, 25, 0 } } };
  162. int[, ,] actual3 = Enumerable.Range(0, 26).ToArray(3, 3, 3);
  163. Assert.IsTrue(expected3.ArrayEquals(actual3));
  164. int[, , ,] expected4 = new int[,,,] { { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } }, { { 9, 10, 11 }, { 12, 13, 14 }, { 15, 16, 17 } } }, { { { 18, 19, 20 }, { 21, 22, 23 }, { 24, 25, 26 } }, { { 27, 28, 29 }, { 30, 31, 32 }, { 0, 0, 0 } } } };
  165. int[, , ,] actual4 = Enumerable.Range(0, 33).ToArray(2, 2, 3, 3);
  166. Assert.IsTrue(expected4.ArrayEquals(actual4));
  167. string[] expectedc1 = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P" };
  168. string[] actualc1 = Enumerator.Generate<string>(65, 16, (X) => ((char)X).ToString()).ToArray(16);
  169. Assert.IsTrue(expectedc1.ArrayEquals(actualc1));
  170. string[,] expectedc2 = new string[,] { { "A", "B", "C", "D", "E", "F", "G", "H" }, { "I", "J", "K", "L", "M", "N", "O", "P" } };
  171. string[,] actualc2 = Enumerator.Generate(65, 16, (X) => ((char)X).ToString()).ToArray(2, 8);
  172. Assert.IsTrue(expected2.ArrayEquals(actual2));
  173. string[, ,] expectedc3 = new string[,,] { { { "A", "B", "C" }, { "D", "E", "F" }, { "G", "H", "I" } }, { { "J", "K", "L" }, { "M", "N", "O" }, { "P", "Q", "R" } }, { { "S", "T", "U" }, { "V", "W", "X" }, { "Y", "Z", "[" } } };
  174. string[, ,] actualc3 = Enumerator.Generate(65, 27, (X) => ((char)X).ToString()).ToArray(3, 3, 3);
  175. Assert.IsTrue(expectedc3.ArrayEquals(actualc3));
  176. string[, , ,] expectedc4 = { { { { "A", "B", "C" }, { "D", "E", "F" }, { "G", "H", "I" } }, { { "J", "K", "L" }, { "M", "N", "O" }, { "P", "Q", "R" } } }, { { { "S", "T", "U" }, { "V", "W", "X" }, { "Y", "Z", "[" } }, { { "\\", "]", "^" }, { "_", "`", "a" }, { "b", "c", "d" } } } };
  177. string[, , ,] actualc4 = Enumerator.Generate(65, 36, (X) => ((char)X).ToString()).ToArray(2, 2, 3, 3);
  178. Assert.IsTrue(expectedc4.ArrayEquals(actualc4));
  179. }
  180. [TestMethod()]
  181. public void AsEnumerableTest()
  182. {
  183. int[, , ,] src1 = Enumerable.Range(0, 625).ToArray(5, 5, 5, 5);
  184. IEnumerable<int> expected1 = Enumerable.Range(0, 625);
  185. Assert.IsTrue(expected1.SequenceEqual(src1.AsEnumerable().OfType<int>()));
  186. int[][][] src2 = new int[][][] { new int[][] { new int[] { 0, 1, 2, 3 }, new int[] { 4 }, new int[] { 5, 6 } },
  187. new int[][] { new int[] { 7, 8 }, new int[] { 9 } },
  188. new int[0][] ,
  189. new int[][]{ new int[] { 10, 11 }, new int[0], new int[] { 12 } } };
  190. int[] actual2 = src2.AsEnumerable<int>().ToArray();
  191. int[] expected2 = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
  192. Assert.IsTrue(expected2.ArrayEquals(actual2));
  193. int[] actual2a = ((System.Array)src2).AsEnumerable().OfType<int>().ToArray();
  194. Assert.IsTrue(expected2.ArrayEquals(actual2a));
  195. }
  196. #endregion
  197. #region Resize
  198. private double add(double a, double b)
  199. {
  200. return a + b;
  201. }
  202. private double sub(double a, double b)
  203. {
  204. return a - b;
  205. }
  206. private double mul(double a, double b)
  207. {
  208. return (float)(a * b);
  209. }
  210. private double div(double a, double b)
  211. {
  212. return (float)(a / b);
  213. }
  214. private float add(float a, float b)
  215. {
  216. return a + b;
  217. }
  218. private float sub(float a, float b)
  219. {
  220. return a - b;
  221. }
  222. private float mul(float a, double b)
  223. {
  224. return (float)(a * b);
  225. }
  226. private float div(float a, double b)
  227. {
  228. return (float)(a / b);
  229. }
  230. [TestMethod()]
  231. public void Resize1DTest()
  232. {
  233. double[] src = Enumerator.Generate<double>(1, 2, 24).ToArray();
  234. Assert.IsTrue(src.Resize(7).ArrayEquals(ArrayReader.GetDoubleArray("Resize1D-1.0.0"), new DoubleComparer(.000001)));
  235. Assert.IsTrue(src.Resize(12).ArrayEquals(ArrayReader.GetDoubleArray("Resize1D-1.0.1"), new DoubleComparer(.000001)));
  236. Assert.IsTrue(src.Resize(17).ArrayEquals(ArrayReader.GetDoubleArray("Resize1D-1.0.2"), new DoubleComparer(.000001)));
  237. Assert.IsTrue(src.Resize(24).ArrayEquals(ArrayReader.GetDoubleArray("Resize1D-1.0.3"), new DoubleComparer(.000001)));
  238. Assert.IsTrue(src.Resize(37).ArrayEquals(ArrayReader.GetDoubleArray("Resize1D-1.0.4"), new DoubleComparer(.000001)));
  239. Assert.IsTrue(src.Resize(47).ArrayEquals(ArrayReader.GetDoubleArray("Resize1D-1.0.5"), new DoubleComparer(.000001)));
  240. Assert.IsTrue(src.Resize(add, sub, mul, div, 7).ArrayEquals(ArrayReader.GetDoubleArray("Resize1D-1.0.0"), new DoubleComparer(.0001)));
  241. Assert.IsTrue(src.Resize(add, sub, mul, div, 12).ArrayEquals(ArrayReader.GetDoubleArray("Resize1D-1.0.1"), new DoubleComparer(.0001)));
  242. Assert.IsTrue(src.Resize(add, sub, mul, div, 17).ArrayEquals(ArrayReader.GetDoubleArray("Resize1D-1.0.2"), new DoubleComparer(.0001)));
  243. Assert.IsTrue(src.Resize(add, sub, mul, div, 24).ArrayEquals(ArrayReader.GetDoubleArray("Resize1D-1.0.3"), new DoubleComparer(.0001)));
  244. Assert.IsTrue(src.Resize(add, sub, mul, div, 37).ArrayEquals(ArrayReader.GetDoubleArray("Resize1D-1.0.4"), new DoubleComparer(.0001)));
  245. Assert.IsTrue(src.Resize(add, sub, mul, div, 47).ArrayEquals(ArrayReader.GetDoubleArray("Resize1D-1.0.5"), new DoubleComparer(.0001)));
  246. float[] src2 = Enumerator.Generate<float>(1, 2, 24).ToArray();
  247. Assert.IsTrue(src2.Resize(7).ArrayEquals(ArrayReader.GetSingleArray("Resize1D-1.0.0"), new SingleComparer(.00001f)));
  248. Assert.IsTrue(src2.Resize(12).ArrayEquals(ArrayReader.GetSingleArray("Resize1D-1.0.1"), new SingleComparer(.00001f)));
  249. Assert.IsTrue(src2.Resize(17).ArrayEquals(ArrayReader.GetSingleArray("Resize1D-1.0.2"), new SingleComparer(.00001f)));
  250. Assert.IsTrue(src2.Resize(24).ArrayEquals(ArrayReader.GetSingleArray("Resize1D-1.0.3"), new SingleComparer(.00001f)));
  251. Assert.IsTrue(src2.Resize(37).ArrayEquals(ArrayReader.GetSingleArray("Resize1D-1.0.4"), new SingleComparer(.00001f)));
  252. Assert.IsTrue(src2.Resize(47).ArrayEquals(ArrayReader.GetSingleArray("Resize1D-1.0.5"), new SingleComparer(.00001f)));
  253. Assert.IsTrue(src2.Resize(add, sub, mul, div, 7).ArrayEquals(ArrayReader.GetSingleArray("Resize1D-1.0.0"), new SingleComparer(.0001f)));
  254. Assert.IsTrue(src2.Resize(add, sub, mul, div, 12).ArrayEquals(ArrayReader.GetSingleArray("Resize1D-1.0.1"), new SingleComparer(.0001f)));
  255. Assert.IsTrue(src2.Resize(add, sub, mul, div, 17).ArrayEquals(ArrayReader.GetSingleArray("Resize1D-1.0.2"), new SingleComparer(.0001f)));
  256. Assert.IsTrue(src2.Resize(add, sub, mul, div, 24).ArrayEquals(ArrayReader.GetSingleArray("Resize1D-1.0.3"), new SingleComparer(.0001f)));
  257. Assert.IsTrue(src2.Resize(add, sub, mul, div, 37).ArrayEquals(ArrayReader.GetSingleArray("Resize1D-1.0.4"), new SingleComparer(.0001f)));
  258. Assert.IsTrue(src2.Resize(add, sub, mul, div, 47).ArrayEquals(ArrayReader.GetSingleArray("Resize1D-1.0.5"), new SingleComparer(.0001f)));
  259. }
  260. [TestMethod()]
  261. public void Resize2DTest()
  262. {
  263. int r = 3 * 5;
  264. double[,] src = Enumerator.Generate<double>(1, 2, r).ToArray(3, 5);
  265. Assert.IsTrue(src.Resize(2, 2).ArrayEquals(ArrayReader.GetDoubleArray("Resize2D-1.0.0"), new DoubleComparer(.000001)));
  266. Assert.IsTrue(src.Resize(2, 12).ArrayEquals(ArrayReader.GetDoubleArray("Resize2D-1.0.1"), new DoubleComparer(.000001)));
  267. Assert.IsTrue(src.Resize(12, 2).ArrayEquals(ArrayReader.GetDoubleArray("Resize2D-1.0.2"), new DoubleComparer(.000001)));
  268. Assert.IsTrue(src.Resize(3, 5).ArrayEquals(ArrayReader.GetDoubleArray("Resize2D-1.0.3"), new DoubleComparer(.000001)));
  269. Assert.IsTrue(src.Resize(12, 12).ArrayEquals(ArrayReader.GetDoubleArray("Resize2D-1.0.4"), new DoubleComparer(.000001)));
  270. Assert.IsTrue(src.Resize(add, sub, mul, div, 2, 2).ArrayEquals(ArrayReader.GetDoubleArray("Resize2D-1.0.0"), new DoubleComparer(.0001)));
  271. Assert.IsTrue(src.Resize(add, sub, mul, div, 2, 12).ArrayEquals(ArrayReader.GetDoubleArray("Resize2D-1.0.1"), new DoubleComparer(.0001)));
  272. Assert.IsTrue(src.Resize(add, sub, mul, div, 12, 2).ArrayEquals(ArrayReader.GetDoubleArray("Resize2D-1.0.2"), new DoubleComparer(.0001)));
  273. Assert.IsTrue(src.Resize(add, sub, mul, div, 3, 5).ArrayEquals(ArrayReader.GetDoubleArray("Resize2D-1.0.3"), new DoubleComparer(.0001)));
  274. Assert.IsTrue(src.Resize(add, sub, mul, div, 12, 12).ArrayEquals(ArrayReader.GetDoubleArray("Resize2D-1.0.4"), new DoubleComparer(.0001)));
  275. float[,] src2 = Enumerator.Generate<float>(1, 2, r).ToArray(3, 5);
  276. Assert.IsTrue(src2.Resize(2, 2).ArrayEquals(ArrayReader.GetSingleArray("Resize2D-1.0.0"), new SingleComparer(.00001f)));
  277. Assert.IsTrue(src2.Resize(2, 12).ArrayEquals(ArrayReader.GetSingleArray("Resize2D-1.0.1"), new SingleComparer(.00001f)));
  278. Assert.IsTrue(src2.Resize(12, 2).ArrayEquals(ArrayReader.GetSingleArray("Resize2D-1.0.2"), new SingleComparer(.00001f)));
  279. Assert.IsTrue(src2.Resize(3, 5).ArrayEquals(ArrayReader.GetSingleArray("Resize2D-1.0.3"), new SingleComparer(.00001f)));
  280. Assert.IsTrue(src2.Resize(12, 12).ArrayEquals(ArrayReader.GetSingleArray("Resize2D-1.0.4"), new SingleComparer(.00001f)));
  281. Assert.IsTrue(src2.Resize(add, sub, mul, div, 2, 2).ArrayEquals(ArrayReader.GetSingleArray("Resize2D-1.0.0"), new SingleComparer(.0001f)));
  282. Assert.IsTrue(src2.Resize(add, sub, mul, div, 2, 12).ArrayEquals(ArrayReader.GetSingleArray("Resize2D-1.0.1"), new SingleComparer(.0001f)));
  283. Assert.IsTrue(src2.Resize(add, sub, mul, div, 12, 2).ArrayEquals(ArrayReader.GetSingleArray("Resize2D-1.0.2"), new SingleComparer(.0001f)));
  284. Assert.IsTrue(src2.Resize(add, sub, mul, div, 3, 5).ArrayEquals(ArrayReader.GetSingleArray("Resize2D-1.0.3"), new SingleComparer(.0001f)));
  285. Assert.IsTrue(src2.Resize(add, sub, mul, div, 12, 12).ArrayEquals(ArrayReader.GetSingleArray("Resize2D-1.0.4"), new SingleComparer(.0001f)));
  286. }
  287. [TestMethod()]
  288. public void Resize3DTest()
  289. {
  290. int r = 3 * 5 * 7;
  291. double[, ,] src = Enumerator.Generate<double>(1, 2, r).ToArray(3, 5, 7);
  292. Assert.IsTrue(src.Resize(2, 2, 3).ArrayEquals(ArrayReader.GetDoubleArray("Resize3D-1.0.0"), new DoubleComparer(.000001)));
  293. Assert.IsTrue(src.Resize(2, 3, 4).ArrayEquals(ArrayReader.GetDoubleArray("Resize3D-1.0.1"), new DoubleComparer(.000001)));
  294. Assert.IsTrue(src.Resize(5, 7, 11).ArrayEquals(ArrayReader.GetDoubleArray("Resize3D-1.0.2"), new DoubleComparer(.000001)));
  295. Assert.IsTrue(src.Resize(3, 5, 7).ArrayEquals(ArrayReader.GetDoubleArray("Resize3D-1.0.3"), new DoubleComparer(.000001)));
  296. Assert.IsTrue(src.Resize(add, sub, mul, div, 2, 2, 3).ArrayEquals(ArrayReader.GetDoubleArray("Resize3D-1.0.0"), new DoubleComparer(.0001)));
  297. Assert.IsTrue(src.Resize(add, sub, mul, div, 2, 3, 4).ArrayEquals(ArrayReader.GetDoubleArray("Resize3D-1.0.1"), new DoubleComparer(.0001)));
  298. Assert.IsTrue(src.Resize(add, sub, mul, div, 5, 7, 11).ArrayEquals(ArrayReader.GetDoubleArray("Resize3D-1.0.2"), new DoubleComparer(.0001)));
  299. Assert.IsTrue(src.Resize(add, sub, mul, div, 3, 5, 7).ArrayEquals(ArrayReader.GetDoubleArray("Resize3D-1.0.3"), new DoubleComparer(.0001)));
  300. float[, ,] src2 = Enumerator.Generate<float>(1, 2, r).ToArray(3, 5, 7);
  301. Assert.IsTrue(src2.Resize(2, 2, 3).ArrayEquals(ArrayReader.GetSingleArray("Resize3D-1.0.0"), new SingleComparer(.0001f)));
  302. Assert.IsTrue(src2.Resize(2, 3, 4).ArrayEquals(ArrayReader.GetSingleArray("Resize3D-1.0.1"), new SingleComparer(.0001f)));
  303. Assert.IsTrue(src2.Resize(5, 7, 11).ArrayEquals(ArrayReader.GetSingleArray("Resize3D-1.0.2"), new SingleComparer(.0001f)));
  304. Assert.IsTrue(src2.Resize(3, 5, 7).ArrayEquals(ArrayReader.GetSingleArray("Resize3D-1.0.3"), new SingleComparer(.0001f)));
  305. Assert.IsTrue(src2.Resize(add, sub, mul, div, 2, 2, 3).ArrayEquals(ArrayReader.GetSingleArray("Resize3D-1.0.0"), new SingleComparer(.0001f)));
  306. Assert.IsTrue(src2.Resize(add, sub, mul, div, 2, 3, 4).ArrayEquals(ArrayReader.GetSingleArray("Resize3D-1.0.1"), new SingleComparer(.0001f)));
  307. Assert.IsTrue(src2.Resize(add, sub, mul, div, 5, 7, 11).ArrayEquals(ArrayReader.GetSingleArray("Resize3D-1.0.2"), new SingleComparer(.0001f)));
  308. Assert.IsTrue(src2.Resize(add, sub, mul, div, 3, 5, 7).ArrayEquals(ArrayReader.GetSingleArray("Resize3D-1.0.3"), new SingleComparer(.0001f)));
  309. }
  310. [TestMethod()]
  311. public void Resize4DTest()
  312. {
  313. int r = 3 * 5 * 7 * 8;
  314. double[, , ,] src = Enumerator.Generate<double>(1, 2, r).ToArray(3, 5, 7, 8);
  315. Assert.IsTrue(src.Resize(2, 3, 4, 5).ArrayEquals(ArrayReader.GetDoubleArray("Resize4D-1.0.0"), new DoubleComparer(.000001)));
  316. Assert.IsTrue(src.Resize(5, 7, 4, 1).ArrayEquals(ArrayReader.GetDoubleArray("Resize4D-1.0.1"), new DoubleComparer(.000001)));
  317. Assert.IsTrue(src.Resize(3, 5, 7, 8).ArrayEquals(ArrayReader.GetDoubleArray("Resize4D-1.0.2"), new DoubleComparer(.000001)));
  318. Assert.IsTrue(src.Resize(add, sub, mul, div, 2, 3, 4, 5).ArrayEquals(ArrayReader.GetDoubleArray("Resize4D-1.0.0"), new DoubleComparer(.0001)));
  319. Assert.IsTrue(src.Resize(add, sub, mul, div, 5, 7, 4, 1).ArrayEquals(ArrayReader.GetDoubleArray("Resize4D-1.0.1"), new DoubleComparer(.0001)));
  320. Assert.IsTrue(src.Resize(add, sub, mul, div, 3, 5, 7, 8).ArrayEquals(ArrayReader.GetDoubleArray("Resize4D-1.0.2"), new DoubleComparer(.0001)));
  321. float[, , ,] src2 = Enumerator.Generate<float>(1, 2, r).ToArray(3, 5, 7, 8);
  322. Assert.IsTrue(src2.Resize(2, 3, 4, 5).ArrayEquals(ArrayReader.GetSingleArray("Resize4D-1.0.0"), new SingleComparer(.0002f)));
  323. Assert.IsTrue(src2.Resize(5, 7, 4, 1).ArrayEquals(ArrayReader.GetSingleArray("Resize4D-1.0.1"), new SingleComparer(.0002f)));
  324. Assert.IsTrue(src2.Resize(3, 5, 7, 8).ArrayEquals(ArrayReader.GetSingleArray("Resize4D-1.0.2"), new SingleComparer(.0002f)));
  325. Assert.IsTrue(src2.Resize(add, sub, mul, div, 2, 3, 4, 5).ArrayEquals(ArrayReader.GetSingleArray("Resize4D-1.0.0"), new SingleComparer(.0002f)));
  326. Assert.IsTrue(src2.Resize(add, sub, mul, div, 5, 7, 4, 1).ArrayEquals(ArrayReader.GetSingleArray("Resize4D-1.0.1"), new SingleComparer(.0002f)));
  327. Assert.IsTrue(src2.Resize(add, sub, mul, div, 3, 5, 7, 8).ArrayEquals(ArrayReader.GetSingleArray("Resize4D-1.0.2"), new SingleComparer(.0002f)));
  328. }
  329. #endregion
  330. #region Rotate
  331. [TestMethod()]
  332. public void Rotate2DTest()
  333. {
  334. int[,] src = Enumerable.Range(0, 12).ToArray(3, 4);
  335. int[,] expectedLeft = new int[,] { { 3, 7, 11 }, { 2, 6, 10 }, { 1, 5, 9 }, { 0, 4, 8 } };
  336. int[,] expectedRight = new int[,] { { 8, 4, 0 }, { 9, 5, 1 }, { 10, 6, 2 }, { 11, 7, 3 } };
  337. int[,] expected180 = new int[,] { { 11, 10, 9, 8 }, { 7, 6, 5, 4 }, { 3, 2, 1, 0 } };
  338. int[,] actualLeft = src.Rotate(270);
  339. int[,] actualRight = src.Rotate(90);
  340. int[,] actual180 = src.Rotate(180);
  341. int[,] actual000 = src.Rotate(360);
  342. try
  343. {
  344. src.Rotate(272);
  345. Assert.Fail();
  346. }
  347. catch (System.ArgumentException ex)
  348. { Assert.IsTrue(ex.ParamName == "angle"); }
  349. catch (System.Exception)
  350. { Assert.Fail(); }
  351. Assert.IsTrue(src.ArrayEquals(src.Rotate(90).Rotate(90).Rotate(90).Rotate(90)));
  352. Assert.IsTrue(src.ArrayEquals(src.Rotate(180).Rotate(90).Rotate(90)));
  353. Assert.IsTrue(src.ArrayEquals(src.Rotate(270).Rotate(90)));
  354. Assert.IsTrue(expectedLeft.ArrayEquals(actualLeft));
  355. Assert.IsTrue(expectedRight.ArrayEquals(actualRight));
  356. Assert.IsTrue(expected180.ArrayEquals(actual180));
  357. Assert.IsTrue(src.ArrayEquals(actual000));
  358. }
  359. [TestMethod()]
  360. public void Rotate3DTest()
  361. {
  362. int[, ,] src = Enumerable.Range(0, 24).ToArray(2, 3, 4);
  363. int[, ,] expected90Z = new int[,,] { { { 8, 4, 0 }, { 9, 5, 1 }, { 10, 6, 2 }, { 11, 7, 3 } }, { { 20, 16, 12 }, { 21, 17, 13 }, { 22, 18, 14 }, { 23, 19, 15 } } };
  364. int[, ,] expected90Y = new int[,,] { { { 3, 15 }, { 7, 19 }, { 11, 23 } }, { { 2, 14 }, { 6, 18 }, { 10, 22 } }, { { 1, 13 }, { 5, 17 }, { 9, 21 } }, { { 0, 12 }, { 4, 16 }, { 8, 20 } } };
  365. int[, ,] expected90X = new int[,,] { { { 12, 13, 14, 15 }, { 0, 1, 2, 3 } }, { { 16, 17, 18, 19 }, { 4, 5, 6, 7 } }, { { 20, 21, 22, 23 }, { 8, 9, 10, 11 } } };
  366. int[, ,] expected270Z = new int[,,] { { { 3, 7, 11 }, { 2, 6, 10 }, { 1, 5, 9 }, { 0, 4, 8 } }, { { 15, 19, 23 }, { 14, 18, 22 }, { 13, 17, 21 }, { 12, 16, 20 } } };
  367. int[, ,] expected270Y = new int[,,] { { { 12, 0 }, { 16, 4 }, { 20, 8 } }, { { 13, 1 }, { 17, 5 }, { 21, 9 } }, { { 14, 2 }, { 18, 6 }, { 22, 10 } }, { { 15, 3 }, { 19, 7 }, { 23, 11 } } };
  368. int[, ,] expected270X = new int[,,] { { { 8, 9, 10, 11 }, { 20, 21, 22, 23 } }, { { 4, 5, 6, 7 }, { 16, 17, 18, 19 } }, { { 0, 1, 2, 3 }, { 12, 13, 14, 15 } } };
  369. int[, ,] expected180Z = new int[,,] { { { 11, 10, 9, 8 }, { 7, 6, 5, 4 }, { 3, 2, 1, 0 } }, { { 23, 22, 21, 20 }, { 19, 18, 17, 16 }, { 15, 14, 13, 12 } } };
  370. int[, ,] expected180Y = new int[,,] { { { 15, 14, 13, 12 }, { 19, 18, 17, 16 }, { 23, 22, 21, 20 } }, { { 3, 2, 1, 0 }, { 7, 6, 5, 4 }, { 11, 10, 9, 8 } } };
  371. int[, ,] expected180X = new int[,,] { { { 20, 21, 22, 23 }, { 16, 17, 18, 19 }, { 12, 13, 14, 15 } }, { { 8, 9, 10, 11 }, { 4, 5, 6, 7 }, { 0, 1, 2, 3 } } };
  372. int[, ,] actual90Z = src.Rotate(RotateAxis.RotateZ, 90);
  373. int[, ,] actual90Y = src.Rotate(RotateAxis.RotateY, 90);
  374. int[, ,] actual90X = src.Rotate(RotateAxis.RotateX, 90);
  375. int[, ,] actual270Z = src.Rotate(RotateAxis.RotateZ, 270);
  376. int[, ,] actual270Y = src.Rotate(RotateAxis.RotateY, 270);
  377. int[, ,] actual270X = src.Rotate(RotateAxis.RotateX, 270);
  378. int[, ,] actual180Z = src.Rotate(RotateAxis.RotateZ, 180);
  379. int[, ,] actual180Y = src.Rotate(RotateAxis.RotateY, 180);
  380. int[, ,] actual180X = src.Rotate(RotateAxis.RotateX, 180);
  381. int[, ,] actual000Z = src.Rotate(RotateAxis.RotateZ, 360);
  382. int[, ,] actual000Y = src.Rotate(RotateAxis.RotateY, 360);
  383. int[, ,] actual000X = src.Rotate(RotateAxis.RotateX, 360);
  384. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateX, 90)));
  385. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateX, 180).Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateX, 90)));
  386. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateX, 270).Rotate(RotateAxis.RotateX, 90)));
  387. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateY, 90)));
  388. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateY, 180).Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateY, 90)));
  389. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateY, 270).Rotate(RotateAxis.RotateY, 90)));
  390. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateZ, 90).Rotate(RotateAxis.RotateZ, 90).Rotate(RotateAxis.RotateZ, 90).Rotate(RotateAxis.RotateZ, 90)));
  391. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateZ, 180).Rotate(RotateAxis.RotateZ, 90).Rotate(RotateAxis.RotateZ, 90)));
  392. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateZ, 270).Rotate(RotateAxis.RotateZ, 90)));
  393. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateZ, 90)
  394. .Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateZ, 90)
  395. .Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateZ, 90)
  396. .Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateZ, 90)));
  397. try
  398. {
  399. src.Rotate(RotateAxis.RotateX, 272);
  400. Assert.Fail();
  401. }
  402. catch (System.ArgumentException ex)
  403. { Assert.IsTrue(ex.ParamName == "angle"); }
  404. catch (System.Exception)
  405. { Assert.Fail(); }
  406. try
  407. {
  408. src.Rotate(RotateAxis.RotateA, 180);
  409. Assert.Fail();
  410. }
  411. catch (System.ArgumentException ex)
  412. { Assert.IsTrue(ex.ParamName == "axis"); }
  413. catch (System.Exception)
  414. { Assert.Fail(); }
  415. Assert.IsTrue(expected90Z.ArrayEquals(actual90Z));
  416. Assert.IsTrue(expected90Y.ArrayEquals(actual90Y));
  417. Assert.IsTrue(expected90X.ArrayEquals(actual90X));
  418. Assert.IsTrue(expected270Z.ArrayEquals(actual270Z));
  419. Assert.IsTrue(expected270Y.ArrayEquals(actual270Y));
  420. Assert.IsTrue(expected270X.ArrayEquals(actual270X));
  421. Assert.IsTrue(expected180Z.ArrayEquals(actual180Z));
  422. Assert.IsTrue(expected180Y.ArrayEquals(actual180Y));
  423. Assert.IsTrue(expected180X.ArrayEquals(actual180X));
  424. Assert.IsTrue(src.ArrayEquals(actual000Z));
  425. Assert.IsTrue(src.ArrayEquals(actual000Y));
  426. Assert.IsTrue(src.ArrayEquals(actual000X));
  427. }
  428. [TestMethod()]
  429. public void Rotate4DTest()
  430. {
  431. int[, , ,] src = Enumerable.Range(0, 360).ToArray(3, 4, 5, 6);
  432. try
  433. {
  434. src.Rotate(RotateAxis.RotateX, 272);
  435. Assert.Fail();
  436. }
  437. catch (System.ArgumentException ex)
  438. { Assert.IsTrue(ex.ParamName == "angle"); }
  439. catch (System.Exception)
  440. { Assert.Fail(); }
  441. try
  442. {
  443. src.Rotate(RotateAxis.RotateB, 180);
  444. Assert.Fail();
  445. }
  446. catch (System.ArgumentException ex)
  447. { Assert.IsTrue(ex.ParamName == "axis"); }
  448. catch (System.Exception)
  449. { Assert.Fail(); }
  450. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateX, 90)));
  451. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateX, 180).Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateX, 90)));
  452. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateX, 270).Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateX, 90)));
  453. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateX, 360).Rotate(RotateAxis.RotateX, 90).Rotate(RotateAxis.RotateX, 90)));
  454. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateX, 450).Rotate(RotateAxis.RotateX, 90)));
  455. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateY, 90)));
  456. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateY, 180).Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateY, 90)));
  457. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateY, 270).Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateY, 90)));
  458. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateY, 360).Rotate(RotateAxis.RotateY, 90).Rotate(RotateAxis.RotateY, 90)));
  459. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateY, 450).Rotate(RotateAxis.RotateY, 90)));
  460. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateZ, 90).Rotate(RotateAxis.RotateZ, 90).Rotate(RotateAxis.RotateZ, 90).Rotate(RotateAxis.RotateZ, 90).Rotate(RotateAxis.RotateZ, 90).Rotate(RotateAxis.RotateZ, 90)));
  461. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateZ, 180).Rotate(RotateAxis.RotateZ, 90).Rotate(RotateAxis.RotateZ, 90).Rotate(RotateAxis.RotateZ, 90).Rotate(RotateAxis.RotateZ, 90)));
  462. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateZ, 270).Rotate(RotateAxis.RotateZ, 90).Rotate(RotateAxis.RotateZ, 90).Rotate(RotateAxis.RotateZ, 90)));
  463. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateZ, 360).Rotate(RotateAxis.RotateZ, 90).Rotate(RotateAxis.RotateZ, 90)));
  464. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateZ, 450).Rotate(RotateAxis.RotateZ, 90)));
  465. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateA, 90).Rotate(RotateAxis.RotateA, 90).Rotate(RotateAxis.RotateA, 90).Rotate(RotateAxis.RotateA, 90).Rotate(RotateAxis.RotateA, 90).Rotate(RotateAxis.RotateA, 90)));
  466. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateA, 180).Rotate(RotateAxis.RotateA, 90).Rotate(RotateAxis.RotateA, 90).Rotate(RotateAxis.RotateA, 90).Rotate(RotateAxis.RotateA, 90)));
  467. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateA, 270).Rotate(RotateAxis.RotateA, 90).Rotate(RotateAxis.RotateA, 90).Rotate(RotateAxis.RotateA, 90)));
  468. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateA, 360).Rotate(RotateAxis.RotateA, 90).Rotate(RotateAxis.RotateA, 90)));
  469. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateA, 450).Rotate(RotateAxis.RotateA, 90)));
  470. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateX, 180).Rotate(RotateAxis.RotateY, 180).Rotate(RotateAxis.RotateZ, 180).Rotate(RotateAxis.RotateA, 180)
  471. .Rotate(RotateAxis.RotateX, 180).Rotate(RotateAxis.RotateY, 180).Rotate(RotateAxis.RotateZ, 180).Rotate(RotateAxis.RotateA, 180)
  472. .Rotate(RotateAxis.RotateX, 180).Rotate(RotateAxis.RotateY, 180).Rotate(RotateAxis.RotateZ, 180).Rotate(RotateAxis.RotateA, 180)
  473. .Rotate(RotateAxis.RotateX, 180).Rotate(RotateAxis.RotateY, 180).Rotate(RotateAxis.RotateZ, 180).Rotate(RotateAxis.RotateA, 180)));
  474. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateA, 540 + 270).Rotate(RotateAxis.RotateA, 270)));
  475. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateNone, 450)));
  476. Assert.IsTrue(src.ArrayEquals(src.Rotate(RotateAxis.RotateNone, 540)));
  477. }
  478. #endregion
  479. #region Flip
  480. [TestMethod()]
  481. public void Flip1DTest()
  482. {
  483. double[] src = Enumerator.Generate<double>(0, 1, 12).ToArray();
  484. Assert.IsTrue(src.Flip().ArrayEquals(ArrayReader.GetDoubleArray("Flip1D-1")));
  485. }
  486. [TestMethod()]
  487. public void Flip2DTest()
  488. {
  489. double[,] src = Enumerator.Generate<double>(0, 1, 12).ToArray(3, 4);
  490. Assert.IsTrue(src.Flip(FlipAxis.None).ArrayEquals(src));
  491. Assert.IsTrue(src.Flip(FlipAxis.FlipX).ArrayEquals(ArrayReader.GetDoubleArray("Flip2D-1.X")));
  492. Assert.IsTrue(src.Flip(FlipAxis.FlipY).ArrayEquals(ArrayReader.GetDoubleArray("Flip2D-1.Y")));
  493. Assert.IsTrue(src.Flip(FlipAxis.FlipXY).ArrayEquals(ArrayReader.GetDoubleArray("Flip2D-1.XY")));
  494. try
  495. {
  496. src.Flip(FlipAxis.FlipZ);
  497. Assert.Fail();
  498. }
  499. catch (System.ArgumentException ex)
  500. {
  501. Assert.IsTrue(ex.ParamName == "axis");
  502. }
  503. catch
  504. {
  505. Assert.Fail();
  506. }
  507. }
  508. [TestMethod()]
  509. public void Flip3DTest()
  510. {
  511. double[, ,] src = Enumerator.Generate<double>(0, 1, 24).ToArray(2, 3, 4);
  512. Assert.IsTrue(src.Flip(FlipAxis.None).ArrayEquals(src));
  513. Assert.IsTrue(src.Flip(FlipAxis.FlipX).ArrayEquals(ArrayReader.GetDoubleArray("Flip3D-1.X")));
  514. Assert.IsTrue(src.Flip(FlipAxis.FlipY).ArrayEquals(ArrayReader.GetDoubleArray("Flip3D-1.Y")));
  515. Assert.IsTrue(src.Flip(FlipAxis.FlipZ).ArrayEquals(ArrayReader.GetDoubleArray("Flip3D-1.Z")));
  516. Assert.IsTrue(src.Flip(FlipAxis.FlipXY).ArrayEquals(ArrayReader.GetDoubleArray("Flip3D-1.XY")));
  517. Assert.IsTrue(src.Flip(FlipAxis.FlipXZ).ArrayEquals(ArrayReader.GetDoubleArray("Flip3D-1.XZ")));
  518. Assert.IsTrue(src.Flip(FlipAxis.FlipYZ).ArrayEquals(ArrayReader.GetDoubleArray("Flip3D-1.YZ")));
  519. Assert.IsTrue(src.Flip(FlipAxis.FlipXYZ).ArrayEquals(ArrayReader.GetDoubleArray("Flip3D-1.XYZ")));
  520. try
  521. {
  522. src.Flip((FlipAxis)63);
  523. Assert.Fail();
  524. }
  525. catch (System.ArgumentException ex)
  526. {
  527. Assert.IsTrue(ex.ParamName == "axis");
  528. }
  529. catch
  530. {
  531. Assert.Fail();
  532. }
  533. }
  534. #endregion
  535. #region Replace and Extract
  536. [TestMethod()]
  537. public void Replace1Test()
  538. {
  539. int[] src1 = Enumerable.Range(0, 12).ToArray(12);
  540. int[] src2 = Enumerable.Range(991, 3).ToArray(3);
  541. int[] expected = Enumerable.Range(0, 12).ToArray(12);
  542. int offset = 3;
  543. for (int i = 0; i < src2.Length; i++)
  544. expected[i + offset] = src2[i];
  545. src1.Replace(src2, offset);
  546. Assert.IsTrue(expected.ArrayEquals(src1));
  547. }
  548. [TestMethod()]
  549. public void Replace2Test()
  550. {
  551. int[,] src1 = Enumerable.Range(0, 12).ToArray(3, 4);
  552. int[,] src2 = Enumerable.Range(991, 4).ToArray(2, 2);
  553. int[,] expected = new int[,] { { 0, 1, 2, 3 }, { 4, 5, 991, 992 }, { 8, 9, 993, 994 } };
  554. int offsetX = 2;
  555. int offsetY = 1;
  556. src1.Replace(src2, offsetY, offsetX);
  557. Assert.IsTrue(expected.ArrayEquals(src1));
  558. }
  559. [TestMethod()]
  560. public void Replace3Test()
  561. {
  562. int[, ,] src1 = Enumerable.Range(0, 27).ToArray(3, 3, 3);
  563. int[, ,] src2 = Enumerable.Range(991, 8).ToArray(2, 2, 2);
  564. int[, ,] expected = new int[,,] { { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } }, { { 9, 10, 11 }, { 12, 991, 992 }, { 15, 993, 994 } }, { { 18, 19, 20 }, { 21, 995, 996 }, { 24, 997, 998 } } };
  565. int offsetX = 1;
  566. int offsetY = 1;
  567. int offsetZ = 1;
  568. src1.Replace(src2, offsetZ, offsetY, offsetX);
  569. Assert.IsTrue(expected.ArrayEquals(src1));
  570. }
  571. [TestMethod()]
  572. public void Replace4Test()
  573. {
  574. int[, , ,] src1 = Enumerable.Range(0, 36).ToArray(2, 2, 3, 3);
  575. int[, , ,] src2 = Enumerable.Range(981, 16).ToArray(2, 2, 2, 2);
  576. int[, , ,] expected = new int[,,,] { { { { 0, 1, 2 }, { 3, 981, 982 }, { 6, 983, 984 } }, { { 9, 10, 11 }, { 12, 985, 986 }, { 15, 987, 988 } } }, { { { 18, 19, 20 }, { 21, 989, 990 }, { 24, 991, 992 } }, { { 27, 28, 29 }, { 30, 993, 994 }, { 33, 995, 996 } } } };
  577. int offsetX = 1;
  578. int offsetY = 1;
  579. int offsetZ = 0;
  580. int offsetA = 0;
  581. src1.Replace(src2, offsetZ, offsetA, offsetY, offsetX);
  582. Assert.IsTrue(expected.ArrayEquals(src1));
  583. }
  584. [TestMethod()]
  585. public void Extract1Test()
  586. {
  587. int[] src1 = Enumerable.Range(0, 12).ToArray(12);
  588. int[] expected = Enumerable.Range(3, 4).ToArray(4);
  589. int offset = 3;
  590. Assert.IsTrue(src1.Extract(offset, 4).ArrayEquals(expected));
  591. }
  592. [TestMethod()]
  593. public void Extract2Test()
  594. {
  595. double[,] src1 = (double[,])ArrayReader.GetDoubleArray("Extract3-0");
  596. double[,] expected = Enumerator.Generate<double>(991, 1, 4).ToArray(2, 2);
  597. double[,] expected2 = new double[,] { { 5, 991 }, { 9, 993 } };
  598. int offsetX = 2;
  599. int offsetY = 1;
  600. Assert.IsTrue(src1.Extract(offsetY, offsetX, 2, 2).ArrayEquals(expected));
  601. offsetX = 1;
  602. offsetY = 1;
  603. Assert.IsTrue(src1.Extract(offsetY, offsetX, 2, 2).ArrayEquals(expected2));
  604. }
  605. [TestMethod()]
  606. public void Extract3Test()
  607. {
  608. double[, ,] src1 = (double[, ,])ArrayReader.GetDoubleArray("Extract3-1");
  609. double[, ,] expected = Enumerator.Generate<double>(991, 1, 8).ToArray(2, 2, 2);
  610. int offsetX = 1;
  611. int offsetY = 1;
  612. int offsetZ = 1;
  613. Assert.IsTrue(src1.Extract(offsetZ, offsetY, offsetX, 2, 2, 2).ArrayEquals(expected));
  614. }
  615. [TestMethod()]
  616. public void Extract4Test()
  617. {
  618. double[, , ,] src1 = (double[, , ,])ArrayReader.GetDoubleArray("Extract4-1");
  619. double[, , ,] expected = Enumerator.Generate<double>(981, 1, 16).ToArray(2, 2, 2, 2);
  620. int offsetX = 1;
  621. int offsetY = 1;
  622. int offsetZ = 0;
  623. int offsetA = 0;
  624. Assert.IsTrue(src1.Extract(offsetA, offsetZ, offsetY, offsetX, 2, 2, 2, 2).ArrayEquals(expected));
  625. }
  626. #endregion
  627. #region Jagged / Fixed Converters
  628. [TestMethod()]
  629. public void ToFromJagged2DTest()
  630. {
  631. int[][] arr = new int[7][];
  632. arr[0] = new int[] { 1, 2, 3, 4, 5 };
  633. arr[1] = null;
  634. arr[2] = new int[0];
  635. arr[3] = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  636. arr[4] = new int[] { 1, 2, 3 };
  637. arr[5] = new int[0];
  638. arr[6] = null;
  639. int[,] actual1 = arr.FromJagged();
  640. int[,] expected1 = new int[,] { { 1, 2, 3, 4, 5, 0, 0, 0, 0 },
  641. { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  642. { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  643. { 1, 2, 3, 4, 5, 6, 7, 8, 9 },
  644. { 1, 2, 3, 0, 0, 0, 0, 0, 0 },
  645. { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  646. { 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
  647. Assert.IsTrue(actual1.ArrayEquals(expected1));
  648. int[,] actual2 = arr.FromJagged(4, 6);
  649. int[,] expected2 = new int[,] { { 1, 2, 3, 4, 5, 0 },
  650. { 0, 0, 0, 0, 0, 0 },
  651. { 0, 0, 0, 0, 0, 0 },
  652. { 1, 2, 3, 4, 5, 6 } };
  653. Assert.IsTrue(actual2.ArrayEquals(expected2));
  654. int[][] actualRT = actual1.ToJagged();
  655. int[][] expectedRT = arr;
  656. for (int i = 0; i < actualRT.Length; i++)
  657. if (actualRT[i] != null)
  658. Assert.IsTrue((actualRT[i].Length == 0 && expectedRT[i] == null) || actualRT[i].ArrayEquals(expectedRT[i]));
  659. int[][] actualRF = actual1.ToJagged(false);
  660. int[][] expectedRF = new int[7][];
  661. expectedRF[0] = new int[] { 1, 2, 3, 4, 5, 0, 0, 0, 0 };
  662. expectedRF[1] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  663. expectedRF[2] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  664. expectedRF[3] = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  665. expectedRF[4] = new int[] { 1, 2, 3, 0, 0, 0, 0, 0, 0 };
  666. expectedRF[5] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  667. expectedRF[6] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  668. for (int i = 0; i < actualRF.Length; i++)
  669. Assert.IsTrue(actualRF[i].ArrayEquals(expectedRF[i]));
  670. }
  671. [TestMethod()]
  672. public void ToFromJagged3DTest()
  673. {
  674. int[][][] arr = GetJagged3DArray();
  675. int[, ,] actual1 = arr.FromJagged();
  676. int[, ,] expected1 = new int[,,] { { { 1, 2, 3, 4, 5, 0 }, { 0, 0, 0, 0, 0, 0 }, { 1, 2, 3, 0, 0, 0 } },
  677. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  678. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  679. { { 1, 2, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0, 0 }, { 1, 2, 0, 0, 0, 0 } },
  680. { { 1, 2, 3, 4, 5, 6 }, { 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  681. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  682. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } } };
  683. Assert.IsTrue(actual1.ArrayEquals(expected1));
  684. int[, ,] actual2 = arr.FromJagged(4, 2, 3);
  685. int[, ,] expected2 = new int[,,] { { { 1, 2, 3 }, { 0, 0, 0 } },
  686. { { 0, 0, 0 }, { 0, 0, 0 } },
  687. { { 0, 0, 0 }, { 0, 0, 0 } },
  688. { { 1, 2, 0 }, { 1, 0, 0 } } };
  689. Assert.IsTrue(actual2.ArrayEquals(expected2));
  690. int[][][] actualRT = actual1.ToJagged();
  691. int[][][] expectedRT = arr;
  692. for (int i1 = 0; i1 < actualRT.Length; i1++)
  693. if (actualRT[i1] != null)
  694. for (int i2 = 0; i2 < actualRT[i1].Length; i2++)
  695. if (actualRT[i1][i2] != null)
  696. Assert.IsTrue((actualRT[i1][i2].Length == 0 && expectedRT[i1][i2] == null) || actualRT[i1][i2].ArrayEquals(expectedRT[i1][i2]));
  697. int[][][] actualRF = actual1.ToJagged(false);
  698. int[][][] expectedRF = GetJagged3DArray2();
  699. for (int i1 = 0; i1 < actualRF.Length; i1++)
  700. if (actualRF[i1] != null)
  701. for (int i2 = 0; i2 < actualRF[i1].Length; i2++)
  702. if (actualRF[i1][i2] != null)
  703. Assert.IsTrue((actualRF[i1][i2].Length == 0 && expectedRF[i1][i2] == null) || actualRF[i1][i2].ArrayEquals(expectedRF[i1][i2]));
  704. }
  705. [TestMethod()]
  706. public void ToFromJagged4DTest()
  707. {
  708. int[][][][] arr = new int[][][][] { GetJagged3DArray(), null, new int[0][][], GetJagged3DArray() };
  709. int[, , ,] actual1 = arr.FromJagged();
  710. int[, , ,] expected1 = new int[,,,] { { { { 1, 2, 3, 4, 5, 0 }, { 0, 0, 0, 0, 0, 0 }, { 1, 2, 3, 0, 0, 0 } },
  711. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  712. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  713. { { 1, 2, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0, 0 }, { 1, 2, 0, 0, 0, 0 } },
  714. { { 1, 2, 3, 4, 5, 6 }, { 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  715. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  716. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } } },
  717. { { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  718. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  719. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  720. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  721. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  722. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  723. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } } },
  724. { { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  725. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  726. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  727. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  728. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  729. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  730. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } } },
  731. { { { 1, 2, 3, 4, 5, 0 }, { 0, 0, 0, 0, 0, 0 }, { 1, 2, 3, 0, 0, 0 } },
  732. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  733. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  734. { { 1, 2, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0, 0 }, { 1, 2, 0, 0, 0, 0 } },
  735. { { 1, 2, 3, 4, 5, 6 }, { 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  736. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
  737. { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } } } };
  738. Assert.IsTrue(actual1.ArrayEquals(expected1));
  739. int[, , ,] actual2 = arr.FromJagged(2, 4, 2, 3);
  740. int[, , ,] expected2 = new int[,,,] { { { { 1, 2, 3 }, { 0, 0, 0 } },
  741. { { 0, 0, 0 }, { 0, 0, 0 } },
  742. { { 0, 0, 0 }, { 0, 0, 0 } },
  743. { { 1, 2, 0 }, { 1, 0, 0 } } },
  744. { { { 0, 0, 0 }, { 0, 0, 0 } },
  745. { { 0, 0, 0 }, { 0, 0, 0 } },
  746. { { 0, 0, 0 }, { 0, 0, 0 } },
  747. { { 0, 0, 0 }, { 0, 0, 0 } } } };
  748. Assert.IsTrue(actual2.ArrayEquals(expected2));
  749. int[][][][] actualRT = actual1.ToJagged();
  750. int[][][][] expectedRT = arr;
  751. for (int i1 = 0; i1 < actualRT.Length; i1++)
  752. if (actualRT[i1] != null)
  753. for (int i2 = 0; i2 < actualRT[i1].Length; i2++)
  754. if (actualRT[i1][i2] != null)
  755. for (int i3 = 0; i3 < actualRT[i1][i2].Length; i3++)
  756. if (actualRT[i1][i2][i3] != null)
  757. Assert.IsTrue((actualRT[i1][i2][i3].Length == 0 && expectedRT[i1][i2][i3] == null) || actualRT[i1][i2][i3].ArrayEquals(expectedRT[i1][i2][i3]));
  758. int[][][][] actualRF = actual1.ToJagged(false);
  759. int[][][][] expectedRF = new int[][][][] { GetJagged3DArray2(), GetJagged3DArray3(), GetJagged3DArray3(), GetJagged3DArray2() };
  760. for (int i1 = 0; i1 < actualRF.Length; i1++)
  761. if (actualRF[i1] != null)
  762. for (int i2 = 0; i2 < actualRF[i1].Length; i2++)
  763. if (actualRF[i1][i2] != null)
  764. for (int i3 = 0; i3 < actualRF[i1][i2].Length; i3++)
  765. if (actualRF[i1][i2][i3] != null)
  766. Assert.IsTrue((actualRF[i1][i2][i3].Length == 0 && expectedRF[i1][i2][i3] == null) || actualRF[i1][i2][i3].ArrayEquals(expectedRF[i1][i2][i3]));
  767. }
  768. private int[][][] GetJagged3DArray()
  769. {
  770. int[][][] arr = new int[7][][];
  771. arr[0] = new int[3][] { new int[] { 1, 2, 3, 4, 5 }, new int[] { }, new int[] { 1, 2, 3 } };
  772. arr[1] = null;
  773. arr[2] = new int[0][];
  774. arr[3] = new int[3][] { new int[] { 1, 2 }, new int[] { 1 }, new int[] { 1, 2 } };
  775. arr[4] = new int[2][] { new int[] { 1, 2, 3, 4, 5, 6 }, new int[] { 1 } };
  776. arr[5] = new int[0][];
  777. arr[6] = null;
  778. return arr;
  779. }
  780. private static int[][][] GetJagged3DArray2()
  781. {
  782. int[][][] arr = new int[7][][];
  783. arr[0] = new int[3][] { new int[] { 1, 2, 3, 4, 5, 0 }, new int[] { 0, 0, 0, 0, 0, 0 }, new int[] { 1, 2, 3, 0, 0, 0 } };
  784. arr[1] = new int[3][] { new int[] { 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0 } };
  785. arr[2] = new i