public void ConstructorTest()
        {
            // Valid input
            {
                var indexes = IndexCollection.Default(lastIndex: 5);

                var randomIndexPermutation = new
                                             RandomIndexPermutation(indexes: indexes);

                int i = 0;
                foreach (var index in randomIndexPermutation.Indexes)
                {
                    Assert.AreEqual(
                        expected: indexes[i],
                        actual: index);
                    i++;
                }
            }

            // indexes is null
            {
                IndexCollection indexes = null;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new RandomIndexPermutation(
                        indexes: indexes);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "indexes");
            }
        }
            /// <summary>
            /// Tests that method
            /// <see cref="RandomIndexPermutation.Next"/>
            /// terminates successfully as expected.
            /// </summary>
            /// <param name="indexes">
            /// The indexes to permute.
            /// </param>
            /// <param name="numberOfRandomPermutations">
            /// The number of permutations to draw.
            /// </param>
            /// <param name="criticalValue">
            /// A quantile of the chi-squared distribution with a number of
            /// degrees of freedom equal to the <see cref="IndexCollection.Count"/>
            /// of <paramref name="indexes"/>
            /// minus <c>1</c>.
            /// To serve as the critical value for the Pearson's
            /// chi-squared test whose null hypothesis assume that the
            /// the distinct possible permutations
            /// are equiprobable.
            /// </param>
            /// <param name="delta">The required accuracy.
            /// Defaults to <c>.01</c>.</param>
            public static void Succeed(
                IndexCollection indexes,
                int numberOfRandomPermutations,
                double criticalValue,
                double delta = .01)
            {
                var randomPermutation = new RandomIndexPermutation(indexes);

                // Generate permutations

                var permutations = new IndexCollection[numberOfRandomPermutations];

                for (int i = 0; i < numberOfRandomPermutations; i++)
                {
                    permutations[i] = randomPermutation.Next();
                }

                // Check the number of distinct generated permutations

                var permutationIdentifiers =
                    IndexCollection.Default(numberOfRandomPermutations - 1);

                var actualDistinctPermutations =
                    IndexPartition.Create(
                        permutationIdentifiers,
                        (i) => { return(permutations[i]); });

                int numberOfActualDistinctPermutations =
                    actualDistinctPermutations.Count;

                Assert.AreEqual(
                    expected: SpecialFunctions.Factorial(indexes.Count),
                    actual: numberOfActualDistinctPermutations);

                // Compute the actual permutation probabilities

                DoubleMatrix actualPermutationProbabilities =
                    DoubleMatrix.Dense(
                        numberOfActualDistinctPermutations, 1);

                int j = 0;

                foreach (var identifier in actualDistinctPermutations.Identifiers)
                {
                    actualPermutationProbabilities[j] =
                        (double)actualDistinctPermutations[identifier].Count
                        /
                        (double)numberOfRandomPermutations;
                    j++;
                }

                // Check that the Chebyshev Inequality holds true
                // for each permutation probability

                var expectedPermutationProbabilities =
                    DoubleMatrix.Dense(
                        numberOfActualDistinctPermutations,
                        1,
                        1.0
                        /
                        (double)numberOfActualDistinctPermutations);

                for (int i = 0; i < numberOfActualDistinctPermutations; i++)
                {
                    ProbabilityDistributionTest.CheckChebyshevInequality(
                        new BernoulliDistribution(expectedPermutationProbabilities[i]),
                        actualPermutationProbabilities[i],
                        numberOfRandomPermutations,
                        delta);
                }

                // Check how good the actual permutation probabilities fit
                // the expected ones

                ProbabilityDistributionTest.CheckGoodnessOfFit(
                    expectedPermutationProbabilities,
                    actualPermutationProbabilities,
                    criticalValue);
            }
