com.wallstop-studios.unity-helpers 2.0.0-rc04 → 2.0.0-rc06

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/CHANGELOG.md.meta +7 -7
  2. package/Editor/AnimationEventEditor.cs.meta +11 -11
  3. package/Editor.meta +8 -8
  4. package/LICENSE.md.meta +7 -7
  5. package/README.md.meta +7 -7
  6. package/Runtime/Core/Attributes/AnimationEventAttribute.cs.meta +11 -11
  7. package/Runtime/Core/Attributes/ChildComponentAttribute.cs.meta +11 -11
  8. package/Runtime/Core/Attributes/KSerializableAttribute.cs.meta +11 -11
  9. package/Runtime/Core/Attributes/NotNullAttribute.cs.meta +11 -11
  10. package/Runtime/Core/Attributes/ParentComponent.cs.meta +11 -11
  11. package/Runtime/Core/Attributes/ReadOnlyAttribute.cs.meta +11 -11
  12. package/Runtime/Core/Attributes/RelationalComponentExtensions.cs.meta +11 -11
  13. package/Runtime/Core/DataStructure/Adapters/FastVector2Int.cs.meta +11 -11
  14. package/Runtime/Core/DataStructure/Adapters/FastVector3Int.cs.meta +11 -11
  15. package/Runtime/Core/DataStructure/Adapters/KGuid.cs.meta +11 -11
  16. package/Runtime/Core/DataStructure/Adapters/KVector2.cs.meta +11 -11
  17. package/Runtime/Core/DataStructure/Adapters.meta +8 -8
  18. package/Runtime/Core/DataStructure/Circle.cs.meta +11 -11
  19. package/Runtime/Core/DataStructure/CyclicBuffer.cs.meta +11 -11
  20. package/Runtime/Core/DataStructure/QuadTree.cs.meta +11 -11
  21. package/Runtime/Core/DataStructure/StringWrapper.cs.meta +11 -11
  22. package/Runtime/Core/DataStructure/TimedCache.cs +4 -3
  23. package/Runtime/Core/DataStructure/TimedCache.cs.meta +11 -11
  24. package/Runtime/Core/DataStructure.meta +8 -8
  25. package/Runtime/Core/Extension/AnimatorExtensions.cs.meta +11 -11
  26. package/Runtime/Core/Extension/CircleExtensions.cs.meta +11 -11
  27. package/Runtime/Core/Extension/ColorExtensions.cs.meta +11 -11
  28. package/Runtime/Core/Extension/DictionaryExtensions.cs.meta +11 -11
  29. package/Runtime/Core/Extension/DirectionExtensions.cs.meta +11 -11
  30. package/Runtime/Core/Extension/HashSetExtensions.cs.meta +11 -11
  31. package/Runtime/Core/Extension/IEnumerableExtensions.cs.meta +11 -11
  32. package/Runtime/Core/Extension/IListExtensions.cs +2 -2
  33. package/Runtime/Core/Extension/IListExtensions.cs.meta +11 -11
  34. package/Runtime/Core/Extension/LoggingExtensions.cs.meta +11 -11
  35. package/Runtime/Core/Extension/RandomExtensions.cs +23 -4
  36. package/Runtime/Core/Extension/RandomExtensions.cs.meta +11 -11
  37. package/Runtime/Core/Extension/StringExtensions.cs.meta +11 -11
  38. package/Runtime/Core/Extension/UnityExtensions.cs +278 -90
  39. package/Runtime/Core/Extension/UnityExtensions.cs.meta +11 -11
  40. package/Runtime/Core/Extension.meta +8 -8
  41. package/Runtime/Core/Helper/ArrayConverter.cs +39 -0
  42. package/Runtime/Core/Helper/ArrayConverter.cs.meta +3 -0
  43. package/Runtime/Core/Helper/Enumerables.cs.meta +11 -11
  44. package/Runtime/Core/Helper/Geometry.cs.meta +11 -11
  45. package/Runtime/Core/Helper/Helpers.cs +209 -84
  46. package/Runtime/Core/Helper/LifetimeHelpers.cs.meta +11 -11
  47. package/Runtime/Core/Helper/Objects.cs.meta +11 -11
  48. package/Runtime/Core/Helper/WallMath.cs +85 -22
  49. package/Runtime/Core/Helper/WallMath.cs.meta +11 -11
  50. package/Runtime/Core/Helper.meta +8 -8
  51. package/Runtime/Core/Math/Line.cs.meta +11 -11
  52. package/Runtime/Core/Math/Parabola.cs.meta +11 -11
  53. package/Runtime/Core/Math/Range.cs.meta +11 -11
  54. package/Runtime/Core/Math.meta +8 -8
  55. package/Runtime/Core/Model/Direction.cs.meta +11 -11
  56. package/Runtime/Core/Model.meta +8 -8
  57. package/Runtime/Core/OneOf/FastOneOf.cs.meta +11 -11
  58. package/Runtime/Core/OneOf/None.cs.meta +11 -11
  59. package/Runtime/Core/OneOf.meta +8 -8
  60. package/Runtime/Core/Random/AbstractRandom.cs +34 -20
  61. package/Runtime/Core/Random/AbstractRandom.cs.meta +11 -11
  62. package/Runtime/Core/Random/DotNetRandom.cs +52 -0
  63. package/Runtime/Core/Random/DotNetRandom.cs.meta +3 -0
  64. package/Runtime/Core/Random/IRandom.cs.meta +11 -11
  65. package/Runtime/Core/Random/NativePcgRandom.cs.meta +11 -11
  66. package/Runtime/Core/Random/PRNG.cs +7 -0
  67. package/Runtime/Core/Random/PRNG.cs.meta +3 -0
  68. package/Runtime/Core/Random/PcgRandom.cs +23 -22
  69. package/Runtime/Core/Random/PcgRandom.cs.meta +11 -11
  70. package/Runtime/Core/Random/RandomState.cs +44 -5
  71. package/Runtime/Core/Random/RandomState.cs.meta +11 -11
  72. package/Runtime/Core/Random/SquirrelRandom.cs +13 -10
  73. package/Runtime/Core/Random/SquirrelRandom.cs.meta +11 -11
  74. package/Runtime/Core/Random/SystemRandom.cs +31 -16
  75. package/Runtime/Core/Random/SystemRandom.cs.meta +11 -11
  76. package/Runtime/Core/Random/ThreadLocalRandom.cs +2 -1
  77. package/Runtime/Core/Random/ThreadLocalRandom.cs.meta +11 -11
  78. package/Runtime/Core/Random/UnityRandom.cs +37 -4
  79. package/Runtime/Core/Random/WyRandom.cs +121 -0
  80. package/Runtime/Core/Random/WyRandom.cs.meta +3 -0
  81. package/Runtime/Core/Random/XorShiftRandom.cs +9 -7
  82. package/Runtime/Core/Random.meta +8 -8
  83. package/Runtime/Core/Serialization/JsonConverters/Vector2Converter.cs.meta +11 -11
  84. package/Runtime/Core/Serialization/JsonConverters/Vector3Converter.cs.meta +11 -11
  85. package/Runtime/Core/Serialization/JsonConverters.meta +8 -8
  86. package/Runtime/Core/Serialization/Serializer.cs +36 -14
  87. package/Runtime/Core/Serialization/Serializer.cs.meta +11 -11
  88. package/Runtime/Core/Serialization.meta +8 -8
  89. package/Runtime/Core/Threading/SingleThreadedThreadPool.cs.meta +11 -11
  90. package/Runtime/Core/Threading.meta +8 -8
  91. package/Runtime/Core.meta +8 -8
  92. package/Runtime/Protobuf-Net.meta +8 -8
  93. package/Runtime/Utils/AnimationEventEqualityComparer.cs.meta +11 -11
  94. package/Runtime/Utils/AnimatorEnumStateMachine.cs.meta +11 -11
  95. package/Runtime/Utils/Buffers.cs.meta +11 -11
  96. package/Runtime/Utils/CircleLineRenderer.cs +17 -5
  97. package/Runtime/Utils/CircleLineRenderer.cs.meta +11 -11
  98. package/Runtime/Utils/Oscillator.cs.meta +11 -11
  99. package/Runtime/Utils/SetTextureImportData.cs.meta +11 -11
  100. package/Runtime/Utils.meta +8 -8
  101. package/Runtime/WallstopStudios.UnityHelpers.asmdef.meta +7 -7
  102. package/Runtime.meta +8 -8
  103. package/Tests/Runtime/DataStructures/SpatialTreeTests.cs +34 -10
  104. package/Tests/Runtime/Helper/ArrayConverterTests.cs +19 -0
  105. package/Tests/Runtime/Helper/ArrayConverterTests.cs.meta +3 -0
  106. package/Tests/Runtime/Helper/WallMathTests.cs +221 -0
  107. package/Tests/Runtime/Helper/WallMathTests.cs.meta +3 -0
  108. package/Tests/Runtime/Helper.meta +3 -0
  109. package/Tests/Runtime/Performance/RandomPerformanceTests.cs +18 -13
  110. package/Tests/Runtime/Performance/SpatialTreePerformanceTest.cs +47 -34
  111. package/Tests/Runtime/Random/DotNetRandomTests.cs +9 -0
  112. package/Tests/Runtime/Random/DotNetRandomTests.cs.meta +3 -0
  113. package/Tests/Runtime/Random/RandomTestBase.cs +89 -15
  114. package/Tests/Runtime/Random/WyRandomTests.cs +9 -0
  115. package/Tests/Runtime/Random/WyRandomTests.cs.meta +3 -0
  116. package/Tests/Runtime/Serialization/JsonSerializationTest.cs +24 -11
  117. package/Tests/Runtime/Utils/SpriteRendererMetadataTests.cs +21 -17
  118. package/Tests.meta +8 -8
  119. package/Third Party Notices.md.meta +7 -7
  120. package/package.json +1 -1
  121. package/package.json.meta +7 -7
