com.wallstop-studios.unity-helpers 2.0.0-rc22 → 2.0.0-rc23

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 (58) hide show
  1. package/Editor/AnimationCopier.cs +39 -10
  2. package/Editor/AnimationCreator.cs +68 -24
  3. package/Editor/AnimationEventEditor.cs +206 -79
  4. package/Editor/AnimatorControllerCopier.cs +42 -14
  5. package/Editor/CustomEditors/MatchColliderToSpriteEditor.cs +5 -2
  6. package/Editor/PrefabCheckWizard.cs +37 -14
  7. package/Editor/SpriteSettingsApplier.cs +23 -9
  8. package/Editor/TextureResizerWizard.cs +37 -14
  9. package/Editor/TextureSettingsApplier.cs +23 -10
  10. package/Editor/Utils/EditorUtilities.cs +4 -2
  11. package/Editor/Utils/ReadOnlyPropertyDrawer.cs +2 -4
  12. package/Runtime/Core/Attributes/AnimationEventAttribute.cs +52 -23
  13. package/Runtime/Core/Attributes/KSerializableAttribute.cs +2 -6
  14. package/Runtime/Core/Attributes/NotNullAttribute.cs +3 -4
  15. package/Runtime/Core/Attributes/ReadOnlyAttribute.cs +1 -3
  16. package/Runtime/Core/Attributes/ValidateAssignmentAttribute.cs +12 -6
  17. package/Runtime/Core/DataStructure/Adapters/FastVector2Int.cs +1 -0
  18. package/Runtime/Core/DataStructure/Adapters/FastVector3Int.cs +13 -8
  19. package/Runtime/Core/DataStructure/Adapters/KGuid.cs +24 -7
  20. package/Runtime/Core/DataStructure/Adapters/KVector2.cs +1 -0
  21. package/Runtime/Core/DataStructure/Circle.cs +1 -1
  22. package/Runtime/Core/DataStructure/CyclicBuffer.cs +42 -44
  23. package/Runtime/Core/DataStructure/ISpatialTree.cs +22 -20
  24. package/Runtime/Core/Extension/CircleExtensions.cs +2 -2
  25. package/Runtime/Core/Extension/DictionaryExtensions.cs +62 -17
  26. package/Runtime/Core/Extension/StringExtensions.cs +30 -10
  27. package/Runtime/Core/Helper/Enumerables.cs +1 -1
  28. package/Runtime/Core/Helper/LifetimeHelpers.cs +2 -1
  29. package/Runtime/Core/Math/Line.cs +1 -1
  30. package/Runtime/Core/Math/Parabola.cs +4 -2
  31. package/Runtime/Core/Math/PointPolygonCheck.cs +12 -5
  32. package/Runtime/Core/Math/Range.cs +9 -8
  33. package/Runtime/Core/Math/XXHash.cs +22 -20
  34. package/Runtime/Core/Model/Direction.cs +26 -9
  35. package/Runtime/Core/OneOf/FastOneOf.cs +11 -4
  36. package/Runtime/Core/OneOf/None.cs +1 -3
  37. package/Runtime/Core/Serialization/JsonConverters/Vector2Converter.cs +13 -4
  38. package/Runtime/Core/Serialization/JsonConverters/Vector3Converter.cs +15 -5
  39. package/Runtime/Core/Threading/SingleThreadedThreadPool.cs +2 -3
  40. package/Runtime/Utils/AnimationEventEqualityComparer.cs +22 -10
  41. package/Runtime/Utils/AnimatorEnumStateMachine.cs +3 -4
  42. package/Runtime/Utils/Buffers.cs +1 -1
  43. package/Runtime/Utils/CenterPointOffset.cs +1 -2
  44. package/Runtime/Utils/CoroutineHandler.cs +1 -1
  45. package/Runtime/Utils/Oscillator.cs +4 -2
  46. package/Runtime/Utils/SetTextureImportData.cs +1 -1
  47. package/Runtime/Utils/SpriteRendererSyncer.cs +5 -3
  48. package/Runtime/Utils/TextureScale.cs +21 -6
  49. package/Tests/Runtime/DataStructures/BalancedKDTreeTests.cs +1 -1
  50. package/Tests/Runtime/DataStructures/CyclicBufferTests.cs +101 -0
  51. package/Tests/Runtime/DataStructures/CyclicBufferTests.cs.meta +3 -0
  52. package/Tests/Runtime/DataStructures/QuadTreeTests.cs +1 -1
  53. package/Tests/Runtime/DataStructures/UnbalancedKDTreeTests.cs +1 -1
  54. package/Tests/Runtime/Extensions/DictionaryExtensionTests.cs +41 -21
  55. package/Tests/Runtime/Extensions/StringExtensionTests.cs +1 -1
  56. package/Tests/Runtime/Performance/QuadTreePerformanceTests.cs +1 -1
  57. package/Tests/Runtime/Performance/UnbalancedKDTreeTests.cs +1 -1
  58. package/package.json +3 -2
