Ejemplo n.º 1
0
        public void ToDoubleMatrixTest()
        {
            // value is null
            {
                DoubleMatrix actual = (DoubleMatrixRow)(null);

                Assert.IsNull(actual);

                actual = DoubleMatrixRow.ToDoubleMatrix(null);

                Assert.IsNull(actual);
            }

            // value is not null
            {
                DoubleMatrix matrix = DoubleMatrix.Dense(2, 3,
                                                         new double[6] {
                    1, 2, 3, 4, 5, 6
                });
                var rows = matrix.AsRowCollection();

                for (int i = 0; i < matrix.NumberOfRows; i++)
                {
                    var          expected = matrix[i, ":"];
                    DoubleMatrix actual   = rows[i];
                    DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);

                    actual = DoubleMatrixRow.ToDoubleMatrix(rows[i]);
                    DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);
                }
            }
        }
Ejemplo n.º 2
0
        public void LengthTest()
        {
            DoubleMatrix matrix = DoubleMatrix.Dense(2, 3,
                                                     new double[6] {
                1, 2, 3, 4, 5, 6
            });
            var rows = matrix.AsRowCollection();

            DoubleMatrixRow target = rows[0];
            var             actual = target.Length;

            var expected = matrix.NumberOfColumns;

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 3
0
        public void NotifyPropertyChangedTest()
        {
            DoubleMatrix matrix = DoubleMatrix.Dense(2, 3,
                                                     new double[6] {
                1, 2, 3, 4, 5, 6
            });

            var rows = matrix.AsRowCollection();

            DoubleMatrixRow target = rows[0];

            target.PropertyChanged += new PropertyChangedEventHandler(
                this.PropertyChangedEventHandler);

            string propertyName = "UNKNOWN";

            target.NotifyPropertyChanged(propertyName);
        }
Ejemplo n.º 4
0
        public void ZDataTest()
        {
            var matrix = DoubleMatrix.Dense(2, 3,
                                            new double[6] {
                1, 2, 3, 4, 5, 6
            });
            var rows = matrix.AsRowCollection();

            int dataColumn = 1;

            rows.ZDataColumn = dataColumn;

            DoubleMatrixRow row = rows[0];

            // Same value: unspecific notification
            var subscriber = new PropertyChangedSubscriber();

            row.PropertyChanged += subscriber.PropertyChangedEventHandler;

            double expected = matrix[row.Index, dataColumn];

            row.ZData = expected;
            ArrayAssert <string> .AreEqual(
                expected : new string[] { "" },
                actual : subscriber.PropertyNames.ToArray());

            var actual = row.ZData;

            Assert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);

            // Different value: specific notification
            subscriber           = new PropertyChangedSubscriber();
            row.PropertyChanged += subscriber.PropertyChangedEventHandler;

            row.ZData = -1;
            Assert.AreEqual(
                matrix[row.Index, dataColumn],
                row.ZData,
                DoubleMatrixTest.Accuracy);
            ArrayAssert <string> .AreEqual(
                expected : new string[] { "", "ZData", "[1]" },
                actual : subscriber.PropertyNames.ToArray());
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Tests the <see cref="IComparable{T}.CompareTo(T)"/> implementation
        /// of the <see cref="DoubleMatrixRow"/> class when dealing with nulls.
        /// </summary>
        /// <param name="obj">An object not <b>null</b>.</param>
        /// <remarks>
        /// <para>
        /// Objects being <b>null</b> are only equal to other <b>null</b>
        /// objects. They are less than every other non <b>null</b> object.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="obj"/> is <b>null</b>.
        /// </exception>
        public static void CompareToWithNulls(DoubleMatrixRow obj)
        {
            if (obj is null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            // <=

            Assert.IsTrue((DoubleMatrixRow)null <= (DoubleMatrixRow)null);

            Assert.IsTrue((DoubleMatrixRow)null <= obj);

            Assert.IsFalse(obj <= (DoubleMatrixRow)null);

            // <

            Assert.IsFalse((DoubleMatrixRow)null < (DoubleMatrixRow)null);

            Assert.IsTrue((DoubleMatrixRow)null < obj);

            Assert.IsFalse(obj < (DoubleMatrixRow)null);

            // >=

            Assert.IsTrue((DoubleMatrixRow)null >= (DoubleMatrixRow)null);

            Assert.IsFalse((DoubleMatrixRow)null >= obj);

            Assert.IsTrue(obj >= (DoubleMatrixRow)null);

            // >

            Assert.IsFalse((DoubleMatrixRow)null > (DoubleMatrixRow)null);

            Assert.IsFalse((DoubleMatrixRow)null > obj);

            Assert.IsTrue(obj > (DoubleMatrixRow)null);
        }
Ejemplo n.º 6
0
        public void IndexerStringSetTest()
        {
            // columnIndex not representing an integer
            {
                DoubleMatrix matrix = DoubleMatrix.Dense(2, 3,
                                                         new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var rows = matrix.AsRowCollection();

                var target = rows[0];

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    target["A"] = -1;
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_CANNOT_PARSE_AS_INT32"),
                    expectedParameterName: "columnIndex");
            }

            // columnIndex is less than 0
            {
                DoubleMatrix matrix = DoubleMatrix.Dense(2, 3,
                                                         new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var rows = matrix.AsRowCollection();

                var target = rows[0];

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    target["-1"] = -1;
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_TAB_INDEX_EXCEEDS_DIMS"),
                    expectedParameterName: "columnIndex");
            }

            // columnIndex is greater than NumberOfColumns - 1
            {
                DoubleMatrix matrix = DoubleMatrix.Dense(2, 3,
                                                         new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var rows = matrix.AsRowCollection();

                var target = rows[0];

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    target["3"] = -1;
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_TAB_INDEX_EXCEEDS_DIMS"),
                    expectedParameterName: "columnIndex");
            }

            // columnIndex is inside the bounds
            {
                var subscriber = new PropertyChangedSubscriber();

                var matrix = DoubleMatrix.Dense(2, 3,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });
                var rows = matrix.AsRowCollection();

                int dataColumn = 2;
                rows.XDataColumn = dataColumn;
                rows.YDataColumn = dataColumn;
                rows.ZDataColumn = dataColumn;

                int             rowIndex = 1;
                DoubleMatrixRow row      = rows[rowIndex];
                row.PropertyChanged += subscriber.PropertyChangedEventHandler;

                row[dataColumn] = -1;
                Assert.AreEqual(
                    matrix[rowIndex, dataColumn],
                    row["2"],
                    DoubleMatrixTest.Accuracy);
                ArrayAssert <string> .AreEqual(
                    expected : new string[] { "", "[2]", "XData", "YData", "ZData" },
                    actual : subscriber.PropertyNames.ToArray());

                row[0] = -1;
                Assert.AreEqual(
                    matrix[rowIndex, dataColumn],
                    row["2"],
                    DoubleMatrixTest.Accuracy);
                ArrayAssert <string> .AreEqual(
                    expected : new string[] { "", "[2]", "XData", "YData", "ZData", "[0]" },
                    actual : subscriber.PropertyNames.ToArray());
            }
        }
Ejemplo n.º 7
0
        public void CompareToTest()
        {
            int             expected, actual;
            DoubleMatrix    matrix, otherMatrix;
            DoubleMatrixRow thisRow, otherRow;

            matrix = DoubleMatrix.Dense(3, 3,
                                        new double[9] {
                1, 2, 3, 4, 5, 6, 1, 2, 3
            },
                                        StorageOrder.RowMajor);

            var rows = matrix.AsRowCollection();

            // m = [  1  2  3
            //        4  5  6
            //        1  2  3

            // STRONGLY TYPED COMPARISON

            // Length difference > 0

            otherMatrix = DoubleMatrix.Dense(3, 4);
            var otherRows = otherMatrix.AsRowCollection();

            thisRow  = rows[0];
            otherRow = otherRows[0];

            expected = -1;
            actual   = thisRow.CompareTo(otherRow);

            Assert.AreEqual(expected, actual);
            Assert.IsTrue(thisRow < otherRow);
            Assert.IsTrue(thisRow <= otherRow);
            Assert.IsFalse(thisRow > otherRow);
            Assert.IsFalse(thisRow >= otherRow);

            Assert.IsTrue(thisRow != otherRow);
            Assert.IsFalse(thisRow == otherRow);
            Assert.IsFalse(thisRow.Equals(otherRow));

            // Length difference < 0

            otherMatrix = DoubleMatrix.Dense(3, 2);
            otherRows   = otherMatrix.AsRowCollection();

            thisRow  = rows[0];
            otherRow = otherRows[0];

            expected = 1;
            actual   = thisRow.CompareTo(otherRow);

            Assert.AreEqual(expected, actual);

            Assert.IsFalse(thisRow < otherRow);
            Assert.IsFalse(thisRow <= otherRow);
            Assert.IsTrue(thisRow > otherRow);
            Assert.IsTrue(thisRow >= otherRow);

            Assert.IsTrue(thisRow != otherRow);
            Assert.IsFalse(thisRow == otherRow);
            Assert.IsFalse(thisRow.Equals(otherRow));

            // Length difference equals 0

            // ----- Rows are equal

            thisRow  = rows[0];
            otherRow = rows[2];

            expected = 0;
            actual   = thisRow.CompareTo(otherRow);

            Assert.AreEqual(expected, actual);

            Assert.IsFalse(thisRow < otherRow);
            Assert.IsTrue(thisRow <= otherRow);
            Assert.IsFalse(thisRow > otherRow);
            Assert.IsTrue(thisRow >= otherRow);

            Assert.IsFalse(thisRow != otherRow);
            Assert.IsTrue(thisRow == otherRow);
            Assert.IsTrue(thisRow.Equals(otherRow));

            // ----- thisRow less than otherRow

            thisRow  = rows[0];
            otherRow = rows[1];

            expected = -1;
            actual   = thisRow.CompareTo(otherRow);

            Assert.AreEqual(expected, actual);

            Assert.IsTrue(thisRow < otherRow);
            Assert.IsTrue(thisRow <= otherRow);
            Assert.IsFalse(thisRow > otherRow);
            Assert.IsFalse(thisRow >= otherRow);

            Assert.IsTrue(thisRow != otherRow);
            Assert.IsFalse(thisRow == otherRow);
            Assert.IsFalse(thisRow.Equals(otherRow));

            // ----- thisRow greater than otherRow

            thisRow  = rows[1];
            otherRow = rows[2];

            expected = 1;
            actual   = thisRow.CompareTo(otherRow);

            Assert.AreEqual(expected, actual);

            Assert.IsFalse(thisRow < otherRow);
            Assert.IsFalse(thisRow <= otherRow);
            Assert.IsTrue(thisRow > otherRow);
            Assert.IsTrue(thisRow >= otherRow);

            Assert.IsTrue(thisRow != otherRow);
            Assert.IsFalse(thisRow == otherRow);
            Assert.IsFalse(thisRow.Equals(otherRow));

            // WEAKLY TYPED COMPARISON

            object weakOther;

            // null other

            thisRow   = rows[1];
            weakOther = null;

            expected = 1;
            actual   = thisRow.CompareTo(weakOther);

            Assert.AreEqual(expected, actual);

            Assert.IsFalse(thisRow.Equals(weakOther));

            // DoubleMatrixRow other

            thisRow   = rows[0];
            weakOther = rows[2];

            expected = 0;
            actual   = thisRow.CompareTo(weakOther);

            Assert.AreEqual(expected, actual);

            Assert.IsTrue(thisRow.Equals(weakOther));

            // other not of type DoubleMatrixRow

            thisRow   = rows[1];
            weakOther = IndexCollection.Default(2);

            ArgumentExceptionAssert.Throw(
                () =>
            {
                thisRow.CompareTo(weakOther);
            },
                expectedType: typeof(ArgumentException),
                expectedPartialMessage: String.Format(
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_OBJ_HAS_WRONG_TYPE"), "DoubleMatrixRow"),
                expectedParameterName: "obj");

            Assert.IsFalse(thisRow.Equals(weakOther));

            // COMPARISONS INVOLVING NULL OBJECTS

            ComparableObjectTest.CompareToWithNulls(thisRow);

            ComparableObjectTest.EqualsWithNulls(thisRow);

            DoubleMatrixRow leftRow  = null;
            DoubleMatrixRow rightRow = rows[0];

            Assert.IsFalse(leftRow == rightRow);
            leftRow  = rows[0];
            rightRow = null;
            Assert.IsFalse(leftRow == rightRow);

            leftRow  = null;
            rightRow = null;
            Assert.IsTrue(leftRow == rightRow);
        }
Ejemplo n.º 8
0
        public void XDataColumnTest()
        {
            // value is less than 0
            {
                var matrix = DoubleMatrix.Dense(2, 3,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });
                var rows = matrix.AsRowCollection();

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    rows.XDataColumn = -1;
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_ROW_DATA_COLUMN_EXCEEDS_DIMS"),
                    expectedParameterName: "value");
            }

            // value is greater than NumberOfColumns - 1
            {
                var matrix = DoubleMatrix.Dense(2, 3,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });
                var rows = matrix.AsRowCollection();

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    rows.XDataColumn = matrix.NumberOfColumns;
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_ROW_DATA_COLUMN_EXCEEDS_DIMS"),
                    expectedParameterName: "value");
            }

            // value is inside the bounds
            {
                var matrix = DoubleMatrix.Dense(2, 3,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });
                var rows = matrix.AsRowCollection();

                int dataColumn = 1;

                rows.XDataColumn = dataColumn;

                DoubleMatrixRow row        = rows[0];
                var             subscriber = new PropertyChangedSubscriber();
                row.PropertyChanged += subscriber.PropertyChangedEventHandler;

                // Same value: unspecific notification
                double expected = matrix[row.Index, dataColumn];
                row[dataColumn] = expected;
                ArrayAssert <string> .AreEqual(
                    expected : new string[] { "" },
                    actual : subscriber.PropertyNames.ToArray());

                var actual = row[dataColumn];
                Assert.AreEqual(
                    expected,
                    actual,
                    DoubleMatrixTest.Accuracy);

                // Different value: specific notification
                row[dataColumn] = -1;
                Assert.AreEqual(
                    matrix[row.Index, dataColumn],
                    row[dataColumn],
                    DoubleMatrixTest.Accuracy);
                ArrayAssert <string> .AreEqual(
                    expected : new string[] { "", "[1]", "XData" },
                    actual : subscriber.PropertyNames.ToArray());
            }
        }