@@ -8,9 +8,10 @@
8
8
  using NUnit.Framework;
9
9
  using Vector2 = UnityEngine.Vector2;
10
10
 
11
- public abstract class SpatialTreeTests<TTree> where TTree : ISpatialTree<Vector2>
11
+ public abstract class SpatialTreeTests<TTree>
12
+ where TTree : ISpatialTree<Vector2>
12
13
  {
13
- private IRandom Random => PcgRandom.Instance;
14
+ private IRandom Random => PRNG.Instance;
14
15
 
15
16
  protected abstract TTree CreateTree(IEnumerable<Vector2> points);
16
17
 
@@ -28,14 +29,18 @@
28
29
  do
29
30
  {
30
31
  point = Helpers.GetRandomPointInCircle(center, radius);
31
- }
32
- while (!points.Add(point));
32
+ } while (!points.Add(point));
33
33
  }
34
34
 
35
35
  TTree quadTree = CreateTree(points);
36
36
 
37
37
  List<Vector2> pointsInRange = quadTree.GetElementsInRange(center, radius).ToList();
38
- Assert.IsTrue(points.SetEquals(pointsInRange), "Found {0} points in range, expected {1}.", pointsInRange.Count, points.Count);
38
+ Assert.IsTrue(
39
+ points.SetEquals(pointsInRange),
40
+ "Found {0} points in range, expected {1}.",
41
+ pointsInRange.Count,
42
+ points.Count
43
+ );
39
44
  // Translate by a unit-square - there should be no points in this range