Exemplo n.º 3
0
        public void GetOptimalStateTest()
        {
            // valid input - random ties resolution
            {
                var context = new CategoricalEntailmentEnsembleOptimizationContext(
                    objectiveFunction:
                    (DoubleMatrix state) => { return(Double.PositiveInfinity); },
                    featureCategoryCounts: new List <int>(1)
                {
                    6
                },
                    numberOfResponseCategories: 4,
                    numberOfCategoricalEntailments: 1,
                    allowEntailmentPartialTruthValues: true,
                    probabilitySmoothingCoefficient: .9,
                    optimizationGoal: OptimizationGoal.Maximization,
                    minimumNumberOfIterations: 5,
                    maximumNumberOfIterations: 1000);

                int    numberOfEvaluations = 10000;
                double delta = .01;

                var parameter = DoubleMatrix.Dense(1, 10, new double[10] {
                    .5, .5, .5, .5, .5, .5, .25, .25, .25, .25
                });

                // Generate states

                var states = new int[numberOfEvaluations];

                var responseIndexes = IndexCollection.Range(6, 9);

                for (int i = 0; i < numberOfEvaluations; i++)
                {
                    var state = context.GetOptimalState(parameter);

                    states[i] = state.Vec(responseIndexes).FindNonzero()[0];
                }

                // Compute the actual inclusion probabilities

                DoubleMatrix actualInclusionProbabilities =
                    DoubleMatrix.Dense(context.NumberOfResponseCategories, 1);

                var stateIndexes = IndexCollection.Default(numberOfEvaluations - 1);

                for (int j = 0; j < context.NumberOfResponseCategories; j++)
                {
                    var samplesContainingCurrentUnit =
                        IndexPartition.Create(
                            stateIndexes,
                            (i) => { return(states[i] == j); });

                    actualInclusionProbabilities[j] =
                        (double)samplesContainingCurrentUnit[true].Count
                        /
                        (double)numberOfEvaluations;
                }

                // Check the number of distinct generated states

                var distinctStates =
                    IndexPartition.Create(
                        states);

                int numberOfDistinctStates =
                    distinctStates.Count;

                Assert.AreEqual(
                    expected: context.NumberOfResponseCategories,
                    actual: numberOfDistinctStates);

                // Check that the Chebyshev Inequality holds true
                // for each inclusion probability

                var expectedInclusionProbabilities =
                    DoubleMatrix.Dense(context.NumberOfResponseCategories, 1,
                                       1.0 / context.NumberOfResponseCategories);

                for (int j = 0; j < context.NumberOfResponseCategories; j++)
                {
                    ProbabilityDistributionTest.CheckChebyshevInequality(
                        new BernoulliDistribution(expectedInclusionProbabilities[j]),
                        actualInclusionProbabilities[j],
                        numberOfEvaluations,
                        delta);
                }

                // Check how good the actual inclusion probabilities fit
                // the expected ones

                // The following assumes a number of response
                // categories equal to 4.
                //
                // The quantile of order .9 for
                // the chi-squared distribution having 4-1
                // degrees of freedom is 6.251389
                // (as from R function qchisq(.9, 3))
                var goodnessOfFitCriticalValue = 6.251389;

                ProbabilityDistributionTest.CheckGoodnessOfFit(
                    expectedInclusionProbabilities,
                    actualInclusionProbabilities,
                    goodnessOfFitCriticalValue);
            }
        }
Exemplo n.º 4
0
        public void CreateFromIndexCollectionTest()
        {
            // elements is null
            {
                bool partitioner(int linearIndex)
                {
                    return(linearIndex < 3);
                }

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var partition = IndexPartition.Create(
                        (IndexCollection)null,
                        partitioner);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "elements");
            }

            // partitioner is null
            {
                Func <int, bool> partitioner = null;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var partition = IndexPartition.Create(
                        IndexCollection.Default(3),
                        partitioner);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "partitioner");
            }

            // Valid parameters
            {
                // Create a matrix.
                var data = new double[16] {
                    -3, 3, 3, -1,
                    0, 2, -2, 2,
                    2, 1, -4, -5,
                    -8, 2, 7, -1
                };
                var matrix = DoubleMatrix.Dense(4, 4, data, StorageOrder.RowMajor);

                // Create the collection of linear indexes corresponding
                // to entries on the matrix main diagonal.
                var elements =
                    IndexCollection.Sequence(0, 1 + matrix.NumberOfRows, matrix.Count);

                // Create a partitioner which returns true if
                // the absolute value in a entry having the specified linear
                // index is less than 3, otherwise false.
                bool partitioner(int linearIndex)
                {
                    return(Math.Abs(matrix[linearIndex]) < 3.0);
                }

                // Partition the diagonal linear indexes through the
                // specified partitioner.
                var actual = IndexPartition.Create(elements, partitioner);

                // Two parts are created, one for diagonal
                // entries less than 3 in absolute value, the other for
                // entries not satisfying that condition.

                // Expected:
                //
                // Part identifier: False
                //      indexes: 0, 10
                //
                // Part identifier: True
                //      indexes: 5, 15
                //
                var expected = new IndexPartition <bool>
                {
                    partIndetifiers = new List <bool>(2)
                    {
                        false,
                        true
                    },

                    parts = new Dictionary <bool, IndexCollection>(2)
                    {
                        { false, IndexCollection.FromArray(new int[2] {
                                0, 10
                            }) },
                        { true, IndexCollection.FromArray(new int[2] {
                                5, 15
                            }) }
                    }
                };

                IndexPartitionAssert.AreEqual(expected, actual);
            }
        }
