private void UnitTestTryGetValue(int maxNode, ref List <string> lines)
        {
            lines.Add("");
            List <Types> randTypes = this.GetRandomTypes(maxNode, false);

            lines = TestHeader(maxNode, ref lines, "TryGetValue", randTypes, 0, false);

            string abcd = this.xyz(maxNode);

            lines.Add($"{TypeToString(randTypes[randTypes.Count - 1])} fetch;", 3);
            lines.Add("bool b1;", 3);

            for (int u = 0; u <= 3; u++)
            {
                UnderlyingHashType under = (UnderlyingHashType)u;
                lines.Add(this.UnitTestNKeyHashDeclare(maxNode, randTypes, under, u == 0), 3);
                lines.Add($"b1 = nKeyHash.TryGetValue({abcd}, out fetch);", 3);
                lines.Add($"Assert.IsFalse(b1);", 3);
                lines.Add($"Assert.AreEqual(fetch, default({this.TypeToString(randTypes[randTypes.Count - 1])}));", 3);
                lines.Add($"nKeyHash.Add({abcd}, val);", 3);
                lines.Add($"b1 = nKeyHash.TryGetValue({abcd}, out fetch);", 3);
                lines.Add($"Assert.IsTrue(b1);", 3);
                lines.Add($"Assert.AreEqual(fetch, val);", 3);
                if (u != 3)
                {
                    lines.Add($"");
                }
            }

            lines.Add("}", 2);
        }
        private void UnitTestAny(int maxNode, ref List <string> lines)
        {
            lines.Add("");
            List <Types> randTypes = this.GetRandomTypes(maxNode, false);

            lines = TestHeader(maxNode, ref lines, "Any", randTypes, 0, true);

            int dent = 3;

            for (int u = 0; u <= 3; u++)
            {
                UnderlyingHashType under = (UnderlyingHashType)u;
                lines.Add(this.UnitTestNKeyHashDeclare(maxNode, randTypes, under, u == 0), 3);
                lines.Add("Assert.IsFalse(nKeyHash.Any());", dent);

                this.ArrayAdds(maxNode, ref lines, dent);

                lines.Add("Assert.IsTrue(nKeyHash.Any());", dent);
                lines.Add("nKeyHash.Clear();", dent);
                lines.Add("Assert.IsFalse(nKeyHash.Any());", dent);

                if (u != 3)
                {
                    lines.Add($"");
                }
            }

            lines.Add("}", 2);
        }
        private void UnitTestClear(int maxNode, ref List <string> lines)
        {
            lines.Add("");
            List <Types> randTypes = this.GetRandomTypes(maxNode, false);

            lines = TestHeader(maxNode, ref lines, "Clear", randTypes, 0, false);

            string abcd = this.xyz(maxNode);

            for (int u = 0; u <= 3; u++)
            {
                UnderlyingHashType under = (UnderlyingHashType)u;
                lines.Add(this.UnitTestNKeyHashDeclare(maxNode, randTypes, under, u == 0), 3);
                lines.Add($"nKeyHash.Add({abcd}, val);", 3);
                lines.Add($"Assert.IsTrue(nKeyHash.ContainsKey({abcd}));", 3);
                lines.Add($"nKeyHash.Clear();", 3);
                lines.Add($"Assert.IsFalse(nKeyHash.ContainsKey({abcd}));", 3);
                lines.Add($"Assert.AreEqual(nKeyHash.Count({this.xyz(maxNode - 1)}), 0);", 3);
                if (u != 3)
                {
                    lines.Add($"");
                }
            }

            lines.Add("}", 2);
        }
        private void UnitTestUpsert(int maxNode, ref List <string> lines)
        {
            /*
             * NKeyHash<bool, int, long> nKeyHash = new NKeyHash<bool, int, long>();
             * long newValue = 0;
             * nKeyHash.Add(a, b, val);
             * nKeyHash.Upsert(a, b, newValue);
             * Assert.AreEqual(newValue, nKeyHash.Get(a, b));
             */

            lines.Add("");
            List <Types> randTypes = this.GetRandomTypes(maxNode, false);

            randTypes.Add(randTypes[randTypes.Count - 1]);

            lines = TestHeader(maxNode, ref lines, "Upsert", randTypes, 1, false);

            string abcd = this.xyz(maxNode);

            for (int u = 0; u <= 3; u++)
            {
                UnderlyingHashType under = (UnderlyingHashType)u;
                lines.Add(this.UnitTestNKeyHashDeclare(maxNode, randTypes, under, u == 0), 3);
                lines.Add($"nKeyHash.Add({abcd}, val);", 3);
                lines.Add($"Assert.AreEqual(val, nKeyHash.Get({abcd}));", 3);
                lines.Add($"nKeyHash.Upsert({abcd}, newVal1);", 3);
                lines.Add($"Assert.AreEqual(newVal1, nKeyHash.Get({abcd}));", 3);
                if (u != 3)
                {
                    lines.Add($"");
                }
            }

            lines.Add("}", 2);
        }
        private void UnitTestAdd(int maxNode, ref List <string> lines)
        {
            lines.Add("");
            List <Types> randTypes = this.GetRandomTypes(maxNode, false);

            lines = TestHeader(maxNode, ref lines, "Add", randTypes, 0, false);

            /*
             * NKeyHash<short, string, long, float, double> NKeyHash = new NKeyHash<short, string, long, float, double>();
             * nKeyHash.Add(a, b, c, d, val);
             * Assert.AreEqual(val, nKeyHash.Get(a, b, c, d));
             */

            string abcd = this.xyz(maxNode);
            string e    = this.GenericCharLower(maxNode);

            for (int u = 0; u <= 3; u++)
            {
                UnderlyingHashType under = (UnderlyingHashType)u;
                lines.Add(this.UnitTestNKeyHashDeclare(maxNode, randTypes, under, u == 0), 3);
                lines.Add($"nKeyHash.Add({abcd}, val);", 3);
                lines.Add($"Assert.AreEqual(val, nKeyHash.Get({abcd}));", 3);
                if (u != 3)
                {
                    lines.Add($"");
                }
            }

            lines.Add("}", 2);
        }
        private string UnitTestNKeyHashDeclare(int maxNode,
                                               List <Types> randTypes,
                                               UnderlyingHashType under,
                                               bool fullDeclare)
        {
            //NKeyHash<short, string, long, float, double> nKeysHash5 = new NKeyHash<short, string, long, float, double>();
            string line = TypesList(maxNode, randTypes);

            if (fullDeclare)
            {
                return($"NKeyHash<{line}> nKeyHash = new NKeyHash<{line}>(UnderlyingHashType.{under});");
            }
            return($"nKeyHash = new NKeyHash<{line}>(UnderlyingHashType.{under});");
        }
 /// <summary>
 /// NKeyHash is a IEnumerable-enabled multidimensional array. It can currently use System.Collections.HashTable, System.Collections.Generic.Dictionary, System.Collections.Generic.SortedDictionary, or System.Collections.Concurrent.ConcurrentDictionary for the underlying base hashable object. NKeyHash uses generics to create nested hashable objects.
 /// </summary>
 /// <param name="cubeUnderlyingHashType">cubeUnderlyingHashType is used to set what the underlying object that is used to store and access the data</param>
 public NKeyHash(UnderlyingHashType cubeUnderlyingHashType = UnderlyingHashType.Dictionary)
 {
     this.UnderlyingHashType = cubeUnderlyingHashType;
 }
