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

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.
@@ -3,6 +3,7 @@
3
3
  using System;
4
4
  using System.Collections.Generic;
5
5
  using System.Linq;
6
+ using System.Runtime.CompilerServices;
6
7
  using Core.Extension;
7
8
  using Core.Serialization;
8
9
  using NUnit.Framework;
@@ -11,7 +12,7 @@
11
12
  public abstract class RandomTestBase
12
13
  {
13
14
  private const int NumGeneratorChecks = 1_000;
14
- private const int SampleCount = 12_500_000;
15
+ private const int SampleCount = 12_750_000;
15
16
 
16
17
  private readonly int[] _samples = new int[1_000];
17
18
 
@@ -30,24 +31,119 @@
30
31
  }
31
32
 
32
33
  [Test]
34
+ [Parallelizable]
35
+ public void Bool()
36
+ {
37
+ TestAndVerify(random => Convert.ToInt32(random.NextBool()), maxLength: 2);
38
+ }
39
+
40
+ [Test]
41
+ [Parallelizable]
33
42
  public void Int()
34
43
  {
35
44
  TestAndVerify(random => random.Next(0, _samples.Length));
36
45
  }
37
46
 
38
47
  [Test]
48
+ [Parallelizable]
49
+ public void IntRange()
50
+ {
51
+ TestAndVerify(random => random.Next(_samples.Length));
52
+ }
53
+
54
+ [Test]
55
+ [Parallelizable]
56
+ public void IntDistribution()
57
+ {
58
+ TestAndVerify(random =>
59
+ (int)(random.Next() / ((1.0 * int.MaxValue) / _samples.Length))
60
+ );
61
+ }
62
+
63
+ [Test]
64
+ [Parallelizable]
65
+ public void IntMaxRange()
66
+ {
67
+ TestAndVerify(random =>
68
+ (int)(
69
+ (random.Next(int.MinValue, int.MaxValue) + (-1.0 * int.MinValue))
70
+ / (1.0 * int.MaxValue - int.MinValue)
71
+ * _samples.Length
72
+ )
73
+ );
74
+ }
75
+
76
+ [Test]
77
+ [Parallelizable]
39
78
  public void Uint()
40
79
  {
41
80
  TestAndVerify(random => (int)random.NextUint(0, (uint)_samples.Length));
42
81
  }
43
82
 
44
83
  [Test]
84
+ [Parallelizable]
85
+ public void UintRange()
86
+ {
87
+ TestAndVerify(random => (int)random.NextUint((uint)_samples.Length));
88
+ }
89
+
90
+ [Test]
91
+ [Parallelizable]
92
+ public void UintDistribution()
93
+ {
94
+ TestAndVerify(random =>
95
+ (int)(random.NextUint() / ((1.0 * uint.MaxValue) / _samples.Length))
96
+ );
97
+ }
98
+
99
+ [Test]
100
+ [Parallelizable]
101
+ public void UintMaxRange()
102
+ {
103
+ TestAndVerify(random =>
104
+ (int)(
105
+ (random.NextUint(uint.MinValue, uint.MaxValue) + (1.0 * uint.MinValue))
106
+ / (1.0 * uint.MaxValue)
107
+ * _samples.Length
108
+ )
109
+ );
110
+ }
111
+
112
+ [Test]
113
+ [Parallelizable]
45
114
  public void Short()
46
115
  {
47
- TestAndVerify(random => random.NextShort(0, (short)_samples.Length));
116
+ TestAndVerify(
117
+ random => random.NextShort(0, (short)_samples.Length),
118
+ maxLength: (short.MaxValue - short.MinValue)
119
+ );
120
+ }
121
+
122
+ [Test]
123
+ [Parallelizable]
124
+ public void ShortRange()
125
+ {
126
+ TestAndVerify(
127
+ random => random.NextShort((short)_samples.Length),
128
+ maxLength: (short.MaxValue - short.MinValue)
129
+ );
130
+ }
131
+
132
+ [Test]
133
+ [Parallelizable]
134
+ public void ShortMaxRange()
135
+ {
136
+ TestAndVerify(random =>
137
+ (int)(
138
+ (random.NextShort(short.MinValue, short.MaxValue) + (-1.0 * short.MinValue))
139
+ / (1.0 * short.MaxValue - short.MinValue)
140
+ * _samples.Length
141
+ )
142
+ );
48
143
  }
