com.wallstop-studios.unity-helpers 2.0.0-rc06 → 2.0.0-rc08
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/Extension/DirectionExtensions.cs +5 -2
- package/Runtime/Core/Extension/IEnumerableExtensions.cs +19 -6
- package/Runtime/Core/Extension/RandomExtensions.cs +9 -87
- package/Runtime/Core/Extension/UnityExtensions.cs +2 -2
- package/Runtime/Core/Helper/Helpers.cs +1 -556
- package/Runtime/Core/Helper/Partials/LogHelpers.cs +13 -0
- package/Runtime/Core/Helper/Partials/LogHelpers.cs.meta +3 -0
- package/Runtime/Core/Helper/Partials/MathHelpers.cs +30 -0
- package/Runtime/Core/Helper/Partials/MathHelpers.cs.meta +3 -0
- package/Runtime/Core/Helper/Partials/ObjectHelpers.cs +388 -0
- package/Runtime/Core/Helper/Partials/ObjectHelpers.cs.meta +3 -0
- package/Runtime/Core/Helper/Partials/TransformHelpers.cs +167 -0
- package/Runtime/Core/Helper/Partials/TransformHelpers.cs.meta +3 -0
- package/Runtime/Core/Helper/Partials.meta +3 -0
- package/Runtime/Core/Random/AbstractRandom.cs +140 -154
- package/Runtime/Core/Random/IRandom.cs +26 -7
- package/Runtime/Core/Random/PerlinNoise.cs +369 -0
- package/Runtime/Core/Random/PerlinNoise.cs.meta +3 -0
- package/Runtime/Core/Random/SquirrelRandom.cs +9 -10
- package/Runtime/Core/Random/SystemRandom.cs +78 -41
- package/Tests/Runtime/Extensions/RandomExtensionTests.cs +27 -0
- package/Tests/Runtime/Extensions/RandomExtensionTests.cs.meta +3 -0
- package/Tests/Runtime/Helper/ObjectHelperTests.cs +402 -0
- package/Tests/Runtime/Helper/ObjectHelperTests.cs.meta +3 -0
- package/Tests/Runtime/Performance/RandomPerformanceTests.cs +58 -3
- package/Tests/Runtime/Random/RandomTestBase.cs +557 -6
- package/Tests/Runtime/Random/SquirrelRandomTests.cs +5 -0
- package/package.json +1 -1
|
@@ -3,15 +3,32 @@
|
|
|
3
3
|
using System;
|
|
4
4
|
using System.Collections.Generic;
|
|
5
5
|
using System.Linq;
|
|
6
|
+
using System.Runtime.CompilerServices;
|
|
7
|
+
using Core.DataStructure.Adapters;
|
|
6
8
|
using Core.Extension;
|
|
7
9
|
using Core.Serialization;
|
|
8
10
|
using NUnit.Framework;
|
|
9
11
|
using UnityHelpers.Core.Random;
|
|
10
12
|
|
|
13
|
+
public enum TestValues
|
|
14
|
+
{
|
|
15
|
+
Value0,
|
|
16
|
+
Value1,
|
|
17
|
+
Value2,
|
|
18
|
+
Value3,
|
|
19
|
+
Value4,
|
|
20
|
+
Value5,
|
|
21
|
+
Value6,
|
|
22
|
+
Value7,
|
|
23
|
+
Value8,
|
|
24
|
+
Value9,
|
|
25
|
+
}
|
|
26
|
+
|
|
11
27
|
public abstract class RandomTestBase
|
|
12
28
|
{
|
|
13
29
|
private const int NumGeneratorChecks = 1_000;
|
|
14
|
-
private const int
|
|
30
|
+
private const int NormalIterations = 1_000;
|
|
31
|
+
private const int SampleCount = 12_750_000;
|
|
15
32
|
|
|
16
33
|
private readonly int[] _samples = new int[1_000];
|
|
17
34
|
|
|
@@ -30,24 +47,143 @@
|
|
|
30
47
|
}
|
|
31
48
|
|
|
32
49
|
[Test]
|
|
50
|
+
[Parallelizable]
|
|
51
|
+
public void Bool()
|
|
52
|
+
{
|
|
53
|
+
TestAndVerify(
|
|
54
|
+
random => Math.Min(_samples.Length - 1, Convert.ToInt32(random.NextBool())),
|
|
55
|
+
maxLength: Math.Min(2, _samples.Length)
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
[Test]
|
|
60
|
+
[Parallelizable]
|
|
33
61
|
public void Int()
|
|
34
62
|
{
|
|
35
63
|
TestAndVerify(random => random.Next(0, _samples.Length));
|
|
36
64
|
}
|
|
37
65
|
|
|
38
66
|
[Test]
|
|
67
|
+
[Parallelizable]
|
|
68
|
+
public void IntRaw()
|
|
69
|
+
{
|
|
70
|
+
int sampleLength = GetSampleLength();
|
|
71
|
+
TestAndVerify(random =>
|
|
72
|
+
(int)((1.0 * random.Next()) / (1.0 * int.MaxValue) * sampleLength)
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
[Test]
|
|
77
|
+
[Parallelizable]
|
|
78
|
+
public void IntRange()
|
|
79
|
+
{
|
|
80
|
+
TestAndVerify(random => random.Next(_samples.Length));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
[Test]
|
|
84
|
+
[Parallelizable]
|
|
85
|
+
public void IntDistribution()
|
|
86
|
+
{
|
|
87
|
+
TestAndVerify(random =>
|
|
88
|
+
(int)(random.Next() / ((1.0 * int.MaxValue) / _samples.Length))
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
[Test]
|
|
93
|
+
[Parallelizable]
|
|
94
|
+
public void IntMaxRange()
|
|
95
|
+
{
|
|
96
|
+
TestAndVerify(random =>
|
|
97
|
+
(int)(
|
|
98
|
+
(random.Next(int.MinValue, int.MaxValue) + (-1.0 * int.MinValue))
|
|
99
|
+
/ (1.0 * int.MaxValue - int.MinValue)
|
|
100
|
+
* _samples.Length
|
|
101
|
+
)
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
[Test]
|
|
106
|
+
[Parallelizable]
|
|
39
107
|
public void Uint()
|
|
40
108
|
{
|
|
41
109
|
TestAndVerify(random => (int)random.NextUint(0, (uint)_samples.Length));
|
|
42
110
|
}
|
|
43
111
|
|
|
44
112
|
[Test]
|
|
113
|
+
[Parallelizable]
|
|
114
|
+
public void UintRange()
|
|
115
|
+
{
|
|
116
|
+
TestAndVerify(random => (int)random.NextUint((uint)_samples.Length));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
[Test]
|
|
120
|
+
[Parallelizable]
|
|
121
|
+
public void UintDistribution()
|
|
122
|
+
{
|
|
123
|
+
TestAndVerify(random =>
|
|
124
|
+
(int)(random.NextUint() / ((1.0 * uint.MaxValue) / _samples.Length))
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
[Test]
|
|
129
|
+
[Parallelizable]
|
|
130
|
+
public void UintMaxRange()
|
|
131
|
+
{
|
|
132
|
+
TestAndVerify(random =>
|
|
133
|
+
(int)(
|
|
134
|
+
(random.NextUint(uint.MinValue, uint.MaxValue) + (1.0 * uint.MinValue))
|
|
135
|
+
/ (1.0 * uint.MaxValue)
|
|
136
|
+
* _samples.Length
|
|
137
|
+
)
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
[Test]
|
|
142
|
+
[Parallelizable]
|
|
45
143
|
public void Short()
|
|
46
144
|
{
|
|
47
|
-
TestAndVerify(
|
|
145
|
+
TestAndVerify(
|
|
146
|
+
random => random.NextShort(0, (short)_samples.Length),
|
|
147
|
+
maxLength: (short.MaxValue - short.MinValue)
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
[Test]
|
|
152
|
+
[Parallelizable]
|
|
153
|
+
public void ShortRaw()
|
|
154
|
+
{
|
|
155
|
+
int sampleLength = GetSampleLength(short.MaxValue);
|
|
156
|
+
TestAndVerify(
|
|
157
|
+
random => (int)((1.0 * random.NextShort()) / (1.0 * short.MaxValue) * sampleLength),
|
|
158
|
+
maxLength: short.MaxValue
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
[Test]
|
|
163
|
+
[Parallelizable]
|
|
164
|
+
public void ShortRange()
|
|
165
|
+
{
|
|
166
|
+
TestAndVerify(
|
|
167
|
+
random => random.NextShort((short)_samples.Length),
|
|
168
|
+
maxLength: (short.MaxValue - short.MinValue)
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
[Test]
|
|
173
|
+
[Parallelizable]
|
|
174
|
+
public void ShortMaxRange()
|
|
175
|
+
{
|
|
176
|
+
TestAndVerify(random =>
|
|
177
|
+
(int)(
|
|
178
|
+
(random.NextShort(short.MinValue, short.MaxValue) + (-1.0 * short.MinValue))
|
|
179
|
+
/ (1.0 * short.MaxValue - short.MinValue)
|
|
180
|
+
* _samples.Length
|
|
181
|
+
)
|
|
182
|
+
);
|
|
48
183
|
}
|
|
49
184
|
|
|
50
185
|
[Test]
|
|
186
|
+
[Parallelizable]
|
|
51
187
|
public void Byte()
|
|
52
188
|
{
|
|
53
189
|
TestAndVerify(
|
|
@@ -61,6 +197,51 @@
|
|
|
61
197
|
}
|
|
62
198
|
|
|
63
199
|
[Test]
|
|
200
|
+
[Parallelizable]
|
|
201
|
+
public void ByteRaw()
|
|
202
|
+
{
|
|
203
|
+
int sampleLength = GetSampleLength(byte.MaxValue);
|
|
204
|
+
TestAndVerify(
|
|
205
|
+
random => (int)((1.0 * random.NextByte()) / (1.0 * byte.MaxValue) * sampleLength),
|
|
206
|
+
maxLength: byte.MaxValue
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
[Test]
|
|
211
|
+
[Parallelizable]
|
|
212
|
+
public void ByteRange()
|
|
213
|
+
{
|
|
214
|
+
TestAndVerify(
|
|
215
|
+
random =>
|
|
216
|
+
random.NextByte(
|
|
217
|
+
(byte)(_samples.Length < byte.MaxValue ? _samples.Length : byte.MaxValue)
|
|
218
|
+
),
|
|
219
|
+
byte.MaxValue
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
[Test]
|
|
224
|
+
[Parallelizable]
|
|
225
|
+
public void ByteMaxRange()
|
|
226
|
+
{
|
|
227
|
+
int sampleCount = Math.Min((byte.MaxValue - byte.MinValue), _samples.Length);
|
|
228
|
+
TestAndVerify(
|
|
229
|
+
random =>
|
|
230
|
+
Math.Clamp(
|
|
231
|
+
(int)(
|
|
232
|
+
(random.NextByte(byte.MinValue, byte.MaxValue) + (-1.0 * byte.MinValue))
|
|
233
|
+
/ (1.0 * byte.MaxValue - byte.MinValue)
|
|
234
|
+
* sampleCount
|
|
235
|
+
),
|
|
236
|
+
0,
|
|
237
|
+
sampleCount - 1
|
|
238
|
+
),
|
|
239
|
+
maxLength: sampleCount
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
[Test]
|
|
244
|
+
[Parallelizable]
|
|
64
245
|
public void Float()
|
|
65
246
|
{
|
|
66
247
|
TestAndVerify(random =>
|
|
@@ -73,6 +254,44 @@
|
|
|
73
254
|
}
|
|
74
255
|
|
|
75
256
|
[Test]
|
|
257
|
+
[Parallelizable]
|
|
258
|
+
public void FloatRange()
|
|
259
|
+
{
|
|
260
|
+
TestAndVerify(random =>
|
|
261
|
+
Math.Clamp(
|
|
262
|
+
(int)Math.Floor(random.NextFloat(_samples.Length)),
|
|
263
|
+
0,
|
|
264
|
+
_samples.Length - 1
|
|
265
|
+
)
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
[Test]
|
|
270
|
+
[Parallelizable]
|
|
271
|
+
public void FloatDistribution()
|
|
272
|
+
{
|
|
273
|
+
TestAndVerify(random => (int)(random.NextFloat() * _samples.Length));
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
[Test]
|
|
277
|
+
[Parallelizable]
|
|
278
|
+
public void FloatMaxRange()
|
|
279
|
+
{
|
|
280
|
+
TestAndVerify(random =>
|
|
281
|
+
Math.Clamp(
|
|
282
|
+
(int)(
|
|
283
|
+
(random.NextFloat(float.MinValue, float.MaxValue) + (-1.0 * float.MinValue))
|
|
284
|
+
/ (1.0 * float.MaxValue - float.MinValue)
|
|
285
|
+
* _samples.Length
|
|
286
|
+
),
|
|
287
|
+
0,
|
|
288
|
+
_samples.Length - 1
|
|
289
|
+
)
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
[Test]
|
|
294
|
+
[Parallelizable]
|
|
76
295
|
public void Double()
|
|
77
296
|
{
|
|
78
297
|
TestAndVerify(random =>
|
|
@@ -85,18 +304,186 @@
|
|
|
85
304
|
}
|
|
86
305
|
|
|
87
306
|
[Test]
|
|
307
|
+
[Parallelizable]
|
|
308
|
+
public void DoubleRange()
|
|
309
|
+
{
|
|
310
|
+
TestAndVerify(random =>
|
|
311
|
+
Math.Clamp(
|
|
312
|
+
(int)Math.Floor(random.NextDouble(_samples.Length)),
|
|
313
|
+
0,
|
|
314
|
+
_samples.Length - 1
|
|
315
|
+
)
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
[Test]
|
|
320
|
+
[Parallelizable]
|
|
321
|
+
public void DoubleDistribution()
|
|
322
|
+
{
|
|
323
|
+
TestAndVerify(random => (int)(random.NextDouble() * _samples.Length));
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
[Test]
|
|
327
|
+
[Parallelizable]
|
|
328
|
+
public void DoubleMaxRange()
|
|
329
|
+
{
|
|
330
|
+
IRandom random = NewRandom();
|
|
331
|
+
for (int i = 0; i < SampleCount; ++i)
|
|
332
|
+
{
|
|
333
|
+
double value = random.NextDouble(double.MinValue, double.MaxValue);
|
|
334
|
+
Assert.IsFalse(double.IsNaN(value));
|
|
335
|
+
Assert.IsFalse(double.IsInfinity(value));
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
[Test]
|
|
340
|
+
[Parallelizable]
|
|
88
341
|
public void Long()
|
|
89
342
|
{
|
|
90
343
|
TestAndVerify(random => (int)random.NextLong(0, _samples.Length));
|
|
91
344
|
}
|
|
92
345
|
|
|
93
346
|
[Test]
|
|
347
|
+
[Parallelizable]
|
|
348
|
+
public void LongRaw()
|
|
349
|
+
{
|
|
350
|
+
int sampleLength = GetSampleLength();
|
|
351
|
+
TestAndVerify(random =>
|
|
352
|
+
(int)(1.0 * random.NextLong() / (1.0 * long.MaxValue) * sampleLength)
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
[Test]
|
|
357
|
+
[Parallelizable]
|
|
358
|
+
public void LongRange()
|
|
359
|
+
{
|
|
360
|
+
TestAndVerify(random => (int)random.NextLong(_samples.Length));
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
[Test]
|
|
364
|
+
[Parallelizable]
|
|
365
|
+
public void LongMaxRange()
|
|
366
|
+
{
|
|
367
|
+
TestAndVerify(random =>
|
|
368
|
+
(int)(
|
|
369
|
+
(random.NextLong(long.MinValue, long.MaxValue) + (-1.0 * long.MinValue))
|
|
370
|
+
/ (1.0 * long.MaxValue - long.MinValue)
|
|
371
|
+
* _samples.Length
|
|
372
|
+
)
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
[Test]
|
|
377
|
+
[Parallelizable]
|
|
94
378
|
public void Ulong()
|
|
95
379
|
{
|
|
96
380
|
TestAndVerify(random => (int)random.NextUlong(0, (ulong)_samples.Length));
|
|
97
381
|
}
|
|
98
382
|
|
|
99
383
|
[Test]
|
|
384
|
+
[Parallelizable]
|
|
385
|
+
public void UlongRaw()
|
|
386
|
+
{
|
|
387
|
+
int sampleLength = GetSampleLength();
|
|
388
|
+
TestAndVerify(random =>
|
|
389
|
+
(int)(1.0 * random.NextUlong() / (1.0 * ulong.MaxValue) * sampleLength)
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
[Test]
|
|
394
|
+
[Parallelizable]
|
|
395
|
+
public void UlongRange()
|
|
396
|
+
{
|
|
397
|
+
TestAndVerify(random => (int)random.NextUlong((ulong)_samples.Length));
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
[Test]
|
|
401
|
+
[Parallelizable]
|
|
402
|
+
public void UlongMaxRange()
|
|
403
|
+
{
|
|
404
|
+
TestAndVerify(random =>
|
|
405
|
+
(int)(
|
|
406
|
+
(random.NextUlong(ulong.MinValue, ulong.MaxValue) + (-1.0 * ulong.MinValue))
|
|
407
|
+
/ (1.0 * ulong.MaxValue - ulong.MinValue)
|
|
408
|
+
* _samples.Length
|
|
409
|
+
)
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
[Test]
|
|
414
|
+
[Parallelizable]
|
|
415
|
+
public void NextBytes()
|
|
416
|
+
{
|
|
417
|
+
IRandom random = NewRandom();
|
|
418
|
+
HashSet<byte> seen = new();
|
|
419
|
+
int total = 0;
|
|
420
|
+
for (int i = 0; i < NormalIterations; ++i)
|
|
421
|
+
{
|
|
422
|
+
int size = random.Next(1, 40);
|
|
423
|
+
total += size;
|
|
424
|
+
byte[] buffer = new byte[size];
|
|
425
|
+
random.NextBytes(buffer);
|
|
426
|
+
foreach (byte value in buffer)
|
|
427
|
+
{
|
|
428
|
+
seen.Add(value);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
double seenTargetMin = Math.Min(total, byte.MaxValue) * 0.9;
|
|
433
|
+
double seenTargetMax = Math.Min(total, byte.MaxValue) * 1.1;
|
|
434
|
+
Assert.LessOrEqual(seenTargetMin, seen.Count);
|
|
435
|
+
Assert.GreaterOrEqual(seenTargetMax, seen.Count);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
[Test]
|
|
439
|
+
[Parallelizable]
|
|
440
|
+
public void NextGaussian()
|
|
441
|
+
{
|
|
442
|
+
IRandom random = NewRandom();
|
|
443
|
+
List<double> values = new();
|
|
444
|
+
for (int i = 0; i < NormalIterations; ++i)
|
|
445
|
+
{
|
|
446
|
+
double value = random.NextGaussian();
|
|
447
|
+
values.Add(value);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
Assert.IsTrue(CheckApproximateNormality(values));
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
[Test]
|
|
454
|
+
[Parallelizable]
|
|
455
|
+
public void NextGuid()
|
|
456
|
+
{
|
|
457
|
+
IRandom random = NewRandom();
|
|
458
|
+
|
|
459
|
+
HashSet<Guid> seen = new();
|
|
460
|
+
for (int i = 0; i < NormalIterations; ++i)
|
|
461
|
+
{
|
|
462
|
+
Guid value = random.NextGuid();
|
|
463
|
+
seen.Add(value);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
Assert.LessOrEqual(NormalIterations * 0.9, seen.Count);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
[Test]
|
|
470
|
+
[Parallelizable]
|
|
471
|
+
public void NextKGuid()
|
|
472
|
+
{
|
|
473
|
+
IRandom random = NewRandom();
|
|
474
|
+
|
|
475
|
+
HashSet<KGuid> seen = new();
|
|
476
|
+
for (int i = 0; i < NormalIterations; ++i)
|
|
477
|
+
{
|
|
478
|
+
KGuid value = random.NextKGuid();
|
|
479
|
+
seen.Add(value);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
Assert.LessOrEqual(NormalIterations * 0.9, seen.Count);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
[Test]
|
|
486
|
+
[Parallelizable]
|
|
100
487
|
public void Copy()
|
|
101
488
|
{
|
|
102
489
|
IRandom random1 = NewRandom();
|
|
@@ -126,6 +513,7 @@
|
|
|
126
513
|
}
|
|
127
514
|
|
|
128
515
|
[Test]
|
|
516
|
+
[Parallelizable]
|
|
129
517
|
public void Json()
|
|
130
518
|
{
|
|
131
519
|
IRandom random = NewRandom();
|
|
@@ -143,13 +531,176 @@
|
|
|
143
531
|
}
|
|
144
532
|
}
|
|
145
533
|
|
|
146
|
-
|
|
534
|
+
[Test]
|
|
535
|
+
[Parallelizable]
|
|
536
|
+
public void NextEnumerable()
|
|
537
|
+
{
|
|
538
|
+
IRandom random = NewRandom();
|
|
539
|
+
for (int i = 0; i < NormalIterations; ++i)
|
|
540
|
+
{
|
|
541
|
+
HashSet<TestValues> selected = Enum.GetValues(typeof(TestValues))
|
|
542
|
+
.OfType<TestValues>()
|
|
543
|
+
.Shuffled(random)
|
|
544
|
+
.Skip(3)
|
|
545
|
+
.ToHashSet();
|
|
546
|
+
|
|
547
|
+
TestValues value = random.NextOf(selected.Shuffled(random));
|
|
548
|
+
Assert.IsTrue(selected.Contains(value));
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
[Test]
|
|
553
|
+
[Parallelizable]
|
|
554
|
+
public void NextArray()
|
|
555
|
+
{
|
|
556
|
+
IRandom random = NewRandom();
|
|
557
|
+
for (int i = 0; i < NormalIterations; ++i)
|
|
558
|
+
{
|
|
559
|
+
TestValues[] values = Enum.GetValues(typeof(TestValues))
|
|
560
|
+
.OfType<TestValues>()
|
|
561
|
+
.Shuffled(random)
|
|
562
|
+
.Skip(3)
|
|
563
|
+
.ToArray();
|
|
564
|
+
|
|
565
|
+
TestValues value = random.NextOf(values);
|
|
566
|
+
Assert.IsTrue(values.Contains(value));
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
[Test]
|
|
571
|
+
[Parallelizable]
|
|
572
|
+
public void NextList()
|
|
573
|
+
{
|
|
574
|
+
IRandom random = NewRandom();
|
|
575
|
+
for (int i = 0; i < NormalIterations; ++i)
|
|
576
|
+
{
|
|
577
|
+
List<TestValues> values = Enum.GetValues(typeof(TestValues))
|
|
578
|
+
.OfType<TestValues>()
|
|
579
|
+
.Shuffled(random)
|
|
580
|
+
.Skip(3)
|
|
581
|
+
.ToList();
|
|
582
|
+
|
|
583
|
+
TestValues value = random.NextOf(values);
|
|
584
|
+
Assert.IsTrue(values.Contains(value));
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
[Test]
|
|
589
|
+
[Parallelizable]
|
|
590
|
+
public void NextHashSet()
|
|
591
|
+
{
|
|
592
|
+
IRandom random = NewRandom();
|
|
593
|
+
for (int i = 0; i < NormalIterations; ++i)
|
|
594
|
+
{
|
|
595
|
+
HashSet<TestValues> values = Enum.GetValues(typeof(TestValues))
|
|
596
|
+
.OfType<TestValues>()
|
|
597
|
+
.Shuffled(random)
|
|
598
|
+
.Skip(3)
|
|
599
|
+
.ToHashSet();
|
|
600
|
+
|
|
601
|
+
TestValues value = random.NextOf(values);
|
|
602
|
+
Assert.IsTrue(values.Contains(value));
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
[Test]
|
|
607
|
+
[Parallelizable]
|
|
608
|
+
public void NextLinkedList()
|
|
609
|
+
{
|
|
610
|
+
IRandom random = NewRandom();
|
|
611
|
+
for (int i = 0; i < NormalIterations; ++i)
|
|
612
|
+
{
|
|
613
|
+
LinkedList<TestValues> values = Enum.GetValues(typeof(TestValues))
|
|
614
|
+
.OfType<TestValues>()
|
|
615
|
+
.Shuffled(random)
|
|
616
|
+
.Skip(3)
|
|
617
|
+
.ToLinkedList();
|
|
618
|
+
|
|
619
|
+
TestValues value = random.NextOf(values);
|
|
620
|
+
Assert.IsTrue(values.Contains(value));
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
[Test]
|
|
625
|
+
[Parallelizable]
|
|
626
|
+
public void NextEnum()
|
|
627
|
+
{
|
|
628
|
+
IRandom random = NewRandom();
|
|
629
|
+
HashSet<TestValues> seenEnums = new();
|
|
630
|
+
for (int i = 0; i < NormalIterations; ++i)
|
|
631
|
+
{
|
|
632
|
+
TestValues value = random.NextEnum<TestValues>();
|
|
633
|
+
_ = seenEnums.Add(value);
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
Assert.AreEqual(Enum.GetValues(typeof(TestValues)).Length, seenEnums.Count);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
[Test]
|
|
640
|
+
[Parallelizable]
|
|
641
|
+
public void NextNoiseMap()
|
|
642
|
+
{
|
|
643
|
+
IRandom random = NewRandom();
|
|
644
|
+
for (int i = 0; i < NormalIterations; ++i)
|
|
645
|
+
{
|
|
646
|
+
int width = random.Next(1, 75);
|
|
647
|
+
int height = random.Next(1, 75);
|
|
648
|
+
float[,] noise = random.NextNoiseMap(width, height);
|
|
649
|
+
Assert.IsNotNull(noise);
|
|
650
|
+
Assert.AreEqual(width * height, noise.Length);
|
|
651
|
+
foreach (float value in noise)
|
|
652
|
+
{
|
|
653
|
+
Assert.LessOrEqual(0f, value);
|
|
654
|
+
Assert.GreaterOrEqual(1.1f, value);
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
protected virtual double GetDeviationFor(string caller)
|
|
660
|
+
{
|
|
661
|
+
return 0.0625;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
private static bool CheckApproximateNormality(IEnumerable<double> data)
|
|
665
|
+
{
|
|
666
|
+
IReadOnlyList<double> input = data as IReadOnlyList<double> ?? data.ToArray();
|
|
667
|
+
int n = input.Count;
|
|
668
|
+
if (n < 3)
|
|
669
|
+
{
|
|
670
|
+
return true;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
double mean = input.Average();
|
|
674
|
+
double variance = input.Sum(x => Math.Pow(x - mean, 2)) / n;
|
|
675
|
+
double stdDev = Math.Sqrt(variance);
|
|
676
|
+
|
|
677
|
+
double skewness = input.Sum(x => Math.Pow((x - mean) / stdDev, 3)) / n;
|
|
678
|
+
double kurtosis = input.Sum(x => Math.Pow((x - mean) / stdDev, 4)) / n - 3; // Excess kurtosis
|
|
679
|
+
|
|
680
|
+
// Thresholds can be defined based on empirical rules or domain-specific values
|
|
681
|
+
const double skewnessThreshold = 0.5;
|
|
682
|
+
const double kurtosisThreshold = 1.0;
|
|
683
|
+
|
|
684
|
+
return Math.Abs(skewness) < skewnessThreshold && Math.Abs(kurtosis) < kurtosisThreshold;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
protected int GetSampleLength(int? sampleLength = null)
|
|
688
|
+
{
|
|
689
|
+
return Math.Min(_samples.Length, sampleLength ?? _samples.Length);
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
private void TestAndVerify(
|
|
693
|
+
Func<IRandom, int> sample,
|
|
694
|
+
int? maxLength = null,
|
|
695
|
+
[CallerMemberName] string caller = ""
|
|
696
|
+
)
|
|
147
697
|
{
|
|
148
698
|
IRandom random = NewRandom();
|
|
699
|
+
int sampleLength = _samples.Length;
|
|
149
700
|
for (int i = 0; i < SampleCount; ++i)
|
|
150
701
|
{
|
|
151
702
|
int index = sample(random);
|
|
152
|
-
if (index < 0 ||
|
|
703
|
+
if (index < 0 || sampleLength <= index)
|
|
153
704
|
{
|
|
154
705
|
Assert.Fail("Index {0} out of range", index);
|
|
155
706
|
}
|
|
@@ -159,9 +710,9 @@
|
|
|
159
710
|
}
|
|
160
711
|
}
|
|
161
712
|
|
|
162
|
-
|
|
713
|
+
sampleLength = GetSampleLength(maxLength);
|
|
163
714
|
double average = SampleCount * 1.0 / sampleLength;
|
|
164
|
-
double deviationAllowed = average *
|
|
715
|
+
double deviationAllowed = average * GetDeviationFor(caller);
|
|
165
716
|
List<int> zeroCountIndexes = new();
|
|
166
717
|
List<int> outsideRange = new();
|
|
167
718
|
for (int i = 0; i < sampleLength; i++)
|