コード例 #1
0
        /// <summary>
        /// Returns a new table that is the union of the input tables.
        /// </summary>
        /// <param name="parameters">The model's parameters</param>
        /// <param name="first">First table</param>
        /// <param name="second">Second table</param>
        /// <param name="newState">Function to calculate state of merged value combinations</param>
        /// <returns>The new table</returns>
        public static ParameterInteraction Merge <T>(IList <ParameterBase> parameters, ParameterInteraction first, ParameterInteraction second, Func <ValueCombinationState, ValueCombinationState, ValueCombinationState> newState)
            where T : new()
        {
            List <int> parameterIndices = first.Parameters.Union(second.Parameters).ToList();

            parameterIndices.Sort();

            var mergedInteraction = new ParameterInteraction(parameterIndices);

            var valueTable = ParameterInteractionTable <T> .GenerateValueTable(parameters, mergedInteraction);

            foreach (var value in valueTable)
            {
                mergedInteraction.Combinations.Add(new ValueCombination(value, mergedInteraction));
            }

            foreach (var combination in mergedInteraction.Combinations)
            {
                var firstMatch  = first.Combinations.First((c) => ParameterInteractionTable <T> .MatchCombination(c, combination));
                var secondMatch = second.Combinations.First((c) => ParameterInteractionTable <T> .MatchCombination(c, combination));
                combination.State = newState(firstMatch.State, secondMatch.State);
            }

            return(mergedInteraction);
        }
コード例 #2
0
        // helper to implement Constraint.SatisfiesConstraint
        internal static ConstraintSatisfaction SatisfiesContraint <T>(Model <T> model, ValueCombination combination, ParameterInteraction interaction) where T : new()
        {
            Debug.Assert(model != null && combination != null && interaction != null);

            var parameterMap = combination.ParameterToValueMap;

            for (int i = 0; i < interaction.Parameters.Count; i++)
            {
                if (!parameterMap.ContainsKey(interaction.Parameters[i]))
                {
                    return(ConstraintSatisfaction.InsufficientData);
                }
            }

            for (int i = 0; i < interaction.Combinations.Count; i++)
            {
                if (ParameterInteractionTable <T> .MatchCombination(interaction.Combinations[i], combination))
                {
                    if (interaction.Combinations[i].State == ValueCombinationState.Excluded)
                    {
                        return(ConstraintSatisfaction.Unsatisfied);
                    }
                }
            }

            return(ConstraintSatisfaction.Satisfied);
        }
コード例 #3
0
        // helper to implement Constraint.SatisfiesConstraint
        internal static ConstraintSatisfaction SatisfiesContraint(Model model, ValueCombination combination, ParameterInteraction interaction)
        {
            Debug.Assert(model != null && combination != null && interaction != null);

            if (!interaction.Parameters.All((i) => combination.ParameterToVaueMap.ContainsKey(i)))
            {
                return(ConstraintSatisfaction.InsufficientData);
            }

            var matches = interaction.Combinations.Where((c) => ParameterInteractionTable.MatchCombination(c, combination));

            if (matches.Any((c) => c.State == ValueCombinationState.Excluded))
            {
                return(ConstraintSatisfaction.Unsatisfied);
            }

            return(ConstraintSatisfaction.Satisfied);
        }
コード例 #4
0
        private void GenerateHigherOrderDependentExclusions(Model <T> model, int order, List <int> parameterInteractionCounts, ParameterInteraction completeInteraction, IEnumerable <ValueCombination> allowedCombinations)
        {
            // generate the combinations for orders between order and completerInteraction.Parameters.Count
            foreach (var count in parameterInteractionCounts)
            {
                IList <int[]> parameterCombinations = GenerateCombinations(completeInteraction.Parameters.Count, count);
                foreach (var combinations in parameterCombinations)
                {
                    var interaction    = new ParameterInteraction(combinations.Select((i) => completeInteraction.Parameters[i]));
                    var possibleValues = ParameterInteractionTable <T> .GenerateValueTable(model.Parameters, interaction);

                    foreach (var value in possibleValues)
                    {
                        interaction.Combinations
                        .Add(new ValueCombination(value, interaction)
                        {
                            State = ValueCombinationState.Covered
                        });
                    }

                    // find combinations that should be excluded
                    var excludedCombinations = new List <ValueCombination>();
                    foreach (var combination in interaction.Combinations)
                    {
                        if (!combination.ParameterToValueMap.Any((p) => !completeInteraction.Parameters.Contains(p.Key)) &&
                            !allowedCombinations.Any((c) => MatchCombination(combination, c)))
                        {
                            excludedCombinations.Add(combination);
                        }
                    }

                    if (excludedCombinations.Count() > 0)
                    {
                        foreach (var combination in excludedCombinations)
                        {
                            combination.State = ValueCombinationState.Excluded;
                        }

                        MergeConstraintInteraction(order, interaction);
                    }
                }
            }
        }