49
144
 
50
145
  [Test]
146
+ [Parallelizable]
51
147
  public void Byte()
52
148
  {
53
149
  TestAndVerify(
@@ -61,6 +157,40 @@
61
157
  }
62
158
 
63
159
  [Test]
160
+ [Parallelizable]
161
+ public void ByteRange()
162
+ {
163
+ TestAndVerify(
164
+ random =>
165
+ random.NextByte(
166
+ (byte)(_samples.Length < byte.MaxValue ? _samples.Length : byte.MaxValue)
167
+ ),
168
+ byte.MaxValue
169
+ );
170
+ }
171
+
172
+ [Test]
173
+ [Parallelizable]
174
+ public void ByteMaxRange()
175
+ {
176
+ int sampleCount = Math.Min((byte.MaxValue - byte.MinValue), _samples.Length);
177
+ TestAndVerify(
178
+ random =>
179
+ Math.Clamp(
180
+ (int)(
181
+ (random.NextByte(byte.MinValue, byte.MaxValue) + (-1.0 * byte.MinValue))
182
+ / (1.0 * byte.MaxValue - byte.MinValue)
183
+ * sampleCount
184
+ ),
185
+ 0,
186
+ sampleCount - 1
187
+ ),
188
+ maxLength: sampleCount
189
+ );
190
+ }
191
+
192
+ [Test]
193
+ [Parallelizable]
64
194
  public void Float()
65
195
  {
66
196
  TestAndVerify(random =>
@@ -73,6 +203,44 @@
73
203
  }
74
204
 
75
205
  [Test]
206
+ [Parallelizable]
207
+ public void FloatRange()
208
+ {
209
+ TestAndVerify(random =>
210
+ Math.Clamp(
211
+ (int)Math.Floor(random.NextFloat(_samples.Length)),
212
+ 0,
213
+ _samples.Length - 1
214
+ )
215
+ );
216
+ }
217
+
218
+ [Test]
219
+ [Parallelizable]
220
+ public void FloatDistribution()
221
+ {
222
+ TestAndVerify(random => (int)(random.NextFloat() * _samples.Length));
223
+ }
224
+
225
+ [Test]
226
+ [Parallelizable]
227
+ public void FloatMaxRange()
228
+ {
229
+ TestAndVerify(random =>
230
+ Math.Clamp(
231
+ (int)(
232
+ (random.NextFloat(float.MinValue, float.MaxValue) + (-1.0 * float.MinValue))
233
+ / (1.0 * float.MaxValue - float.MinValue)
234
+ * _samples.Length
235
+ ),
236
+ 0,
237
+ _samples.Length - 1
238
+ )
239
+ );
240
+ }
241
+
242
+ [Test]
243
+ [Parallelizable]
76
244
  public void Double()
77
245
  {
78
246
  TestAndVerify(random =>
@@ -85,18 +253,94 @@
85
253
  }
86
254
 
87
255
  [Test]
256
+ [Parallelizable]
257
+ public void DoubleRange()
258
+ {
259
+ TestAndVerify(random =>
260
+ Math.Clamp(
261
+ (int)Math.Floor(random.NextDouble(_samples.Length)),
262
+ 0,
263
+ _samples.Length - 1
264
+ )
265
+ );
266
+ }
267
+
268
+ [Test]
269
+ [Parallelizable]
270
+ public void DoubleDistribution()
271
+ {
272
+ TestAndVerify(random => (int)(random.NextDouble() * _samples.Length));
273
+ }
274
+
275
+ [Test]
276
+ [Parallelizable]
277
+ public void DoubleMaxRange()
278
+ {
279
+ IRandom random = NewRandom();
280
+ for (int i = 0; i < SampleCount; ++i)
281
+ {
282
+ double value = random.NextDouble(double.MinValue, double.MaxValue);
283
+ Assert.IsFalse(double.IsNaN(value));
284
+ Assert.IsFalse(double.IsInfinity(value));
285
+ }
286
+ }
287
+
288
+ [Test]
289
+ [Parallelizable]
88
290
  public void Long()
89
291
  {
90
292
  TestAndVerify(random => (int)random.NextLong(0, _samples.Length));
91
293
  }
92
294
 
93
295
  [Test]
296
+ [Parallelizable]
297
+ public void LongRange()
298
+ {
299
+ TestAndVerify(random => (int)random.NextLong(_samples.Length));
300
+ }
301
+
302
+ [Test]
303
+ [Parallelizable]
304
+ public void LongMaxRange()
305
+ {
306
+ TestAndVerify(random =>
307
+ (int)(
308
+ (random.NextLong(long.MinValue, long.MaxValue) + (-1.0 * long.MinValue))
309
+ / (1.0 * long.MaxValue - long.MinValue)
310
+ * _samples.Length
311
+ )
312
+ );
313
+ }
314
+
315
+ [Test]
316
+ [Parallelizable]
94
317
  public void Ulong()
95
318
  {
96
319
  TestAndVerify(random => (int)random.NextUlong(0, (ulong)_samples.Length));
97
320
  }
98
321
 
99
322
  [Test]
323
+ [Parallelizable]
324
+ public void UlongRange()
325
+ {
326
+ TestAndVerify(random => (int)random.NextUlong((ulong)_samples.Length));
327
+ }
328
+
329
+ [Test]
330
+ [Parallelizable]
331
+ public void UlongMaxRange()
332
+ {
333
+ TestAndVerify(random =>
334
+ (int)(
335
+ (random.NextUlong(ulong.MinValue, ulong.MaxValue) + (-1.0 * ulong.MinValue))
336
+ / (1.0 * ulong.MaxValue - ulong.MinValue)
337
+ * _samples.Length
338
+ )
339
+ );
340
+ }
341
+
342
+ [Test]
343
+ [Parallelizable]
100
344
  public void Copy()
101
345
  {
102
346
  IRandom random1 = NewRandom();
@@ -126,6 +370,7 @@
126
370
  }
127
371
 
128
372
  [Test]
373
+ [Parallelizable]
129
374
  public void Json()
130
375
  {
131
376
  IRandom random = NewRandom();
@@ -143,13 +388,23 @@
143
388
  }
144
389
  }
145
390
 
146
- private void TestAndVerify(Func<IRandom, int> sample, int? maxLength = null)
391
+ protected virtual double DeviationFor(string caller)
392
+ {
393
+ return 0.0625;
394
+ }
395
+
396
+ private void TestAndVerify(
397
+ Func<IRandom, int> sample,
398
+ int? maxLength = null,
399
+ [CallerMemberName] string caller = ""
400
+ )
147
401
  {
148
402
  IRandom random = NewRandom();
403
+ int sampleLength = _samples.Length;
149
404
  for (int i = 0; i < SampleCount; ++i)
150
405
  {
151
406
  int index = sample(random);
152
- if (index < 0 || _samples.Length <= index)
407
+ if (index < 0 || sampleLength <= index)
153
408
  {
154
409
  Assert.Fail("Index {0} out of range", index);
155
410
  }
@@ -159,9 +414,9 @@
159
414
  }
160
415
  }
161
416
 
162
- int sampleLength = Math.Min(_samples.Length, maxLength ?? _samples.Length);
417
+ sampleLength = Math.Min(sampleLength, maxLength ?? sampleLength);
163
418
  double average = SampleCount * 1.0 / sampleLength;
164
- double deviationAllowed = average * 0.05;
419
+ double deviationAllowed = average * DeviationFor(caller);
165
420
  List<int> zeroCountIndexes = new();
166
421
  List<int> outsideRange = new();
167
422
  for (int i = 0; i < sampleLength; i++)
@@ -5,5 +5,10 @@
5
5
  public sealed class SquirrelRandomTests : RandomTestBase
6
6
  {
7
7
  protected override IRandom NewRandom() => new SquirrelRandom();
8
+
9
+ protected override double DeviationFor(string caller)
10
+ {
11
+ return 0.075;
12
+ }
8
13
  }
9
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.wallstop-studios.unity-helpers",
3
- "version": "2.0.0-rc06",
3
+ "version": "2.0.0-rc07",
4
4
  "displayName": "Unity Helpers",
5
5
  "description": "Various Unity Helper Library",
6
6
  "dependencies": {},