-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer_Tests.cs
More file actions
184 lines (171 loc) · 5.3 KB
/
Timer_Tests.cs
File metadata and controls
184 lines (171 loc) · 5.3 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using NUnit.Framework;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
namespace Memory.Timers
{
[TestFixture]
public class Timer_Tests
{
List<int> Validate(string expected, string actual)
{
expected = expected.Replace("#", "(\\d+)");
var regex = new Regex(expected);
var match = regex.Match(actual);
if (!match.Success)
Assert.Fail($"Your string \n{actual} does not match pattern {expected}");
var result = match.Groups.Cast<Group>().Skip(1).Select(z => int.Parse(z.Value)).ToList();
return result;
}
[Test]
public void SingleLineReport()
{
var writer = new StringWriter();
using (Timer.Start(writer))
{
}
Validate(@"\*\s+: (\d+)\n", writer.ToString());
/*Пример ответа
* : 0
*/
}
[Test]
public void CustomTimerName()
{
var writer = new StringWriter();
using (Timer.Start(writer, "MyTimer"))
{ }
Validate(@"MyTimer\s+: (\d+)\n", writer.ToString());
/*Пример ответа
MyTimer : 0
*/
}
[Test]
public void Nesting()
{
var writer = new StringWriter();
using (var timer = Timer.Start(writer, "A"))
{
using (timer.StartChildTimer("B"))
{ }
using (timer.StartChildTimer("C"))
{ }
}
Validate(@"A\s+: (\d+)\n[ ]{4}B\s+: (\d+)\n[ ]{4}C\s+: (\d+)\n[ ]{4}Rest\s+: (\d+)",
writer.ToString());
/* Пример ответа
A : 0
B : 0
C : 0
Rest : 0
*/
}
[Test]
public void DeepNestingStructure1()
{
var writer = new StringWriter();
using (var timerA = Timer.Start(writer,"A"))
{
using (var timerB = timerA.StartChildTimer("B"))
{
using (timerB.StartChildTimer("C"))
{ }
}
}
Validate(@"A\s+: (\d+)\n[ ]{4}B\s+: (\d+)\n[ ]{8}C\s+: (\d+)\n[ ]{8}Rest\s+: (\d+)\n[ ]{4}Rest\s+: (\d+)\n",
writer.ToString());
/* Пример ответа
A : 0
B : 0
C : 0
Rest : 0
Rest : 0
*/
}
[Test]
public void DeepNestingStructure2()
{
var writer = new StringWriter();
using (var timer = Timer.Start(writer, "A"))
{
using (timer.StartChildTimer("B"))
{ }
using (var timer2 = timer.StartChildTimer("C"))
{
using(timer2.StartChildTimer("D"))
{
}
}
}
Validate(@"A\s+: (\d+)\n[ ]{4}B\s+: (\d+)\n[ ]{4}C\s+: (\d+)\n[ ]{8}D\s+: (\d+)\n[ ]{8}Rest\s+: (\d+)\n[ ]{4}Rest\s+: (\d+)\n",
writer.ToString());
/* Пример ответа
A : 0
B : 0
C : 0
D : 0
Rest : 0
Rest : 0
*/
}
[Test]
public void DeepNestingStructure3()
{
var writer = new StringWriter();
using (var timer = Timer.Start(writer, "A"))
{
using (timer.StartChildTimer("B"))
{ }
using (var timer2 = timer.StartChildTimer("C"))
{
using(timer2.StartChildTimer("D"))
{
}
}
using (timer.StartChildTimer("E"))
{ }
}
Validate(@"A\s+: (\d+)\n[ ]{4}B\s+: (\d+)\n[ ]{4}C\s+: (\d+)\n[ ]{8}D\s+: (\d+)\n[ ]{8}Rest\s+: (\d+)\n[ ]{4}E\s+: (\d+)\n[ ]{4}Rest\s+: (\d+)\n",
writer.ToString());
/* Пример ответа
A : 0
B : 0
C : 0
D : 0
Rest : 0
E : 0
Rest : 0
*/
}
[Test]
public void DeepNestingTime()
{
var writer = new StringWriter();
using (var timer = Timer.Start(writer,"A"))
{
using (timer.StartChildTimer("B"))
{
Thread.Sleep(100);
}
using (timer.StartChildTimer("C"))
{
Thread.Sleep(200);
}
Thread.Sleep(300);
}
var values = Validate(@"A\s+: (\d+)\n[ ]{4}B\s+: (\d+)\n[ ]{4}C\s+: (\d+)\n[ ]{4}Rest\s+: (\d+)",
writer.ToString());
Assert.True(values[0] == values[1] + values[2] + values[3]);
Assert.AreEqual(2, (double)values[2] / values[1], 1);
Assert.AreEqual(3, (double)values[3] / values[1], 1);
/*Пример ответа
A : 600
B : 100
C : 200
Rest : 300
*/
}
}
}