コード例 #5
0
        // helper to implement Constraint.GetExcludeCombinations
        internal static ParameterInteraction GetExcludedCombinations <T>(Model model, Constraint constraint, Parameter first, Parameter second, T value, Func <T, T, bool> comparison)
        {
            Debug.Assert(first != null && model != null && constraint != null && comparison != null);
            List <int> parameterIndices = new List <int>();

            parameterIndices.Add(model.Parameters.IndexOf(first));
            if (second != null)
            {
                parameterIndices.Add(model.Parameters.IndexOf(second));
            }
            parameterIndices.Sort();

            ParameterInteraction interaction = new ParameterInteraction(parameterIndices);
            List <int[]>         valueTable  = ParameterInteractionTable.GenerateValueTable(model.Parameters, interaction);

            foreach (var valueIndices in valueTable)
            {
                T value1, value2;

                value1 = (T)first[valueIndices[0]];
                if (second == null)
                {
                    value2 = value;
                }
                else
                {
                    value2 = (T)second[valueIndices[1]];
                }

                ValueCombinationState comboState = comparison(value1, value2) ? ValueCombinationState.Covered : ValueCombinationState.Excluded;
                interaction.Combinations.Add(new ValueCombination(valueIndices, interaction)
                {
                    State = comboState
                });
            }

            return(interaction);
        }
コード例 #6
0
        // for the following system:
        // if A == 0 then B == 0
        // if B == 0 then C == 0
        // if C == 0 then A == 1
        // all combinations with A == 0 need to be excluded, but this is impossible to determine when looking at individual constraints
        // to do this:
        //      - create groups of constraints where all members have a parameter that overlaps with another constraint
        //      - create ParameterInteraction for each group of constraints
        //      - for the existing uncovered combinations, if they excluded in every matching combination in the group interaction, mark excluded
        //      - higher order combinations that match the order of a constraint's interaction can also need to be exculded
        private void GenerateDependentConstraintInteractions(Model model, int order, List <ParameterInteraction> constraintInteractions)
        {
            // group all the constraint parameter interactions that share parameters
            var dependentConstraintSets = new List <List <ParameterInteraction> >();

            while (constraintInteractions.Count > 0)
            {
                var dependentConstraintSet = new List <ParameterInteraction>();
                var interactionsToExplore  = new List <ParameterInteraction>();

                interactionsToExplore.Add(constraintInteractions[0]);
                constraintInteractions.RemoveAt(0);

                while (interactionsToExplore.Count > 0)
                {
                    ParameterInteraction current = interactionsToExplore[0];
                    interactionsToExplore.RemoveAt(0);
                    dependentConstraintSet.Add(current);

                    var dependentInteractions =
                        (from constraint in constraintInteractions
                         where constraint.Parameters.Any((i) => current.Parameters.Contains(i))
                         select constraint).ToList();

                    foreach (var dependentInteraction in dependentInteractions)
                    {
                        constraintInteractions.Remove(dependentInteraction);
                    }

                    interactionsToExplore.AddRange(dependentInteractions);
                }

                dependentConstraintSets.Add(dependentConstraintSet);
            }

            // walk over the groups of constraints
            foreach (var dependentConstraintSet in dependentConstraintSets)
            {
                // if there's only one constraint no more processing is necessary
                if (dependentConstraintSet.Count <= 1)
                {
                    continue;
                }

                // merge the interactions of all the constraints
                var uniqueParameters           = new Dictionary <int, bool>();
                var parameterInteractionCounts = new List <int>();
                for (int i = 0; i < dependentConstraintSet.Count; i++)
                {
                    foreach (var parameter in dependentConstraintSet[i].Parameters)
                    {
                        uniqueParameters[parameter] = true;
                    }

                    if (dependentConstraintSet[i].Parameters.Count > order)
                    {
                        parameterInteractionCounts.Add(dependentConstraintSet[i].Parameters.Count);
                    }
                }

                var sortedParameters = uniqueParameters.Keys.ToList();
                sortedParameters.Sort();

                ParameterInteraction completeInteraction = new ParameterInteraction(sortedParameters);
                var valueTable = ParameterInteractionTable.GenerateValueTable(model.Parameters, completeInteraction);
                foreach (var value in valueTable)
                {
                    completeInteraction.Combinations
                    .Add(new ValueCombination(value, completeInteraction)
                    {
                        State = ValueCombinationState.Covered
                    });
                }

                // calculate the excluded combinations in the new uber interaction
                var completeInteractionExcludedCombinations = new List <ValueCombination>();

                // find the combinations from the uber interaction that aren't excluded
                // if a combination is a subset of any of these it is not excluded
                var allowedCombinations = new List <ValueCombination>();

                foreach (var combination in completeInteraction.Combinations)
                {
                    bool exclude = false;
                    foreach (var constraint in completeConstraints)
                    {
                        if (constraint.SatisfiesContraint(model, combination) == ConstraintSatisfaction.Unsatisfied)
                        {
                            exclude = true;
                            break;
                        }
                    }

                    if (exclude)
                    {
                        combination.State = ValueCombinationState.Excluded;
                        completeInteractionExcludedCombinations.Add(combination);
                    }
                    else
                    {
                        allowedCombinations.Add(combination);
                    }
                }

                // find the existing combinations that are never allowed in the uber interaction
                var individualInteractionExcludedCombinations =
                    from interaction in Interactions
                    from combination in interaction.Combinations
                    where !combination.ParameterToValueMap.Any((p) => !completeInteraction.Parameters.Contains(p.Key)) &&
                    !allowedCombinations.Any((c) => MatchCombination(combination, c))
                    select combination;

                // mark the combinations in the table as excluded
                foreach (var combination in individualInteractionExcludedCombinations)
                {
                    combination.State = ValueCombinationState.Excluded;
                }

                GenerateHigherOrderDependentExclusions(model, order, parameterInteractionCounts, completeInteraction, allowedCombinations);
            }
        }