@@ -18,7 +18,10 @@
18
18
  private const uint k_Prime32v5 = 374761393u;
19
19
 
20
20
  public static uint Hash32(string text) => Hash32(text, Encoding.UTF8);
21
- public static uint Hash32(string text, Encoding encoding) => Hash32(encoding.GetBytes(text));
21
+
22
+ public static uint Hash32(string text, Encoding encoding) =>
23
+ Hash32(encoding.GetBytes(text));
24
+
22
25
  public static uint Hash32(byte[] buffer)
23
26
  {
24
27
  unsafe
@@ -71,7 +74,10 @@
71
74
  }
72
75
 
73
76
  public static ulong Hash64(string text) => Hash64(text, Encoding.UTF8);
74
- public static ulong Hash64(string text, Encoding encoding) => Hash64(encoding.GetBytes(text));
77
+
78
+ public static ulong Hash64(string text, Encoding encoding) =>
79
+ Hash64(encoding.GetBytes(text));
80
+
75
81
  public static ulong Hash64(byte[] buffer)
76
82
  {
77
83
  unsafe
@@ -120,7 +126,6 @@
120
126
  acc += (ulong)len;
121
127
  acc = processRemaining64(pInput, acc, remainingLen);
122
128
 
123
-
124
129
  return avalanche64(acc);
125
130
  }
126
131
 
@@ -130,17 +135,19 @@
130
135
  ref ulong acc1,
131
136
  ref ulong acc2,
132
137
  ref ulong acc3,
133
- ref ulong acc4)
138
+ ref ulong acc4
139
+ )
134
140
  {
135
141
  processLane64(ref acc1, ref pInput);
136
142
  processLane64(ref acc2, ref pInput);
137
143
  processLane64(ref acc3, ref pInput);
138
144
  processLane64(ref acc4, ref pInput);
139
145
 
140
- ulong acc = Bits.RotateLeft(acc1, 1)
141
- + Bits.RotateLeft(acc2, 7)
142
- + Bits.RotateLeft(acc3, 12)
143
- + Bits.RotateLeft(acc4, 18);
146
+ ulong acc =
147
+ Bits.RotateLeft(acc1, 1)
148
+ + Bits.RotateLeft(acc2, 7)
149
+ + Bits.RotateLeft(acc3, 12)
150
+ + Bits.RotateLeft(acc4, 18);
144
151
 
145
152
  mergeAccumulator64(ref acc, acc1);
146
153
  mergeAccumulator64(ref acc, acc2);
@@ -158,10 +165,7 @@
158
165
  }
159
166
 
160
167
  [MethodImpl(MethodImplOptions.AggressiveInlining)]
161
- private static unsafe ulong processRemaining64(
162
- byte* pInput,
163
- ulong acc,
164
- int remainingLen)
168
+ private static unsafe ulong processRemaining64(byte* pInput, ulong acc, int remainingLen)
165
169
  {
166
170
  for (ulong lane; remainingLen >= 8; remainingLen -= 8, pInput += 8)
167
171
  {
@@ -223,7 +227,8 @@
223
227
  ref uint acc1,
224
228
  ref uint acc2,
225
229
  ref uint acc3,
226
- ref uint acc4)
230
+ ref uint acc4
231
+ )
227
232
  {
228
233
  processLane32(ref pInput, ref acc1);
229
234
  processLane32(ref pInput, ref acc2);
@@ -231,9 +236,9 @@
231
236
  processLane32(ref pInput, ref acc4);
232
237
 
233
238
  return Bits.RotateLeft(acc1, 1)
234
- + Bits.RotateLeft(acc2, 7)
235
- + Bits.RotateLeft(acc3, 12)
236
- + Bits.RotateLeft(acc4, 18);
239
+ + Bits.RotateLeft(acc2, 7)
240
+ + Bits.RotateLeft(acc3, 12)
241
+ + Bits.RotateLeft(acc4, 18);
237
242
  }