40
45
  Vector2 offset = center;
41
46
  offset.x -= radius * 2;
@@ -43,8 +48,14 @@
43
48
 
44
49
  pointsInRange = quadTree.GetElementsInRange(offset, radius).ToList();
45
50
  Assert.AreEqual(
46
- 0, pointsInRange.Count, "Found {0} points within {1} range of {2} (original center {3})",
47
- pointsInRange.Count, radius, offset, center);
51
+ 0,
52
+ pointsInRange.Count,
53
+ "Found {0} points within {1} range of {2} (original center {3})",
54
+ pointsInRange.Count,
55
+ radius,
56
+ offset,
57
+ center
58
+ );
48
59
  }
49
60
 
50
61
  [Test]
@@ -58,10 +69,19 @@
58
69
  List<Vector2> points = new(1) { testPoint };
59
70
 
60
71
  TTree quadTree = CreateTree(points);
61
- List<Vector2> pointsInRange = quadTree.GetElementsInRange(point, range * 0.99f).ToList();
72
+ List<Vector2> pointsInRange = quadTree
73
+ .GetElementsInRange(point, range * 0.99f)
74
+ .ToList();
62
75
  Assert.AreEqual(0, pointsInRange.Count);
63
76
  pointsInRange = quadTree.GetElementsInRange(point, range * 1.01f).ToList();
64
- Assert.AreEqual(1, pointsInRange.Count, "Failed to find point {0} from test point {1} with {2:0.00} range.", point, testPoint, range);
77
+ Assert.AreEqual(
78
+ 1,
79
+ pointsInRange.Count,
80
+ "Failed to find point {0} from test point {1} with {2:0.00} range.",
81
+ point,
82
+ testPoint,
83
+ range
84
+ );
65
85
  Assert.AreEqual(testPoint, pointsInRange[0]);
66
86
  }
67
87
 
@@ -95,7 +115,11 @@
95
115
  nearestNeighborCount = 16;
96
116
  quadTree.GetApproximateNearestNeighbors(center, nearestNeighborCount, nearestNeighbors);
97
117
  Assert.AreEqual(nearestNeighborCount, nearestNeighbors.Count);
