MeshQueueTests.cs
4.94 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
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine.XR.ARSubsystems;
using LegacyMeshId = UnityEngine.XR.MeshId;
namespace UnityEngine.XR.ARFoundation
{
[TestFixture]
public class MeshQueueTestFixture
{
[Test]
public void HighPriorityItemsTakePrecedence()
{
var emptyDict = new Dictionary<LegacyMeshId, MeshInfo>();
var queue = new MeshQueue();
for (int i = 0; i < 100; ++i)
{
queue.EnqueueUnique(new MeshInfo
{
MeshId = GetRandomMeshId(),
ChangeState = MeshChangeState.Added,
PriorityHint = Random.Range(0, 100)
});
}
int? lastPriorityHint = null;
while (queue.count > 0)
{
bool dequeued = queue.TryDequeue(emptyDict, out MeshInfo meshInfo);
Assert.That(dequeued, "Could not dequeue even with an empty dictionary");
if (lastPriorityHint.HasValue)
{
Assert.That(meshInfo.PriorityHint <= lastPriorityHint.Value);
}
lastPriorityHint = meshInfo.PriorityHint;
}
}
[Test]
public void AddedMeshesTakePrecedence()
{
var emptyDict = new Dictionary<LegacyMeshId, MeshInfo>();
var queue = new MeshQueue();
for (int i = 0; i < 100; ++i)
{
queue.EnqueueUnique(new MeshInfo
{
MeshId = GetRandomMeshId(),
ChangeState = (Random.Range(0f, 1f) < .5f || i == 0) ? MeshChangeState.Added : MeshChangeState.Updated,
PriorityHint = Random.Range(0, 100)
});
}
MeshChangeState? lastChangeState = null;
while (queue.count > 0)
{
bool dequeued = queue.TryDequeue(emptyDict, out MeshInfo meshInfo);
Assert.That(dequeued, "Could not dequeue even with an empty dictionary");
if (lastChangeState.HasValue)
{
Assert.That((meshInfo.ChangeState == lastChangeState.Value) || (meshInfo.ChangeState == MeshChangeState.Updated && lastChangeState.Value == MeshChangeState.Added),
"All added meshes did not come first");
}
lastChangeState = meshInfo.ChangeState;
}
}
[Test]
public void GeneratingMeshesAreNotDequeued()
{
var generating = new Dictionary<LegacyMeshId, MeshInfo>();
var queue = new MeshQueue();
for (int i = 0; i < 100; ++i)
{
var meshId = GetRandomMeshId();
var meshInfo = new MeshInfo
{
MeshId = meshId,
ChangeState = MeshChangeState.Added,
PriorityHint = Random.Range(0, 100)
};
queue.EnqueueUnique(meshInfo);
if (Random.Range(0f, 1f) < .5f)
{
generating[meshId] = meshInfo;
}
}
while (generating.Count < queue.count)
{
bool result = queue.TryDequeue(generating, out MeshInfo meshInfo);
Assert.That(result, "Could not dequeue a mesh info even though there are more items to dequeue.");
Assert.That(!generating.ContainsKey(meshInfo.MeshId), "Should not dequeue a mesh info while it is generating.");
}
}
[Test]
public void QueueIsUnique()
{
var generating = new Dictionary<LegacyMeshId, MeshInfo>();
var queue = new MeshQueue();
var uniqueMeshIds = new List<LegacyMeshId>();
for (int i = 0; i < 100; ++i)
{
LegacyMeshId meshId;
if (i == 0 || Random.Range(0f, 1f) < .5f)
{
meshId = MakeMeshId((ulong)i, (ulong)i);
uniqueMeshIds.Add(meshId);
}
else
{
meshId = uniqueMeshIds[Random.Range(0, uniqueMeshIds.Count - 1)];
}
var meshInfo = new MeshInfo
{
MeshId = meshId,
ChangeState = MeshChangeState.Added,
PriorityHint = Random.Range(0, 100)
};
queue.EnqueueUnique(meshInfo);
Assert.That(uniqueMeshIds.Count == queue.count);
}
}
LegacyMeshId MakeMeshId(ulong a, ulong b)
{
return ARMeshManager.GetLegacyMeshId(new TrackableId(a, b));
}
LegacyMeshId GetRandomMeshId()
{
return MakeMeshId(
(ulong)Random.Range(0, int.MaxValue),
(ulong)Random.Range(0, int.MaxValue));
}
}
}