238
243
 
239
244
  [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -245,10 +250,7 @@
245
250
  }
246
251
 
247
252
  [MethodImpl(MethodImplOptions.AggressiveInlining)]
248
- private static unsafe uint processRemaining32(
249
- byte* pInput,
250
- uint acc,
251
- int remainingLen)
253
+ private static unsafe uint processRemaining32(byte* pInput, uint acc, int remainingLen)
252
254
  {
253
255
  for (uint lane; remainingLen >= 4; remainingLen -= 4, pInput += 4)
254
256
  {
@@ -7,15 +7,32 @@
7
7
  [Serializable]
8
8
  public enum Direction : short
9
9
  {
10
- [EnumMember] None = 0,
11
- [EnumMember] North = 1,
12
- [EnumMember] NorthEast = 2,
13
- [EnumMember] East = 4,
14
- [EnumMember] SouthEast = 8,
15
- [EnumMember] South = 16,
16
- [EnumMember] SouthWest = 32,
17
- [EnumMember] West = 64,
18
- [EnumMember] NorthWest = 128
10
+ [EnumMember]
11
+ None = 0,
12
+
13
+ [EnumMember]
14
+ North = 1,
15
+
16
+ [EnumMember]
17
+ NorthEast = 2,
18
+
19
+ [EnumMember]
20
+ East = 4,
21
+
22
+ [EnumMember]
23
+ SouthEast = 8,
24
+
25
+ [EnumMember]
26
+ South = 16,
27
+
28
+ [EnumMember]
29
+ SouthWest = 32,
30
+
31
+ [EnumMember]
32
+ West = 64,
33
+
34
+ [EnumMember]
35
+ NorthWest = 128,
19
36
  }
20
37
 
21
38
  public static class DirectionConstants
@@ -44,14 +44,15 @@
44
44
  {
45
45
  if (_index != 0)
46
46
  {
47
- throw new InvalidOperationException($"Cannot return as T0 as result is T{_index}");
47
+ throw new InvalidOperationException(
48
+ $"Cannot return as T0 as result is T{_index}"
49
+ );
48
50
  }
49
51
 
50
52
  return _value0;
51
53
  }
52
54
  }
53
55
 
54
-
55
56
  public T1 AsT1
56
57
  {
57
58
  [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -59,7 +60,9 @@
59
60
  {
60
61
  if (_index != 1)
61
62
  {
62
- throw new InvalidOperationException($"Cannot return as T1 as result is T{_index}");
63
+ throw new InvalidOperationException(
64
+ $"Cannot return as T1 as result is T{_index}"
65
+ );
63
66
  }
64
67
 
65
68
  return _value1;
@@ -73,7 +76,9 @@
73
76
  {
74
77
  if (_index != 2)
75
78
  {
76
- throw new InvalidOperationException($"Cannot return as T2 as result is T{_index}");
79
+ throw new InvalidOperationException(
80
+ $"Cannot return as T2 as result is T{_index}"
81
+ );
77
82
  }
78
83
 
79
84
  return _value2;
@@ -89,7 +94,9 @@
89
94
  }
90
95
 
91
96
  public static implicit operator FastOneOf<T0, T1, T2>(T0 value) => new(0, value0: value);
97
+
92
98
  public static implicit operator FastOneOf<T0, T1, T2>(T1 value) => new(1, value1: value);
99
+
93
100
  public static implicit operator FastOneOf<T0, T1, T2>(T2 value) => new(2, value2: value);
94
101
 
95
102
  [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -1,6 +1,4 @@
1
1
  namespace UnityHelpers.Core.OneOf
2
2
  {
3
- public readonly struct None
4
- {
5
- }
3
+ public readonly struct None { }
6
4
  }
@@ -11,14 +11,19 @@
11
11
 
12
12
  private Vector2Converter() { }
13
13
 
14
- public override Vector2 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
14
+ public override Vector2 Read(
15
+ ref Utf8JsonReader reader,
16
+ Type typeToConvert,
17
+ JsonSerializerOptions options
18
+ )
15
19
  {
16
20
  if (reader.TokenType != JsonTokenType.StartObject)
17
21
  {
18
22
  throw new JsonException($"Invalid token type {reader.TokenType}");
19
23
  }
20
24
 
21
- float x = 0, y = 0;
25
+ float x = 0,
26
+ y = 0;
22
27
 
23
28
  while (reader.Read())
24
29
  {
@@ -54,7 +59,11 @@
54
59
  throw new JsonException("Incomplete JSON for Vector2");
55
60
  }
56
61
 
57
- public override void Write(Utf8JsonWriter writer, Vector2 value, JsonSerializerOptions options)
62
+ public override void Write(
63
+ Utf8JsonWriter writer,
64
+ Vector2 value,
65
+ JsonSerializerOptions options
66
+ )
58
67
  {
59
68
  writer.WriteStartObject();
60
69
  writer.WriteNumber("x", value.x);
@@ -62,4 +71,4 @@
62
71
  writer.WriteEndObject();
63
72
  }
64
73
  }
65
- }
74
+ }
@@ -1,9 +1,9 @@
1
1
  namespace UnityHelpers.Core.Serialization.JsonConverters
2
2
  {
3
3
  using System;
4
- using UnityEngine;
5
4
  using System.Text.Json;
6
5
  using System.Text.Json.Serialization;
6
+ using UnityEngine;
7
7
 
8
8
  public sealed class Vector3Converter : JsonConverter<Vector3>
9
9
  {
@@ -11,14 +11,20 @@
11
11
 
12
12
  private Vector3Converter() { }
13
13
 
14
- public override Vector3 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
14
+ public override Vector3 Read(
15
+ ref Utf8JsonReader reader,
16
+ Type typeToConvert,
17
+ JsonSerializerOptions options
18
+ )
15
19
  {
16
20
  if (reader.TokenType != JsonTokenType.StartObject)
17
21
  {
18
22
  throw new JsonException($"Invalid token type {reader.TokenType}");
19
23
  }
20
24
 
21
- float x = 0, y = 0, z = 0;
25
+ float x = 0,
26
+ y = 0,
27
+ z = 0;
22
28
 
23
29
  while (reader.Read())
24
30
  {
@@ -59,7 +65,11 @@
59
65
  throw new JsonException("Incomplete JSON for Vector3");
60
66
  }
61
67
 
62
- public override void Write(Utf8JsonWriter writer, Vector3 value, JsonSerializerOptions options)
68
+ public override void Write(
69
+ Utf8JsonWriter writer,
70
+ Vector3 value,
71
+ JsonSerializerOptions options
72
+ )
63
73
  {
64
74
  writer.WriteStartObject();
65
75
  writer.WriteNumber("x", value.x);
@@ -68,4 +78,4 @@
68
78
  writer.WriteEndObject();
69
79
  }
70
80
  }
71
- }
81
+ }
@@ -83,8 +83,7 @@
83
83
  do
84
84
  {
85
85
  active = Interlocked.CompareExchange(ref _active, 0, _active);
86
- }
87
- while (active != 0);
86
+ } while (active != 0);
88
87
 
89
88
  if (disposing)
90
89
  {
@@ -105,4 +104,4 @@
105
104
  _disposed = true;
106
105
  }
107
106
  }
108
- }
107
+ }
@@ -4,14 +4,13 @@
4
4
  using System.Collections.Generic;
