Esempio n. 1
0
        private static void TestTreeInvariants(string[] testValues)
        {
            var tree = BKTree.Create(testValues);

            foreach (var value in testValues)
            {
                // With a threshold of 0, we should only find exactly the item we're searching for.
                Assert.Single(tree.Find(value, threshold: 0), value.ToLower());
            }

            foreach (var value in testValues)
            {
                // With a threshold of 1, we should always at least find the item we're looking for.
                // But we may also find additional items along with it.
                var items = tree.Find(value, threshold: 1);
                Assert.Contains(value.ToLower(), items);

                // We better not be finding all items.
                Assert.NotEqual(testValues.Length, items.Count);
            }

            foreach (var value in testValues)
            {
                // If we delete each individual character in each search string, we should still
                // find the value in the tree.
                for (var i = 0; i < value.Length; i++)
                {
                    var items = tree.Find(Delete(value, i), threshold: null);
                    Assert.Contains(value.ToLower(), items);

                    // We better not be finding all items.
                    Assert.NotEqual(testValues.Length, items.Count);
                }
            }

            foreach (var value in testValues)
            {
                // If we add a random character at any location in a string, we should still
                // be able to find it.
                for (var i = 0; i <= value.Length; i++)
                {
                    var items = tree.Find(Insert(value, i, 'Z'), threshold: null);
                    Assert.Contains(value.ToLower(), items);

                    // We better not be finding all items.
                    Assert.NotEqual(testValues.Length, items.Count);
                }
            }

            foreach (var value in testValues)
            {
                // If we transpose any characters in a string, we should still
                // be able to find it.
                for (var i = 0; i < value.Length - 1; i++)
                {
                    var items = tree.Find(Transpose(value, i), threshold: null);
                    Assert.Contains(value.ToLower(), items);
                }
            }
        }
Esempio n. 2
0
        public void SimpleTests()
        {
            string[] testValues =
            {
                "cook",
                "book",
                "books",
                "cake",
                "what",
                "water",
                "Cape",
                "Boon",
                "Cook",
                "Cart"
            };
            var tree = BKTree.Create(testValues);

            var results1 = tree.Find("wat", threshold: 1);

            Assert.Single(results1, "what");

            var results2 = tree.Find("wat", threshold: 2);

            Assert.True(results2.SetEquals(Expected("cart", "what", "water")));

            var results3 = tree.Find("caqe", threshold: 1);

            Assert.True(results3.SetEquals(Expected("cake", "cape")));
        }
Esempio n. 3
0
        public void Test2()
        {
            string[] testValues = { "Leeds", "York", "Bristol", "Leicester", "Hull", "Durham" };
            var      tree       = BKTree.Create(testValues);

            var results = tree.Find("hill", threshold: null);

            Assert.True(results.SetEquals(Expected("hull")));

            results = tree.Find("liecester", threshold: null);
            Assert.True(results.SetEquals(Expected("leicester")));

            results = tree.Find("leicestre", threshold: null);
            Assert.True(results.SetEquals(Expected("leicester")));

            results = tree.Find("lecester", threshold: null);
            Assert.True(results.SetEquals(Expected("leicester")));
        }