示例#1
0
        public void TestRegistration()
        {
            AutoCompleteMachine cm = new AutoCompleteMachine();

            // 登録チェック
            cm.RegisterWord("test");
            Assert.AreEqual(true, cm.HasItem("test"));

            // 削除チェック
            cm.RemoveWord("test");
            Assert.AreEqual(false, cm.HasItem("test"));

            // 削除後登録チェック
            cm.RegisterWord("test");
            Assert.AreEqual(true, cm.HasItem("test"));

            // 多重登録禁止チェック
            {
                bool catched_multiple_registering_exception = false;
                try
                {
                    cm.RegisterWord("test");
                }
                catch (AutoCompleteMachineMultipleRegisteringException)
                {
                    catched_multiple_registering_exception = true;
                }
                finally
                {
                    Assert.AreEqual(true, catched_multiple_registering_exception);
                    Assert.AreEqual(true, cm.HasItem("test"));
                    Assert.AreEqual(1, cm.GetNumOfWords());
                }
            }

            // 存在しないアイテムの削除チェック
            {
                bool catched_multiple_removing_exception = false;
                try
                {
                    cm.RemoveWord("test");
                    cm.RemoveWord("test");
                }
                catch (AutoCompleteMachineMultipleRemovingException)
                {
                    catched_multiple_removing_exception = true;
                }
                finally
                {
                    Assert.AreEqual(true, catched_multiple_removing_exception);
                    Assert.AreEqual(false, cm.HasItem("test"));
                }
            }
        }
示例#2
0
        public void TestAutoComplete()
        {
            AutoCompleteMachine cm = new AutoCompleteMachine();

            // キーワード登録
            cm.RegisterWord("test");
            cm.RegisterWord("hoge");
            cm.RegisterWord("fuga");
            cm.RegisterWord("hoghog");
            cm.RegisterWord("testest");
            cm.RegisterWord("fuf");
            cm.RegisterWord("fuffy");

            // 補完
            Assert.AreEqual("test", cm.AutoCompleteWord("t"));
            Assert.AreEqual("test", cm.AutoCompleteWord("te"));
            Assert.AreEqual("", cm.AutoCompleteWord("ts"));
            Assert.AreEqual("hog", cm.AutoCompleteWord("h"));
            Assert.AreEqual("testest", cm.AutoCompleteWord("teste"));
            Assert.AreEqual("fu", cm.AutoCompleteWord("f"));
            Assert.AreEqual("fu", cm.AutoCompleteWord("fu"));
            Assert.AreEqual("fuf", cm.AutoCompleteWord("fuf"));
            Assert.AreEqual("", cm.AutoCompleteWord(""));

            // 候補
            {
                List <string> cand = cm.GetCandidates("t");
                Assert.AreEqual(2, cand.Count);
                Assert.AreEqual("test", cand[0]);
                Assert.AreEqual("testest", cand[1]);
            }
            {
                List <string> cand = cm.GetCandidates("h");
                Assert.AreEqual(2, cand.Count);
                Assert.AreEqual("hoge", cand[0]);
                Assert.AreEqual("hoghog", cand[1]);
            }
            {
                List <string> cand = cm.GetCandidates("f");
                Assert.AreEqual(3, cand.Count);
                Assert.AreEqual("fuga", cand[0]);
                Assert.AreEqual("fuf", cand[1]);
                Assert.AreEqual("fuffy", cand[2]);
            }
            {
                List <string> cand = cm.GetCandidates("fu");
                Assert.AreEqual(3, cand.Count);
                Assert.AreEqual("fuga", cand[0]);
                Assert.AreEqual("fuf", cand[1]);
                Assert.AreEqual("fuffy", cand[2]);
            }
            {
                List <string> cand = cm.GetCandidates("fug");
                Assert.AreEqual(1, cand.Count);
                Assert.AreEqual("fuga", cand[0]);
            }
            {
                List <string> cand = cm.GetCandidates("fuf");
                Assert.AreEqual(2, cand.Count);
                Assert.AreEqual("fuf", cand[0]);
                Assert.AreEqual("fuffy", cand[1]);
            }
        }