5
5
  using UnityEngine;
6
6
 
7
- public sealed class AnimationEventEqualityComparer : EqualityComparer<AnimationEvent>, IComparer<AnimationEvent>
7
+ public sealed class AnimationEventEqualityComparer
8
+ : EqualityComparer<AnimationEvent>,
9
+ IComparer<AnimationEvent>
8
10
  {
9
11
  public static readonly AnimationEventEqualityComparer Instance = new();
10
12
 
11
- private AnimationEventEqualityComparer()
12
- {
13
-
14
- }
13
+ private AnimationEventEqualityComparer() { }
15
14
 
16
15
  // Returns a shallow copy with equatable values propagated
17
16
  public AnimationEvent Copy(AnimationEvent instance)
@@ -93,13 +92,18 @@
93
92
 
94
93
  return true;
95
94
  }
96
-
97
95
 
98
96
  public override int GetHashCode(AnimationEvent instance)
99
97
  {
100
98
  return HashCode.Combine(
101
- instance.time, instance.functionName, instance.intParameter, instance.floatParameter,
102
- instance.stringParameter, instance.objectReferenceParameter, instance.messageOptions);
99
+ instance.time,
100
+ instance.functionName,
101
+ instance.intParameter,
102
+ instance.floatParameter,
103
+ instance.stringParameter,
104
+ instance.objectReferenceParameter,
105
+ instance.messageOptions
106
+ );
103
107
  }
