/// <summary> /// Determines whether the specified target has the /// expected state. /// </summary> /// <param name="target">The target.</param> /// <param name="expectedName">The expected name.</param> /// <param name="expectedCategories">The expected categories.</param> /// <param name="expectedReadOnlyFlag"> /// If set to <c>true</c>, the target is expected to be /// read-only; otherwise, <c>false</c>.</param> /// <exception cref="AssertFailedException"> /// Target categorical variable has an unexpected state. /// </exception> public static void IsStateAsExpected( CategoricalVariable target, string expectedName, List <Category> expectedCategories, bool expectedReadOnlyFlag) { var actualName = (string)Reflector.GetField(target, "name"); Assert.AreEqual(expectedName, actualName); Assert.AreEqual(expectedName, target.Name); if (target.Categories.Count != expectedCategories.Count) { throw new AssertFailedException( "Target categorical variable has an unexpected number " + "of categories."); } Assert.AreEqual(expectedCategories.Count, target.NumberOfCategories); for (int i = 0; i < expectedCategories.Count; i++) { CategoryAssert.AreEqual(expectedCategories[i], target.Categories[i]); } int j = 0; foreach (var code in target.CategoryCodes) { Assert.AreEqual(target.Categories[j++].Code, code); } j = 0; foreach (var label in target.CategoryLabels) { Assert.AreEqual(target.Categories[j++].Label, label); } Assert.AreEqual(expectedReadOnlyFlag, target.IsReadOnly); }
/// <summary> /// Determines whether the specified target has the expected state. /// </summary> /// <param name="target">The target.</param> /// <param name="expectedFirstPosition"> /// The expected first position.</param> /// <param name="expectedLastPosition"> /// The expected last position.</param> /// <param name="expectedFirstValue"> /// The expected first value.</param> /// <param name="expectedLastValue"> /// The expected last value.</param> /// <param name="expectedTargetFrequencyDistribution"> /// The expected target frequency distribution.</param> /// <exception cref="AssertFailedException"> /// Actual target value is not in the expected frequency distribution. /// </exception> public static void IsStateAsExpected( NumericalBlock target, int expectedFirstPosition, int expectedLastPosition, double expectedFirstValue, double expectedLastValue, Dictionary <double, int> expectedTargetFrequencyDistribution) { int actualFirstPosition; int actualLastPosition; double actualFirstValue; double actualLastValue; Dictionary <double, int> actualTargetFrequencyDistribution; if (typeof(NumericalBin) == target.GetType()) { actualFirstPosition = (int)Reflector.GetBaseField(target, "firstPosition"); actualLastPosition = (int)Reflector.GetBaseField(target, "lastPosition"); actualFirstValue = (double)Reflector.GetBaseField(target, "firstValue"); actualLastValue = (double)Reflector.GetBaseField(target, "lastValue"); actualTargetFrequencyDistribution = (Dictionary <double, int>)Reflector.GetBaseField( target, "targetFrequencyDistribution"); } else // Here if type is NumericalBlock { actualFirstPosition = (int)Reflector.GetField(target, "firstPosition"); actualLastPosition = (int)Reflector.GetField(target, "lastPosition"); actualFirstValue = (double)Reflector.GetField(target, "firstValue"); actualLastValue = (double)Reflector.GetField(target, "lastValue"); actualTargetFrequencyDistribution = (Dictionary <double, int>)Reflector.GetField( target, "targetFrequencyDistribution"); } Assert.AreEqual(expectedFirstPosition, actualFirstPosition); Assert.AreEqual(expectedLastPosition, actualLastPosition); Assert.AreEqual(expectedFirstValue, actualFirstValue); Assert.AreEqual(expectedLastValue, actualLastValue); Assert.AreEqual(expectedFirstPosition, target.FirstPosition); Assert.AreEqual(expectedLastPosition, target.LastPosition); Assert.AreEqual(expectedFirstValue, target.FirstValue); Assert.AreEqual(expectedLastValue, target.LastValue); foreach (var pair in actualTargetFrequencyDistribution) { if (!expectedTargetFrequencyDistribution.TryGetValue( pair.Key, out int expectedFrequency)) { throw new AssertFailedException( "Actual target value is not in the expected " + "frequency distribution."); } else { Assert.AreEqual(expectedFrequency, pair.Value); } } }
/// <summary> /// Checks that the specified <see cref="ComplexMatrix"/> /// instances are equal. /// </summary> /// <param name="expected">The expected matrix.</param> /// <param name="actual">The actual matrix.</param> /// <param name="delta">The required accuracy.</param> public static void AreEqual( ComplexMatrix expected, ComplexMatrix actual, double delta) { // Null references ? Assert.IsNotNull(expected); Assert.IsNotNull(actual); // Matrix Fields object expectedImplementor = Reflector.GetField( expected, "implementor"); Assert.IsNotNull(expectedImplementor); var expectedColumnMajorOrderedEntries = (Complex[])Reflector.ExecuteMember( expectedImplementor, "AsColumnMajorDenseArray", null); Assert.IsNotNull(expectedColumnMajorOrderedEntries); object actualImplementor = Reflector.GetField( actual, "implementor"); Assert.IsNotNull(actualImplementor); var actualColumnMajorOrderedEntries = (Complex[])Reflector.ExecuteMember( actualImplementor, "AsColumnMajorDenseArray", null); Assert.IsNotNull(actualColumnMajorOrderedEntries); Assert.AreEqual( expectedColumnMajorOrderedEntries.Length, actualColumnMajorOrderedEntries.Length); for (int l = 0; l < actualColumnMajorOrderedEntries.Length; l++) { ComplexAssert.AreEqual( expectedColumnMajorOrderedEntries[l], actualColumnMajorOrderedEntries[l], ComplexMatrixTest.Accuracy); } int expectedNumberOfRows = (int)Reflector.GetProperty( expectedImplementor, "NumberOfRows"); int actualNumberOfRows = (int)Reflector.GetProperty( actualImplementor, "NumberOfRows"); Assert.AreEqual(expectedNumberOfRows, actualNumberOfRows); int expectedNumberOfColumns = (int)Reflector.GetProperty( expectedImplementor, "NumberOfColumns"); int actualNumberOfColumns = (int)Reflector.GetProperty( actualImplementor, "NumberOfColumns"); Assert.AreEqual(expectedNumberOfColumns, actualNumberOfColumns); // Matrix Names MatrixNamesAreEqual(expected, actual); }
/// <summary> /// Determines whether the specified matrix has the expected state. /// </summary> /// <param name="expectedState">The expected state.</param> /// <param name="actualMatrix">The actual matrix.</param> /// <param name="delta">The required precision.</param> public static void IsStateAsExpected( ComplexMatrixState expectedState, ComplexMatrix actualMatrix, double delta) { // Null reference ? Assert.IsNotNull(actualMatrix); // Matrix Fields object implementor = Reflector.GetField( actualMatrix, "implementor"); Assert.IsNotNull(implementor); Complex[] actualColumnMajorOrderedEntries; actualColumnMajorOrderedEntries = (Complex[])Reflector.ExecuteMember( implementor, "AsColumnMajorDenseArray", null); Assert.IsNotNull(actualColumnMajorOrderedEntries); var expectedColumnMajorOrderedEntries = expectedState.AsColumnMajorDenseArray; Assert.AreEqual( expectedColumnMajorOrderedEntries.Length, actualColumnMajorOrderedEntries.Length); for (int Id = 0; Id < actualColumnMajorOrderedEntries.Length; Id++) { ComplexAssert.AreEqual( expectedColumnMajorOrderedEntries[Id], actualColumnMajorOrderedEntries[Id], delta); } int actualNumberOfRows = (int)Reflector.GetProperty( implementor, "NumberOfRows"); int expectedNumberOfRows = expectedState.NumberOfRows; Assert.AreEqual(expectedNumberOfRows, actualNumberOfRows); int actualNumberOfColumns = (int)Reflector.GetProperty( implementor, "NumberOfColumns"); int expectedNumberOfColumns = expectedState.NumberOfColumns; Assert.AreEqual(expectedNumberOfColumns, actualNumberOfColumns); // Matrix Names // Matrix name Assert.AreEqual(expectedState.Name, actualMatrix.Name); // Row names // Both have row names bool expectedHasRowNames = expectedState.RowNames is not null; Assert.AreEqual(expectedHasRowNames, actualMatrix.HasRowNames); if (expectedHasRowNames) { CompareDimensionNames("row", expectedState.RowNames, actualMatrix.RowNames); } // Column names // Both have column names bool expectedHasColumnNames = expectedState.ColumnNames is not null; Assert.AreEqual(expectedHasColumnNames, actualMatrix.HasColumnNames); if (expectedHasColumnNames) { CompareDimensionNames("column", expectedState.ColumnNames, actualMatrix.ColumnNames); } }
/// <summary> /// Determines whether the specified target has the /// expected state. /// </summary> /// <param name="target">The target.</param> /// <param name="expectedFeatureVariables"> /// The expected feature variables.</param> /// <param name="expectedResponseVariable"> /// The expected response variable.</param> /// <param name="expectedFeaturePremises"> /// The expected feature premises.</param> /// <param name="expectedResponseConclusion"> /// The expected response conclusion.</param> /// <param name="expectedTruthValue"> /// The expected truth value.</param> /// <exception cref="AssertFailedException"> /// Target categorical entailment has an unexpected state. /// </exception> public static void IsStateAsExpected( CategoricalEntailment target, IReadOnlyList <CategoricalVariable> expectedFeatureVariables, CategoricalVariable expectedResponseVariable, IReadOnlyList <SortedSet <double> > expectedFeaturePremises, double expectedResponseConclusion, double expectedTruthValue) { if (target.FeatureVariables.Count != expectedFeatureVariables.Count) { throw new AssertFailedException( "The list of target feature variables has an unexpected count."); } for (int i = 0; i < expectedFeatureVariables.Count; i++) { CategoricalVariableAssert.AreEqual( expectedFeatureVariables[i], target.FeatureVariables[i]); } CategoricalVariableAssert.AreEqual( expectedResponseVariable, target.ResponseVariable); var targetFeaturePremises = (List <SortedSet <double> >)Reflector.GetField(target, "featurePremises"); for (int i = 0; i < expectedFeaturePremises.Count; i++) { if (!expectedFeaturePremises[i].SetEquals( target.FeaturePremises[i])) { throw new AssertFailedException( "The target feature premises are not as expected."); } if (!expectedFeaturePremises[i].SetEquals( targetFeaturePremises[i])) { throw new AssertFailedException( "The target feature premises are not as expected."); } } bool[] expectedIsProperPremise = new bool[expectedFeaturePremises.Count]; for (int i = 0; i < expectedFeaturePremises.Count; i++) { expectedIsProperPremise[i] = !( expectedFeaturePremises[i].Count == 0 || expectedFeaturePremises[i].Count == expectedFeatureVariables[i].NumberOfCategories); } bool[] actualIsProperPremise = (bool[])Reflector.GetField(target, "isNonemptyProperPremise"); ArrayAssert <bool> .AreEqual( expected : expectedIsProperPremise, actual : actualIsProperPremise); Assert.AreEqual( expected: expectedResponseConclusion, actual: target.ResponseConclusion, delta: DoubleMatrixTest.Accuracy); Assert.AreEqual( expected: expectedTruthValue, actual: target.TruthValue, delta: DoubleMatrixTest.Accuracy); }