-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAssert.cs
682 lines (625 loc) · 28.4 KB
/
Assert.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// Constraints:
// No use of the ConditionalAttribute
// strings, Exceptions or any other managed objects cannot be passed as arguments to functions if they are to work with Unity.Burst
#if DEBUG
#define BOOLEAN_CONDITION_CHECKS
#define NULL_CHECKS
#define FILE_PATH_CHECKS
#define ARRAY_BOUNDS_CHECKS
#define COMPARISON_CHECKS
#define ARITHMETIC_LOGIC_CHECKS
#define MEMORY_CHECKS
#endif
using System;
using System.IO;
using System.Runtime.CompilerServices;
namespace DevTools
{
public static partial class Assert
{
internal const string ASSERTION_FAILED_TAG = "Assertion Failed: ";
public static UnreachableException Unreachable()
{
#if DEBUG
throw new UnreachableException();
#else
return null;
#endif
}
#region BOOLEAN_CONDITION_CHECKS
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__BOOLEAN_CONDITION_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__BOOLEAN_CONDITION_CHECKS)]
public static void IsTrue(bool condition)
{
#if BOOLEAN_CONDITION_CHECKS
if (!condition)
{
throw new Exception(ASSERTION_FAILED_TAG + "Expected 'true'.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__BOOLEAN_CONDITION_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__BOOLEAN_CONDITION_CHECKS)]
public static void IsFalse(bool condition)
{
#if BOOLEAN_CONDITION_CHECKS
if (condition)
{
throw new Exception(ASSERTION_FAILED_TAG + "Expected 'false'.");
}
#endif
}
#endregion
#region NULL_CHECKS
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__NULL_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__NULL_CHECKS)]
public static void IsNull(object obj)
{
#if NULL_CHECKS
if (obj != null)
{
throw new InvalidDataException(ASSERTION_FAILED_TAG + "Expected null.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__NULL_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__NULL_CHECKS)]
public static void IsNull<T>(T? obj)
where T : struct
{
#if NULL_CHECKS
if (obj != null)
{
throw new InvalidDataException(ASSERTION_FAILED_TAG + "Expected null.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__NULL_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__NULL_CHECKS)]
unsafe public static void IsNull(void* ptr)
{
#if NULL_CHECKS
if (ptr != null)
{
throw new InvalidDataException(ASSERTION_FAILED_TAG + "Expected null.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__NULL_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__NULL_CHECKS)]
public static void IsNotNull(object obj)
{
#if NULL_CHECKS
if (obj == null)
{
throw new NullReferenceException(ASSERTION_FAILED_TAG + "Expected not-null.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__NULL_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__NULL_CHECKS)]
public static void IsNotNull<T>(T? obj)
where T : struct
{
#if NULL_CHECKS
if (obj == null)
{
throw new NullReferenceException(ASSERTION_FAILED_TAG + "Expected not-null.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__NULL_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__NULL_CHECKS)]
unsafe public static void IsNotNull(void* ptr)
{
#if NULL_CHECKS
if (ptr == null)
{
throw new NullReferenceException(ASSERTION_FAILED_TAG + "Expected not-null.");
}
#endif
}
#endregion
#region FILE_PATH_CHECKS
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__FILE_PATH_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__FILE_PATH_CHECKS)]
public static void FileExists(string path)
{
#if FILE_PATH_CHECKS
IsNotNull(path); // File.Exists only returns 'false' in case 'path' is null (no explicit throw, which is what I want)
if (!File.Exists(path))
{
throw new FileNotFoundException(ASSERTION_FAILED_TAG + path + " not found");
}
#endif
}
#endregion
#region ARRAY_BOUNDS_CHECKS
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__ARRAY_BOUNDS_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__ARRAY_BOUNDS_CHECKS)]
public static void IsWithinArrayBounds(long index, long arrayLength)
{
#if ARRAY_BOUNDS_CHECKS
IsNonNegative(arrayLength);
if ((ulong)index >= (ulong)arrayLength)
{
throw new IndexOutOfRangeException(ASSERTION_FAILED_TAG + $"{ index } is out of range (length { arrayLength } - 1).");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__ARRAY_BOUNDS_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__ARRAY_BOUNDS_CHECKS)]
public static void IsWithinArrayBounds(ulong index, ulong arrayLength)
{
#if ARRAY_BOUNDS_CHECKS
if (index >= arrayLength)
{
throw new IndexOutOfRangeException(ASSERTION_FAILED_TAG + $"{ index } is out of range (length { arrayLength } - 1).");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__ARRAY_BOUNDS_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__ARRAY_BOUNDS_CHECKS)]
public static void IsValidSubarray(int index, int numEntries, int arrayLength)
{
#if ARRAY_BOUNDS_CHECKS
IsWithinArrayBounds(index, arrayLength);
IsNonNegative(numEntries);
if (index + numEntries > arrayLength)
{
throw new IndexOutOfRangeException(ASSERTION_FAILED_TAG + $"{ nameof(index) } + { nameof(numEntries) } is { index + numEntries }, which is larger than length { arrayLength }.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__ARRAY_BOUNDS_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__ARRAY_BOUNDS_CHECKS)]
public static void SubarraysDoNotOverlap(int firstIndex, int secondIndex, int firstNumEntries, int secondNumEntries)
{
#if ARRAY_BOUNDS_CHECKS
if (firstIndex < secondIndex)
{
if (firstIndex + firstNumEntries > secondIndex)
{
throw new IndexOutOfRangeException(ASSERTION_FAILED_TAG + $"Subarray from { firstIndex } to { firstIndex + firstNumEntries - 1} overlaps with subarray from { secondIndex } to { secondIndex + secondNumEntries - 1 }.");
}
}
else
{
if (secondIndex + secondNumEntries > firstIndex)
{
throw new IndexOutOfRangeException(ASSERTION_FAILED_TAG + $"Subarray from { secondIndex } to { secondIndex + secondNumEntries - 1} overlaps with subarray from { firstIndex } to { firstIndex + firstNumEntries - 1 }.");
}
}
#endif
}
#endregion
#region COMPARISON_CHECKS
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
/// <remarks> Remember: Zero is neither positive nor negative. </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsPositive(long value)
{
#if COMPARISON_CHECKS
if (value <= 0)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be positive.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
/// <remarks> Remember: Zero is neither positive nor negative. </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsPositive(float value)
{
#if COMPARISON_CHECKS
if (value <= 0f)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be positive.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
/// <remarks> Remember: Zero is neither positive nor negative. </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsPositive(double value)
{
#if COMPARISON_CHECKS
if (value <= 0d)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be positive.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
/// <remarks> Remember: Zero is neither positive nor negative. </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsPositive(decimal value)
{
#if COMPARISON_CHECKS
if (value <= 0m)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be positive.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
/// <remarks> Remember: Zero is neither positive nor negative. </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsNegative(long value)
{
#if COMPARISON_CHECKS
if (value >= 0)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be negative.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
/// <remarks> Remember: Zero is neither positive nor negative. </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsNegative(float value)
{
#if COMPARISON_CHECKS
if (value >= 0f)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be negative.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
/// <remarks> Remember: Zero is neither positive nor negative. </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsNegative(double value)
{
#if COMPARISON_CHECKS
if (value >= 0d)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be negative.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
/// <remarks> Remember: Zero is neither positive nor negative. </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsNegative(decimal value)
{
#if COMPARISON_CHECKS
if (value >= 0m)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be negative.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
/// <remarks> Remember: Zero is neither positive nor negative. </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsNonNegative(long value)
{
#if COMPARISON_CHECKS
if (value < 0)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be positive or equal to zero.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
/// <remarks> Remember: Zero is neither positive nor negative. </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsNonNegative(float value)
{
#if COMPARISON_CHECKS
if (value < 0f)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be positive or equal to zero.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
/// <remarks> Remember: Zero is neither positive nor negative. </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsNonNegative(double value)
{
#if COMPARISON_CHECKS
if (value < 0d)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be positive or equal to zero.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
/// <remarks> Remember: Zero is neither positive nor negative. </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsNonNegative(decimal value)
{
#if COMPARISON_CHECKS
if (value < 0m)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be positive or equal to zero.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
/// <remarks> Remember: Zero is neither positive nor negative. </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsNotPositive(long value)
{
#if COMPARISON_CHECKS
if (value > 0)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be negative or equal to zero.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
/// <remarks> Remember: Zero is neither positive nor negative. </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsNotPositive(float value)
{
#if COMPARISON_CHECKS
if (value > 0f)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be negative or equal to zero.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
/// <remarks> Remember: Zero is neither positive nor negative. </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsNotPositive(double value)
{
#if COMPARISON_CHECKS
if (value > 0d)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be negative or equal to zero.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
/// <remarks> Remember: Zero is neither positive nor negative. </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsNotPositive(decimal value)
{
#if COMPARISON_CHECKS
if (value > 0m)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be negative or equal to zero.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void AreEqual<T>(T a, T b)
where T : IEquatable<T>
{
#if COMPARISON_CHECKS
if (!a.Equals(b))
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ a } was expected to be equal to { b }.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void AreNotEqual<T>(T a, T b)
where T : IEquatable<T>
{
#if COMPARISON_CHECKS
if (a.Equals(b))
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ a } was expected not to be equal to { b }.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
/// <remarks> The comparison is inclusive. </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsBetween<T>(T value, T min, T max)
where T : IComparable<T>
{
#if COMPARISON_CHECKS
if ((value.CompareTo(min) < 0) || (value.CompareTo(max) > 0))
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"Min: { min }, Max: { max }, Value: { value }.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsSmallerOrEqual<T>(T value, T limit)
where T : IComparable<T>
{
#if COMPARISON_CHECKS
if (value.CompareTo(limit) > 0)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be smaller than or equal to { limit }.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsSmaller<T>(T value, T limit)
where T : IComparable<T>
{
#if COMPARISON_CHECKS
if (value.CompareTo(limit) > -1)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be smaller than { limit }.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsGreaterOrEqual<T>(T value, T limit)
where T : IComparable<T>
{
#if COMPARISON_CHECKS
if (value.CompareTo(limit) < 0)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be greater than or equal to { limit }.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsGreater<T>(T value, T limit)
where T : IComparable<T>
{
#if COMPARISON_CHECKS
if (value.CompareTo(limit) < 1)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected to be greater than { limit }.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsNotSmaller<T>(T value, T limit)
where T : IComparable<T>
{
#if COMPARISON_CHECKS
if (value.CompareTo(limit) < 0)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected not to be smaller than { limit }.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__COMPARISON_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__COMPARISON_CHECKS)]
public static void IsNotGreater<T>(T value, T limit)
where T : IComparable<T>
{
#if COMPARISON_CHECKS
if (value.CompareTo(limit) > 0)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"{ value } was expected not to be greater than { limit }.");
}
#endif
}
#endregion
#region ARITHMETIC_LOGIC_CHECKS
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__ARITHMETIC_LOGIC_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__ARITHMETIC_LOGIC_CHECKS)]
unsafe public static void IsSafeBoolean(bool x)
{
#if ARITHMETIC_LOGIC_CHECKS
if (*(byte*)&x > 1)
{
throw new InvalidDataException(ASSERTION_FAILED_TAG + $"The numerical value of the bool { nameof(x) } is { *(byte*)&x } which can lead to undefined behavior.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__ARITHMETIC_LOGIC_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__ARITHMETIC_LOGIC_CHECKS)]
unsafe public static void IsDefinedBitShift<T>(int amount)
where T : unmanaged
{
#if ARITHMETIC_LOGIC_CHECKS
if ((uint)amount >= (uint)sizeof(T) * 8u)
{
throw new ArgumentOutOfRangeException(ASSERTION_FAILED_TAG + $"Shifting a { typeof(T) } by { amount } results in undefined behavior.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__ARITHMETIC_LOGIC_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__ARITHMETIC_LOGIC_CHECKS)]
public static void IsDefinedBitShift<T>(uint amount)
where T : unmanaged
{
IsDefinedBitShift<T>((int)amount);
}
#endregion
#region MEMORY_CHECKS
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__MEMORY_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__MEMORY_CHECKS)]
unsafe public static void AreNotAliased(void* ptrA, long bytesA, void* ptrB, long bytesB)
{
#if MEMORY_CHECKS
bool overlap = (ulong)ptrA < (ulong)ptrB && ((ulong)ptrA + (ulong)bytesA - 1 >= (ulong)ptrB);
overlap |= (ulong)ptrB < (ulong)ptrA && ((ulong)ptrB + (ulong)bytesB - 1 >= (ulong)ptrA);
overlap |= (ulong)ptrB == (ulong)ptrA;
if (overlap)
{
throw new MemberAccessException(ASSERTION_FAILED_TAG + $"The memory block, associated with the base address { Dump.Hex((ulong)ptrA) } and byte length { bytesA } overlaps with the region in memory associated with the base address {Dump.Hex((ulong)ptrB) } and byte length {bytesB }.");
}
#endif
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__MEMORY_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__MEMORY_CHECKS)]
unsafe public static void AreNotAliased<T, U>(T* ptrA, long lengthA, U* ptrB, long lengthB)
where T : unmanaged
where U : unmanaged
{
AreNotAliased((void*)ptrA, sizeof(T) * lengthA, (void*)ptrB, sizeof(U) * lengthB);
}
/// <summary> Part of: <inheritdoc cref="Assert.GroupAttribute.__NAME__MEMORY_CHECKS"/> </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Assert.Group(Assert.GroupAttribute.__NAME__MEMORY_CHECKS)]
unsafe public static void IsMemoryAligned<T>(T* ptr)
where T : unmanaged
{
#if MEMORY_CHECKS
switch (sizeof(T))
{
case 2:
case 4:
case 8:
case 16:
case 32:
case 64:
{
if ((ulong)ptr % (uint)sizeof(T) != 0)
{
throw new DataMisalignedException(ASSERTION_FAILED_TAG + $"The address { Dump.Hex((ulong)ptr) } of a { typeof(T) } of size { sizeof(T) } is misaligned by { (ulong)ptr % (uint)sizeof(T) } bytes.");
}
return;
}
default: return;
}
#endif
}
#endregion
}
}