Пример #1
0
    public static List Permutations(List x)
    {
        var permuter = new Permuter(x);

        permuter.Permute(x.Length);
        return(permuter.Permutations);
    }
Пример #2
0
        /// <summary>
        /// Returns all permutations of the static property data sources.
        /// </summary>
        public override IEnumerable <object[]> GetData(MethodInfo methodUnderTest)
        {
            //TODO: Only works for immutable arguments.
            // Get arguments from each data source, using MemberDataAttribute.
            object[][][] dataSources = propertyNames_.Select(
                propertyName => new MemberDataAttribute(propertyName)
                .GetData(methodUnderTest)
                .ToArray( )
                ).ToArray( );

            // Permute indices into data sources.
            int parameterCount    = methodUnderTest.GetParameters( ).Length;
            var dataSourceIndices = dataSources.Select(dataSource => Enumerable.Range(0, dataSource.Length));

            foreach (int[] indices in Permuter.Permute(dataSourceIndices))
            {
                object[] buffer      = new object[parameterCount];
                int      bufferIndex = 0;

                // Build arguments buffer from data source at each index.
                for (int i = 0; i < indices.Length; ++i)
                {
                    int      index      = indices[i];
                    object[] dataSource = dataSources[i][index];
                    foreach (object data in dataSource)
                    {
                        buffer[bufferIndex++] = data;
                    }
                }

                yield return(buffer);
            }
        }
 private void GivenTests(Permuter permuter)
 {
     Assert.IsTrue(permuter.HasPalindrome("civic"));
     Assert.IsTrue(permuter.HasPalindrome("ivicc"));
     Assert.IsFalse(permuter.HasPalindrome("civil"));
     Assert.IsFalse(permuter.HasPalindrome("livci"));
 }
Пример #4
0
        Permute_OneRowWithTwoItemsOneRowWithSingleItem_WithTwoRowsOfDoubleItems_4RowsWithTwoWithFourItemsAndTwoWithThreeItems
            ()
        {
            List <Dictionary <string, int> > result = GetOneRowWithTwoItemsOneRowWithSingleItemResult();

            Permuter.Permute(result, GetTwoRowsWithDoubleItemsResult());

            Dictionary <string, int>[] expectedResult = new Dictionary <string, int>[]
            {
                CreateResult(new KI("int", 2),
                             new KI("int2", 3),
                             new KI("int4", 9),
                             new KI("int5", 10)),
                CreateResult(new KI("int", 2),
                             new KI("int2", 3),
                             new KI("int4", 12),
                             new KI("int5", 13)),
                CreateResult(new KI("int", 4),
                             new KI("int4", 9),
                             new KI("int5", 10)),
                CreateResult(new KI("int", 4),
                             new KI("int4", 12),
                             new KI("int5", 13))
            };
            AssertResult(expectedResult, result);
        }
Пример #5
0
        static void Main(string[] args)
        {
            var permute = new Permuter<string>(new List<string>(){"a", "b", "c", "d"});

            var results = permute.AllPermutations();

            foreach (var list in results)
            {
                foreach (var x in list)
                {
                    Console.Write(x.ToString());
                }
                Console.WriteLine();
            }

            var permute2 = new Permuter<int>(new List<int>(){1, 2, 3, 4, 5, 6});
            var results2 = permute2.AllPermutations();

            foreach (var list in results2)
            {
                foreach (var x in list)
                {
                    Console.Write(x.ToString());
                }
                Console.WriteLine();
            }

            Console.ReadKey();
        }
Пример #6
0
        public void Permute_SingleItem_WithEmptyList_NoChange()
        {
            List <Dictionary <string, int> > result = GetSingleItemResult();

            Permuter.Permute(result, "int2", new int[] {});
            Asserter.Assert(new DictionaryContentAsserter <string, int>(GetSingleItemResult(), result));
        }
Пример #7
0
        /// <summary>
        /// Enumerates all solutions.  WARNING: if there are n numbers then
        /// there are n! permutations.  This number gets large very quickly.
        /// Assumes the first and last points in the array are fixed.
        /// </summary>
        public void Solve()
        {
            var initSolution = new List<int>();

            for (int i = 1; i <= solution.Count - 2; i++)
            {
                initSolution.Add(solution[i]);
            }

            var permute = new Permuter<int>(initSolution);
            var results = permute.AllPermutations();

            double bestSolutionCost = objective.Value(solution);
            var bestSolution = solution;

            foreach (var permutation in results)
            {
                //add start location to the start and end or list
                permutation.Insert(0, 0);
                permutation.Add(0);

                var cost = this.objective.Value(permutation);

                if (cost < bestSolutionCost)
                {
                    bestSolutionCost = cost;
                    bestSolution = permutation;
                }
            }
        }