98
- Assert.IsTrue(nearestNeighbors.All(neighbor => (neighbor - center).magnitude <= 5.6f), "Max: {0}", nearestNeighbors.Select(neighbor => (neighbor - center).magnitude).Max());
118
+ Assert.IsTrue(
119
+ nearestNeighbors.All(neighbor => (neighbor - center).magnitude <= 5.6f),
120
+ "Max: {0}",
121
+ nearestNeighbors.Select(neighbor => (neighbor - center).magnitude).Max()
122
+ );
99
123
 
100
124
  center = new Vector2(-100, -100);
101
125
 
@@ -0,0 +1,19 @@
1
+ namespace UnityHelpers.Tests.Tests.Runtime.Helper
2
+ {
3
+ using System.Linq;
4
+ using Core.Helper;
5
+ using Core.Random;
6
+ using NUnit.Framework;
7
+
8
+ public sealed class ArrayConverterTests
9
+ {
10
+ [Test]
11
+ public void IntToByteArray()
12
+ {
13
+ int[] ints = Enumerable.Range(0, 1000).Select(_ => PRNG.Instance.Next()).ToArray();
14
+ byte[] bytes = ArrayConverter.IntArrayToByteArrayBlockCopy(ints);
15
+ int[] decoded = ArrayConverter.ByteArrayToIntArrayBlockCopy(bytes);
16
+ Assert.IsTrue(ints.SequenceEqual(decoded));
17
+ }
18
+ }
19
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 35d3b0e1ba0e43bbb9d899a44b36812a
3
+ timeCreated: 1732492102
@@ -0,0 +1,221 @@
1
+ namespace UnityHelpers.Tests.Tests.Runtime.Helper
2
+ {
3
+ using Core.Extension;
4
+ using Core.Helper;
5
+ using Core.Random;
6
+ using NUnit.Framework;
7
+ using UnityEngine;
8
+
9
+ public sealed class WallMathTests
10
+ {
11
+ private const int TestIterations = 10_000;
12
+
13
+ [Test]
14
+ public void ApproximatelyRandom()
15
+ {
16
+ for (int i = 0; i < TestIterations; ++i)
17
+ {
18
+ float target = PRNG.Instance.NextFloat(-1_000, 1_000);
19
+ float delta = PRNG.Instance.NextFloat(0f, 10f);
20
+
21
+ float insideOffset = delta * PRNG.Instance.NextFloat();
22
+ if (PRNG.Instance.NextBool())
23
+ {
24
+ insideOffset *= -1;
25
+ }
26
+
27
+ float inside = target + insideOffset;
28
+ Assert.IsTrue(
29
+ target.Approximately(inside, delta),
30
+ $"Target {target} is not close enough to {inside} with a delta of {delta}."
31
+ );
32
+
33
+ float outsideOffset = delta * PRNG.Instance.NextFloat(1.001f, 10f);
34
+ if (PRNG.Instance.NextBool())
35
+ {
36
+ outsideOffset *= -1;
37
+ }
38
+
39
+ float outside = target + outsideOffset;
40
+ Assert.IsFalse(
41
+ target.Approximately(outside, delta),
42
+ $"Target {target} was unexpectedly close to {outside} with a delta of {delta}."
43
+ );
44
+ }
45
+ }
46
+
47
+ [Test]
48
+ public void ApproximatelyExpected()
49
+ {
50
+ Assert.IsTrue(0f.Approximately(0f, 0f));
51
+ Assert.IsTrue(0f.Approximately(0.5f, 1f));
52
+ Assert.IsFalse(0.001f.Approximately(0f, 0f));
53
+ Assert.IsFalse(100f.Approximately(5f, 2.4f));
54
+
55
+ Assert.IsTrue(0.001f.Approximately(0.0001f));
56
+ }
57
+
58
+ [Test]
59
+ public void ClampRectInsideRandom()
60
+ {
61
+ for (int i = 0; i < TestIterations; ++i)
62
+ {
63
+ Vector2 position = PRNG.Instance.NextVector2InRange(10f);
64
+
65
+ Vector2 size;
66
+ do
67
+ {
68
+ size = PRNG.Instance.NextVector2InRange(10f);
69
+ } while (size.x < 0 || size.y < 0);
70
+
71
+ Rect rect = new(position, size);
72
+
73
+ Vector2 inside = new(
74
+ PRNG.Instance.NextFloat(rect.min.x, rect.max.x),
75
+ PRNG.Instance.NextFloat(rect.min.y, rect.max.y)
76
+ );
77
+ Assert.IsTrue(rect.Contains(inside), $"Rect {rect} does not contain {inside}.");
78
+ Vector2 clamped = rect.Clamp(inside);
79
+ Assert.AreEqual(inside, clamped);
80
+ }
81
+ }
82
+
83
+ [Test]
84
+ public void ClampRectInsideExpected()
85
+ {
86
+ Rect rect = new Rect(Vector2.one, Vector2.one);
87
+ Vector2 clamped = rect.Clamp(rect.center);
88
+ Assert.AreEqual(rect.center, clamped);
89
+
90
+ Vector2 inside =
91
+ rect.center
92
+ + new Vector2(PRNG.Instance.NextFloat(0.1f), PRNG.Instance.NextFloat(0.1f));
93
+ clamped = rect.Clamp(inside);
94
+ Assert.AreEqual(
95
+ inside,
96
+ clamped,
97
+ $"Expected inside point {inside} unexpectedly was clamped to {clamped} for rect {rect}."
98
+ );
99
+ }
100
+
101
+ [Test]
102
+ public void ClampRectOutsideRandom()
103
+ {
104
+ Rect rect = new(Vector2.one, Vector2.one);
105
+ for (int i = 0; i < TestIterations; ++i)
106
+ {
107
+ Vector2 outside;
108
+ do
109
+ {
110
+ outside = PRNG.Instance.NextVector2InRange(10f, rect.center);
111
+ } while (rect.Contains(outside));
112
+
113
+ Vector2 originalOutside = outside;
114
+ Vector2 clamped = rect.Clamp(ref outside);
115
+ Debug.Log($"Clamped {originalOutside} to {clamped}.");
116
+ Assert.AreNotEqual(originalOutside, clamped);
117
+ Assert.AreEqual(clamped, outside);
118
+
119
+ // Shrink just a little to ensure within bounds, clamping will put it right on the edge
120
+ const float shrinkScaleToAccountForPrecision = 0.99f;
121
+ Vector2 delta = rect.center - clamped;
122
+ delta *= shrinkScaleToAccountForPrecision;
123
+ clamped = rect.center + delta;
124
+ Assert.IsTrue(
125
+ rect.Contains(clamped),
126
+ $"Expected rect {rect} to contain {clamped}, but it did not."
127
+ );
128
+ }
129
+ }
130
+
131
+ [Test]
132
+ public void ClampRectOutsideExpected()
133
+ {
134
+ Rect rect = new(Vector2.one, Vector2.one);
135
+ Vector2 outside = new(rect.center.x, 100f);
136
+ Vector2 clamped = rect.Clamp(ref outside);
137
+ Assert.AreEqual(clamped, outside);
138
+ Assert.AreEqual(new Vector2(rect.center.x, 2f), clamped);
139
+
140
+ // Shrink just a little to ensure within bounds, clamping will put it right on the edge
141
+ const float shrinkScaleToAccountForPrecision = 0.99f;
142
+ Vector2 delta = rect.center - clamped;
143
+ delta *= shrinkScaleToAccountForPrecision;
144
+ clamped = rect.center + delta;
145
+ Assert.IsTrue(
146
+ rect.Contains(clamped),
147
+ $"Expected rect to contain {clamped}, but it did not."
148
+ );
149
+ }
150
+
151
+ [Test]
152
+ public void ClampFloat()
153
+ {
154
+ float value = 1.25f;
155
+ float clamped = value.Clamp(0, 1);
156
+ Assert.AreEqual(1, clamped);
157
+ Assert.AreEqual(1.25f, value);
158
+
159
+ value = 0.5f;
160
+ clamped = value.Clamp(0, 1);
161
+ Assert.AreEqual(0.5f, clamped);
162
+ Assert.AreEqual(0.5f, value);
163
+ }
164
+
165
+ [Test]
166
+ public void WrappedIncrementWrapped()
167
+ {
168
+ int ten = 10;
169
+ int one = ten.WrappedIncrement(10);
170
+ Assert.AreEqual(1, one);
171
+ Assert.AreEqual(10, ten);
172
+
173
+ int toChangeToOne = 10;
174
+ int returned = WallMath.WrappedIncrement(ref toChangeToOne, 10);
175
+ Assert.AreEqual(1, returned);
176
+ Assert.AreEqual(1, toChangeToOne);
177
+ }
178
+
179
+ [Test]
180
+ public void WrappedIncrementUnwrapped()
181
+ {
182
+ int ten = 10;
183
+ int eleven = ten.WrappedIncrement(100);
184
+ Assert.AreEqual(11, eleven);
185
+ Assert.AreEqual(10, ten);
186
+
187
+ int toChangeToEleven = 10;
188
+ int returned = WallMath.WrappedIncrement(ref toChangeToEleven, 100);
189
+ Assert.AreEqual(11, returned);
190
+ Assert.AreEqual(11, toChangeToEleven);
191
+ }
192
+
193
+ [Test]
194
+ public void WrappedAddWrapped()
195
+ {
196
+ int ten = 10;
197
+ int two = ten.WrappedAdd(2, 10);
198
+ Assert.AreEqual(10, ten);
199
+ Assert.AreEqual(2, two);
200
+
201
+ int toChangeToTwo = 10;
202
+ int returned = WallMath.WrappedAdd(ref toChangeToTwo, 2, 10);
203
+ Assert.AreEqual(2, returned);
204
+ Assert.AreEqual(2, toChangeToTwo);
205
+ }
206
+
207
+ [Test]
208
+ public void WrappedAddUnwrapped()
209
+ {
210
+ int ten = 10;
211
+ int twelve = ten.WrappedAdd(2, 100);
212
+ Assert.AreEqual(10, ten);
213
+ Assert.AreEqual(12, twelve);
214
+
215
+ int toChangeToTwelve = 10;
216
+ int returned = WallMath.WrappedAdd(ref toChangeToTwelve, 2, 100);
217
+ Assert.AreEqual(12, returned);
218
+ Assert.AreEqual(12, toChangeToTwelve);
219
+ }
220
+ }
221
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 11e0e2d4098d47159de89eff698b2510
3
+ timeCreated: 1732563313
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: d1cf06665d214b7aa507e42eb1fdfbe4
3
+ timeCreated: 1732492095
@@ -10,28 +10,34 @@
10
10
  [Test]
11
11
  public void Benchmark()
12
12
  {
13
- TimeSpan timeout = TimeSpan.FromSeconds(1);
13
+ TimeSpan timeout = TimeSpan.FromSeconds(1.125);
14
14
 
15
15
  UnityEngine.Debug.Log($"| Random | Next | NextFloat | NextDouble |");
16
16
  UnityEngine.Debug.Log($"| ------ | ---- | --------- | ---------- |");
17
-
17
+
18
18
  RunTest(new PcgRandom(), timeout);
19
19
  RunTest(new SystemRandom(), timeout);
20
20
  RunTest(new SquirrelRandom(), timeout);
21
21
  RunTest(new XorShiftRandom(), timeout);
22
+ RunTest(new DotNetRandom(), timeout);
23
+ RunTest(new WyRandom(), timeout);
22
24
  }
23
25
 
24
- private void RunTest<T>(T random, TimeSpan timeout) where T : IRandom
26
+ private static void RunTest<T>(T random, TimeSpan timeout)
27
+ where T : IRandom
25
28
  {
26
29
  int nextInt = RunNext(timeout, random);
27
30
  int nextFloat = RunNextFloat(timeout, random);
28
31
  int nextDouble = RunNextDouble(timeout, random);
29
32
 
30
- UnityEngine.Debug.Log($"| {random.GetType().Name} | {(nextInt / timeout.TotalSeconds):N0} | {(nextFloat / timeout.TotalSeconds):N0} | {(nextDouble / timeout.TotalSeconds):N0} |");
33
+ UnityEngine.Debug.Log(
34
+ $"| {random.GetType().Name} | {(nextInt / timeout.TotalSeconds):N0} | {(nextFloat / timeout.TotalSeconds):N0} | {(nextDouble / timeout.TotalSeconds):N0} |"
35
+ );
31
36
  }
32
37
 
33
38
  // Copy-pasta'd for maximum speed
34
- private int RunNext<T>(TimeSpan timeout, T random) where T : IRandom
39
+ private static int RunNext<T>(TimeSpan timeout, T random)
40
+ where T : IRandom
35
41
  {
36
42
  int count = 0;
37
43
  Stopwatch timer = Stopwatch.StartNew();
@@ -39,13 +45,13 @@
39
45
  {
40
46
  _ = random.Next();
41
47
  ++count;
42
- }
43
- while (timer.Elapsed < timeout);
48
+ } while (timer.Elapsed < timeout);
44
49
 
45
50
  return count;
46
51
  }
47
52
 
48
- private int RunNextFloat<T>(TimeSpan timeout, T random) where T : IRandom
53
+ private static int RunNextFloat<T>(TimeSpan timeout, T random)
54
+ where T : IRandom
49
55
  {
50
56
  int count = 0;
51
57
  Stopwatch timer = Stopwatch.StartNew();
@@ -53,13 +59,13 @@
53
59
  {
54
60
  _ = random.NextFloat();
55
61
  ++count;
56
- }
57
- while (timer.Elapsed < timeout);
62
+ } while (timer.Elapsed < timeout);
58
63
 
59
64
  return count;
60
65
  }
