< Summary

Information
Class: MyNet.Primitives.Intervals.ValueRange<T>
Assembly: MyNet.Primitives
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Intervals/ValueRange.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 128
Line coverage: 100%
Branch coverage
81%
Covered branches: 26
Total branches: 32
Branch coverage: 81.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%66100%
get_HasMin()100%11100%
get_HasMax()100%11100%
Contains(...)91.66%1212100%
Clamp(...)75%88100%
EnsureInRange(...)50%22100%
ToString()50%44100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Intervals/ValueRange.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ValueRange.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Runtime.CompilerServices;
 9
 10namespace MyNet.Primitives.Intervals;
 11
 12/// <summary>
 13/// Represents a range of values of type T, defined by optional minimum and maximum boundaries. The range can be inclusi
 14/// </summary>
 15/// <typeparam name="T">The type of the values in the range.</typeparam>
 16public sealed class ValueRange<T>
 17    where T : struct, IComparable<T>
 18{
 19    /// <summary>
 20    /// Initializes a new instance of the <see cref="ValueRange{T}"/> class with optional minimum and maximum boundaries
 21    /// </summary>
 22    /// <param name="min">The minimum value of the range, or null if the range has no lower bound.</param>
 23    /// <param name="max">The maximum value of the range, or null if the range has no upper bound.</param>
 24    /// <param name="minInclusive">Indicates whether the minimum value is inclusive.</param>
 25    /// <param name="maxInclusive">Indicates whether the maximum value is inclusive.</param>
 26    /// <exception cref="ArgumentException">Thrown when the minimum value is greater than the maximum value.</exception>
 27    public ValueRange(T? min = null, T? max = null, bool minInclusive = true, bool maxInclusive = true)
 28    {
 1529        if (min.HasValue && max.HasValue && min.Value.CompareTo(max.Value) > 0)
 30        {
 331            throw new ArgumentException("Min must be lower than max.");
 32        }
 33
 1234        Min = min;
 1235        Max = max;
 1236        IsMinInclusive = minInclusive;
 1237        IsMaxInclusive = maxInclusive;
 1238    }
 39
 40    /// <summary>
 41    /// Gets the minimum value of the range, or null if the range has no lower bound. This property represents the lower
 42    /// </summary>
 43    public T? Min { get; }
 44
 45    /// <summary>
 46    /// Gets the maximum value of the range, or null if the range has no upper bound. This property represents the upper
 47    /// </summary>
 48    public T? Max { get; }
 49
 50    /// <summary>
 51    /// Gets a value indicating whether the minimum boundary of the range is inclusive. If this property is true, values
 52    /// </summary>
 53    public bool IsMinInclusive { get; }
 54
 55    /// <summary>
 56    /// Gets a value indicating whether the maximum boundary of the range is inclusive. If this property is true, values
 57    /// </summary>
 58    public bool IsMaxInclusive { get; }
 59
 60    /// <summary>
 61    /// Gets a value indicating whether the range has a defined minimum boundary. If this property is true, the Min prop
 62    /// </summary>
 2163    public bool HasMin => Min.HasValue;
 64
 65    /// <summary>
 66    /// Gets a value indicating whether the range has a defined maximum boundary. If this property is true, the Max prop
 67    /// </summary>
 1868    public bool HasMax => Max.HasValue;
 69
 70    /// <summary>
 71    /// Determines whether a specified value falls within the range defined by the minimum and maximum boundaries, takin
 72    /// </summary>
 73    /// <param name="value">The value to check against the range.</param>
 74    /// <returns>True if the value is within the range; otherwise, false.</returns>
 75    public bool Contains(T value)
 76    {
 1277        var validMin =
 1278            !HasMin ||
 1279            value.CompareTo(Min!.Value) > 0 ||
 1280            (value.CompareTo(Min.Value) == 0 && IsMinInclusive);
 81
 1282        var validMax =
 1283            !HasMax ||
 1284            value.CompareTo(Max!.Value) < 0 ||
 1285            (value.CompareTo(Max.Value) == 0 && IsMaxInclusive);
 86
 1287        return validMin && validMax;
 88    }
 89
 90    /// <summary>
 91    /// Clamps a specified value to the range defined by the minimum and maximum boundaries. If the value is less than t
 92    /// </summary>
 93    /// <param name="value">The value to clamp to the range.</param>
 94    /// <returns>The clamped value.</returns>
 95    public T Clamp(T value) =>
 996        HasMin &&
 997        value.CompareTo(Min!.Value) < 0
 998            ? Min.Value
 999            : HasMax &&
 9100              value.CompareTo(Max!.Value) > 0
 9101                ? Max.Value
 9102                : value;
 103
 104    /// <summary>
 105    /// Ensures that a specified value falls within the range defined by the minimum and maximum boundaries. If the valu
 106    /// </summary>
 107    /// <param name="value">The value to check against the range.</param>
 108    /// <param name="propertyName">The name of the property being validated.</param>
 109    /// <returns>The value if it is within the range.</returns>
 110    /// <exception cref="ArgumentOutOfRangeException">Thrown if the value is not within the range.</exception>
 111    public T EnsureInRange(T value, [CallerMemberName] string propertyName = "") =>
 3112        !Contains(value)
 3113            ? throw new ArgumentOutOfRangeException(propertyName, value, "Value must be inside range.")
 3114            : value;
 115
 116    /// <summary>
 117    /// Returns a string representation of the value range, indicating the minimum and maximum boundaries along with the
 118    /// </summary>
 119    /// <returns>A string representation of the value range.</returns>
 120    public override string ToString()
 121    {
 3122        var start = IsMinInclusive ? "[" : "]";
 3123        var end = IsMaxInclusive ? "]" : "[";
 124
 3125        return $"{start}{Min}; {Max}{end}";
 126    }
 127}
 128