~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit.Tests/Document/RandomizedLineManagerTest.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using ICSharpCode.AvalonEdit.Rendering;
 
7
using NUnit.Framework;
 
8
 
 
9
namespace ICSharpCode.AvalonEdit.Document
 
10
{
 
11
        /// <summary>
 
12
        /// A randomized test for the line manager.
 
13
        /// </summary>
 
14
        [TestFixture]
 
15
        public class RandomizedLineManagerTest
 
16
        {
 
17
                TextDocument document;
 
18
                Random rnd;
 
19
                
 
20
                [TestFixtureSetUp]
 
21
                public void FixtureSetup()
 
22
                {
 
23
                        int seed = Environment.TickCount;
 
24
                        Console.WriteLine("RandomizedLineManagerTest Seed: " + seed);
 
25
                        rnd = new Random(seed);
 
26
                }
 
27
                
 
28
                [SetUp]
 
29
                public void Setup()
 
30
                {
 
31
                        document = new TextDocument();
 
32
                }
 
33
                
 
34
                [Test]
 
35
                public void ShortReplacements()
 
36
                {
 
37
                        char[] chars = { 'a', 'b', '\r', '\n' };
 
38
                        char[] buffer = new char[20];
 
39
                        for (int i = 0; i < 2500; i++) {
 
40
                                int offset = rnd.Next(0, document.TextLength);
 
41
                                int length = rnd.Next(0, document.TextLength - offset);
 
42
                                int newTextLength = rnd.Next(0, 20);
 
43
                                for (int j = 0; j < newTextLength; j++) {
 
44
                                        buffer[j] = chars[rnd.Next(0, chars.Length)];
 
45
                                }
 
46
                                
 
47
                                document.Replace(offset, length, new string(buffer, 0, newTextLength));
 
48
                                CheckLines();
 
49
                        }
 
50
                }
 
51
                
 
52
                [Test]
 
53
                public void LargeReplacements()
 
54
                {
 
55
                        char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', '\r', '\n' };
 
56
                        char[] buffer = new char[1000];
 
57
                        for (int i = 0; i < 20; i++) {
 
58
                                int offset = rnd.Next(0, document.TextLength);
 
59
                                int length = rnd.Next(0, (document.TextLength - offset) / 4);
 
60
                                int newTextLength = rnd.Next(0, 1000);
 
61
                                for (int j = 0; j < newTextLength; j++) {
 
62
                                        buffer[j] = chars[rnd.Next(0, chars.Length)];
 
63
                                }
 
64
                                
 
65
                                string newText = new string(buffer, 0, newTextLength);
 
66
                                string expectedText = document.Text.Remove(offset, length).Insert(offset, newText);
 
67
                                document.Replace(offset, length, newText);
 
68
                                Assert.AreEqual(expectedText, document.Text);
 
69
                                CheckLines();
 
70
                        }
 
71
                }
 
72
                
 
73
                void CheckLines()
 
74
                {
 
75
                        string text = document.Text;
 
76
                        int lineNumber = 1;
 
77
                        int lineStart = 0;
 
78
                        for (int i = 0; i < text.Length; i++) {
 
79
                                char c = text[i];
 
80
                                if (c == '\r' && i + 1 < text.Length && text[i + 1] == '\n') {
 
81
                                        DocumentLine line = document.GetLineByNumber(lineNumber);
 
82
                                        Assert.AreEqual(lineNumber, line.LineNumber);
 
83
                                        Assert.AreEqual(2, line.DelimiterLength);
 
84
                                        Assert.AreEqual(lineStart, line.Offset);
 
85
                                        Assert.AreEqual(i - lineStart, line.Length);
 
86
                                        i++; // consume \n
 
87
                                        lineNumber++;
 
88
                                        lineStart = i+1;
 
89
                                } else if (c == '\r' || c == '\n') {
 
90
                                        DocumentLine line = document.GetLineByNumber(lineNumber);
 
91
                                        Assert.AreEqual(lineNumber, line.LineNumber);
 
92
                                        Assert.AreEqual(1, line.DelimiterLength);
 
93
                                        Assert.AreEqual(lineStart, line.Offset);
 
94
                                        Assert.AreEqual(i - lineStart, line.Length);
 
95
                                        lineNumber++;
 
96
                                        lineStart = i+1;
 
97
                                }
 
98
                        }
 
99
                        Assert.AreEqual(lineNumber, document.LineCount);
 
100
                }
 
101
                
 
102
                [Test]
 
103
                public void CollapsingTest()
 
104
                {
 
105
                        char[] chars = { 'a', 'b', '\r', '\n' };
 
106
                        char[] buffer = new char[20];
 
107
                        HeightTree heightTree = new HeightTree(document, 10);
 
108
                        List<CollapsedLineSection> collapsedSections = new List<CollapsedLineSection>();
 
109
                        for (int i = 0; i < 2500; i++) {
 
110
//                              Console.WriteLine("Iteration " + i);
 
111
//                              Console.WriteLine(heightTree.GetTreeAsString());
 
112
//                              foreach (CollapsedLineSection cs in collapsedSections) {
 
113
//                                      Console.WriteLine(cs);
 
114
//                              }
 
115
                                
 
116
                                switch (rnd.Next(0, 10)) {
 
117
                                        case 0:
 
118
                                        case 1:
 
119
                                        case 2:
 
120
                                        case 3:
 
121
                                        case 4:
 
122
                                        case 5:
 
123
                                                int offset = rnd.Next(0, document.TextLength);
 
124
                                                int length = rnd.Next(0, document.TextLength - offset);
 
125
                                                int newTextLength = rnd.Next(0, 20);
 
126
                                                for (int j = 0; j < newTextLength; j++) {
 
127
                                                        buffer[j] = chars[rnd.Next(0, chars.Length)];
 
128
                                                }
 
129
                                                
 
130
                                                document.Replace(offset, length, new string(buffer, 0, newTextLength));
 
131
                                                break;
 
132
                                        case 6:
 
133
                                        case 7:
 
134
                                                int startLine = rnd.Next(1, document.LineCount + 1);
 
135
                                                int endLine = rnd.Next(startLine, document.LineCount + 1);
 
136
                                                collapsedSections.Add(heightTree.CollapseText(document.GetLineByNumber(startLine), document.GetLineByNumber(endLine)));
 
137
                                                break;
 
138
                                        case 8:
 
139
                                                if (collapsedSections.Count > 0) {
 
140
                                                        CollapsedLineSection cs = collapsedSections[rnd.Next(0, collapsedSections.Count)];
 
141
                                                        // unless the text section containing the CollapsedSection was deleted:
 
142
                                                        if (cs.Start != null) {
 
143
                                                                cs.Uncollapse();
 
144
                                                        }
 
145
                                                        collapsedSections.Remove(cs);
 
146
                                                }
 
147
                                                break;
 
148
                                        case 9:
 
149
                                                foreach (DocumentLine ls in document.Lines) {
 
150
                                                        heightTree.SetHeight(ls, ls.LineNumber);
 
151
                                                }
 
152
                                                break;
 
153
                                }
 
154
                                var treeSections = new HashSet<CollapsedLineSection>(heightTree.GetAllCollapsedSections());
 
155
                                int expectedCount = 0;
 
156
                                foreach (CollapsedLineSection cs in collapsedSections) {
 
157
                                        if (cs.Start != null) {
 
158
                                                expectedCount++;
 
159
                                                Assert.IsTrue(treeSections.Contains(cs));
 
160
                                        }
 
161
                                }
 
162
                                Assert.AreEqual(expectedCount, treeSections.Count);
 
163
                                CheckLines();
 
164
                                HeightTests.CheckHeights(document, heightTree);
 
165
                        }
 
166
                }
 
167
        }
 
168
}