Exemple #8
0
 /// <summary>
 /// NKeyHash is a IEnumerable-enabled multidimensional array. It can currently use System.Collections.HashTable, System.Collections.Generic.Dictionary, System.Collections.Generic.SortedDictionary, or System.Collections.Concurrent.ConcurrentDictionary for the underlying base hashable object. NKeyHash uses generics to create nested hashable objects.
 /// </summary>
 /// <param name="cubeUnderlyingHashType">Used to set what the underlying object that is used to store and access the data.</param>
 public NKeyHash(UnderlyingHashType cubeUnderlyingHashType = UnderlyingHashType.Dictionary)
 {
     this.UnderlyingHashType = cubeUnderlyingHashType;
     this.Values             = new NKeyHash <A, NKeyHash <B, NKeyHash <C, NKeyHash <D, NKeyHash <E, NKeyHash <F, NKeyHash <G, NKeyHash <H, VALUE> > > > > > > >(this.UnderlyingHashType);
 }
        private void UnitTestForloop(int maxNode, ref List <string> lines)
        {
            lines.Add("");
            List <Types> randTypes = this.GetRandomTypes(maxNode, true);

            lines = TestHeader(maxNode, ref lines, "Forloop", randTypes, 0, true);

            int           dent = 3;
            StringBuilder line;

            string keyTypes = TypesList(maxNode, randTypes);


            lines.Add($"bool[] found = new bool[9];", dent);
            lines.Add("bool allFound = false;", dent);

            for (int u = 0; u <= 3; u++)
            {
                UnderlyingHashType under = (UnderlyingHashType)u;
                lines.Add(this.UnitTestNKeyHashDeclare(maxNode, randTypes, under, u == 0), 3);
                this.ArrayAdds(maxNode, ref lines, dent);

                lines.Add($"", dent);
                lines.Add($"found = new bool[9];", dent);
                lines.Add($"", dent);

                lines.Add($"foreach (NKeyValue<{keyTypes}> mkv in nKeyHash)", dent);
                lines.Add("{", dent++);

                for (int t = 0; t < 9; t++)
                {
                    line = new StringBuilder();
                    line.Append($"if(found[{t}] == false ");

                    for (int n = 0; n < maxNode; n++)
                    {
                        line.Append($" && mkv.Key{n} == {GenericCharLower(n)}[{t}]");
                    }
                    line.Append($" && mkv.Value == val[{t}]");

                    line.Append(")");
                    lines.Add(line, dent);
                    // lines.Add($"if (found[0] == false && mkv.Key0 == a[0] && mkv.Key1 == b[0] && mkv.Key2 == c[0] && mkv.Value == val[0])", dent);

                    lines.Add("{", dent++);
                    lines.Add($"found[{t}] = true;", dent);
                    lines.Add($"continue;", dent);
                    lines.Add("}", --dent);
                    lines.Add($"");
                }

                lines.Add("");
                lines.Add("throw new System.Exception(\"Too many found\");", dent);

                lines.Add("}", --dent);

                lines.Add("");

                line = new StringBuilder();
                line.Append("allFound = ");

                for (int t = 0; t < 9; t++)
                {
                    line.Append($"found[{t}]");
                    if (t != 8)
                    {
                        line.Append(" &&");
                    }
                }
                line.Append(";");
                lines.Add(line, dent);

                lines.Add("Assert.IsTrue(allFound);", dent);
                lines.Add($"", dent);



                ArrayAddRemoveNKeyHash(maxNode, ref lines, dent, 7, true);
                ArrayAddRemoveNKeyHash(maxNode, ref lines, dent, 8, true);
                lines.Add($"", dent);



                lines.Add($"found = new bool[9];", dent);
                lines.Add($"", dent);

                lines.Add($"foreach (NKeyValue<{keyTypes}> mkv in nKeyHash)", dent);
                lines.Add("{", dent++);

                for (int t = 0; t < 7; t++)
                {
                    line = new StringBuilder();
                    line.Append($"if(found[{t}] == false ");

                    for (int n = 0; n < maxNode; n++)
                    {
                        line.Append($" && mkv.Key{n} == {GenericCharLower(n)}[{t}]");
                    }
                    line.Append($" && mkv.Value == val[{t}]");

                    line.Append(")");
                    lines.Add(line, dent);
                    // lines.Add($"if (found[0] == false && mkv.Key0 == a[0] && mkv.Key1 == b[0] && mkv.Key2 == c[0] && mkv.Value == val[0])", dent);

                    lines.Add("{", dent++);
                    lines.Add($"found[{t}] = true;", dent);
                    lines.Add($"continue;", dent);
                    lines.Add("}", --dent);
                    lines.Add($"");
                }

                lines.Add("");
                lines.Add("throw new System.Exception(\"Too many found\");", dent);

                lines.Add("}", --dent);

                lines.Add("");

                line = new StringBuilder();
                line.Append("allFound = ");

                for (int t = 0; t < 7; t++)
                {
                    line.Append($"found[{t}]");
                    if (t != 6)
                    {
                        line.Append(" &&");
                    }
                }
                line.Append(";");
                lines.Add(line, dent);

                lines.Add("Assert.IsTrue(allFound);", dent);



                if (u != 3)
                {
                    lines.Add($"");
                }
            }



            lines.Add("}", 2);
        }
        private void UnitTestCount(int maxNode, ref List <string> lines)
        {
            lines.Add("");
            List <Types> randTypes = this.GetRandomTypes(maxNode, true);

            lines = TestHeader(maxNode, ref lines, "Count", randTypes, 0, true);

            string        abc  = this.xyz(maxNode - 1);
            StringBuilder line = new StringBuilder(100);

            for (int u = 0; u <= 3; u++)
            {
                UnderlyingHashType under = (UnderlyingHashType)u;
                lines.Add(this.UnitTestNKeyHashDeclare(maxNode, randTypes, under, u == 0), 3);

                int dent = 3;
                int dex  = 0;

                for (int t = 0; t < 9; t++)
                {
                    line = new StringBuilder();
                    line.Append("nKeyHash.Add(");

                    for (int n = 0; n <= maxNode; n++)
                    {
                        if (n == maxNode - 1)
                        {
                            dex = t;
                        }
                        else
                        {
                            dex = 0;
                        }

                        if (n != maxNode)
                        {
                            line.Append($"{this.GenericCharLower(n)}[{dex}],");
                        }
                        else
                        {
                            line.Append($"val[{t}]");
                        }
                    }

                    line.Append(");");
                    lines.Add(line, dent);
                }



                line = new StringBuilder();

                line.Append("Assert.AreEqual(nKeyHash.Count(");
                for (int n = 0; n < maxNode - 1; n++)
                {
                    line.Append($"{this.GenericCharLower(n)}[0]");
                    if (n != maxNode - 2)
                    {
                        line.Append(",");
                    }
                }
                line.Append("), 9);");
                lines.Add(line, dent);



                if (u != 3)
                {
                    lines.Add($"");
                }
            }

            lines.Add("}", 2);
        }