Пример #8
0
        public void Permute_Empty_WithEmptyListOfList_Empty()
        {
            List <Dictionary <string, int> > result = GetEmptyResult();

            Permuter.Permute(result, GetEmptyResult());

            Asserter.Assert(new DictionaryContentAsserter <string, int>(GetEmptyResult(), result));
        }
Пример #9
0
        public void Permute_Empty_WithTwoRowsOfDoubleItems_TwoRowsOfDoubleItems()
        {
            List <Dictionary <string, int> > result = GetEmptyResult();

            Permuter.Permute(result, GetTwoRowsWithDoubleItemsResult());

            Asserter.Assert(new DictionaryContentAsserter <string, int>(GetTwoRowsWithDoubleItemsResult(), result));
        }
Пример #10
0
        public void Permute_SingleRowWithTwoItems_WithEmptyListOfList_NoChange()
        {
            List <Dictionary <string, int> > result = GetSingleRowWithTwoItemsResult();

            Permuter.Permute(result, GetEmptyResult());

            Asserter.Assert(new DictionaryContentAsserter <string, int>(GetSingleRowWithTwoItemsResult(), result));
        }
Пример #11
0
        public void Permute_TwoRowsWithSingleItem_WithEmptyListOfList_NoChange()
        {
            List <Dictionary <string, int> > result = GetTwoRowsWithSingleItemEachResult();

            Permuter.Permute(result, GetEmptyResult());

            Asserter.Assert(new DictionaryContentAsserter <string, int>(GetTwoRowsWithSingleItemEachResult(), result));
        }
 private void EdgeCaseTestSetup(Permuter permuter)
 {
     Assert.IsTrue(permuter.HasPalindrome(""));
     Assert.IsTrue(permuter.HasPalindrome(" "));
     Assert.IsTrue(permuter.HasPalindrome(null));
     Assert.IsTrue(permuter.HasPalindrome("a"));
     Assert.IsTrue(permuter.HasPalindrome("aa"));
 }
 private void SimpleTests(Permuter permuter)
 {
     //I don't like copy/pasting my tests like this.  Any ideas for a better way?
     Assert.IsTrue(permuter.HasPalindrome("abba"));
     Assert.IsTrue(permuter.HasPalindrome("abcba"));
     Assert.IsTrue(permuter.HasPalindrome("aabbc"));
     Assert.IsFalse(permuter.HasPalindrome("abc"));
 }
Пример #14
0
        public void MultiMemberDataAttribute_succeeds_for_multiple_data_sources( )
        {
            var method          = IntCharMethod(default(int), default(char));
            var attribute       = new MultiMemberDataAttribute(IntsName, CharsName);
            var rawPropertyData = new[] { ToObjectArray(ints_), ToObjectArray(chars_) };
            var propertyData    = Permuter.Permute(rawPropertyData);

            var attributeData = attribute.GetData(method).ToArray( );

            Assert.Equal(propertyData, attributeData, Comparer);
        }
Пример #15
0
        public void Permute_Empty_WithSingleItem_SingleItem()
        {
            List <Dictionary <string, int> > result = GetEmptyResult();

            Permuter.Permute(result, "int", 9);

            IDictionary <string, int>[] expectedResult = new IDictionary <string, int>[]
            { CreateResult(new KI("int", 9)) };

            Asserter.Assert(new DictionaryContentAsserter <string, int>(expectedResult, result));
        }
        private static IEnumerable <object[]> GetDataCore(IEnumerable <IEnumerable <IInstanceCreator> > parameterCreators)
        {
            foreach (var permutation in Permuter.Permute(parameterCreators))
            {
                object[] data = new object[permutation.Length];
                for (int i = 0; i < data.Length; ++i)
                {
                    data[i] = permutation[i].CreateInstance( );
                }

                yield return(data);
            }
        }
Пример #17
0
        public void Permute_Empty_WithTwoItems_TwoRowsOfSingleItem()
        {
            List <Dictionary <string, int> > result = GetEmptyResult();

            Permuter.Permute(result, "int", new int[] { 9, 12 });

            Dictionary <string, int>[] expectedResult = new Dictionary <string, int>[]
            {
                CreateResult(new KI("int", 9)),
                CreateResult(new KI("int", 12))
            };
            AssertResult(expectedResult, result);
        }