104
108
 
105
109
  public int Compare(AnimationEvent lhs, AnimationEvent rhs)
@@ -125,7 +129,11 @@
125
129
  return timeComparison;
126
130
  }
127
131
 
128
- int functionNameComparison = string.Compare(lhs.functionName, rhs.functionName, StringComparison.Ordinal);
132
+ int functionNameComparison = string.Compare(
133
+ lhs.functionName,
134
+ rhs.functionName,
135
+ StringComparison.Ordinal
136
+ );
129
137
  if (functionNameComparison != 0)
130
138
  {
131
139
  return functionNameComparison;
@@ -137,7 +145,11 @@
137
145
  return intParameterComparison;
138
146
  }
139
147
 
140
- int stringParameterComparison = string.Compare(lhs.stringParameter, rhs.stringParameter, StringComparison.Ordinal);
148
+ int stringParameterComparison = string.Compare(
149
+ lhs.stringParameter,
150
+ rhs.stringParameter,
151
+ StringComparison.Ordinal
152
+ );
141
153
  if (stringParameterComparison != 0)
142
154
  {
143
155
  return stringParameterComparison;
@@ -1,12 +1,12 @@
1
1
  namespace UnityHelpers.Utils
2
2
  {
3
- using Core.Extension;
4
3
  using System;
5
4
  using System.Collections.Generic;
6
5
  using System.Globalization;
7
6
  using System.Linq;
8
7
  using System.Runtime.Serialization;
9
8
  using System.Text.Json.Serialization;
9
+ using Core.Extension;
10
10
  using UnityEngine;
11
11
 
12
12
  /// <summary>
@@ -15,8 +15,7 @@
15
15
  /// <typeparam name="T">Specific Enum being managed.</typeparam>
16
16
  [DataContract]
17
17
  public sealed class AnimatorEnumStateMachine<T>
18
- where T : struct, IConvertible, IComparable,
19
- IFormattable // This is as close as we can get to saying where T : Enum (https://stackoverflow.com/questions/79126/create-generic-method-constraining-t-to-an-enum)
18
+ where T : struct, IConvertible, IComparable, IFormattable // This is as close as we can get to saying where T : Enum (https://stackoverflow.com/questions/79126/create-generic-method-constraining-t-to-an-enum)
20
19
  {
21
20
  private static readonly T[] Values = Enum.GetValues(typeof(T)).OfType<T>().ToArray();
22
21
 
@@ -86,4 +85,4 @@
86
85
  return this.ToJson();
87
86
  }
88
87
  }
89
- }
88
+ }
@@ -29,4 +29,4 @@
29
29
  public static readonly Stack<T> Stack = new();
30
30
  public static readonly LinkedList<T> LinkedList = new();
31
31
  }
32
- }
32
+ }
@@ -27,5 +27,4 @@
27
27
  }
28
28
  }
29
29
  }
30
-
31
- }
30
+ }
@@ -1,4 +1,4 @@
1
1
  namespace UnityHelpers.Utils
2
2
  {
3
3
  public sealed class CoroutineHandler : RuntimeSingleton<CoroutineHandler> { }
4
- }
4
+ }
@@ -10,7 +10,7 @@
10
10
  public float height = 1f;
11
11
 
12
12
  private Vector3 _initialLocalPosition;
13
-
13
+
14
14
  private void Awake()
15
15
  {
16
16
  _initialLocalPosition = transform.localPosition;
@@ -19,7 +19,9 @@
19
19
  private void Update()
20
20
  {
21
21
  float time = Time.time;
22
- transform.localPosition = _initialLocalPosition + new Vector3(Mathf.Cos(time * speed) * width, Mathf.Sin(time * speed) * height);
22
+ transform.localPosition =
23
+ _initialLocalPosition
24
+ + new Vector3(Mathf.Cos(time * speed) * width, Mathf.Sin(time * speed) * height);
23
25
  }
24
26
  }
