Skip to content

Commit d7e5feb

Browse files
Added Sign-Checks
Assert.IsNonNegative, Assert.IsPositive etc.
1 parent 8014e32 commit d7e5feb

File tree

5 files changed

+207
-24
lines changed

5 files changed

+207
-24
lines changed

AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[assembly: AssemblyConfiguration("")]
1010
[assembly: AssemblyCompany("")]
1111
[assembly: AssemblyProduct("C Sharp Dev Tools")]
12-
[assembly: AssemblyCopyright("Copyright © 2020")]
12+
[assembly: AssemblyCopyright("Copyright © 2020 Maximilian Kalimon")]
1313
[assembly: AssemblyTrademark("")]
1414
[assembly: AssemblyCulture("")]
1515

@@ -28,6 +28,6 @@
2828
// Build Number
2929
// Revision
3030
//
31-
[assembly: AssemblyVersion("1.0.4")]
32-
[assembly: AssemblyFileVersion("1.0.4")]
31+
[assembly: AssemblyVersion("1.0.5")]
32+
[assembly: AssemblyFileVersion("1.0.5")]
3333

Assert.cs

Lines changed: 195 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ public static void FileExists(string path)
113113
public static void IsWithinArrayBounds(int index, int arrayLength)
114114
{
115115
#if BOUNDS_CHECKS
116-
IsNotSmaller(arrayLength, 0);
116+
IsNonNegative(arrayLength);
117117

118118
if ((uint)index >= (uint)arrayLength)
119119
{
120-
throw new IndexOutOfRangeException($"{ index } out of range (length { arrayLength } - 1).");
120+
throw new IndexOutOfRangeException($"{ index } is out of range (length { arrayLength } - 1).");
121121
}
122122
#endif
123123
}
@@ -130,11 +130,11 @@ public static void IsValidSubarray(int index, int NumEntries, int arrayLength)
130130
{
131131
#if SUBARRAY_CHECKS
132132
IsWithinArrayBounds(index, arrayLength);
133-
IsNotSmaller(NumEntries, 0);
133+
IsNonNegative(NumEntries);
134134

135135
if (index + NumEntries > arrayLength)
136136
{
137-
throw new IndexOutOfRangeException($"index + NumEntries is { index + NumEntries }, which is larger than length { arrayLength }.");
137+
throw new IndexOutOfRangeException($"{ nameof(index) } + { nameof(NumEntries) } is { index + NumEntries }, which is larger than length { arrayLength }.");
138138
}
139139
#endif
140140
}
@@ -164,6 +164,197 @@ public static void SubarraysDoNotOverlap(int firstIndex, int secondIndex, int fi
164164

165165

166166
#region COMPARE_CHECKS
167+
/// <summary> Remember: Zero is neither positive nor negative. </summary>
168+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
169+
public static void IsPositive(long value)
170+
{
171+
#if COMPARE_CHECKS
172+
if (value <= 0)
173+
{
174+
throw new ArgumentOutOfRangeException($"{ value } was expected to be positive.");
175+
}
176+
#endif
177+
}
178+
179+
/// <summary> Remember: Zero is neither positive nor negative. </summary>
180+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
181+
public static void IsPositive(float value)
182+
{
183+
184+
#if COMPARE_CHECKS
185+
if (value <= 0f)
186+
{
187+
throw new ArgumentOutOfRangeException($"{ value } was expected to be positive.");
188+
}
189+
#endif
190+
}
191+
192+
/// <summary> Remember: Zero is neither positive nor negative. </summary>
193+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
194+
public static void IsPositive(double value)
195+
{
196+
197+
#if COMPARE_CHECKS
198+
if (value <= 0d)
199+
{
200+
throw new ArgumentOutOfRangeException($"{ value } was expected to be positive.");
201+
}
202+
#endif
203+
}
204+
205+
/// <summary> Remember: Zero is neither positive nor negative. </summary>
206+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
207+
public static void IsPositive(decimal value)
208+
{
209+
#if COMPARE_CHECKS
210+
if (value <= 0m)
211+
{
212+
throw new ArgumentOutOfRangeException($"{ value } was expected to be positive.");
213+
}
214+
#endif
215+
}
216+
217+
/// <summary> Remember: Zero is neither positive nor negative. </summary>
218+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
219+
public static void IsNegative(long value)
220+
{
221+
#if COMPARE_CHECKS
222+
if (value >= 0)
223+
{
224+
throw new ArgumentOutOfRangeException($"{ value } was expected to be negative.");
225+
}
226+
#endif
227+
}
228+
229+
/// <summary> Remember: Zero is neither positive nor negative. </summary>
230+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
231+
public static void IsNegative(float value)
232+
{
233+
234+
#if COMPARE_CHECKS
235+
if (value <= 0f)
236+
{
237+
throw new ArgumentOutOfRangeException($"{ value } was expected to be negative.");
238+
}
239+
#endif
240+
}
241+
242+
/// <summary> Remember: Zero is neither positive nor negative. </summary>
243+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
244+
public static void IsNegative(double value)
245+
{
246+
247+
#if COMPARE_CHECKS
248+
if (value <= 0d)
249+
{
250+
throw new ArgumentOutOfRangeException($"{ value } was expected to be negative.");
251+
}
252+
#endif
253+
}
254+
255+
/// <summary> Remember: Zero is neither positive nor negative. </summary>
256+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
257+
public static void IsNegative(decimal value)
258+
{
259+
#if COMPARE_CHECKS
260+
if (value <= 0m)
261+
{
262+
throw new ArgumentOutOfRangeException($"{ value } was expected to be negative.");
263+
}
264+
#endif
265+
}
266+
267+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
268+
public static void IsNonNegative(long value)
269+
{
270+
#if COMPARE_CHECKS
271+
if (value < 0)
272+
{
273+
throw new ArgumentOutOfRangeException($"{ value } was expected to be positive or equal to zero.");
274+
}
275+
#endif
276+
}
277+
278+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
279+
public static void IsNonNegative(float value)
280+
{
281+
282+
#if COMPARE_CHECKS
283+
if (value < 0f)
284+
{
285+
throw new ArgumentOutOfRangeException($"{ value } was expected to be positive or equal to zero.");
286+
}
287+
#endif
288+
}
289+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
290+
public static void IsNonNegative(double value)
291+
{
292+
293+
#if COMPARE_CHECKS
294+
if (value < 0d)
295+
{
296+
throw new ArgumentOutOfRangeException($"{ value } was expected to be positive or equal to zero.");
297+
}
298+
#endif
299+
}
300+
301+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
302+
public static void IsNonNegative(decimal value)
303+
{
304+
#if COMPARE_CHECKS
305+
if (value < 0m)
306+
{
307+
throw new ArgumentOutOfRangeException($"{ value } was expected to be positive or equal to zero.");
308+
}
309+
#endif
310+
}
311+
312+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
313+
public static void IsNotPositive(long value)
314+
{
315+
#if COMPARE_CHECKS
316+
if (value > 0)
317+
{
318+
throw new ArgumentOutOfRangeException($"{ value } was expected to be negative or equal to zero.");
319+
}
320+
#endif
321+
}
322+
323+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
324+
public static void IsNotPositive(float value)
325+
{
326+
327+
#if COMPARE_CHECKS
328+
if (value > 0f)
329+
{
330+
throw new ArgumentOutOfRangeException($"{ value } was expected to be negative or equal to zero.");
331+
}
332+
#endif
333+
}
334+
335+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
336+
public static void IsNotPositive(double value)
337+
{
338+
339+
#if COMPARE_CHECKS
340+
if (value > 0d)
341+
{
342+
throw new ArgumentOutOfRangeException($"{ value } was expected to be negative or equal to zero.");
343+
}
344+
#endif
345+
}
346+
347+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
348+
public static void IsNotPositive(decimal value)
349+
{
350+
#if COMPARE_CHECKS
351+
if (value > 0m)
352+
{
353+
throw new ArgumentOutOfRangeException($"{ value } was expected to be negative or equal to zero.");
354+
}
355+
#endif
356+
}
357+
167358
[MethodImpl(MethodImplOptions.AggressiveInlining)]
168359
public static void AreEqual<T>(T a, T b)
169360
where T : IEquatable<T>

Log.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace DevTools
1+
namespace DevTools
22
{
33
unsafe public static class Log
44
{
@@ -26,7 +26,7 @@ public static string Bits<T>(T value, bool spaces = true)
2626
public static string Bits(void* ptr, int bytes, bool spaces = true)
2727
{
2828
Assert.IsNotNull(ptr);
29-
Assert.IsGreater(bytes, -1);
29+
Assert.IsNonNegative(bytes);
3030

3131
byte* address = (byte*)ptr;
3232
string result = string.Empty;
@@ -45,7 +45,7 @@ public static string Bits(void* ptr, int bytes, bool spaces = true)
4545

4646
public static string Hex(byte value)
4747
{
48-
return HexValues[value >> 4].ToString() + HexValues[value & 15].ToString();
48+
return HexValues[value >> 4].ToString() + HexValues[value & 0b1111].ToString();
4949
}
5050

5151
public static string Hex<T>(T value, bool spaces = true)
@@ -57,7 +57,7 @@ public static string Hex<T>(T value, bool spaces = true)
5757
public static string Hex(void* ptr, int bytes, bool spaces = true)
5858
{
5959
Assert.IsNotNull(ptr);
60-
Assert.IsGreater(bytes, -1);
60+
Assert.IsNonNegative(bytes);
6161

6262
byte* address = (byte*)ptr;
6363
int iterations = 0;
@@ -74,4 +74,4 @@ public static string Hex(void* ptr, int bytes, bool spaces = true)
7474
return result;
7575
}
7676
}
77-
}
77+
}

manifest.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
{
22
"name": "csharpdevtools",
33
"displayName": "C Sharp Dev Tools",
4-
"version": "1.0.4",
5-
"unity": "2018.4",
4+
"version": "1.0.5",
65
"description": "C Sharp Dev Tools is a small framework for defensive development with conditionally compiled assertions and logging tools.",
7-
"keywords": [
8-
"Unity"
9-
],
106
"author": {
117
"name": "Maximilian Kalimon",
128
"email": "mkalimon@gmx.de"
@@ -15,4 +11,4 @@
1511
"url": "https://github.com/MrUnbelievable92/C-Sharp-Dev-Tools",
1612
"type": "git"
1713
}
18-
}
14+
}

package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
{
22
"name": "csharpdevtools",
33
"displayName": "C Sharp Dev Tools",
4-
"version": "1.0.4",
5-
"unity": "2018.4",
4+
"version": "1.0.5",
65
"description": "C Sharp Dev Tools is a small framework for defensive development with conditionally compiled assertions and logging tools.",
7-
"keywords": [
8-
"Unity"
9-
],
106
"author": {
117
"name": "Maximilian Kalimon",
128
"email": "mkalimon@gmx.de"
@@ -15,4 +11,4 @@
1511
"url": "https://github.com/MrUnbelievable92/C-Sharp-Dev-Tools",
1612
"type": "git"
1713
}
18-
}
14+
}

0 commit comments

Comments
 (0)