Exemplo n.º 5
0
        public void CreateFromRowCollectionTest()
        {
            // elements is null
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var partition = IndexPartition.Create((DoubleMatrixRowCollection)null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "elements");
            }

            // elements is not null
            {
                // Create a matrix.
                var data = new double[18] {
                    0, 0, 1,
                    0, 0, 1,
                    0, 1, 0,
                    0, 1, 0,
                    1, 0, 0,
                    1, 0, 0
                };
                var matrix = DoubleMatrix.Dense(6, 3, data, StorageOrder.RowMajor);

                // Partition the matrix row indexes by the contents of each row:
                // a part is created for each distinct row.
                var elements = matrix.AsRowCollection();
                var actual   = IndexPartition.Create(elements);

                // Each part is identified by its corresponding row and contains
                // the indexes of the rows which are equal to the identifier.

                // Expected:
                //
                // Part identifier: 0                0                1
                //
                //      indexes: 0, 1
                //
                // Part identifier: 0                1                0
                //
                //      indexes: 2, 3
                //
                // Part identifier: 1                0                0
                //
                //      indexes: 4, 5
                //

                var expected = new IndexPartition <DoubleMatrixRow>
                {
                    partIndetifiers = new List <DoubleMatrixRow>(3)
                    {
                        elements[0],
                        elements[2],
                        elements[4]
                    },

                    parts = new Dictionary <DoubleMatrixRow, IndexCollection>(3)
                    {
                        { elements[0], IndexCollection.Default(1) },
                        { elements[2], IndexCollection.Range(2, 3) },
                        { elements[4], IndexCollection.Range(4, 5) }
                    }
                };

                IndexPartitionAssert.AreEqual(expected, actual);
            }
        }
