-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBotTests.cs
More file actions
135 lines (122 loc) · 5.33 KB
/
Copy pathBotTests.cs
File metadata and controls
135 lines (122 loc) · 5.33 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
using System;
using System.Diagnostics;
using NUnit.Framework;
namespace rocket_bot
{
[TestFixture]
public class BotTests
{
[SetUp]
public void Init()
{
random = new Random(223243);
channel = new Channel<Rocket>();
rocket = new Rocket(new Vector(100, 100), Vector.Zero, -0.5 * Math.PI);
}
private Random random;
private Channel<Rocket> channel;
private Rocket rocket;
private const int PerformanceIterations = 200;
private const int PerformanceMoves = 35;
private const int CorrectnessIterations = 50;
private const int CorrectnessMoves = 4;
private void CheckAlmostAlwaysEquals(Rocket expectedResult, Bot bot, Rocket initialRocket, int times = 1000)
{
var successes = 0;
for (var i = 0; i < times; ++i)
{
var actualResult = bot.GetNextMove(initialRocket);
if (actualResult.Equals(expectedResult)) ++successes;
}
Assert.GreaterOrEqual(successes, 0.8 * times);
}
private void ComparePerformances(Bot singleThreadBot, Bot multiThreadBot, Rocket initialRocket, int times = 40)
{
var successes = 0;
var stopWatch = new Stopwatch();
for (var i = 0; i < times; ++i)
{
stopWatch.Restart();
singleThreadBot.GetNextMove(initialRocket);
stopWatch.Stop();
var singleThreadBotTime = stopWatch.Elapsed;
stopWatch.Restart();
multiThreadBot.GetNextMove(initialRocket);
stopWatch.Stop();
var multiThreadBotTime = stopWatch.Elapsed;
if (multiThreadBotTime < singleThreadBotTime) ++successes;
}
Assert.GreaterOrEqual(successes, 0.8 * times,
$"Ваше решение в два потока должно работать быстрее решения в один поток в {times * 0.8} случаев из {times}");
}
[TestCase(1)]
[TestCase(2)]
[TestCase(4)]
[TestCase(0)]
public void TestCanTakeTarget(int threadCount)
{
var level = new Level(rocket, new[] {new Vector(100, 90), new Vector(0, 0) }, LevelsFactory.StandardPhysics);
var expectedResult = level.InitialRocket.Move(Turn.Left, level); // поворачиваем к (0,0), через лево короче.
RunTestCase(threadCount, level, expectedResult);
}
[TestCase(1)]
[TestCase(2)]
[TestCase(4)]
[TestCase(0)]
public void TestCannotTakeTarget(int threadCount)
{
var level = new Level(rocket, new[] {new Vector(0, 100), new Vector(0, 0)}, LevelsFactory.StandardPhysics);
var expectedResult = level.InitialRocket.Move(Turn.Left, level); // поворачиваем к (0, 100)
RunTestCase(threadCount, level, expectedResult);
}
[TestCase(1)]
[TestCase(2)]
[TestCase(4)]
[TestCase(0)]
public void TestGoToNextTarget(int threadCount)
{
var level = new Level(rocket, new[] {new Vector(99, 79), new Vector(120, 79)},
LevelsFactory.StandardPhysics);
var expectedResult = level.InitialRocket.Move(Turn.Right, level); // первый возьмем по инерции, поворачиваем к (120, 79)
RunTestCase(threadCount, level, expectedResult);
}
[TestCase(1)]
[TestCase(2)]
[TestCase(3)]
[TestCase(4)]
[TestCase(0)]
public void TestGoStraight(int threadCount)
{
var level = new Level(rocket, new[] {new Vector(100, 90), new Vector(100, 0)},
LevelsFactory.StandardPhysics);
var expectedResult = level.InitialRocket.Move(Turn.None, level); // следующие два чекпоинта прямо. Летим прямо.
RunTestCase(threadCount, level, expectedResult);
}
[TestCase(1)]
[TestCase(2)]
[TestCase(4)]
[TestCase(0)]
public void TestGoToNextCheckpoint(int threadCount)
{
rocket = rocket.IncreaseCheckpoints();
var level = new Level(rocket, new[] {new Vector(200, 100), new Vector(0, 100)},
LevelsFactory.StandardPhysics);
var expectedResult = level.InitialRocket.Move(Turn.Left, level); // Нам к (0, 100), потому что первый уже взят.
RunTestCase(threadCount, level, expectedResult);
}
private void RunTestCase(int threadCount, Level level, Rocket expectedResult)
{
channel.AppendIfLastItemIsUnchanged(level.InitialRocket, null);
if (threadCount == 0)
{
ComparePerformances(new Bot(level, channel, PerformanceMoves, PerformanceIterations, random, 1),
new Bot(level, channel, PerformanceMoves, PerformanceIterations, random, 2), level.InitialRocket);
}
else
{
var bot = new Bot(level, channel, CorrectnessMoves, CorrectnessIterations, random, threadCount);
CheckAlmostAlwaysEquals(expectedResult, bot, level.InitialRocket);
}
}
}
}