forked from MicroGSD/RoadArchitect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GSDUnitTests.cs
365 lines (324 loc) · 16.7 KB
/
GSDUnitTests.cs
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
using UnityEngine;
#if UNITY_EDITOR
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
#endif
namespace GSD.Roads {
#if UNITY_EDITOR
public static class GSDUnitTests
{
private static GSDRoadSystem RoadSystem;
/// <remarks>
/// WARNING: Only call this on an empty scene that has some terrains on it. MicroGSD LLC is not responsbile for data loss if this function is called by user.
/// </remarks>
public static void RoadArchitectUnitTests() {
CleanupTests();
//Create new road system and turn off updates:
GameObject tRoadSystemObj = new GameObject("RoadArchitectSystem1");
RoadSystem = tRoadSystemObj.AddComponent<GSDRoadSystem>(); //Add road system component.
RoadSystem.opt_bAllowRoadUpdates = false;
int numTests = 5;
double totalTestTime = 0f;
for (int i = 1; i <= numTests; i++)
{
UnityEngine.Debug.Log("Running test " + i);
double testTime = RunTest(i);
totalTestTime += testTime;
UnityEngine.Debug.Log("Test " + i + " complete. Test time: " + testTime + "ms");
}
UnityEngine.Debug.Log("All tests completed. Total test time: " + totalTestTime + "ms");
//Turn updates back on and update road:
RoadSystem.opt_bAllowRoadUpdates = true;
RoadSystem.UpdateAllRoads();
}
private static long RunTest(int test)
{
System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();
switch(test)
{
case 1:
RoadArchitectUnitTest1(); //Bridges
break;
case 2:
RoadArchitectUnitTest2(); //2L intersections
break;
case 3:
RoadArchitectUnitTest3(); //4L intersections
break;
case 4:
RoadArchitectUnitTest4(); //Large suspension bridge
break;
case 5:
RoadArchitectUnitTest5(); //Long road:
break;
}
stopwatch.Stop();
long testTime = stopwatch.ElapsedMilliseconds;
return testTime;
}
public static void CleanupTests()
{
Debug.Log("Cleaning up tests");
//Get the existing road system, if it exists:
GameObject GSDRS = (GameObject)GameObject.Find("RoadArchitectSystem1");
DestroyTerrainHistory(GSDRS);
Object.DestroyImmediate(GSDRS);
FlattenTerrains();
}
private static void DestroyTerrainHistory(GameObject GSDRS)
{
//Destroy the terrain histories:
if (GSDRS != null)
{
Object[] tRoads = GSDRS.GetComponents<GSDRoad>();
foreach (GSDRoad xRoad in tRoads)
{
GSD.Roads.GSDTerraforming.TerrainsReset(xRoad);
}
}
}
private static void FlattenTerrains()
{
//Reset all terrains to 0,0
Object[] zTerrains = Object.FindObjectsOfType<Terrain>();
foreach (Terrain xTerrain in zTerrains)
{
xTerrain.terrainData.SetHeights(0, 0, new float[513, 513]);
}
}
private static void RoadArchitectUnitTest1() {
//Create node locations:
List<Vector3> nodeLocations = new List<Vector3>();
int MaxCount = 18;
float tMod = 100f;
Vector3 xVect = new Vector3(50f, 40f, 50f);
for (int i = 0; i < MaxCount; i++) {
//tLocs.Add(xVect + new Vector3(tMod * Mathf.Pow((float)i / ((float)MaxCount * 0.15f), 2f), 1f*((float)i*1.25f), tMod * i));
nodeLocations.Add(xVect + new Vector3(tMod * Mathf.Pow((float)i / ((float)25 * 0.15f), 2f), 0f, tMod * i));
}
//Get road system create road:
GSDRoad tRoad = GSDRoadAutomation.CreateRoad_Programmatically(RoadSystem, ref nodeLocations);
//Bridge0: (Arch)
tRoad.GSDSpline.mNodes[4].bIsBridgeStart = true;
tRoad.GSDSpline.mNodes[4].bIsBridgeMatched = true;
tRoad.GSDSpline.mNodes[7].bIsBridgeEnd = true;
tRoad.GSDSpline.mNodes[7].bIsBridgeMatched = true;
tRoad.GSDSpline.mNodes[4].BridgeCounterpartNode = tRoad.GSDSpline.mNodes[7];
tRoad.GSDSpline.mNodes[4].LoadWizardObjectsFromLibrary("Arch12m-2L", true, true);
//Bridge1: (Federal causeway)
tRoad.GSDSpline.mNodes[8].bIsBridgeStart = true;
tRoad.GSDSpline.mNodes[8].bIsBridgeMatched = true;
tRoad.GSDSpline.mNodes[8].BridgeCounterpartNode = tRoad.GSDSpline.mNodes[10];
tRoad.GSDSpline.mNodes[8].LoadWizardObjectsFromLibrary("Causeway1-2L", true, true);
tRoad.GSDSpline.mNodes[10].bIsBridgeEnd = true;
tRoad.GSDSpline.mNodes[10].bIsBridgeMatched = true;
//Bridge2: (Steel)
tRoad.GSDSpline.mNodes[11].bIsBridgeStart = true;
tRoad.GSDSpline.mNodes[11].bIsBridgeMatched = true;
tRoad.GSDSpline.mNodes[11].BridgeCounterpartNode = tRoad.GSDSpline.mNodes[13];
tRoad.GSDSpline.mNodes[11].LoadWizardObjectsFromLibrary("Steel-2L", true, true);
tRoad.GSDSpline.mNodes[13].bIsBridgeEnd = true;
tRoad.GSDSpline.mNodes[13].bIsBridgeMatched = true;
//Bridge3: (Causeway)
tRoad.GSDSpline.mNodes[14].bIsBridgeStart = true;
tRoad.GSDSpline.mNodes[14].bIsBridgeMatched = true;
tRoad.GSDSpline.mNodes[16].bIsBridgeEnd = true;
tRoad.GSDSpline.mNodes[16].bIsBridgeMatched = true;
tRoad.GSDSpline.mNodes[14].BridgeCounterpartNode = tRoad.GSDSpline.mNodes[16];
tRoad.GSDSpline.mNodes[14].LoadWizardObjectsFromLibrary("Causeway4-2L", true, true);
}
/// <summary>
/// Create 2L intersections:
/// </summary>
private static void RoadArchitectUnitTest2() {
//Create node locations:
float StartLocX = 800f;
float StartLocY = 200f;
float StartLocYSep = 200f;
float tHeight = 20f;
GSDRoad bRoad = null; if(bRoad == null) { } //Buffer
GSDRoad tRoad = null; if(tRoad == null) { } //Buffer
//Create base road:
List<Vector3> nodeLocations = new List<Vector3>();
for (int i = 0; i < 9; i++) {
nodeLocations.Add(new Vector3(StartLocX + (i * 200f), tHeight, 600f));
}
bRoad = GSDRoadAutomation.CreateRoad_Programmatically(RoadSystem, ref nodeLocations);
//Get road system, create road #1:
nodeLocations.Clear();
for (int i = 0; i < 5; i++) {
nodeLocations.Add(new Vector3(StartLocX, tHeight, StartLocY + (i * StartLocYSep)));
}
tRoad = GSDRoadAutomation.CreateRoad_Programmatically(RoadSystem, ref nodeLocations);
//UnitTest_IntersectionHelper(bRoad, tRoad, GSDRoadIntersection.iIntersectionTypeEnum.TrafficLight1, GSDRoadIntersection.RoadTypeEnum.NoTurnLane);
//Get road system, create road #2:
nodeLocations.Clear();
for (int i = 0; i < 5; i++) {
nodeLocations.Add(new Vector3(StartLocX + (StartLocYSep * 2f), tHeight, StartLocY + (i * StartLocYSep)));
}
tRoad = GSDRoadAutomation.CreateRoad_Programmatically(RoadSystem, ref nodeLocations);
//UnitTest_IntersectionHelper(bRoad, tRoad, GSDRoadIntersection.iIntersectionTypeEnum.TrafficLight1, GSDRoadIntersection.RoadTypeEnum.TurnLane);
//Get road system, create road #3:
nodeLocations.Clear();
for (int i = 0; i < 5; i++) {
nodeLocations.Add(new Vector3(StartLocX + (StartLocYSep * 4f), tHeight, StartLocY + (i * StartLocYSep)));
}
tRoad = GSDRoadAutomation.CreateRoad_Programmatically(RoadSystem, ref nodeLocations);
//UnitTest_IntersectionHelper(bRoad, tRoad, GSDRoadIntersection.iIntersectionTypeEnum.TrafficLight1, GSDRoadIntersection.RoadTypeEnum.BothTurnLanes);
//Get road system, create road #4:
nodeLocations.Clear();
for (int i = 0; i < 5; i++) {
nodeLocations.Add(new Vector3(StartLocX + (StartLocYSep * 6f), tHeight, StartLocY + (i * StartLocYSep)));
}
tRoad = GSDRoadAutomation.CreateRoad_Programmatically(RoadSystem, ref nodeLocations);
//UnitTest_IntersectionHelper(bRoad, tRoad, GSDRoadIntersection.iIntersectionTypeEnum.TrafficLight1, GSDRoadIntersection.RoadTypeEnum.TurnLane);
//Get road system, create road #4:
nodeLocations.Clear();
for (int i = 0; i < 5; i++) {
nodeLocations.Add(new Vector3(StartLocX + (StartLocYSep * 8f), tHeight, StartLocY + (i * StartLocYSep)));
}
tRoad = GSDRoadAutomation.CreateRoad_Programmatically(RoadSystem, ref nodeLocations);
//UnitTest_IntersectionHelper(bRoad, tRoad, GSDRoadIntersection.iIntersectionTypeEnum.TrafficLight1, GSDRoadIntersection.RoadTypeEnum.TurnLane);
GSDRoadAutomation.CreateIntersections_ProgrammaticallyForRoad(bRoad, GSDRoadIntersection.iIntersectionTypeEnum.None, GSDRoadIntersection.RoadTypeEnum.NoTurnLane);
//Now count road intersections, if not 5 throw error
int iCount = 0;
foreach (GSDSplineN tNode in bRoad.GSDSpline.mNodes) {
if (tNode.bIsIntersection) {
iCount += 1;
}
}
if (iCount != 5) {
Debug.LogError("Unit Test #2 failed: " + iCount.ToString() + " intersections instead of 5.");
}
}
/// <summary>
/// This will create an intersection if two nodes overlap on the road. Only good if the roads only overlap once.
/// </summary>
/// <param name="bRoad"></param>
/// <param name="tRoad"></param>
private static void UnitTest_IntersectionHelper(GSDRoad bRoad, GSDRoad tRoad, GSDRoadIntersection.iIntersectionTypeEnum iDefaultIntersectionType, GSDRoadIntersection.RoadTypeEnum rType) {
GSDSplineN tInter1 = null;
GSDSplineN tInter2 = null;
foreach (GSDSplineN tNode in bRoad.GSDSpline.mNodes) {
foreach (GSDSplineN xNode in tRoad.GSDSpline.mNodes) {
if (GSDRootUtil.IsApproximately(Vector3.Distance(tNode.transform.position, xNode.transform.position), 0f, 0.05f)) {
tInter1 = tNode;
tInter2 = xNode;
break;
}
}
}
if (tInter1 != null && tInter2 != null) {
GameObject tInter = GSD.Roads.GSDIntersections.CreateIntersection(tInter1, tInter2);
GSDRoadIntersection GSDRI = tInter.GetComponent<GSDRoadIntersection>();
GSDRI.iDefaultIntersectionType = iDefaultIntersectionType;
GSDRI.rType = rType;
}
}
/// <summary>
/// Create 4L intersections:
/// </summary>
private static void RoadArchitectUnitTest3() {
//Create node locations:
float StartLocX = 200f;
float StartLocY = 2500f;
float StartLocYSep = 300f;
float tHeight = 20f;
GSDRoad bRoad; //Buffer
GSDRoad tRoad; //Buffer
//Create base road:
List<Vector3> nodeLocations = new List<Vector3>();
for (int i = 0; i < 9; i++) {
nodeLocations.Add(new Vector3(StartLocX + (i * StartLocYSep), tHeight, StartLocY + (StartLocYSep * 2f)));
}
bRoad = GSDRoadAutomation.CreateRoad_Programmatically(RoadSystem, ref nodeLocations);
bRoad.opt_Lanes = 4;
//Get road system, create road #1:
nodeLocations.Clear();
for (int i = 0; i < 5; i++) {
nodeLocations.Add(new Vector3(StartLocX, tHeight, StartLocY + (i * StartLocYSep)));
}
tRoad = GSDRoadAutomation.CreateRoad_Programmatically(RoadSystem, ref nodeLocations);
tRoad.opt_Lanes = 4;
UnitTest_IntersectionHelper(bRoad, tRoad, GSDRoadIntersection.iIntersectionTypeEnum.TrafficLight1, GSDRoadIntersection.RoadTypeEnum.NoTurnLane);
//Get road system, create road #2:
nodeLocations.Clear();
for (int i = 0; i < 5; i++) {
nodeLocations.Add(new Vector3(StartLocX + (StartLocYSep * 2f), tHeight, StartLocY + (i * StartLocYSep)));
}
tRoad = GSDRoadAutomation.CreateRoad_Programmatically(RoadSystem, ref nodeLocations);
tRoad.opt_Lanes = 4;
UnitTest_IntersectionHelper(bRoad, tRoad, GSDRoadIntersection.iIntersectionTypeEnum.TrafficLight1, GSDRoadIntersection.RoadTypeEnum.NoTurnLane);
//Get road system, create road #3:
nodeLocations.Clear();
for (int i = 0; i < 5; i++) {
nodeLocations.Add(new Vector3(StartLocX + (StartLocYSep * 4f), tHeight, StartLocY + (i * StartLocYSep)));
}
tRoad = GSDRoadAutomation.CreateRoad_Programmatically(RoadSystem, ref nodeLocations);
tRoad.opt_Lanes = 4;
UnitTest_IntersectionHelper(bRoad, tRoad, GSDRoadIntersection.iIntersectionTypeEnum.TrafficLight1, GSDRoadIntersection.RoadTypeEnum.NoTurnLane);
//Get road system, create road #4:
nodeLocations.Clear();
for (int i = 0; i < 5; i++) {
nodeLocations.Add(new Vector3(StartLocX + (StartLocYSep * 6f), tHeight, StartLocY + (i * StartLocYSep)));
}
tRoad = GSDRoadAutomation.CreateRoad_Programmatically(RoadSystem, ref nodeLocations);
tRoad.opt_Lanes = 4;
UnitTest_IntersectionHelper(bRoad, tRoad, GSDRoadIntersection.iIntersectionTypeEnum.TrafficLight1, GSDRoadIntersection.RoadTypeEnum.NoTurnLane);
//Get road system, create road #5:
nodeLocations.Clear();
for (int i = 0; i < 5; i++) {
nodeLocations.Add(new Vector3(StartLocX + (StartLocYSep * 8f), tHeight, StartLocY + (i * StartLocYSep)));
}
tRoad = GSDRoadAutomation.CreateRoad_Programmatically(RoadSystem, ref nodeLocations);
tRoad.opt_Lanes = 4;
UnitTest_IntersectionHelper(bRoad, tRoad, GSDRoadIntersection.iIntersectionTypeEnum.TrafficLight1, GSDRoadIntersection.RoadTypeEnum.NoTurnLane);
//Now count road intersections, if not 5 throw error
int iCount = 0;
foreach (GSDSplineN tNode in bRoad.GSDSpline.mNodes) {
if (tNode.bIsIntersection) {
iCount += 1;
}
}
if (iCount != 5) {
Debug.LogError("Unit Test #3 failed: " + iCount.ToString() + " intersections instead of 5.");
}
}
//Large suspension bridge:
private static void RoadArchitectUnitTest4() {
//Create base road:
List<Vector3> nodeLocations = new List<Vector3>();
for (int i = 0; i < 5; i++) {
nodeLocations.Add(new Vector3(3500f, 90f, 200f + (800f * i)));
}
GSDRoad tRoad = GSDRoadAutomation.CreateRoad_Programmatically(RoadSystem, ref nodeLocations);
//Suspension bridge:
tRoad.GSDSpline.mNodes[1].bIsBridgeStart = true;
tRoad.GSDSpline.mNodes[1].bIsBridgeMatched = true;
tRoad.GSDSpline.mNodes[3].bIsBridgeEnd = true;
tRoad.GSDSpline.mNodes[3].bIsBridgeMatched = true;
tRoad.GSDSpline.mNodes[1].BridgeCounterpartNode = tRoad.GSDSpline.mNodes[3];
tRoad.GSDSpline.mNodes[1].LoadWizardObjectsFromLibrary("SuspL-2L", true, true);
}
//Long road
private static void RoadArchitectUnitTest5() {
//Create base road:
List<Vector3> nodeLocations = new List<Vector3>();
for(int i = 0; i < 48; i++) {
nodeLocations.Add(new Vector3(3000f, 40f, 10f + (79f * i)));
}
for (int i = 0; i < 35; i++) {
nodeLocations.Add(new Vector3(2900f - (79f * i), 30f, 3960f));
}
for (int i = 0; i < 40; i++) {
nodeLocations.Add(new Vector3(30, 30f, 3960f - (79f * i)));
}
GSDRoadAutomation.CreateRoad_Programmatically(RoadSystem, ref nodeLocations);
}
}
#endif
}