25
27
  }
@@ -66,4 +66,4 @@
66
66
  # endif
67
67
  }
68
68
  }
69
- }
69
+ }
@@ -47,7 +47,8 @@
47
47
 
48
48
  private void LateUpdate()
49
49
  {
50
- SpriteRenderer localToMatch = dynamicToMatch != null ? GetDynamicSpriteRenderer() : toMatch;
50
+ SpriteRenderer localToMatch =
51
+ dynamicToMatch != null ? GetDynamicSpriteRenderer() : toMatch;
51
52
  if (localToMatch == null)
52
53
  {
53
54
  _spriteRenderer.sprite = null;
@@ -75,7 +76,8 @@
75
76
 
76
77
  if (matchOrderInLayer)
77
78
  {
78
- _spriteRenderer.sortingOrder = DynamicSortingOrderOverride ?? localToMatch.sortingOrder;
79
+ _spriteRenderer.sortingOrder =
80
+ DynamicSortingOrderOverride ?? localToMatch.sortingOrder;
79
81
  }
80
82
 
81
83
  _spriteRenderer.size = localToMatch.size;
@@ -95,4 +97,4 @@
95
97
  return _cachedSpriteRenderer;
96
98
  }
97
99
  }
98
- }
100
+ }
@@ -38,7 +38,12 @@
38
38
  ThreadedScale(tex, newWidth, newHeight, true);
39
39
  }
40
40
 
41
- private static void ThreadedScale(Texture2D tex, int newWidth, int newHeight, bool useBilinear)
41
+ private static void ThreadedScale(
42
+ Texture2D tex,
43
+ int newWidth,
44
+ int newHeight,
45
+ bool useBilinear
46
+ )
42
47
  {
43
48
  texColors = tex.GetPixels();
44
49
  newColors = new Color[newWidth * newHeight];
@@ -123,9 +128,18 @@
123
128
  int xFloor = (int)Mathf.Floor(x * ratioX);
124
129
  float xLerp = x * ratioX - xFloor;
125
130
  newColors[yw + x] = ColorLerpUnclamped(
126
- ColorLerpUnclamped(texColors[y1 + xFloor], texColors[y1 + xFloor + 1], xLerp),
127
- ColorLerpUnclamped(texColors[y2 + xFloor], texColors[y2 + xFloor + 1], xLerp),
128
- y * ratioY - yFloor);
131
+ ColorLerpUnclamped(
132
+ texColors[y1 + xFloor],
133
+ texColors[y1 + xFloor + 1],
134
+ xLerp
135
+ ),
136
+ ColorLerpUnclamped(
137
+ texColors[y2 + xFloor],
138
+ texColors[y2 + xFloor + 1],
139
+ xLerp
140
+ ),
141
+ y * ratioY - yFloor
142
+ );
129
143
  }
130
144
  }
131
145
 
@@ -158,7 +172,8 @@
158
172
  c1.r + (c2.r - c1.r) * value,
159
173
  c1.g + (c2.g - c1.g) * value,
160
174
  c1.b + (c2.b - c1.b) * value,
161
- c1.a + (c2.a - c1.a) * value);
175
+ c1.a + (c2.a - c1.a) * value
176
+ );
162
177
  }
163
178
  }
164
- }
179
+ }
@@ -1,8 +1,8 @@
1
1
  namespace UnityHelpers.Tests.DataStructures
