- Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathTestFastStringBuilder.pas
More file actions
Latest commit
147 lines (129 loc) · 3.53 KB
/
Copy pathTestFastStringBuilder.pas
File metadata and controls
147 lines (129 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
unit TestFastStringBuilder;
interface
uses
DUnitX.TestFramework,
FastStringBuilder;
type
[TestFixture]
TTestFastStringBuilder<T> = class
public
[Setup]
procedureSetup;
[Teardown]
procedureTearDown;
[Test]
//[Testcase('RandomIntSort 8','10000000')]
//[Testcase('RandomIntSort 1', '2')]
[Testcase('BuildSame 1', '5')]
[Testcase('BuildSame 1', '30')]
[Testcase('BuildSame 2', '27')]
[Testcase('BuildSame 3', '100')]
[Testcase('BuildSame 4', '1000')]
[Testcase('BuildSame 5', '10000')]
[Testcase('BuildSame 6', '100000')]
[Testcase('BuildSame 7', '1000000')]
procedureTestBuildRepeated(Len: integer);
[Testcase('Sandwich','1')]
procedureSandwichBetweenChars;
[Testcase('Sandwich','1')]
procedureSandwhichBetweenStrings;
[Testcase('Hardcoded sandwich','1')]
procedureHardcodedSandwhich;
private
FData: TStringBuilder;
end;
implementation
{ TTestFastStringBuilder<T> }
procedureTTestFastStringBuilder<T>.HardcodedSandwhich;
var
s: string;
c: char;
norm: string;
begin
s:= 'items:{}';
FData.Append('{');
if TypeInfo(T) = TypeInfo(char) thenbegin
FData.Append('{');
norm := '{' + '{' + '}';
endelseif TypeInfo(T) = TypeInfo(string) thenbegin
FData.Append(s);
norm := '{' + s + '}';
end;
FData.Append('}');
Assert.IsTrue(FData.ToString = norm,'HardcodedSandwhich failed, expected: '+norm+' got: '+FData.ToString);
end;
procedureTTestFastStringBuilder<T>.SandwhichBetweenStrings;
var
s: string;
c: char;
norm: string;
begin
s:= 'test';
c:= 'a';
FData.Append(s);
if TypeInfo(char) = TypeInfo(T) thenbegin
FData.Append(c);
norm:= s+c+s;
endelseif TypeInfo(T) = TypeInfo(string) thenbegin
FData.Append(s);
norm:= s+s+s;
end;
FData.Append(s);
Assert.IsTrue(FData.ToString = norm,'SandwhichBetweenStrings failed, expected: '+norm+' got: '+FData.ToString);
end;
procedureTTestFastStringBuilder<T>.SandwichBetweenChars;
var
s: string;
c: char;
norm: string;
begin
s:= 'items:{}';
c:= '{';
FData.Append(c);
if TypeInfo(T) = TypeInfo(char) thenbegin
FData.Append(c);
norm:= c+c+c;
endelseif TypeInfo(T) = TypeInfo(string) thenbegin
FData.Append(s);
norm:= c+s+c;
end;
FData.Append(c);
Assert.IsTrue(FData.ToString = norm,'SandwhichBetweenStrings failed, expected: '+norm+' got: '+FData.ToString);
end;
procedureTTestFastStringBuilder<T>.Setup;
begin
FData:= TStringBuilder.Create;
end;
procedureTTestFastStringBuilder<T>.TearDown;
begin
FData.Free;
end;
procedureTTestFastStringBuilder<T>.TestBuildRepeated(Len: integer);
const
teststring = 'test';
var
i: integer;
c: char;
s: string;
begin
for i:= 0to Len-1dobegin
if TypeInfo(T) = TypeInfo(string) thenbegin
FData.Append(teststring);
endelseif TypeInfo(T) = TypeInfo(Char) thenbegin
c:= 'a';
FData.Append(c);
endelse Assert.IsFalse(true, 'Only char and string are allowed in string builder');
end; {for i}
if TypeInfo(T) = TypeInfo(char) thenbegin
Assert.IsTrue(FData.ToString = StringOfChar(c, Len), 'TestBuildRepeated, expected: '+StringOfChar(c, Len)+ 'got: '+FData.ToString);
endelseif TypeInfo(T) = TypeInfo(string) thenbegin
for i := 0to Len-1dobegin
s:= s + teststring;
end;
Assert.IsTrue(FData.ToString = s, 'TestBuildRepeated, expected: '+s+ 'got: '+FData.ToString);
end;
end;
initialization
TDUnitX.RegisterTestFixture(TTestFastStringBuilder<char>);
TDUnitX.RegisterTestFixture(TTestFastStringBuilder<string>);
end.