Beispiel #1
0
        static void Test_LHI_Delete()
        {
            // TEST DATA:
            // --------------------
            // "keep it as simple as possible\r\n (head: 0, len:31)
            // \n                                 (head:32, len: 1)
            // but\n                              (head:33, len: 4)
            // \r                                 (head:37, len: 1)
            // not simpler."\r                    (head:38, len:14)
            // \r                                 (head:52, len: 1)
            //  - Albert Einstein                 (head:53, len:18)
            // --------------------
            const string                TestData = "\"keep it as simple as possible\r\n\nbut\n\rnot simpler.\"\r\r - Albert Einstein";
            TextBuffer                  text     = new TextBuffer(1, 32);
            SplitArray <int>            lhi      = new SplitArray <int>(1, 8);
            SplitArray <LineDirtyState> lds      = new SplitArray <LineDirtyState>(1, 8);

            lds.Add(LineDirtyState.Clean);

            // prepare
            lhi.Add(0);
            LineLogic.LHI_Insert(lhi, lds, text, TestData, 0);
            text.Add(TestData.ToCharArray());
            TestUtl.AssertEquals("0 32 33 37 38 52 53", lhi.ToString());
            TestUtl.AssertEquals("DDDDDDD", MakeLdsText(lds));
            for (int i = 0; i < lds.Count; i++)
            {
                lds[i] = LineDirtyState.Clean;
            }

            //--- delete range in line ---
            // valid range
            LineLogic.LHI_Delete(lhi, lds, text, 2, 5);
            text.RemoveRange(2, 5);
            TestUtl.AssertEquals("0 29 30 34 35 49 50", lhi.ToString());
            TestUtl.AssertEquals("DCCCCCC", MakeLdsText(lds));

            // invalid range (before begin to middle)
            try{ LineLogic.LHI_Delete(lhi, lds, text, -1, 5); throw new ApplicationException(); }
            catch (Exception ex) { TestUtl.AssertType <AssertException>(ex); }

            //--- delete range between different lines ---
            text.Clear(); lhi.Clear(); lhi.Add(0); lds.Clear(); lds.Add(LineDirtyState.Clean);
            LineLogic.LHI_Insert(lhi, lds, text, TestData, 0);
            text.Add(TestData.ToCharArray());
            // "keep it as simple as possible\r\n (head: 0, len:31)
            // \n                                 (head:32, len: 1)
            // but\n                              (head:33, len: 4)
            // \r                                 (head:37, len: 1)
            // not simpler."\r                    (head:38, len:14)
            // \r                                 (head:52, len: 1)
            //  - Albert Einstein[EOF]            (head:53, len:18)

            // delete only one EOL code
            //----
            // "keep it as simple as possible\r\n (head: 0, len:31)
            // but\n                              (head:32, len: 4)
            // \r                                 (head:36, len: 1)
            // not simpler."\r                    (head:37, len:14)
            // \r                                 (head:51, len: 1)
            //  - Albert Einstein[EOF]            (head:52, len:18)
            //----
            for (int i = 0; i < lds.Count; i++)
            {
                lds[i] = LineDirtyState.Clean;
            }
            LineLogic.LHI_Delete(lhi, lds, text, 32, 33);
            text.RemoveAt(32);
            TestUtl.AssertEquals("0 32 36 37 51 52", lhi.ToString());
            TestUtl.AssertEquals("CDCCCC", MakeLdsText(lds));

            // delete middle of the first line to not EOF pos
            //----
            // "keep it as simple as not simpler."\r (head: 0, len:35)
            // \r                                    (head:36, len: 1)
            //  - Albert Einstein[EOF]               (head:37, len:18)
            //----
            for (int i = 0; i < lds.Count; i++)
            {
                lds[i] = LineDirtyState.Clean;
            }
            LineLogic.LHI_Delete(lhi, lds, text, 22, 37);
            text.RemoveRange(22, 37);
            TestUtl.AssertEquals("0 36 37", lhi.ToString());
            TestUtl.AssertEquals("DCC", MakeLdsText(lds));

            // delete all
            //----
            // [EOF] (head:0, len:0)
            //----
            for (int i = 0; i < lds.Count; i++)
            {
                lds[i] = LineDirtyState.Clean;
            }
            LineLogic.LHI_Delete(lhi, lds, text, 0, 55);
            text.RemoveRange(0, 55);
            TestUtl.AssertEquals("0", lhi.ToString());
            TestUtl.AssertEquals("D", MakeLdsText(lds));

            //--- special care about CR+LF ---
            // (1) deletion creates a new CR+LF
            // (2) deletion breaks a CR+LF at the left side of the deletion range
            // (3) deletion breaks a CR+LF at the left side of the deletion range
            //--------------------------------
            // (1)
            {
                text.Clear(); lhi.Clear(); lhi.Add(0); lds.Clear(); lds.Add(LineDirtyState.Clean);
                LineLogic.LHI_Insert(lhi, lds, text, "foo\rx\nbar", 0);
                text.Add("foo\rx\nbar".ToCharArray());

                for (int i = 0; i < lds.Count; i++)
                {
                    lds[i] = LineDirtyState.Clean;
                }
                LineLogic.LHI_Delete(lhi, lds, text, 4, 5);
                text.RemoveRange(4, 5);
                TestUtl.AssertEquals("0 5", lhi.ToString());
                TestUtl.AssertEquals("DC", MakeLdsText(lds));
            }

            // (2)
            {
                text.Clear(); lhi.Clear(); lhi.Add(0); lds.Clear(); lds.Add(LineDirtyState.Clean);
                LineLogic.LHI_Insert(lhi, lds, text, "foo\r\nbar", 0);
                text.Add("foo\r\nbar".ToCharArray());

                for (int i = 0; i < lds.Count; i++)
                {
                    lds[i] = LineDirtyState.Clean;
                }
                LineLogic.LHI_Delete(lhi, lds, text, 4, 6);
                text.RemoveRange(4, 6);
                TestUtl.AssertEquals("0 4", lhi.ToString());
                TestUtl.AssertEquals("DD", MakeLdsText(lds));
            }

            // (3)
            {
                text.Clear(); lhi.Clear(); lhi.Add(0); lds.Clear(); lds.Add(LineDirtyState.Clean);
                LineLogic.LHI_Insert(lhi, lds, text, "foo\r\nbar", 0);
                text.Add("foo\r\nbar".ToCharArray());

                for (int i = 0; i < lds.Count; i++)
                {
                    lds[i] = LineDirtyState.Clean;
                }
                LineLogic.LHI_Delete(lhi, lds, text, 2, 4);
                text.RemoveRange(2, 4);
                TestUtl.AssertEquals("0 3", lhi.ToString());
                TestUtl.AssertEquals("DC", MakeLdsText(lds));
            }

            // (1)+(2)+(3)
            {
                text.Clear(); lhi.Clear(); lhi.Add(0); lds.Clear(); lds.Add(LineDirtyState.Clean);
                LineLogic.LHI_Insert(lhi, lds, text, "\r\nfoo\r\n", 0);
                text.Add("\r\nfoo\r\n".ToCharArray());

                for (int i = 0; i < lds.Count; i++)
                {
                    lds[i] = LineDirtyState.Clean;
                }
                LineLogic.LHI_Delete(lhi, lds, text, 1, 6);
                text.RemoveRange(1, 6);
                TestUtl.AssertEquals("0 2", lhi.ToString());
                TestUtl.AssertEquals("DC", MakeLdsText(lds));
            }

            //--- misc ---
            // insert "\n" after '\r' at end of document (boundary check for LHI_Insert)
            {
                text.Clear(); lhi.Clear(); lhi.Add(0); lds.Clear(); lds.Add(LineDirtyState.Clean);
                LineLogic.LHI_Insert(lhi, lds, text, "\r", 0);
                text.Add("\r".ToCharArray());

                for (int i = 0; i < lds.Count; i++)
                {
                    lds[i] = LineDirtyState.Clean;
                }
                LineLogic.LHI_Insert(lhi, lds, text, "\n", 1);
                text.Add("\n".ToCharArray());
                TestUtl.AssertEquals("0 2", lhi.ToString());
                TestUtl.AssertEquals("CD", MakeLdsText(lds));
            }

            // insert "\n" after '\r' at end of document (boundary check for LHI_Delete)
            {
                text.Clear(); lhi.Clear(); lhi.Add(0); lds.Clear(); lds.Add(LineDirtyState.Clean);
                LineLogic.LHI_Insert(lhi, lds, text, "\r\n", 0);
                text.Add("\r\n".ToCharArray());

                for (int i = 0; i < lds.Count; i++)
                {
                    lds[i] = LineDirtyState.Clean;
                }
                LineLogic.LHI_Delete(lhi, lds, text, 1, 2);
                text.RemoveRange(1, 2);
                TestUtl.AssertEquals("0 1", lhi.ToString());
                TestUtl.AssertEquals("DD", MakeLdsText(lds));
            }
        }