61
66
 
62
- private int RunNextDouble<T>(TimeSpan timeout, T random) where T : IRandom
67
+ private static int RunNextDouble<T>(TimeSpan timeout, T random)
68
+ where T : IRandom
63
69
  {
64
70
  int count = 0;
65
71
  Stopwatch timer = Stopwatch.StartNew();
@@ -67,8 +73,7 @@
67
73
  {
68
74
  _ = random.NextDouble();
69
75
  ++count;
70
- }
71
- while (timer.Elapsed < timeout);
76
+ } while (timer.Elapsed < timeout);
72
77
 
73
78
  return count;
74
79
  }
@@ -1,17 +1,18 @@
1
1
  namespace UnityHelpers.Tests.Performance
2
2
  {
3
- using NUnit.Framework;
4
- using System.Collections.Generic;
5
- using System.Diagnostics;
6
3
  using System;
7
4
  using System.Collections;
5
+ using System.Collections.Generic;
6
+ using System.Diagnostics;
8
7
  using System.Linq;
9
8
  using System.Threading.Tasks;
10
- using UnityEngine;
11
9
  using Core.DataStructure;
10
+ using NUnit.Framework;
11
+ using UnityEngine;
12
12
  using UnityEngine.TestTools;
13
13
 
14
- public abstract class SpatialTreePerformanceTest<TTree> where TTree : ISpatialTree<Vector2>
14
+ public abstract class SpatialTreePerformanceTest<TTree>
15
+ where TTree : ISpatialTree<Vector2>
15
16
  {
16
17
  protected abstract TTree CreateTree(IEnumerable<Vector2> points);
17
18
 
@@ -23,11 +24,15 @@
23
24
 
24
25
  Vector2[] points = new Vector2[1_000_000];
25
26
  float radius = 500;
26
- ParallelLoopResult result = Parallel.For(0, 1_000_000, index =>
27
- {
28
- // ReSharper disable once PossibleLossOfFraction
29
- points[index] = new Vector2(index % 1_000, index / 1_000);
30
- });
27
+ ParallelLoopResult result = Parallel.For(
28
+ 0,
29
+ 1_000_000,
30
+ index =>
31
+ {
32
+ // ReSharper disable once PossibleLossOfFraction
33
+ points[index] = new Vector2(index % 1_000, index / 1_000);
34
+ }
35
+ );
31
36
 
32
37
  while (!result.IsCompleted)
33
38
  {
@@ -37,7 +42,9 @@
37
42
  Stopwatch timer = Stopwatch.StartNew();
38
43
  TTree tree = CreateTree(points);
39
44
  timer.Stop();
40
- UnityEngine.Debug.Log($"| Construction (1 million points) | {(int)Math.Floor(1 / timer.Elapsed.TotalSeconds)} ({timer.Elapsed.TotalSeconds} total) |");
45
+ UnityEngine.Debug.Log(
46
+ $"| Construction (1 million points) | {(int)Math.Floor(1 / timer.Elapsed.TotalSeconds)} ({timer.Elapsed.TotalSeconds} seconds total) |"
47
+ );
41
48
  Vector2 center = tree.Boundary.center;
42
49
  int count = 0;
43
50
  TimeSpan timeout = TimeSpan.FromSeconds(1);
@@ -47,10 +54,11 @@
47
54
  int elementsInRange = tree.GetElementsInRange(center, radius).Count();
48
55
  Assert.AreEqual(785456, elementsInRange);
49
56
  ++count;
50
- }
51
- while (timer.Elapsed < timeout);
57
+ } while (timer.Elapsed < timeout);
52
58
 
53
- UnityEngine.Debug.Log($"| Elements In Range - Full | {(int)Math.Floor(count / timeout.TotalSeconds)} |");
59
+ UnityEngine.Debug.Log(
60
+ $"| Elements In Range - Full | {(int)Math.Floor(count / timeout.TotalSeconds)} |"
61
+ );
54
62
 
55
63
  radius /= 2f;
56
64
  count = 0;
@@ -60,9 +68,10 @@
60
68
  int elementsInRange = tree.GetElementsInRange(center, radius).Count();
61
69
  Assert.AreEqual(196364, elementsInRange);
62
70
  ++count;
63
- }
64
- while (timer.Elapsed < timeout);
65
- UnityEngine.Debug.Log($"| Elements In Range - Half | {(int)Math.Floor(count / timeout.TotalSeconds)} |");
71
+ } while (timer.Elapsed < timeout);
72
+ UnityEngine.Debug.Log(
73
+ $"| Elements In Range - Half | {(int)Math.Floor(count / timeout.TotalSeconds)} |"
74
+ );
66
75
 