Exemplo n.º 6
0
        public void CreateFromDoubleMatrixTest()
        {
            // elements is null
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var partition = IndexPartition.Create((DoubleMatrix)null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "elements");
            }

            // elements is a vector
            {
                // Create a matrix
                var data = new double[18] {
                    0, 0, 1,
                    0, 0, 1,
                    0, 1, 0,
                    0, 1, 0,
                    1, 0, 0,
                    1, 0, 0
                };
                var matrix = DoubleMatrix.Dense(6, 3, data, StorageOrder.RowMajor);

                // Partition the matrix row indexes by the contents of column 0:
                // a part is created for each distinct value in column 0
                var elements = matrix[":", 0];
                var actual   = IndexPartition.Create(elements);

                // Each part is identified by its corresponding value and contains
                // the indexes of the rows in which the identifier
                // is positioned in column 0

                // Expected:
                //
                // Part identifier: 0
                //      indexes: 0, 1, 2, 3
                //
                // Part identifier: 1
                //      indexes: 4, 5

                IndexPartition <double> expected = new()
                {
                    partIndetifiers = new List <double>(2)
                    {
                        0.0,
                        1.0
                    },

                    parts = new Dictionary <double, IndexCollection>(2)
                    {
                        { 0.0, IndexCollection.Default(3) },
                        { 1.0, IndexCollection.Range(4, 5) }
                    }
                };

                IndexPartitionAssert.AreEqual(expected, actual);
            }

            // elements is a matrix of signs
            {
                // Create a matrix.
                var data = new double[8] {
                    0, 1, -2, -3,
                    0, -1, 2, 3
                };
                var matrix = DoubleMatrix.Dense(2, 4, data, StorageOrder.RowMajor);

                // Check the sign of its entries
                var signs = DoubleMatrix.Dense(matrix.NumberOfRows, matrix.NumberOfColumns);
                for (int i = 0; i < matrix.Count; i++)
                {
                    signs[i] = Math.Sign(matrix[i]);
                }

                // Partition the matrix linear indexes by the sign of each entry
                var actual = IndexPartition.Create(signs);

                // The partition contains three parts, the zero part, identified by 0,
                // the negative part (identified by -1), and the positive one
                // (identified by 1).

                // Expected:
                //
                // Part identifier: -1
                //      indexes: 3, 4, 6
                //
                // Part identifier: 0
                //      indexes: 0, 1
                //
                // Part identifier: 1
                //      indexes: 2, 5, 7
                //

                IndexPartition <double> expected = new()
                {
                    partIndetifiers = new List <double>(3)
                    {
                        -1.0,
                        0.0,
                        1.0
                    },

                    parts = new Dictionary <double, IndexCollection>(3)
                    {
                        { -1.0, IndexCollection.FromArray(new int[] { 3, 4, 6 }) },
                        { 0.0, IndexCollection.Default(1) },
                        { 1.0, IndexCollection.FromArray(new int[] { 2, 5, 7 }) }
                    }
                };

                IndexPartitionAssert.AreEqual(expected, actual);
            }

            // elements is a matrix of data
            {
                // Create a matrix
                var data = new double[6] {
                    1, 3,
                    0, 2,
                    2, 1
                };
                var elements = DoubleMatrix.Dense(3, 2, data, StorageOrder.RowMajor);

                // Partition the matrix linear indexes by the content of
                // matrix entries: a part is created for each distinct matrix value
                var actual = IndexPartition.Create(elements);

                // Each part is identified by its corresponding value and contains
                // the linear indexes of the entries in which the identifier
                // is positioned.

                // This code example produces the following output:
                //
                //
                // Part identifier: 0
                //      indexes: 1
                //
                // Part identifier: 1
                //      indexes: 0, 5
                //
                // Part identifier: 2
                //      indexes: 2, 4
                //
                // Part identifier: 3
                //      indexes: 3
                //

                var expected = new IndexPartition <double>
                {
                    partIndetifiers = new List <double>(3)
                    {
                        0.0,
                        1.0,
                        2.0,
                        3.0
                    },

                    parts = new Dictionary <double, IndexCollection>(3)
                    {
                        { 0.0, IndexCollection.FromArray(new int[] { 1 }) },
                        { 1.0, IndexCollection.FromArray(new int[] { 0, 5 }) },
                        { 2.0, IndexCollection.FromArray(new int[] { 2, 4 }) },
                        { 3.0, IndexCollection.FromArray(new int[] { 3 }) }
                    }
                };

                IndexPartitionAssert.AreEqual(expected, actual);
            }
        }
Exemplo n.º 7
0
            /// <summary>
            /// Tests that method
            /// <see cref="RandomSampling.NextDoubleMatrix"/>
            /// terminates successfully as expected.
            /// </summary>
            /// <param name="testableRandomSampling">
            /// The testable random sampling providing the instance
            /// on which to invoke the methods to test and their expected
            /// behaviors.
            /// </param>
            /// <param name="numberOfSamples">
            /// The number of samples to draw.
            /// </param>
            /// <param name="delta">The required accuracy.
            /// Defaults to <c>.01</c>.</param>
            public static void Succeed(
                TestableRandomSampling testableRandomSampling,
                int numberOfSamples,
                double delta = .01)
            {
                var randomSampling = testableRandomSampling.RandomSampling;

                // Generate samples

                var samples = DoubleMatrix.Dense(
                    numberOfSamples,
                    randomSampling.PopulationSize);

                for (int i = 0; i < numberOfSamples; i++)
                {
                    samples[i, ":"] = randomSampling.NextDoubleMatrix();
                }

                // Compute the actual inclusion probabilities

                DoubleMatrix actualInclusionProbabilities =
                    DoubleMatrix.Dense(randomSampling.PopulationSize, 1);

                var sampleIndexes = IndexCollection.Default(numberOfSamples - 1);

                for (int j = 0; j < randomSampling.PopulationSize; j++)
                {
                    var samplesContainingUnit =
                        IndexPartition.Create(
                            sampleIndexes,
                            (i) => { return(samples[i, j] == 1.0); });

                    actualInclusionProbabilities[j] =
                        (double)samplesContainingUnit[true].Count
                        /
                        (double)numberOfSamples;
                }

                // Check the number of distinct generated samples

                var distinctSamples =
                    IndexPartition.Create(samples.AsRowCollection());

                int numberOfDistinctSamples =
                    distinctSamples.Count;

                Assert.AreEqual(
                    SpecialFunctions.BinomialCoefficient(
                        randomSampling.PopulationSize,
                        randomSampling.SampleSize),
                    numberOfDistinctSamples);

                // Check that the Chebyshev Inequality holds true
                // for each inclusion probability

                var expectedInclusionProbabilities =
                    testableRandomSampling.InclusionProbabilities;

                for (int j = 0; j < randomSampling.PopulationSize; j++)
                {
                    ProbabilityDistributionTest.CheckChebyshevInequality(
                        new BernoulliDistribution(expectedInclusionProbabilities[j]),
                        actualInclusionProbabilities[j],
                        numberOfSamples,
                        delta);
                }

                // Check how good the actual inclusion probabilities fit
                // the expected ones

                ProbabilityDistributionTest.CheckGoodnessOfFit(
                    expectedInclusionProbabilities,
                    actualInclusionProbabilities,
                    testableRandomSampling.GoodnessOfFitCriticalValue);
            }