Пример #18
0
        public void Permute_SingleItem_WithSingleItem_SingleRowWithBothItems()
        {
            List <Dictionary <string, int> > result = GetSingleItemResult();

            Permuter.Permute(result, "value", 9);

            IDictionary <string, int>[] expectedResult = new Dictionary <string, int>[]
            {
                CreateResult(new KI("int", 2),
                             new KI("value", 9))
            };
            Asserter.Assert(new DictionaryContentAsserter <string, int>(expectedResult, result));
        }
Пример #19
0
        private static object[][] GetFlattenedPermutations(object[][][] rawData)
        {
            var permutations          = Permuter.Permute(rawData);
            var flattenedPermutations = new object[permutations.Length][];

            for (int i = 0; i < permutations.Length; ++i)
            {
                object[][] permutation = permutations[i];
                object[]   flattened   = permutation.SelectMany(data => data).ToArray( );
                flattenedPermutations[i] = flattened;
            }

            return(flattenedPermutations);
        }
Пример #20
0
        public void Permute_SingleItem_WithTwoRowsOfSingleItems_TwoRowsWithSingleItemAndEachOfItems()
        {
            List <Dictionary <string, int> > result = GetSingleItemResult();

            Permuter.Permute(result, GetTwoRowsWithSingleItemEachAtInt5Result());

            Dictionary <string, int>[] expectedResult = new Dictionary <string, int>[]
            {
                CreateResult(new KI("int", 2),
                             new KI("int5", 9)),
                CreateResult(new KI("int", 2),
                             new KI("int5", 12))
            };
            AssertResult(expectedResult, result);
        }
        private void TimingTestExecution(Permuter permuter,
                                         string input,
                                         int numberofruns)
        {
            Trace.Write("Testing " + permuter.ToString() + " with " + numberofruns + ": ");
            var watch = new Stopwatch();

            watch.Start();

            for (int i = 0; i < numberofruns; i++)
            {
                permuter.HasPalindrome(input);
            }
            watch.Stop();
            Trace.WriteLine(watch.ElapsedMilliseconds);
        }
Пример #22
0
        public void Permute_TwoRowsWithSingleItem_WithSingleItem_TwoRowsWithTwoItems()
        {
            List <Dictionary <string, int> > result = GetTwoRowsWithSingleItemEachResult();

            Permuter.Permute(result, "int2", 9);

            IDictionary <string, int>[] expectedResult = new Dictionary <string, int>[]
            {
                CreateResult(new KI("int", 2),
                             new KI("int2", 9)),
                CreateResult(new KI("int", 4),
                             new KI("int2", 9))
            };

            AssertResult(expectedResult, result);
        }
Пример #23
0
        public void Permute_SingleRowWithTwoItems_WithTwoItems_TwoRowsWithTheTwoItemsAndEachOfItems()
        {
            List <Dictionary <string, int> > result = GetSingleRowWithTwoItemsResult();

            Permuter.Permute(result, "int3", new int[] { 9, 12 });

            Dictionary <string, int>[] expectedResult = new Dictionary <string, int>[]
            {
                CreateResult(new KI("int", 2),
                             new KI("int2", 4),
                             new KI("int3", 9)),
                CreateResult(new KI("int", 2),
                             new KI("int2", 4),
                             new KI("int3", 12))
            };

            AssertResult(expectedResult, result);
        }
Пример #24
0
    static void Main(string[] args)
    {
        Permuter p;

        // allYHVH
        Value[] yhv = new Value[3] {
            Value.Yod, Value.He, Value.Vau
        };
        p = new Permuter(yhv, 4, false, true, "allYHVHfullName");

        // "unique" YHVH
        Value[] yhvh = new Value[4] {
            Value.Yod, Value.He, Value.Vau, Value.HeEarth
        };
        p = new Permuter(yhvh, 4, true, true, "twelvetribesfullname");

        // I Ching
        Value[] yinYang = new Value[2] {
            Value.Yin, Value.Yang
        };
        p = new Permuter(yinYang, 6, false, true, "IChing");

        // Bagua
        p = new Permuter(yinYang, 3, false, true, "bagua");

        // 16 gates
        Value[] fourElements = new Value[4] {
            Value.Earth, Value.Water, Value.Air, Value.Fire
        };
        p = new Permuter(fourElements, 2, false, true, "sixteengates");

        // runes
        p = new Permuter(fourElements, 4, true, true, "runes");

        // possible order of zodiac signs
        // Value[] zodiac = new Value[12] { Value.Aries, Value.Taurus,Value.Gemini,Value.Cancer,Value.Leo,
        // Value.Virgo,Value.Libra,Value.Scorpio,Value.Sagitarius, Value.Capricorn, Value.Aquarius, Value.Pisces };
        // p = new Permuter(zodiac, 12, false, true, "zodiac");
    }