67
76
  radius /= 2f;
68
77
  count = 0;
@@ -72,9 +81,10 @@
72
81
  int elementsInRange = tree.GetElementsInRange(center, radius).Count();
73
82
  Assert.AreEqual(49080, elementsInRange);
74
83
  ++count;
75
- }
76
- while (timer.Elapsed < timeout);
77
- UnityEngine.Debug.Log($"| Elements In Range - Quarter | {(int)Math.Floor(count / timeout.TotalSeconds)} |");
84
+ } while (timer.Elapsed < timeout);
85
+ UnityEngine.Debug.Log(
86
+ $"| Elements In Range - Quarter | {(int)Math.Floor(count / timeout.TotalSeconds)} |"
87
+ );
78
88
 
79
89
  radius = 1f;
80
90
  count = 0;
@@ -84,9 +94,10 @@
84
94
  int elementsInRange = tree.GetElementsInRange(center, radius).Count();
85
95
  Assert.AreEqual(4, elementsInRange);
86
96
  ++count;
87
- }
88
- while (timer.Elapsed < timeout);
89
- UnityEngine.Debug.Log($"| Elements In Range - 1 Range | {(int)Math.Floor(count / timeout.TotalSeconds)} |");
97
+ } while (timer.Elapsed < timeout);
98
+ UnityEngine.Debug.Log(
99
+ $"| Elements In Range - 1 Range | {(int)Math.Floor(count / timeout.TotalSeconds)} |"
100
+ );
90
101
 
