示例#1
0
        public void WordDetection()
        {
            Document        doc      = new Document();
            DefaultWordProc wordProc = new DefaultWordProc();

            string[] samples = new string[] { "aa", "11", ",,", "\x3042\x3042", "\x963f\x963f", "\n\n", "  " };
            int[]    expected;
            int[]    expectedForSameClass;

            // NextWordStart
            expected             = new int[] { 0, 2, 2, 4, 4 };
            expectedForSameClass = new int[] { 0, 4, 4, 4, 4 };
            for (int left = 0; left < samples.Length; left++)
            {
                for (int right = 0; right < samples.Length; right++)
                {
                    doc.Text = samples[left] + samples[right];
                    for (int i = 0; i < doc.Length; i++)
                    {
                        int actual = wordProc.NextWordStart(doc, i);
                        if (left == right)
                        {
                            Assert.AreEqual(expectedForSameClass[i], actual);
                        }
                        else
                        {
                            Assert.AreEqual(expected[i], actual);
                        }
                    }
                }
            }
            Assert.AreEqual(doc.Length, wordProc.NextWordStart(doc, doc.Length));

            // combining character
            doc.Text = ".a\x0300.";
            Assert.AreEqual(1, wordProc.NextWordStart(doc, 1));
            Assert.AreEqual(3, wordProc.NextWordStart(doc, 2));
            Assert.AreEqual(3, wordProc.NextWordStart(doc, 3));

            // combining character
            doc.Text = "aa\x0300a";
            Assert.AreEqual(4, wordProc.NextWordStart(doc, 1));
            Assert.AreEqual(4, wordProc.NextWordStart(doc, 2));
            Assert.AreEqual(4, wordProc.NextWordStart(doc, 3));
            MyAssert.Throws <ArgumentNullException>(delegate {
                wordProc.NextWordStart(null, 0);
            });
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                wordProc.NextWordStart(doc, -1);
            });
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                wordProc.NextWordStart(doc, doc.Length + 1);
            });

            // NextWordEnd
            expected             = new int[] { 2, 2, 2, 4, 4 };
            expectedForSameClass = new int[] { 4, 4, 4, 4, 4 };
            for (int left = 0; left < samples.Length; left++)
            {
                for (int right = 0; right < samples.Length; right++)
                {
                    doc.Text = samples[left] + samples[right];
                    for (int i = 0; i < doc.Length; i++)
                    {
                        int actual = wordProc.NextWordEnd(doc, i);
                        if (left == right)
                        {
                            Assert.AreEqual(expectedForSameClass[i], actual);
                        }
                        else
                        {
                            Assert.AreEqual(expected[i], actual);
                        }
                    }
                }
            }
            Assert.AreEqual(doc.Length, wordProc.NextWordEnd(doc, doc.Length));

            // combining character
            doc.Text = ".a\x0300.";
            Assert.AreEqual(1, wordProc.NextWordEnd(doc, 1));
            Assert.AreEqual(3, wordProc.NextWordEnd(doc, 2));
            Assert.AreEqual(3, wordProc.NextWordEnd(doc, 3));

            // combining character
            doc.Text = "aa\x0300a";
            Assert.AreEqual(4, wordProc.NextWordEnd(doc, 1));
            Assert.AreEqual(4, wordProc.NextWordEnd(doc, 2));
            Assert.AreEqual(4, wordProc.NextWordEnd(doc, 3));
            MyAssert.Throws <ArgumentNullException>(delegate {
                wordProc.NextWordEnd(null, 0);
            });
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                wordProc.NextWordEnd(doc, -1);
            });
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                wordProc.NextWordEnd(doc, doc.Length + 1);
            });

            // PrevWordStart
            expected             = new int[] { 0, 0, 2, 2, 4 };
            expectedForSameClass = new int[] { 0, 0, 0, 0, 4 };
            for (int left = 0; left < samples.Length; left++)
            {
                for (int right = 0; right < samples.Length; right++)
                {
                    doc.Text = samples[left] + samples[right];
                    for (int i = 0; i < doc.Length; i++)
                    {
                        int actual = wordProc.PrevWordStart(doc, i);
                        if (left == right)
                        {
                            Assert.AreEqual(expectedForSameClass[i], actual);
                        }
                        else
                        {
                            Assert.AreEqual(expected[i], actual);
                        }
                    }
                }
            }
            Assert.AreEqual(doc.Length, wordProc.PrevWordStart(doc, doc.Length));

            // combining character
            doc.Text = ".a\x0300.";
            Assert.AreEqual(1, wordProc.PrevWordStart(doc, 1));
            Assert.AreEqual(1, wordProc.PrevWordStart(doc, 2));
            Assert.AreEqual(3, wordProc.PrevWordStart(doc, 3));

            // combining character
            doc.Text = "aa\x0300a";
            Assert.AreEqual(0, wordProc.PrevWordStart(doc, 1));
            Assert.AreEqual(0, wordProc.PrevWordStart(doc, 2));
            Assert.AreEqual(0, wordProc.PrevWordStart(doc, 3));
            MyAssert.Throws <ArgumentNullException>(delegate {
                wordProc.PrevWordStart(null, 0);
            });
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                wordProc.PrevWordStart(doc, -1);
            });
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                wordProc.PrevWordStart(doc, doc.Length + 1);
            });

            // PrevWordEnd
            expected             = new int[] { 0, 0, 2, 2, 4 };
            expectedForSameClass = new int[] { 0, 0, 0, 0, 4 };
            for (int left = 0; left < samples.Length; left++)
            {
                for (int right = 0; right < samples.Length; right++)
                {
                    doc.Text = samples[left] + samples[right];
                    for (int i = 0; i < doc.Length; i++)
                    {
                        int actual = wordProc.PrevWordEnd(doc, i);
                        if (left == right)
                        {
                            Assert.AreEqual(expectedForSameClass[i], actual);
                        }
                        else
                        {
                            Assert.AreEqual(expected[i], actual);
                        }
                    }
                }
            }
            Assert.AreEqual(doc.Length, wordProc.PrevWordEnd(doc, doc.Length));

            // combining character
            doc.Text = ".a\x0300.";
            Assert.AreEqual(1, wordProc.PrevWordEnd(doc, 1));
            Assert.AreEqual(1, wordProc.PrevWordEnd(doc, 2));
            Assert.AreEqual(3, wordProc.PrevWordEnd(doc, 3));

            // combining character
            doc.Text = "aa\x0300a";
            Assert.AreEqual(0, wordProc.PrevWordEnd(doc, 1));
            Assert.AreEqual(0, wordProc.PrevWordEnd(doc, 2));
            Assert.AreEqual(0, wordProc.PrevWordEnd(doc, 3));
            MyAssert.Throws <ArgumentNullException>(delegate {
                wordProc.PrevWordEnd(null, 0);
            });
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                wordProc.PrevWordEnd(doc, -1);
            });
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                wordProc.PrevWordEnd(doc, doc.Length + 1);
            });
        }
