Skip to content

Commit

Permalink
assertion tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
MiloszKrajewski committed Oct 3, 2023
1 parent 4866426 commit a808b06
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/K4os.Compression.LZ4.Legacy/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal static class Extensions
/// <param name="value">Argument value.</param>
/// <param name="name">Name of argument.</param>
/// <typeparam name="T">Type of argument.</typeparam>
// [Conditional("DEBUG")]
[Conditional("DEBUG")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AssertIsNotNull<T>(
[NotNull] this T? value, [CallerArgumentExpression("value")] string? name = null)
Expand Down
3 changes: 2 additions & 1 deletion src/K4os.Compression.LZ4.Streams/Internal/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

Expand Down Expand Up @@ -42,7 +43,7 @@ public static ConfiguredValueTaskAwaitable Weave(this ValueTask task) =>
/// <param name="value">Argument value.</param>
/// <param name="name">Name of argument.</param>
/// <typeparam name="T">Type of argument.</typeparam>
// [Conditional("DEBUG")]
[Conditional("DEBUG")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AssertIsNotNull<T>(
[NotNull] this T? value, [CallerArgumentExpression("value")] string? name = null)
Expand Down
22 changes: 9 additions & 13 deletions src/K4os.Compression.LZ4/Engine/LL.tools.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using K4os.Compression.LZ4.Internal;

//------------------------------------------------------------------------------

// ReSharper disable IdentifierTypo
// ReSharper disable IdentifierTypo
// ReSharper disable InconsistentNaming
// ReSharper disable AccessToStaticMemberViaDerivedType
// ReSharper disable ConditionIsAlwaysTrueOrFalse
// ReSharper disable BuiltInTypeReferenceStyle

using System.Diagnostics;
using System.Runtime.CompilerServices;
using K4os.Compression.LZ4.Internal;

using size_t = System.UInt32;
using uptr_t = System.UInt64;

//------------------------------------------------------------------------------

namespace K4os.Compression.LZ4.Engine
{
internal unsafe partial class LL
Expand All @@ -24,10 +20,10 @@ internal unsafe partial class LL

[Conditional("DEBUG")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Assert(bool condition, string message = null)
public static void Assert(
bool value, [CallerArgumentExpression("value")] string message = null)
{
if (!condition)
throw new ArgumentException(message ?? "Assert failed");
value.AssertTrue(message);
}

public static bool Enforce32 { get; set; } = false;
Expand Down
15 changes: 15 additions & 0 deletions src/K4os.Compression.LZ4/Internal/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#nullable enable

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

Expand All @@ -14,6 +15,20 @@ internal static T Required<T>(
[CallerArgumentExpression("value")] string name = null!) =>
value ?? ThrowArgumentNullException<T>(name);

[Conditional("DEBUG")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AssertTrue(
this bool value,
[CallerArgumentExpression("value")] string? name = null)
{
if (!value) ThrowAssertionFailed(name);
}

[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowAssertionFailed(string? name) =>
throw new ArgumentException($"{name ?? "<unknown>"} assertion failed");

[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
private static T ThrowArgumentNullException<T>(string name) =>
Expand Down

0 comments on commit a808b06

Please sign in to comment.