91
102
  int nearestNeighborCount = 500;
92
103
  List<Vector2> nearestNeighbors = new();
@@ -97,9 +108,10 @@
97
108
  tree.GetApproximateNearestNeighbors(center, nearestNeighborCount, nearestNeighbors);
98
109
  Assert.IsTrue(nearestNeighbors.Count <= nearestNeighborCount);
99
110
  ++count;
100
- }
101
- while (timer.Elapsed < timeout);
102
- UnityEngine.Debug.Log($"| ANN - 500 | {(int)Math.Floor(count / timeout.TotalSeconds)} |");
111
+ } while (timer.Elapsed < timeout);
112
+ UnityEngine.Debug.Log(
113
+ $"| ANN - 500 | {(int)Math.Floor(count / timeout.TotalSeconds)} |"
114
+ );
103
115
 
104
116
  nearestNeighborCount = 100;
105
117
  count = 0;
@@ -109,9 +121,10 @@
109
121
  tree.GetApproximateNearestNeighbors(center, nearestNeighborCount, nearestNeighbors);
110
122
  Assert.IsTrue(nearestNeighbors.Count <= nearestNeighborCount);
111
123
  ++count;
112
- }
113
- while (timer.Elapsed < timeout);
114
- UnityEngine.Debug.Log($"| ANN - 100 | {(int)Math.Floor(count / timeout.TotalSeconds)} |");
124
+ } while (timer.Elapsed < timeout);
125
+ UnityEngine.Debug.Log(
126
+ $"| ANN - 100 | {(int)Math.Floor(count / timeout.TotalSeconds)} |"
127
+ );
115
128
 