Exemplo n.º 8
0
        public void EqualityTest()
        {
            ComplexMatrix    matrix, otherMatrix;
            ComplexMatrixRow thisRow, otherRow;

            matrix = ComplexMatrix.Dense(3, 3,
                                         new Complex[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 = ComplexMatrix.Dense(3, 4);
            var otherRows = otherMatrix.AsRowCollection();

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

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

            // Length difference < 0

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

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

            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];

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

            // ----- thisRow less than otherRow

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

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

            // ----- thisRow greater than otherRow

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

            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;

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

            // ComplexMatrixRow other

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

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

            // other not of type ComplexMatrixRow

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

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

            // COMPARISONS INVOLVING NULL OBJECTS

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

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

            leftRow  = null;
            rightRow = null;
            Assert.IsTrue(leftRow == rightRow);
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
0
        public static void ContainSameItems(
            IList <T> expected,
            IList <T> actual,
            Action <T, T> areEqual)
        {
            if (null == expected && null == actual)
            {
                return;
            }

            if (((null == expected) && (null != actual))
                ||
                ((null != expected) && (null == actual)))
            {
                throw new AssertFailedException(
                          "One List instance is null, the other is not.");
            }

            if (expected.Count == 0)
            {
                if (actual.Count != 0)
                {
                    throw new AssertFailedException(
                              "The expected instance is empty, the other is not.");
                }
            }
            else
            {
                if (actual.Count == 0)
                {
                    throw new AssertFailedException(
                              "The expected instance is nonempty, the other is not.");
                }
                else
                {
                    // Check that expected is a subset of actual
                    var uncheckedActualPositions =
                        IndexCollection.Default(actual.Count - 1).ToList();

                    for (int i = 0; i < expected.Count; i++)
                    {
                        bool expectedItemIsMissing = true;
                        var  expectedItem          = expected[i];
                        for (int j = 0; j < actual.Count; j++)
                        {
                            var actualItem = actual[j];
                            try
                            {
                                areEqual(expectedItem, actualItem);
                                uncheckedActualPositions.Remove(j);
                                expectedItemIsMissing = false;
                                break;
                            }
                            catch (AssertFailedException)
                            {
                            }
                        }
                        if (expectedItemIsMissing)
                        {
                            throw new AssertFailedException(
                                      msg: string.Format(
                                          "Missing expected item {0}.",
                                          expectedItem));
                        }
                    }

                    // Check that actual is a subset of expected
                    for (int i = 0; i < uncheckedActualPositions.Count; i++)
                    {
                        bool actualItemIsMissing = true;
                        var  actualItem          = actual[uncheckedActualPositions[i]];
                        for (int j = 0; j < expected.Count; j++)
                        {
                            var expectedItem = expected[j];
                            try
                            {
                                areEqual(expectedItem, actualItem);
                                actualItemIsMissing = false;
                                break;
                            }
                            catch (AssertFailedException)
                            {
                            }
                        }
                        if (actualItemIsMissing)
                        {
                            throw new AssertFailedException(
                                      msg: string.Format(
                                          "Missing actual item {0}.",
                                          actualItem));
                        }
                    }
                }
            }
        }