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)
5
using System.Collections.Generic;
6
using ICSharpCode.AvalonEdit.Rendering;
9
namespace ICSharpCode.AvalonEdit.Document
12
/// A randomized test for the line manager.
15
public class RandomizedLineManagerTest
17
TextDocument document;
21
public void FixtureSetup()
23
int seed = Environment.TickCount;
24
Console.WriteLine("RandomizedLineManagerTest Seed: " + seed);
25
rnd = new Random(seed);
31
document = new TextDocument();
35
public void ShortReplacements()
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)];
47
document.Replace(offset, length, new string(buffer, 0, newTextLength));
53
public void LargeReplacements()
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)];
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);
75
string text = document.Text;
78
for (int i = 0; i < text.Length; 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);
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);
99
Assert.AreEqual(lineNumber, document.LineCount);
103
public void CollapsingTest()
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);
116
switch (rnd.Next(0, 10)) {
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)];
130
document.Replace(offset, length, new string(buffer, 0, newTextLength));
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)));
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) {
145
collapsedSections.Remove(cs);
149
foreach (DocumentLine ls in document.Lines) {
150
heightTree.SetHeight(ls, ls.LineNumber);
154
var treeSections = new HashSet<CollapsedLineSection>(heightTree.GetAllCollapsedSections());
155
int expectedCount = 0;
156
foreach (CollapsedLineSection cs in collapsedSections) {
157
if (cs.Start != null) {
159
Assert.IsTrue(treeSections.Contains(cs));
162
Assert.AreEqual(expectedCount, treeSections.Count);
164
HeightTests.CheckHeights(document, heightTree);