-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSystemBaseSingletonAccessTests.cs
433 lines (339 loc) · 18.5 KB
/
SystemBaseSingletonAccessTests.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#define SOME_DEFINE
using System;
using NUnit.Framework;
using Unity.Collections;
using Unity.Jobs;
#pragma warning disable 649
namespace Unity.Entities.Tests
{
partial class SystemBaseSingletonAccessTests : ECSTestsFixture
{
SystemBase_TestSystem TestSystem;
[SetUp]
public void SetUp()
{
TestSystem = World.GetOrCreateSystemManaged<SystemBase_TestSystem>();
}
public partial class SystemBase_TestSystem : SystemBase
{
public int SingletonProperty
{
get => SystemAPI.GetSingleton<EcsTestData>().value;
set => SystemAPI.SetSingleton(new EcsTestData {value = value});
}
public struct SingletonDataInSystem : IComponentData
{
public float value;
}
protected override void OnUpdate() {}
public void SingletonPropertyGetterAndSetter()
{
EntityManager.CreateEntity(typeof(EcsTestData));
var defaultSingletonValue = SingletonProperty;
Assert.AreEqual(expected: 0, defaultSingletonValue);
SingletonProperty = 10;
Assert.AreEqual(10, SystemAPI.GetSingleton<EcsTestData>().value);
}
public void GetSetSingleton()
{
EntityManager.CreateEntity(typeof(EcsTestData));
SystemAPI.SetSingleton(new EcsTestData(10));
Assert.AreEqual(10, SystemAPI.GetSingleton<EcsTestData>().value);
}
public void GetSingletonRW_ByRef_Modifies_Singleton()
{
EntityManager.CreateEntity(typeof(EcsTestData));
ref var singleton = ref SystemAPI.GetSingletonRW<EcsTestData>().ValueRW;
singleton.value = 10;
Assert.AreEqual(10, SystemAPI.GetSingleton<EcsTestData>().value);
SystemAPI.GetSingletonRW<EcsTestData>().ValueRW.value = 33;
Assert.AreEqual(33, SystemAPI.GetSingleton<EcsTestData>().value);
ref var s1 = ref SystemAPI.GetSingletonRW<EcsTestData>().ValueRW;
s1.value = 42;
ref var s2 = ref SystemAPI.GetSingletonRW<EcsTestData>().ValueRW;
s2.value = 31;
Assert.AreEqual(s2.value, s1.value);
}
public void GetSingletonRW_ByValue_DoesNot_Modify_Singleton()
{
const int expected = 42;
var e = EntityManager.CreateEntity(typeof(EcsTestData));
EntityManager.SetComponentData(e, new EcsTestData
{
value = expected
});
var singleton = SystemAPI.GetSingletonRW<EcsTestData>().ValueRW;
singleton.value = 10;
Assert.AreEqual(expected, SystemAPI.GetSingleton<EcsTestData>().value);
}
public void GetSetSingletonInSystem()
{
EntityManager.CreateEntity(typeof(SystemBase_TestSystem.SingletonDataInSystem));
SystemAPI.SetSingleton(new SystemBase_TestSystem.SingletonDataInSystem() { value = 10.0f });
Assert.AreEqual(10, SystemAPI.GetSingleton<SystemBase_TestSystem.SingletonDataInSystem>().value);
}
T GenericMethodWithSingletonAccess<T>(T value) where T : unmanaged, IComponentData
{
SystemAPI.SetSingleton(value);
return SystemAPI.GetSingleton<T>();
}
public void GetSetSingletonWithGenericParameter()
{
EntityManager.CreateEntity(typeof(EcsTestData));
GenericMethodWithSingletonAccess(new EcsTestData(10));
Assert.AreEqual(10, SystemAPI.GetSingleton<EcsTestData>().value);
}
struct TestStruct { }
int SingletonAccessInGenericMethodWithInParameter<T>(in T inArgument) where T: struct
{
return SystemAPI.GetSingleton<EcsTestData>().value;
}
public void GetSingletonInGenericMethodWithInParameter()
{
var entity = EntityManager.CreateEntity(typeof(EcsTestData));
EntityManager.SetComponentData(entity, new EcsTestData{ value = 10 });
Assert.AreEqual(expected: 10, actual: SingletonAccessInGenericMethodWithInParameter(new TestStruct()));
}
public void SingletonMethodsWithValidFilter_GetsAndSets()
{
var queryWithFilter1 = EntityManager.CreateEntityQuery(typeof(EcsTestData), typeof(SharedData1));
queryWithFilter1.SetSharedComponentFilterManaged(new SharedData1(1));
var queryWithFilter2 = EntityManager.CreateEntityQuery(typeof(EcsTestData), typeof(SharedData1));
queryWithFilter2.SetSharedComponentFilterManaged(new SharedData1(2));
var entity1 = EntityManager.CreateEntity(typeof(EcsTestData), typeof(SharedData1));
EntityManager.SetComponentData(entity1, new EcsTestData(-1));
EntityManager.SetSharedComponentManaged(entity1, new SharedData1(1));
var entity2 = EntityManager.CreateEntity(typeof(EcsTestData), typeof(SharedData1));
EntityManager.SetComponentData(entity2, new EcsTestData(-1));
EntityManager.SetSharedComponentManaged(entity2, new SharedData1(2));
Assert.DoesNotThrow(() => queryWithFilter1.SetSingleton(new EcsTestData(1)));
Assert.DoesNotThrow(() => queryWithFilter2.SetSingleton(new EcsTestData(2)));
Assert.DoesNotThrow(() => queryWithFilter1.GetSingletonEntity());
Assert.DoesNotThrow(() => queryWithFilter2.GetSingletonEntity());
var data1 = queryWithFilter1.GetSingleton<EcsTestData>();
Assert.AreEqual(1, data1.value);
var data2 = queryWithFilter2.GetSingleton<EcsTestData>();
Assert.AreEqual(2, data2.value);
// These need to be reset or the AllSharedComponentReferencesAreFromChunks check will fail
queryWithFilter1.ResetFilter();
queryWithFilter2.ResetFilter();
}
public void SingletonMethodsWithInvalidFilter_Throws()
{
var queryWithFilterMissingEntity = EntityManager.CreateEntityQuery(typeof(EcsTestData), typeof(SharedData1));
queryWithFilterMissingEntity.SetSharedComponentFilterManaged(new SharedData1(1));
var queryWithFilterWithAdditionalEntity = EntityManager.CreateEntityQuery(typeof(EcsTestData), typeof(SharedData1));
queryWithFilterWithAdditionalEntity.SetSharedComponentFilterManaged(new SharedData1(2));
var entity1 = EntityManager.CreateEntity(typeof(EcsTestData), typeof(SharedData1));
EntityManager.SetSharedComponentManaged(entity1, new SharedData1(2));
var entity2 = EntityManager.CreateEntity(typeof(EcsTestData), typeof(SharedData1));
EntityManager.SetSharedComponentManaged(entity2, new SharedData1(2));
Assert.Throws<InvalidOperationException>(() => queryWithFilterMissingEntity.GetSingleton<EcsTestData>());
Assert.Throws<InvalidOperationException>(() => queryWithFilterMissingEntity.SetSingleton(new EcsTestData(1)));
Assert.Throws<InvalidOperationException>(() => queryWithFilterMissingEntity.GetSingletonEntity());
Assert.Throws<InvalidOperationException>(() => queryWithFilterWithAdditionalEntity.GetSingleton<EcsTestData>());
Assert.Throws<InvalidOperationException>(() => queryWithFilterWithAdditionalEntity.SetSingleton(new EcsTestData(1)));
Assert.Throws<InvalidOperationException>(() => queryWithFilterWithAdditionalEntity.GetSingletonEntity());
// These need to be reset or the AllSharedComponentReferencesAreFromChunks check will fail
queryWithFilterMissingEntity.ResetFilter();
queryWithFilterWithAdditionalEntity.ResetFilter();
}
public void GetSetSingletonMultipleComponents()
{
var entity = EntityManager.CreateEntity(typeof(EcsTestData3), typeof(EcsTestData), typeof(EcsTestData2));
EntityManager.SetComponentData(entity, new EcsTestData(10));
Assert.AreEqual(10, SystemAPI.GetSingleton<EcsTestData>().value);
SystemAPI.SetSingleton(new EcsTestData2(100));
Assert.AreEqual(100, EntityManager.GetComponentData<EcsTestData2>(entity).value0);
}
public void GetSetSingletonInEntitiesForEach()
{
EntityManager.CreateEntity(typeof(EcsTestData2));
EntityManager.CreateEntity(typeof(EcsTestData));
var query = SystemAPI.QueryBuilder().WithAllRW<EcsTestData>().Build();
Entities.WithoutBurst().ForEach((in EcsTestData2 data2) => { query.SetSingleton(new EcsTestData(10)); }).Run();
int value = 0;
Entities.WithoutBurst().ForEach((in EcsTestData2 data2) => { value = query.GetSingleton<EcsTestData>().value; }).Run();
Assert.AreEqual(10, value);
}
public void GetSetSingletonZeroThrows()
{
Assert.Throws<InvalidOperationException>(() => SystemAPI.SetSingleton(new EcsTestData()));
Assert.Throws<InvalidOperationException>(() => SystemAPI.GetSingleton<EcsTestData>());
}
// Throws due to a singleton component only being allowed on a single Entity
public void GetSetSingletonMultipleThrows()
{
EntityManager.CreateEntity(typeof(EcsTestData));
EntityManager.CreateEntity(typeof(EcsTestData));
Assert.Throws<InvalidOperationException>(() => SystemAPI.SetSingleton(new EcsTestData()));
Assert.Throws<InvalidOperationException>(() => SystemAPI.GetSingleton<EcsTestData>());
}
public void RequireSingletonWorks()
{
RequireForUpdate<EcsTestData>();
GetEntityQuery(typeof(EcsTestData2));
EntityManager.CreateEntity(typeof(EcsTestData2));
Assert.IsFalse(ShouldRunSystem());
EntityManager.CreateEntity(typeof(EcsTestData));
Assert.IsTrue(ShouldRunSystem());
}
public void HasSingletonWorks()
{
Assert.IsFalse(SystemAPI.HasSingleton<EcsTestData>());
EntityManager.CreateEntity(typeof(EcsTestData));
Assert.IsTrue(SystemAPI.HasSingleton<EcsTestData>());
}
public void HasSingleton_ReturnsTrueWithEntityWithOnlyComponent()
{
Assert.IsFalse(SystemAPI.HasSingleton<EcsTestData>());
EntityManager.CreateEntity(typeof(EcsTestData));
Assert.IsTrue(SystemAPI.HasSingleton<EcsTestData>());
}
public void HasSingleton_MoreThanOneInstance_Throws()
{
EntityManager.CreateEntity(typeof(EcsTestData));
EntityManager.CreateEntity(typeof(EcsTestData));
Assert.Throws<InvalidOperationException>(() => SystemAPI.HasSingleton<EcsTestData>());
}
public void GetSingletonEntityWorks()
{
var entity = EntityManager.CreateEntity(typeof(EcsTestData));
var singletonEntity = SystemAPI.GetSingletonEntity<EcsTestData>();
Assert.AreEqual(entity, singletonEntity);
}
public void GetSingletonThroughQueryWorks()
{
EntityQuery query = GetEntityQuery(ComponentType.ReadOnly<EcsTestData>(), ComponentType.ReadOnly<EcsTestData2>());
RequireForUpdate(query);
var entity = EntityManager.CreateEntity(typeof(EcsTestData), typeof(EcsTestData2));
EntityManager.SetComponentData(entity, new EcsTestData() { value = 3 });
Assert.AreEqual(3, query.GetSingleton<EcsTestData>().value);
}
public void SetSingletonWithExpressionWithPreprocessorTriviaWorks()
{
EntityManager.CreateEntity(typeof(EcsTestData));
SystemAPI.SetSingleton(new EcsTestData()
{
#if SOME_DEFINE
value = 3
#endif
});
Assert.AreEqual(3, SystemAPI.GetSingleton<EcsTestData>().value);
}
public void GetSingletonWithGeneric()
{
EntityManager.CreateEntity(typeof(EcsTestGeneric<int>));
SystemAPI.SetSingleton(new EcsTestGeneric<int>{ value = 10 });
Assert.AreEqual(10, SystemAPI.GetSingleton<EcsTestGeneric<int>>().value);
}
#if !UNITY_DISABLE_MANAGED_COMPONENTS
public void GetSetSingleton_ManagedComponents()
{
var entity = EntityManager.CreateEntity();
EntityManager.AddComponentObject(entity, new EcsTestManagedComponent());
const string kTestVal = "SomeString";
SystemAPI.ManagedAPI.GetSingleton<EcsTestManagedComponent>().value = kTestVal;
Assert.AreEqual(kTestVal, SystemAPI.ManagedAPI.GetSingleton<EcsTestManagedComponent>().value);
}
public void HasSingletonWorks_ManagedComponents()
{
Assert.IsFalse(SystemAPI.ManagedAPI.HasSingleton<EcsTestManagedComponent>());
EntityManager.CreateEntity(typeof(EcsTestManagedComponent));
Assert.IsTrue(SystemAPI.ManagedAPI.HasSingleton<EcsTestManagedComponent>());
}
#endif
}
[Test]
public void SystemBase_SingletonGetterAndSetter() => TestSystem.SingletonPropertyGetterAndSetter();
[Test]
public void SystemBase_GetSetSingleton() => TestSystem.GetSetSingleton();
[Test]
public void SystemBase_GetSingletonRW_ByRef_Modifies_Singleton() => TestSystem.GetSingletonRW_ByRef_Modifies_Singleton();
[Test]
public void SystemBase_GetSingletonRW_ByValue_DoesNot_Modify_Singleton() => TestSystem.GetSingletonRW_ByValue_DoesNot_Modify_Singleton();
[Test]
public void SystemBase_GetSetSingletonInSystem() => TestSystem.GetSetSingletonInSystem();
[Test]
public void SystemBase_GetSetSingletonWithGenericParameter() => TestSystem.GetSetSingletonWithGenericParameter();
[Test]
public void SystemBase_SingletonAccessInGenericMethodWithInParameter() => TestSystem.GetSingletonInGenericMethodWithInParameter();
[Test]
public void SystemBase_SingletonMethodsWithValidFilter_GetsAndSets() => TestSystem.SingletonMethodsWithValidFilter_GetsAndSets();
[Test]
[TestRequiresDotsDebugOrCollectionChecks("Test requires system safety checks")]
public void SystemBase_SingletonMethodsWithInvalidFilter_Throws() => TestSystem.SingletonMethodsWithInvalidFilter_Throws();
[Test]
[TestRequiresDotsDebugOrCollectionChecks("Test requires system safety checks")]
public void SystemBase_GetSetSingletonZeroThrows() => TestSystem.GetSetSingletonZeroThrows();
[Test]
[TestRequiresDotsDebugOrCollectionChecks("Test requires system safety checks")]
public void SystemBase_GetSetSingletonMultipleThrows() => TestSystem.GetSetSingletonMultipleThrows();
[Test]
public void SystemBase_GetSetSingletonMultipleComponents() => TestSystem.GetSetSingletonMultipleComponents();
[Test]
public void SystemBase_GetSetSingletonInEntitiesForEach() => TestSystem.GetSetSingletonInEntitiesForEach();
[Test]
public void SystemBase_RequireSingletonWorks() => TestSystem.RequireSingletonWorks();
[Test]
public void SystemBase_HasSingletonWorks() => TestSystem.HasSingletonWorks();
[Test]
public void SystemBase_HasSingleton_ReturnsTrueWithEntityWithOnlyComponent() => TestSystem.HasSingleton_ReturnsTrueWithEntityWithOnlyComponent();
[Test]
[TestRequiresDotsDebugOrCollectionChecks("requires opt-in debug checks")]
public void SystemBase_HasSingleton_MoreThanOneInstance_Throws() => TestSystem.HasSingleton_MoreThanOneInstance_Throws();
[Test]
public void SystemBase_GetSingletonEntityWorks() => TestSystem.GetSingletonEntityWorks();
[Test]
public void SystemBase_GetSingletonThroughQueryWorks() => TestSystem.GetSingletonThroughQueryWorks();
[Test]
public void SystemBase_SetSingletonWithExpressionWithPreprocessorTriviaWorks() => TestSystem.SetSingletonWithExpressionWithPreprocessorTriviaWorks();
[Test]
public void SystemBase_GetSingletonWithGeneric() => TestSystem.GetSingletonWithGeneric();
#if !UNITY_DISABLE_MANAGED_COMPONENTS
[Test]
public void SystemBase_GetSetSingleton_ManagedComponents() => TestSystem.GetSetSingleton_ManagedComponents();
[Test]
public void SystemBase_HasSingletonWorks_ManagedComponents() => TestSystem.HasSingletonWorks_ManagedComponents();
#endif
public partial class JobWritesComponentSystem : SystemBase
{
protected override void OnCreate()
{
EntityManager.CreateEntity(ComponentType.ReadWrite<EcsTestData>());
}
protected override void OnUpdate()
{
new TestJob().Schedule();
}
public partial struct TestJob : IJobEntity
{
internal void Execute(ref EcsTestData normalComponent)
{
normalComponent.value = 1;
}
}
}
[UpdateAfter(typeof(JobWritesComponentSystem))]
public partial class ReadComponentSystem : SystemBase
{
protected override void OnUpdate() { }
protected override void OnStartRunning()
{
// For dependencies to complete properly, BeforeOnUpdate needs to have been called.
CompleteDependency();
SystemAPI.SetSingleton<EcsTestData>(default);
}
}
[Test]
public void SystemBase_SingletonInOnStartRunning()
{
var sysA = World.CreateSystem<JobWritesComponentSystem>();
var sysB = World.CreateSystem<ReadComponentSystem>();
sysA.Update(World.Unmanaged);
sysB.Update(World.Unmanaged);
World.DestroySystem(sysB);
World.DestroySystem(sysA);
}
}
}