示例#2
0
        static void Test_WordDetection()
        {
            Document        doc      = new Document();
            DefaultWordProc wordProc = new DefaultWordProc();

            string[] samples = new string[] { "aa", "11", ",,", "\x3042\x3042", "\x963f\x963f", "\n\n", "  " };
            int[]    expected;
            int[]    expectedForSameClass;

            // NextWordStart
            expected             = new int[] { 0, 2, 2, 4, 4 };
            expectedForSameClass = new int[] { 0, 4, 4, 4, 4 };
            for (int left = 0; left < samples.Length; left++)
            {
                for (int right = 0; right < samples.Length; right++)
                {
                    doc.Text = samples[left] + samples[right];
                    for (int i = 0; i < doc.Length; i++)
                    {
                        int actual = wordProc.NextWordStart(doc, i);
                        if (left == right)
                        {
                            TestUtl.AssertEquals(expectedForSameClass[i], actual);
                        }
                        else
                        {
                            TestUtl.AssertEquals(expected[i], actual);
                        }
                    }
                }
            }
            TestUtl.AssertEquals(doc.Length, wordProc.NextWordStart(doc, doc.Length));

            // combining character
            doc.Text = ".a\x0300.";
            TestUtl.AssertEquals(1, wordProc.NextWordStart(doc, 1));
            TestUtl.AssertEquals(3, wordProc.NextWordStart(doc, 2));
            TestUtl.AssertEquals(3, wordProc.NextWordStart(doc, 3));

            // combining character
            doc.Text = "aa\x0300a";
            TestUtl.AssertEquals(4, wordProc.NextWordStart(doc, 1));
            TestUtl.AssertEquals(4, wordProc.NextWordStart(doc, 2));
            TestUtl.AssertEquals(4, wordProc.NextWordStart(doc, 3));

            try{ wordProc.NextWordStart(null, 0); throw new Exception(); }
            catch (Exception ex) { TestUtl.AssertType <ArgumentNullException>(ex); }

            try{ wordProc.NextWordStart(doc, -1); throw new Exception(); }
            catch (Exception ex) { TestUtl.AssertType <ArgumentOutOfRangeException>(ex); }

            try{ wordProc.NextWordStart(doc, doc.Length + 1); throw new Exception(); }
            catch (Exception ex) { TestUtl.AssertType <ArgumentOutOfRangeException>(ex); }

            // NextWordEnd
            expected             = new int[] { 2, 2, 2, 4, 4 };
            expectedForSameClass = new int[] { 4, 4, 4, 4, 4 };
            for (int left = 0; left < samples.Length; left++)
            {
                for (int right = 0; right < samples.Length; right++)
                {
                    doc.Text = samples[left] + samples[right];
                    for (int i = 0; i < doc.Length; i++)
                    {
                        int actual = wordProc.NextWordEnd(doc, i);
                        if (left == right)
                        {
                            TestUtl.AssertEquals(expectedForSameClass[i], actual);
                        }
                        else
                        {
                            TestUtl.AssertEquals(expected[i], actual);
                        }
                    }
                }
            }
            TestUtl.AssertEquals(doc.Length, wordProc.NextWordEnd(doc, doc.Length));

            // combining character
            doc.Text = ".a\x0300.";
            TestUtl.AssertEquals(1, wordProc.NextWordEnd(doc, 1));
            TestUtl.AssertEquals(3, wordProc.NextWordEnd(doc, 2));
            TestUtl.AssertEquals(3, wordProc.NextWordEnd(doc, 3));

            // combining character
            doc.Text = "aa\x0300a";
            TestUtl.AssertEquals(4, wordProc.NextWordEnd(doc, 1));
            TestUtl.AssertEquals(4, wordProc.NextWordEnd(doc, 2));
            TestUtl.AssertEquals(4, wordProc.NextWordEnd(doc, 3));

            try{ wordProc.NextWordEnd(null, 0); throw new Exception(); }
            catch (Exception ex) { TestUtl.AssertType <ArgumentNullException>(ex); }

            try{ wordProc.NextWordEnd(doc, -1); throw new Exception(); }
            catch (Exception ex) { TestUtl.AssertType <ArgumentOutOfRangeException>(ex); }

            try{ wordProc.NextWordEnd(doc, doc.Length + 1); throw new Exception(); }
            catch (Exception ex) { TestUtl.AssertType <ArgumentOutOfRangeException>(ex); }

            // PrevWordStart
            expected             = new int[] { 0, 0, 2, 2, 4 };
            expectedForSameClass = new int[] { 0, 0, 0, 0, 4 };
            for (int left = 0; left < samples.Length; left++)
            {
                for (int right = 0; right < samples.Length; right++)
                {
                    doc.Text = samples[left] + samples[right];
                    for (int i = 0; i < doc.Length; i++)
                    {
                        int actual = wordProc.PrevWordStart(doc, i);
                        if (left == right)
                        {
                            TestUtl.AssertEquals(expectedForSameClass[i], actual);
                        }
                        else
                        {
                            TestUtl.AssertEquals(expected[i], actual);
                        }
                    }
                }
            }
            TestUtl.AssertEquals(doc.Length, wordProc.PrevWordStart(doc, doc.Length));

            // combining character
            doc.Text = ".a\x0300.";
            TestUtl.AssertEquals(1, wordProc.PrevWordStart(doc, 1));
            TestUtl.AssertEquals(1, wordProc.PrevWordStart(doc, 2));
            TestUtl.AssertEquals(3, wordProc.PrevWordStart(doc, 3));

            // combining character
            doc.Text = "aa\x0300a";
            TestUtl.AssertEquals(0, wordProc.PrevWordStart(doc, 1));
            TestUtl.AssertEquals(0, wordProc.PrevWordStart(doc, 2));
            TestUtl.AssertEquals(0, wordProc.PrevWordStart(doc, 3));

            try{ wordProc.PrevWordStart(null, 0); throw new Exception(); }
            catch (Exception ex) { TestUtl.AssertType <ArgumentNullException>(ex); }

            try{ wordProc.PrevWordStart(doc, -1); throw new Exception(); }
            catch (Exception ex) { TestUtl.AssertType <ArgumentOutOfRangeException>(ex); }

            try{ wordProc.PrevWordStart(doc, doc.Length + 1); throw new Exception(); }
            catch (Exception ex) { TestUtl.AssertType <ArgumentOutOfRangeException>(ex); }

            // PrevWordEnd
            expected             = new int[] { 0, 0, 2, 2, 4 };
            expectedForSameClass = new int[] { 0, 0, 0, 0, 4 };
            for (int left = 0; left < samples.Length; left++)
            {
                for (int right = 0; right < samples.Length; right++)
                {
                    doc.Text = samples[left] + samples[right];
                    for (int i = 0; i < doc.Length; i++)
                    {
                        int actual = wordProc.PrevWordEnd(doc, i);
                        if (left == right)
                        {
                            TestUtl.AssertEquals(expectedForSameClass[i], actual);
                        }
                        else
                        {
                            TestUtl.AssertEquals(expected[i], actual);
                        }
                    }
                }
            }
            TestUtl.AssertEquals(doc.Length, wordProc.PrevWordEnd(doc, doc.Length));

            // combining character
            doc.Text = ".a\x0300.";
            TestUtl.AssertEquals(1, wordProc.PrevWordEnd(doc, 1));
            TestUtl.AssertEquals(1, wordProc.PrevWordEnd(doc, 2));
            TestUtl.AssertEquals(3, wordProc.PrevWordEnd(doc, 3));

            // combining character
            doc.Text = "aa\x0300a";
            TestUtl.AssertEquals(0, wordProc.PrevWordEnd(doc, 1));
            TestUtl.AssertEquals(0, wordProc.PrevWordEnd(doc, 2));
            TestUtl.AssertEquals(0, wordProc.PrevWordEnd(doc, 3));

            try{ wordProc.PrevWordEnd(null, 0); throw new Exception(); }
            catch (Exception ex) { TestUtl.AssertType <ArgumentNullException>(ex); }

            try{ wordProc.PrevWordEnd(doc, -1); throw new Exception(); }
            catch (Exception ex) { TestUtl.AssertType <ArgumentOutOfRangeException>(ex); }

            try{ wordProc.PrevWordEnd(doc, doc.Length + 1); throw new Exception(); }
            catch (Exception ex) { TestUtl.AssertType <ArgumentOutOfRangeException>(ex); }
        }