2
2
  {
3
3
  using System.Collections.Generic;
4
- using UnityEngine;
5
4
  using Core.DataStructure;
5
+ using UnityEngine;
6
6
 
7
7
  public sealed class BalancedKDTreeTests : SpatialTreeTests<KDTree<Vector2>>
8
8
  {
@@ -0,0 +1,101 @@
1
+ namespace UnityHelpers.Tests.DataStructures
2
+ {
3
+ using System;
4
+ using System.Collections.Generic;
5
+ using System.Linq;
6
+ using Core.DataStructure;
7
+ using Core.Random;
8
+ using NUnit.Framework;
9
+
10
+ public sealed class CyclicBufferTests
11
+ {
12
+ private const int NumTries = 100;
13
+ private const int CapacityMultiplier = 3;
14
+
15
+ [Test]
16
+ public void InvalidCapacity()
17
+ {
18
+ Assert.Throws<ArgumentException>(() => new CyclicBuffer<int>(-1));
19
+ Assert.Throws<ArgumentException>(() => new CyclicBuffer<int>(int.MinValue));
20
+ for (int i = 0; i < NumTries; i++)
21
+ {
22
+ Assert.Throws<ArgumentException>(
23
+ () => new CyclicBuffer<int>(PRNG.Instance.Next(int.MinValue, -1))
24
+ );
25
+ }
26
+ }
27
+
28
+ [Test]
29
+ public void ZeroCapacityOk()
30
+ {
31
+ CyclicBuffer<int> buffer = new(0);
32
+ CollectionAssert.AreEquivalent(Array.Empty<int>(), buffer);
33
+ for (int i = 0; i < NumTries; ++i)
34
+ {
35
+ buffer.Add(PRNG.Instance.Next());
36
+ CollectionAssert.AreEquivalent(Array.Empty<int>(), buffer);
37
+ }
38
+ }
39
+
40
+ [Test]
41
+ public void IntMaxCapacityOk()
42
+ {
43
+ CyclicBuffer<int> buffer = new(int.MaxValue);
44
+ CollectionAssert.AreEquivalent(Array.Empty<int>(), buffer);
45
+ const int tries = 50;
46
+ List<int> expected = new(tries);
47
+ for (int i = 0; i < tries; ++i)
48
+ {
49
+ int value = PRNG.Instance.Next();
50
+ buffer.Add(value);
51
+ expected.Add(value);
52
+ CollectionAssert.AreEquivalent(expected, buffer);
53
+ }
54
+ }
55
+
56
+ [Test]
57
+ public void OneCapacityOk()
58
+ {
59
+ CyclicBuffer<int> buffer = new(1);
60
+ CollectionAssert.AreEquivalent(Array.Empty<int>(), buffer);
61
+ int[] expected = new int[1];
62
+ for (int i = 0; i < NumTries; ++i)
63
+ {
64
+ int value = PRNG.Instance.Next();
65
+ buffer.Add(value);
66
+ expected[0] = value;
67
+ CollectionAssert.AreEquivalent(expected, buffer);
68
+ }
69
+ }
70
+
71
+ [Test]
72
+ public void NormalAndWrappingBehavior()
73
+ {
74
+ List<int> expected = new();
75
+ for (int i = 0; i < NumTries; ++i)
76
+ {
77
+ int capacity = PRNG.Instance.Next(100, 1_000);
78
+ CyclicBuffer<int> buffer = new(capacity);
79
+ CollectionAssert.AreEquivalent(Array.Empty<int>(), buffer);
80
+ expected.Clear();
81
+ for (int j = 0; j < capacity * CapacityMultiplier; ++j)
82
+ {
83
+ int newValue = PRNG.Instance.Next();
84
+ if (capacity <= j)
85
+ {
86
+ expected[j % capacity] = newValue;
87
+ }
88
+ else
89
+ {
90
+ expected.Add(newValue);
91
+ }
92
+ buffer.Add(newValue);
93
+ Assert.IsTrue(
94
+ expected.SequenceEqual(buffer),
95
+ $"Failure at iteration {i}, j={j}, capacity={capacity}, capacityMultiplier={CapacityMultiplier}"
96
+ );
97
+ }
98
+ }
99
+ }
100
+ }
101
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: e6e1c3d02ef74aabb922aa776a8648f0
3
+ timeCreated: 1740364919
@@ -1,8 +1,8 @@
1
1
  namespace UnityHelpers.Tests.DataStructures
2
2
  {
3
3
  using System.Collections.Generic;
4
- using UnityEngine;
5
4
  using Core.DataStructure;
5
+ using UnityEngine;
6
6
 
7
7
  public sealed class QuadTreeTests : SpatialTreeTests<QuadTree<Vector2>>
8
8
  {
@@ -1,8 +1,8 @@
1
1
  namespace UnityHelpers.Tests.DataStructures
2
2
  {
3
3
  using System.Collections.Generic;
4
- using UnityEngine;
5
4
  using Core.DataStructure;
5
+ using UnityEngine;
6
6
 
7
7
  public sealed class UnbalancedKDTreeTests : SpatialTreeTests<KDTree<Vector2>>
8
8
  {