Пример #25
0
        Permute_OneRowWithTwoItemsOneRowWithSingleItem_WithTwoItems_4RowsWithTwoWithThreeItemsAndTwoWithTwoItems
            ()
        {
            List <Dictionary <string, int> > result = GetOneRowWithTwoItemsOneRowWithSingleItemResult();

            Permuter.Permute(result, "int3", new int[] { 9, 12 });

            Dictionary <string, int>[] expectedResult = new Dictionary <string, int>[]
            {
                CreateResult(new KI("int", 2),
                             new KI("int2", 3),
                             new KI("int3", 9)),
                CreateResult(new KI("int", 2),
                             new KI("int2", 3),
                             new KI("int3", 12)),
                CreateResult(new KI("int", 4),
                             new KI("int3", 9)),
                CreateResult(new KI("int", 4),
                             new KI("int3", 12))
            };
            AssertResult(expectedResult, result);
        }
Пример #26
0
        public void Permute_TwoRowsWithSingleItem_WithTwoRowsOfDoubleItems_4RowsWithThreeItems()
        {
            List <Dictionary <string, int> > result = GetTwoRowsWithSingleItemEachResult();

            Permuter.Permute(result, GetTwoRowsWithDoubleItemsResult());

            Dictionary <string, int>[] expectedResult = new Dictionary <string, int>[]
            {
                CreateResult(new KI("int", 2),
                             new KI("int4", 9),
                             new KI("int5", 10)),
                CreateResult(new KI("int", 2),
                             new KI("int4", 12),
                             new KI("int5", 13)),
                CreateResult(new KI("int", 4),
                             new KI("int4", 9),
                             new KI("int5", 10)),
                CreateResult(new KI("int", 4),
                             new KI("int4", 12),
                             new KI("int5", 13))
            };
            AssertResult(expectedResult, result);
        }
Пример #27
0
 public void Permute_NullResultSingleItem_Throws()
 {
     Assert.Throws <ArgumentNullException>(() => Permuter.Permute(null, "", 9));
 }
Пример #28
0
 public void Permute_NullResultListResults_Throws()
 {
     Assert.Throws <ArgumentNullException>(() => Permuter.Permute(null, GetSingleItemResult()));
 }
Пример #29
0
        public void Permute_NullListOfItems_Throws()
        {
            List <Dictionary <string, object> > result = new List <Dictionary <string, object> >();

            Assert.Throws <ArgumentNullException>(() => Permuter.Permute(result, null));
        }
Пример #30
0
 internal BacktrackPointWithPartitions(
     Dictionary<Node,int> indexDict,
     Dictionary<Node,int> rangeDict,
     CompoundTerm outLabel,
     int index,
     int relationalPassIndex,
     Set<CompoundTerm> relationalEdgeLabels,
     Set<Node> xtoNodes,
     Set<Node> ytoNodes,
     Map<Pair<IComparable, bool>, Set<Node>> xPartitions,
     Map<Pair<IComparable, bool>, Set<Node>> yPartitions
     )
 {
     this.index = index;
     this.outLabel = outLabel;
     this.relationalPassIndex = relationalPassIndex;
     this.indexDict = new Dictionary<Node, int>();
     this.rangeDict = new Dictionary<Node, int>();
     this.relationalEdgeLabels = relationalEdgeLabels;
     this.xtoNodes = xtoNodes;
     this.ytoNodes = ytoNodes;
     foreach (KeyValuePair<Node,int> pair in indexDict)
     {
         this.indexDict.Add(pair.Key, pair.Value);
     }
     foreach (KeyValuePair<Node, int> pair in rangeDict)
     {
         this.rangeDict.Add(pair.Key, pair.Value);
     }
     this.permuter = new Permuter(xtoNodes.Count);
     this.xPartitions = xPartitions;
     this.yPartitions = yPartitions;
 }
Пример #31
0
        public void Permute_NullSingleItem_Succeeds()
        {
            var result = new List <Dictionary <string, object> >();

            Permuter.Permute(result, "", (object)null);
        }
Пример #32
0
 public void Permute_NullResultListOfListOfItems_Throws()
 {
     Assert.Throws <ArgumentNullException>(() => Permuter.Permute(null, "", new int[][] { new int[] { 9 } }));
 }