コード例 #7
0
ファイル: VariationGenerator.cs プロジェクト: yeungxh/TestApi
        // this is the actual generation function
        // returns a list of indices that allow lookup of the actual value in the model
        private static IList <VariationIndexTagPair> GenerateVariationIndices <T>(ParameterInteractionTable <T> interactions, int variationSize, int seed, long maxVariations, object defaultTag) where T : new()
        {
            Random random = new Random(seed);
            List <VariationIndexTagPair> variations = new List <VariationIndexTagPair>();

            // while there a uncovered values
            while (!interactions.IsCovered())
            {
                int[]  candidate    = new int[variationSize];
                object variationTag = defaultTag;

                // this is a scatch variable so new arrays won't be allocated for every candidate
                int[] proposedCandidate = new int[variationSize];
                for (int i = 0; i < candidate.Length; i++)
                {
                    // -1 indicates an empty slot
                    candidate[i] = -1;
                }

                IEnumerable <ParameterInteraction> candidateInteractions = interactions.Interactions;
                // while there are empty slots
                while (candidate.Any((i) => i == -1))
                {
                    // if all the slots are empty
                    if (candidate.All((i) => i == -1))
                    {
                        // then pick the first uncovered combination from the most uncovered parameter interaction
                        int mostUncovered =
                            interactions.Interactions.Max((i) => i.GetUncoveredCombinationsCount());

                        var interaction = interactions.Interactions.First((i) => i.GetUncoveredCombinationsCount() == mostUncovered);
                        var combination = interaction.Combinations.First((c) => c.State == ValueCombinationState.Uncovered);

                        foreach (var valuePair in combination.ParameterToValueMap)
                        {
                            candidate[valuePair.Key] = valuePair.Value;
                        }

                        variationTag      = combination.Tag == null || combination.Tag == defaultTag ? variationTag : combination.Tag;
                        combination.State = ValueCombinationState.Covered;
                    }
                    else
                    {
                        // find interactions that aren't covered by the current candidate variation
                        var incompletelyCoveredInteractions =
                            from interaction in candidateInteractions
                            where interaction.Parameters.Any((i) => candidate[i] == -1)
                            select interaction;

                        candidateInteractions = incompletelyCoveredInteractions;
                        // find values that can be added to the current candidate
                        var compatibleValues = new List <ValueCombination>();
                        foreach (var interaction in incompletelyCoveredInteractions)
                        {
                            foreach (var combination in interaction.Combinations)
                            {
                                if (IsCompatibleValue(combination, candidate))
                                {
                                    compatibleValues.Add(combination);
                                }
                            }
                        }

                        // get the uncovered values
                        var uncoveredValues = compatibleValues.Where((v) => v.State == ValueCombinationState.Uncovered).ToList();

                        // calculate what the candidate will look like if add an uncovered value
                        var proposedCandidates = new List <CandidateCoverage>();
                        foreach (var uncoveredValue in uncoveredValues)
                        {
                            CreateProposedCandidate(uncoveredValue, candidate, proposedCandidate);

                            if (!IsExcluded(interactions.ExcludedCombinations, proposedCandidate))
                            {
                                var coverage = new CandidateCoverage
                                {
                                    Value         = uncoveredValue,
                                    CoverageCount = uncoveredValues.Count((v) => IsCovered(v, proposedCandidate)),
                                };

                                proposedCandidates.Add(coverage);
                            }
                        }

                        // if any of the proposed candidates isn't exclude
                        if (proposedCandidates.Count > 0)
                        {
                            // find the value that will cover the most combinations
                            int              maxCovered    = proposedCandidates.Max((c) => c.CoverageCount);
                            double           maxWeight     = proposedCandidates.Where((c) => c.CoverageCount == maxCovered).Max((c) => c.Value.Weight);
                            ValueCombination proposedValue = proposedCandidates.First((c) => c.CoverageCount == maxCovered && c.Value.Weight == maxWeight).Value;

                            // add this value to candidate and mark all values as such
                            foreach (var valuePair in proposedValue.ParameterToValueMap)
                            {
                                candidate[valuePair.Key] = valuePair.Value;
                            }

                            variationTag = proposedValue.Tag == null || proposedValue.Tag == defaultTag ? variationTag : proposedValue.Tag;

                            // get the newly covered values so they can be marked
                            var newlyCoveredValue = uncoveredValues.Where((v) => IsCovered(v, candidate)).ToList();

                            foreach (var value in newlyCoveredValue)
                            {
                                value.State = ValueCombinationState.Covered;
                            }
                        }
                        else
                        {
                            // no uncovered values can be added with violating a constraint, add a random covered value
                            var compatibleWeightBuckets       = compatibleValues.GroupBy((v) => v.Weight).OrderByDescending((v) => v.Key);
                            ValueCombination value            = null;
                            bool             combinationFound = false;
                            foreach (var bucket in compatibleWeightBuckets)
                            {
                                int count    = bucket.Count();
                                int attempts = 0;

                                do
                                {
                                    value = bucket.ElementAt(random.Next(count - 1));
                                    CreateProposedCandidate(value, candidate, proposedCandidate);

                                    if (!interactions.ExcludedCombinations.Any((c) => IsCovered(c, proposedCandidate)))
                                    {
                                        combinationFound = true;
                                    }

                                    attempts++;

                                    // this is a heuristic, since we're pulling random values just going to count probably
                                    // means we've attempted duplicates, going to 2 * count means we've probably tried
                                    // everything at least once
                                    if (attempts > count * 2)
                                    {
                                        break;
                                    }
                                }while (!combinationFound);

                                if (combinationFound)
                                {
                                    break;
                                }
                            }

                            if (!combinationFound)
                            {
                                throw new InternalVariationGenerationException("Unable to find candidate with no exclusions.");
                            }

                            // add this value to candidate and mark all values as such

                            foreach (var valuePair in value.ParameterToValueMap)
                            {
                                candidate[valuePair.Key] = valuePair.Value;
                            }

                            variationTag = value.Tag == null || value.Tag == defaultTag ? variationTag : value.Tag;
                        }
                    }
                }

                variations.Add(new VariationIndexTagPair {
                    Indices = candidate, Tag = variationTag
                });

                // more variations than are need to exhaustively test the model have been adde
                if (variations.Count > maxVariations)
                {
                    throw new InternalVariationGenerationException("More variations than an exhaustive suite produced.");
                }
            }

            return(variations);
        }