116
129
  nearestNeighborCount = 10;
117
130
  count = 0;
@@ -121,9 +134,10 @@
121
134
  tree.GetApproximateNearestNeighbors(center, nearestNeighborCount, nearestNeighbors);
122
135
  Assert.IsTrue(nearestNeighbors.Count <= nearestNeighborCount);
123
136
  ++count;
124
- }
125
- while (timer.Elapsed < timeout);
126
- UnityEngine.Debug.Log($"| ANN - 10 | {(int)Math.Floor(count / timeout.TotalSeconds)} |");
137
+ } while (timer.Elapsed < timeout);
138
+ UnityEngine.Debug.Log(
139
+ $"| ANN - 10 | {(int)Math.Floor(count / timeout.TotalSeconds)} |"
140
+ );
127
141
 
128
142
  nearestNeighborCount = 1;
129
143
  count = 0;
@@ -133,8 +147,7 @@
133
147
  tree.GetApproximateNearestNeighbors(center, nearestNeighborCount, nearestNeighbors);
134
148
  Assert.IsTrue(nearestNeighbors.Count <= nearestNeighborCount);
135
149
  ++count;
136
- }
137
- while (timer.Elapsed < timeout);
150
+ } while (timer.Elapsed < timeout);
138
151
  UnityEngine.Debug.Log($"| ANN - 1 | {(int)Math.Floor(count / timeout.TotalSeconds)} |");
139
152
  }
140
153
  }
@@ -0,0 +1,9 @@
1
+ namespace UnityHelpers.Tests.Random
2
+ {
3
+ using Core.Random;
4
+
5
+ public sealed class DotNetRandomTests : RandomTestBase
6
+ {
7
+ protected override IRandom NewRandom() => new DotNetRandom();
8
+ }
9
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 45efbe46a3704e97b7d86757c8528a5c
3
+ timeCreated: 1732482168