Esempio n. 1
0
        public void RotationTest()
        {
            string[, ] matrix0 = new string[, ]
            {
                { "a", "b", "c", "d", "e" },
                { "g", "h", "i", "j", "k" },
                { "l", "m", "n", "o", "p" },
            };
            string[, ] rotate1 = new string[, ] // matrix0 clockwise rotated 90-degree
            {
                { "l", "g", "a" },
                { "m", "h", "b" },
                { "n", "i", "c" },
                { "o", "j", "d" },
                { "p", "k", "e" },
            };
            string[, ] rotate2 = new string[, ] // matrix0 clockwise rotated 180-degree, or flip vertically then horizontally
            {
                { "p", "o", "n", "m", "l" },
                { "k", "j", "i", "h", "g" },
                { "e", "d", "c", "b", "a" },
            };
            string[, ] rotate3 = new string[, ] // matrix0 clockwise rotated 270-degree
            {
                { "e", "k", "p" },
                { "d", "j", "o" },
                { "c", "i", "n" },
                { "b", "h", "m" },
                { "a", "g", "l" },
            };

            string[, ] matrix1 = Questions.GetMatrixRotation(matrix0, 90);
            string[, ] matrix2 = Questions.GetMatrixRotation(matrix0, 180);
            string[, ] matrix3 = Questions.GetMatrixRotation(matrix0, 270);

            Assert.IsTrue(matrix1.Compare(rotate1));
            Assert.IsTrue(matrix2.Compare(rotate2));
            Assert.IsTrue(matrix3.Compare(rotate3));
        }
Esempio n. 2
0
        public void CommonExtensionsMatrixTest()
        {
            #region Array/Matrix Test Data

            string[, ] matrix0 = new string[, ]
            {
                { "a", "b", "c", "d", "e" },
                { "g", "h", "i", "j", "k" },
            };
            string[, ] matrix1 = new string[, ] // matrix0 clockwise rotated 90-degree
            {
                { "g", "a" },
                { "h", "b" },
                { "i", "c" },
                { "j", "d" },
                { "k", "e" },
            };
            string[, ] matrix2 = new string[, ] // matrix0 clockwise rotated 180-degree, or flip vertically then horizontally
            {
                { "k", "j", "i", "h", "g" },
                { "e", "d", "c", "b", "a" },
            };
            string[, ] matrix3 = new string[, ] // matrix0 counter-clockwise rotated
            {
                { "e", "k" },
                { "d", "j" },
                { "c", "i" },
                { "b", "h" },
                { "a", "g" },
            };
            string[, ] matrix4 = new string[, ] // matrix0 flip horizontally
            {
                { "e", "d", "c", "b", "a" },
                { "k", "j", "i", "h", "g" },
            };
            string[, ] matrix5 = new string[, ] // matrix1 flip horizontally or matrix3 flip vertically
            {
                { "a", "g" },
                { "b", "h" },
                { "c", "i" },
                { "d", "j" },
                { "e", "k" },
            };
            string[, ] matrix6 = new string[, ] // matrix2 flip horizontally
            {
                { "g", "h", "i", "j", "k" },
                { "a", "b", "c", "d", "e" },
            };

            #endregion

            #region 2-D Array Test

            string[, ] result1 = matrix0.RotateClockwise();
            string[, ] result2 = matrix0.RotateClockwise180();
            string[, ] result3 = matrix0.RotateCounterClockwise();
            string[, ] result4 = matrix0.FlipHorizontally();
            string[, ] result5 = matrix3.FlipVertically();
            string[, ] result6 = matrix2.FlipHorizontally();

            Assert.IsFalse(result1.Compare(matrix0));
            Assert.IsFalse(result2.Compare(matrix0));
            Assert.IsFalse(result3.Compare(matrix0));
            Assert.IsFalse(result4.Compare(matrix0));
            Assert.IsFalse(result5.Compare(matrix0));
            Assert.IsFalse(result6.Compare(matrix0));

            Assert.IsTrue(result1.Compare(matrix1));
            Assert.IsTrue(result1.Compare(matrix1.Clone <string>()));
            Assert.IsTrue(result2.Compare(matrix2));
            Assert.IsTrue(result3.Compare(matrix3));
            Assert.IsTrue(result4.Compare(matrix4));
            Assert.IsTrue(result5.Compare(matrix5));
            Assert.IsTrue(result5.Compare(matrix3.FlipVertically()));
            Assert.IsTrue(result5.Compare(matrix0.RotateClockwise().FlipHorizontally()));
            Assert.IsTrue(result6.Compare(matrix6));

            object[, ] matrix7 = new object[, ]
            {
                { "a", "b", "c", "d", "e" },
                { "g", "h", "i", "j", "k" },
            };
            object[, ] result7 = new object[, ]
            {
                { "a", "b", 102, "d", "e" },
                { "g", "h", "i", "j", "k" },
            };

            Assert.IsFalse(result7.Compare(matrix7));

            #endregion

            #region Matrix Test

            Matrix <string> objectMatrix = new Matrix <string>(matrix0);
            Matrix <string> clonedMatrix = objectMatrix.Clone();
            Assert.IsTrue(objectMatrix.Compare(clonedMatrix));

            Assert.IsTrue(result1.Compare(objectMatrix.RotateClockwise()));
            Assert.IsTrue(result2.Compare(objectMatrix.RotateClockwise180()));
            Assert.IsTrue(result3.Compare(objectMatrix.RotateCounterClockwise()));
            Assert.IsTrue(result4.Compare(objectMatrix.FlipHorizontally()));
            Assert.IsTrue(result5.Compare(objectMatrix.RotateCounterClockwise().FlipVertically()));
            Assert.IsTrue(result6.Compare(objectMatrix.FlipVertically()));

            for (int x = 0; x < objectMatrix.Rows; x++)
            {
                for (int y = 0; y < objectMatrix.Columns; y++)
                {
                    Assert.IsTrue(objectMatrix[x, y] == clonedMatrix[x, y]);
                }
            }

            object[, ] matrix8 = new object[, ]
            {
                { "00", "01", "02", "03", "04" },
                { "10", "11", "12", "13", "14" },
                { "20", "21", "22", "23", "24" },
                { "30", "31", "32", "33", "34" },
                { "40", "41", "42", "43", "44" },
            };
            object[, ] result8 = new object[, ]
            {
                { "00", "10", "20", "30", "40" },
                { "01", "11", "21", "31", "41" },
                { "02", "12", "22", "32", "42" },
                { "03", "13", "23", "33", "43" },
                { "04", "14", "24", "34", "44" },
            };

            matrix8.RotateInline(MatrixRotationType.Clockwise);
            Assert.IsFalse(result8.Compare(matrix8));
            Assert.IsTrue(result8.Compare(matrix8.FlipHorizontally()));

            matrix8.RotateInline(MatrixRotationType.Clockwise180);
            Assert.IsFalse(result8.Compare(matrix8));

            matrix8.RotateInline(MatrixRotationType.Clockwise);
            matrix8.RotateInline(MatrixRotationType.CounterClockwise);
            Assert.IsTrue(result8.Compare(matrix8.FlipVertically()));

            #endregion
        }