コード例 #8
0
        // this is the actual generation function
        // returns a list of indices that allow lookup of the actual value in the model
        private static IList <int[]> GenerateVariationIndices(ParameterInteractionTable interactions, int variationSize, int seed, int maxVariations)
        {
            Random       random     = new Random(seed);
            List <int[]> variations = new List <int[]>();

            // while there a uncovered values
            while (!interactions.IsCovered())
            {
                int[] candidate = new int[variationSize];
                for (int i = 0; i < candidate.Length; i++)
                {
                    // -1 indicates an empty slot
                    candidate[i] = -1;
                }

                // while there are empty slots
                while (candidate.Any((i) => i == -1))
                {
                    // if all the slots are empty
                    if (candidate.All((i) => i == -1))
                    {
                        // then pick the first uncovered combination from the most uncovered parameter interaction
                        int mostUncovered =
                            interactions.Interactions.Max((i) => i.GetUncoveredCombinationsCount());

                        var interaction = interactions.Interactions.First((i) => i.GetUncoveredCombinationsCount() == mostUncovered);
                        var combination = interaction.Combinations.First((c) => c.State == ValueCombinationState.Uncovered);

                        foreach (var valuePair in combination.ParameterToVaueMap)
                        {
                            candidate[valuePair.Key] = valuePair.Value;
                        }

                        combination.State = ValueCombinationState.Covered;
                    }
                    else
                    {
                        // find interactions that aren't covered by the current candidate variation
                        var incompletelyCoveredInteractions =
                            from interaction in interactions.Interactions
                            where interaction.Parameters.Any((i) => candidate[i] == -1)
                            select interaction;

                        // find values that can be added to the current candidate
                        var compatibleValues =
                            from interaction in incompletelyCoveredInteractions
                            from combination in interaction.Combinations
                            where IsCompatibleValue(combination, candidate)
                            select combination;

                        // get the uncovered values
                        var uncoveredValues = compatibleValues.Where((v) => v.State == ValueCombinationState.Uncovered).ToList();

                        // calculate what the candidate will look like if add an uncovered value
                        var proposedCandidates =
                            from value in uncoveredValues
                            select new
                        {
                            Value     = value,
                            Candidate = CreateProposedCandidate(value, candidate)
                        };

                        // if any of the proposed candidates isn't exclude
                        if (proposedCandidates.Any((a) => !IsExcluded(interactions.ExcludedCombinations(), a.Candidate)))
                        {
                            // find the value that will cover the most combinations
                            int maxCovered = proposedCandidates.Max(
                                (a) => uncoveredValues.Count(
                                    (v) => IsCovered(v, a.Candidate) &&
                                    !IsExcluded(interactions.ExcludedCombinations(), a.Candidate)));

                            ValueCombination proposedValue = proposedCandidates.First(
                                (a) => uncoveredValues.Count(
                                    (v) => IsCovered(v, a.Candidate) &&
                                    !IsExcluded(interactions.ExcludedCombinations(), a.Candidate)) == maxCovered).Value;

                            // add this value to candidate and mark all values as such

                            foreach (var valuePair in proposedValue.ParameterToVaueMap)
                            {
                                candidate[valuePair.Key] = valuePair.Value;
                            }

                            // get the newly covered values so they can be marked
                            var newlyCoveredValue = uncoveredValues.Where((v) => IsCovered(v, candidate)).ToList();

                            foreach (var value in newlyCoveredValue)
                            {
                                value.State = ValueCombinationState.Covered;
                            }
                        }
                        else
                        {
                            // no uncovered values can be added with violating a constraint, add a random covered value
                            int count    = compatibleValues.Count();
                            int attempts = 0;
                            ValueCombination value;
                            int[]            proposedCandidate;
                            do
                            {
                                value             = compatibleValues.ElementAt(random.Next(count - 1));
                                proposedCandidate = CreateProposedCandidate(value, candidate);

                                // this is a heuristic, since we're pulling random values just going to count probably
                                // means we've attempted duplicates, going to 2 * count means we've probably tried
                                // everything at least once
                                if (attempts > count * 2)
                                {
                                    throw new InternalVariationGenerationException("Unable to find candidate with no exclusions.");
                                }

                                attempts++;
                            }while (interactions.ExcludedCombinations().Any((c) => IsCovered(c, CreateProposedCandidate(value, candidate))));

                            // add this value to candidate and mark all values as such

                            foreach (var valuePair in value.ParameterToVaueMap)
                            {
                                candidate[valuePair.Key] = valuePair.Value;
                            }
                        }
                    }
                }

                variations.Add(candidate);

                // more variations than are need to exhaustively test the model have been adde
                if (variations.Count > maxVariations)
                {
                    throw new InternalVariationGenerationException("More variations than an exhaustive suite produced.");
                }
            }

            return(variations);
        }