com.wallstop-studios.unity-helpers 2.0.0-rc05 → 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.
- package/Runtime/Core/DataStructure/TimedCache.cs +4 -3
- package/Runtime/Core/Extension/IListExtensions.cs +2 -2
- package/Runtime/Core/Extension/RandomExtensions.cs +23 -4
- package/Runtime/Core/Extension/UnityExtensions.cs +278 -90
- package/Runtime/Core/Helper/ArrayConverter.cs +39 -0
- package/Runtime/Core/Helper/ArrayConverter.cs.meta +3 -0
- package/Runtime/Core/Helper/Helpers.cs +209 -84
- package/Runtime/Core/Helper/WallMath.cs +85 -22
- package/Runtime/Core/Random/AbstractRandom.cs +34 -20
- package/Runtime/Core/Random/DotNetRandom.cs +3 -5
- package/Runtime/Core/Random/PRNG.cs +7 -0
- package/Runtime/Core/Random/PRNG.cs.meta +3 -0
- package/Runtime/Core/Random/PcgRandom.cs +4 -6
- package/Runtime/Core/Random/RandomState.cs +31 -3
- package/Runtime/Core/Random/SquirrelRandom.cs +3 -5
- package/Runtime/Core/Random/SystemRandom.cs +20 -11
- package/Runtime/Core/Random/ThreadLocalRandom.cs +2 -1
- package/Runtime/Core/Random/UnityRandom.cs +2 -4
- package/Runtime/Core/Random/WyRandom.cs +2 -4
- package/Runtime/Core/Random/XorShiftRandom.cs +3 -5
- package/Runtime/Core/Serialization/Serializer.cs +36 -14
- package/Runtime/Utils/CircleLineRenderer.cs +17 -5
- package/Tests/Runtime/DataStructures/SpatialTreeTests.cs +34 -10
- package/Tests/Runtime/Helper/ArrayConverterTests.cs +19 -0
- package/Tests/Runtime/Helper/ArrayConverterTests.cs.meta +3 -0
- package/Tests/Runtime/Helper/WallMathTests.cs +221 -0
- package/Tests/Runtime/Helper/WallMathTests.cs.meta +3 -0
- package/Tests/Runtime/Helper.meta +3 -0
- package/Tests/Runtime/Performance/SpatialTreePerformanceTest.cs +47 -34
- package/Tests/Runtime/Random/RandomTestBase.cs +23 -3
- package/Tests/Runtime/Serialization/JsonSerializationTest.cs +24 -11
- package/Tests/Runtime/Utils/SpriteRendererMetadataTests.cs +21 -17
- package/package.json +1 -1
|
@@ -3,11 +3,14 @@
|
|
|
3
3
|
using System;
|
|
4
4
|
using System.Collections.Generic;
|
|
5
5
|
using System.Linq;
|
|
6
|
+
using Core.Extension;
|
|
7
|
+
using Core.Serialization;
|
|
6
8
|
using NUnit.Framework;
|
|
7
9
|
using UnityHelpers.Core.Random;
|
|
8
10
|
|
|
9
11
|
public abstract class RandomTestBase
|
|
10
12
|
{
|
|
13
|
+
private const int NumGeneratorChecks = 1_000;
|
|
11
14
|
private const int SampleCount = 12_500_000;
|
|
12
15
|
|
|
13
16
|
private readonly int[] _samples = new int[1_000];
|
|
@@ -96,14 +99,13 @@
|
|
|
96
99
|
[Test]
|
|
97
100
|
public void Copy()
|
|
98
101
|
{
|
|
99
|
-
const int numGeneratorChecks = 1_000;
|
|
100
102
|
IRandom random1 = NewRandom();
|
|
101
103
|
IRandom random2 = random1.Copy();
|
|
102
104
|
Assert.AreEqual(random1.InternalState, random2.InternalState);
|
|
103
105
|
// UnityRandom has shared state, the below test is not possible for it. We did all we could.
|
|
104
106
|
if (NewRandom() is not UnityRandom)
|
|
105
107
|
{
|
|
106
|
-
for (int i = 0; i <
|
|
108
|
+
for (int i = 0; i < NumGeneratorChecks; ++i)
|
|
107
109
|
{
|
|
108
110
|
Assert.AreEqual(random1.Next(), random2.Next());
|
|
109
111
|
Assert.AreEqual(random1.InternalState, random2.InternalState);
|
|
@@ -115,7 +117,7 @@
|
|
|
115
117
|
Assert.AreEqual(random1.InternalState, random3.InternalState);
|
|
116
118
|
if (NewRandom() is not UnityRandom)
|
|
117
119
|
{
|
|
118
|
-
for (int i = 0; i <
|
|
120
|
+
for (int i = 0; i < NumGeneratorChecks; ++i)
|
|
119
121
|
{
|
|
120
122
|
Assert.AreEqual(random1.Next(), random3.Next());
|
|
121
123
|
Assert.AreEqual(random1.InternalState, random3.InternalState);
|
|
@@ -123,6 +125,24 @@
|
|
|
123
125
|
}
|
|
124
126
|
}
|
|
125
127
|
|
|
128
|
+
[Test]
|
|
129
|
+
public void Json()
|
|
130
|
+
{
|
|
131
|
+
IRandom random = NewRandom();
|
|
132
|
+
string json = random.ToJson();
|
|
133
|
+
IRandom deserialized = Serializer.JsonDeserialize<IRandom>(json, random.GetType());
|
|
134
|
+
Assert.AreEqual(random.InternalState, deserialized.InternalState);
|
|
135
|
+
|
|
136
|
+
if (NewRandom() is not UnityRandom)
|
|
137
|
+
{
|
|
138
|
+
for (int i = 0; i < NumGeneratorChecks; ++i)
|
|
139
|
+
{
|
|
140
|
+
Assert.AreEqual(random.Next(), deserialized.Next());
|
|
141
|
+
Assert.AreEqual(random.InternalState, deserialized.InternalState);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
126
146
|
private void TestAndVerify(Func<IRandom, int> sample, int? maxLength = null)
|
|
127
147
|
{
|
|
128
148
|
IRandom random = NewRandom();
|
|
@@ -29,12 +29,12 @@
|
|
|
29
29
|
[Test]
|
|
30
30
|
public void SerializationWorks()
|
|
31
31
|
{
|
|
32
|
-
IRandom random =
|
|
32
|
+
IRandom random = PRNG.Instance;
|
|
33
33
|
TestDataObject input = new()
|
|
34
34
|
{
|
|
35
35
|
field = Guid.NewGuid().ToString(),
|
|
36
36
|
Property = random.Next(),
|
|
37
|
-
NamedProperty = random.NextFloat()
|
|
37
|
+
NamedProperty = random.NextFloat(),
|
|
38
38
|
};
|
|
39
39
|
|
|
40
40
|
int dictionaryProperties = random.Next(4, 10);
|
|
@@ -51,21 +51,34 @@
|
|
|
51
51
|
|
|
52
52
|
string json = input.ToJson();
|
|
53
53
|
Assert.IsTrue(
|
|
54
|
-
json.Contains("DifferentPropertyName"),
|
|
54
|
+
json.Contains("DifferentPropertyName"),
|
|
55
|
+
$"DifferentPropertyName failed to serialize! JSON: {json}"
|
|
56
|
+
);
|
|
55
57
|
|
|
56
58
|
TestDataObject deserialized = Serializer.JsonDeserialize<TestDataObject>(json);
|
|
57
|
-
Assert.AreEqual(input.field, deserialized.field, $"Unexpected {nameof(deserialized.field)}! JSON: {json}");
|
|
58
59
|
Assert.AreEqual(
|
|
59
|
-
input.
|
|
60
|
+
input.field,
|
|
61
|
+
deserialized.field,
|
|
62
|
+
$"Unexpected {nameof(deserialized.field)}! JSON: {json}"
|
|
63
|
+
);
|
|
60
64
|
Assert.AreEqual(
|
|
61
|
-
input.
|
|
62
|
-
|
|
63
|
-
|
|
65
|
+
input.Property,
|
|
66
|
+
deserialized.Property,
|
|
67
|
+
$"Unexpected {nameof(deserialized.Property)}! JSON: {json}"
|
|
68
|
+
);
|
|
69
|
+
Assert.AreEqual(
|
|
70
|
+
input.NamedProperty,
|
|
71
|
+
deserialized.NamedProperty,
|
|
72
|
+
$"Unexpected {nameof(deserialized.NamedProperty)}! JSON: {json}"
|
|
73
|
+
);
|
|
74
|
+
Assert.IsTrue(
|
|
64
75
|
input.DictionaryProperty.ContentEquals(deserialized.DictionaryProperty),
|
|
65
|
-
$"Unexpected {nameof(deserialized.DictionaryProperty)}! JSON: {json}"
|
|
76
|
+
$"Unexpected {nameof(deserialized.DictionaryProperty)}! JSON: {json}"
|
|
77
|
+
);
|
|
66
78
|
Assert.IsTrue(
|
|
67
79
|
input.ListProperty.SequenceEqual(deserialized.ListProperty),
|
|
68
|
-
$"Unexpected {nameof(deserialized.ListProperty)}! JSON: {json}"
|
|
80
|
+
$"Unexpected {nameof(deserialized.ListProperty)}! JSON: {json}"
|
|
81
|
+
);
|
|
69
82
|
}
|
|
70
83
|
}
|
|
71
|
-
}
|
|
84
|
+
}
|
|
@@ -32,7 +32,11 @@
|
|
|
32
32
|
|
|
33
33
|
private SpriteRendererMetadata CreateMetadata()
|
|
34
34
|
{
|
|
35
|
-
GameObject go = new(
|
|
35
|
+
GameObject go = new(
|
|
36
|
+
"TestSpriteRendererMetadata",
|
|
37
|
+
typeof(SpriteRenderer),
|
|
38
|
+
typeof(SpriteRendererMetadata)
|
|
39
|
+
);
|
|
36
40
|
_spawned.Add(go);
|
|
37
41
|
return go.GetComponent<SpriteRendererMetadata>();
|
|
38
42
|
}
|
|
@@ -44,8 +48,13 @@
|
|
|
44
48
|
|
|
45
49
|
private Color CreateColor()
|
|
46
50
|
{
|
|
47
|
-
IRandom random =
|
|
48
|
-
Color color = new(
|
|
51
|
+
IRandom random = PRNG.Instance;
|
|
52
|
+
Color color = new(
|
|
53
|
+
random.NextFloat(),
|
|
54
|
+
random.NextFloat(),
|
|
55
|
+
random.NextFloat(),
|
|
56
|
+
random.NextFloat()
|
|
57
|
+
);
|
|
49
58
|
return color;
|
|
50
59
|
}
|
|
51
60
|
|
|
@@ -74,9 +83,8 @@
|
|
|
74
83
|
Material originalMaterial = metadata.OriginalMaterial;
|
|
75
84
|
do
|
|
76
85
|
{
|
|
77
|
-
newColor.r =
|
|
78
|
-
}
|
|
79
|
-
while (newColor == originalColor);
|
|
86
|
+
newColor.r = PRNG.Instance.NextFloat();
|
|
87
|
+
} while (newColor == originalColor);
|
|
80
88
|
|
|
81
89
|
SpriteRendererMetadata second = CreateMetadata();
|
|
82
90
|
SpriteRenderer spriteRenderer = metadata.GetComponent<SpriteRenderer>();
|
|
@@ -91,9 +99,8 @@
|
|
|
91
99
|
Color updatedColor = newColor;
|
|
92
100
|
do
|
|
93
101
|
{
|
|
94
|
-
updatedColor.g =
|
|
95
|
-
}
|
|
96
|
-
while (updatedColor == newColor);
|
|
102
|
+
updatedColor.g = PRNG.Instance.NextFloat();
|
|
103
|
+
} while (updatedColor == newColor);
|
|
97
104
|
|
|
98
105
|
metadata.PushColor(spriteRenderer, updatedColor);
|
|
99
106
|
Assert.AreEqual(spriteRenderer.color, metadata.CurrentColor);
|
|
@@ -108,9 +115,8 @@
|
|
|
108
115
|
Color latestColor = updatedColor;
|
|
109
116
|
do
|
|
110
117
|
{
|
|
111
|
-
latestColor.b =
|
|
112
|
-
}
|
|
113
|
-
while (latestColor == updatedColor);
|
|
118
|
+
latestColor.b = PRNG.Instance.NextFloat();
|
|
119
|
+
} while (latestColor == updatedColor);
|
|
114
120
|
|
|
115
121
|
metadata.PushColor(second, latestColor);
|
|
116
122
|
Assert.AreEqual(spriteRenderer.color, metadata.CurrentColor);
|
|
@@ -120,9 +126,8 @@
|
|
|
120
126
|
Assert.IsFalse(metadata.Colors.Contains(newColor));
|
|
121
127
|
do
|
|
122
128
|
{
|
|
123
|
-
newColor.a =
|
|
124
|
-
}
|
|
125
|
-
while (newColor == latestColor);
|
|
129
|
+
newColor.a = PRNG.Instance.NextFloat();
|
|
130
|
+
} while (newColor == latestColor);
|
|
126
131
|
|
|
127
132
|
metadata.PushColor(second, newColor);
|
|
128
133
|
Assert.AreEqual(spriteRenderer.color, metadata.CurrentColor);
|
|
@@ -189,7 +194,6 @@
|
|
|
189
194
|
yield break;
|
|
190
195
|
}
|
|
191
196
|
|
|
192
|
-
|
|
193
197
|
[UnityTest]
|
|
194
198
|
public IEnumerator PopColorIdempotent()
|
|
195
199
|
{
|
|
@@ -392,4 +396,4 @@
|
|
|
392
396
|
yield break;
|
|
393
397
|
}
|
|
394
398
|
}
|
|
395
|
-
}
|
|
399
|
+
}
|