< Summary

Information
Class: MyNet.Primitives.IntervalExtensions
Assembly: MyNet.Primitives
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Intervals/Extensions/IntervalExtensions.cs
Tag: 323_28699572109
Line coverage
56%
Covered lines: 17
Uncovered lines: 13
Coverable lines: 30
Total lines: 139
Line coverage: 56.6%
Branch coverage
28%
Covered branches: 14
Total branches: 50
Branch coverage: 28%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsBefore(...)0%110100%
IsAfter(...)100%210%
Touches(...)62.5%1616100%
Overlaps(...)100%11100%
IsInside(...)100%210%
StartsBefore(...)0%2040%
EndsAfter(...)0%2040%
Clamp(...)50%88100%
IsEmpty(...)0%7280%
StartValue(...)100%11100%
EndValue(...)100%11100%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="IntervalExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using MyNet.Primitives.Intervals;
 9
 10#pragma warning disable IDE0130 // Namespace does not match folder structure
 11namespace MyNet.Primitives;
 12#pragma warning restore IDE0130 // Namespace does not match folder structure
 13
 14public static class IntervalExtensions
 15{
 16    extension<T>(IInterval<T> left)
 17        where T : struct, IComparable<T>
 18    {
 19        /// <summary>
 20        /// Determines whether the current interval is strictly before the specified interval. An interval A is consider
 21        /// </summary>
 22        /// <param name="right">The interval to compare with the current interval.</param>
 23        /// <returns><c>true</c> if the current interval is strictly before the specified interval; otherwise, <c>false<
 24        public bool IsBefore(IInterval<T> right)
 25        {
 026            if (!left.HasEnd || !right.HasStart)
 027                return false;
 28
 029            var comparison =
 030                left.End!.Value.Value.CompareTo(right.Start!.Value.Value);
 31
 032            return comparison < 0 || (comparison <= 0 && (!left.End.Value.IsInclusive || !right.Start.Value.IsInclusive)
 33        }
 34
 35        /// <summary>
 36        /// Determines whether the current interval is strictly after the specified interval. An interval A is considere
 37        /// </summary>
 38        /// <param name="right">The interval to compare with the current interval.</param>
 39        /// <returns><c>true</c> if the current interval is strictly after the specified interval; otherwise, <c>false</
 040        public bool IsAfter(IInterval<T> right) => right.IsBefore(left);
 41
 42        /// <summary>
 43        /// Determines whether the current interval touches the specified interval. Two intervals are considered to touc
 44        /// </summary>
 45        /// <param name="right">The interval to compare with the current interval.</param>
 46        /// <returns><c>true</c> if the current interval touches the specified interval; otherwise, <c>false</c>.</retur
 47        public bool Touches(IInterval<T> right)
 48        {
 1849            var touchesOnRight = false;
 1850            if (left.HasEnd && right.HasStart)
 51            {
 1852                var comparison = left.End!.Value.Value.CompareTo(right.Start!.Value.Value);
 1853                touchesOnRight = comparison == 0 && !(left.End.Value.IsInclusive && right.Start.Value.IsInclusive);
 54            }
 55
 1856            var touchesOnLeft = false;
 1857            if (right.HasEnd && left.HasStart)
 58            {
 1859                var comparison = right.End!.Value.Value.CompareTo(left.Start!.Value.Value);
 1860                touchesOnLeft = comparison == 0 && !(right.End.Value.IsInclusive && left.Start.Value.IsInclusive);
 61            }
 62
 1863            return touchesOnRight || touchesOnLeft;
 64        }
 65
 66        /// <summary>
 67        /// Determines whether the current interval overlaps with the specified interval. Two intervals are considered t
 68        /// </summary>
 69        /// <param name="right">The interval to compare with the current interval.</param>
 70        /// <returns><c>true</c> if the current interval overlaps with the specified interval; otherwise, <c>false</c>.<
 671        public bool Overlaps(IInterval<T> right) => left.Intersects(right);
 72
 73        /// <summary>
 74        /// Determines whether the current interval is entirely contained within the specified container interval. An in
 75        /// </summary>
 76        /// <param name="container">The interval to check against.</param>
 77        /// <returns><c>true</c> if the current interval is inside the specified container interval; otherwise, <c>false
 078        public bool IsInside(IInterval<T> container) => container.Contains(left);
 79
 80        /// <summary>
 81        /// Determines whether the current interval starts before the specified interval. An interval A is considered to
 82        /// </summary>
 83        /// <param name="right">The interval to compare with the current interval.</param>
 84        /// <returns><c>true</c> if the current interval starts before the specified interval; otherwise, <c>false</c>.<
 85        public bool StartsBefore(IInterval<T> right)
 086            => !left.HasStart ? right.HasStart : right.HasStart && left.Start!.Value.Value.CompareTo(right.Start!.Value.
 87
 88        /// <summary>
 89        /// Determines whether the current interval ends after the specified interval. An interval A is considered to en
 90        /// </summary>
 91        /// <param name="right">The interval to compare with the current interval.</param>
 92        /// <returns><c>true</c> if the current interval ends after the specified interval; otherwise, <c>false</c>.</re
 93        public bool EndsAfter(IInterval<T> right)
 094            => !left.HasEnd ? right.HasEnd : right.HasEnd && left.End!.Value.Value.CompareTo(right.End!.Value.Value) > 0
 95
 96        /// <summary>
 97        /// Clamps a value to be within the current interval. If the value is less than the start of the interval, it re
 98        /// </summary>
 99        /// <param name="value">The value to clamp.</param>
 100        /// <returns>The clamped value.</returns>
 101        public T Clamp(T value) =>
 6102            left.HasStart && value.CompareTo(left.Start!.Value.Value) < 0
 6103                ? left.Start.Value.Value
 6104                : left.HasEnd && value.CompareTo(left.End!.Value.Value) > 0
 6105                    ? left.End.Value.Value
 6106                    : value;
 107
 108        /// <summary>
 109        /// Determines whether the current interval is empty, meaning that it has no points in common. An interval is co
 110        /// </summary>
 111        /// <returns>True if the interval is empty; otherwise, false.</returns>
 112        public bool IsEmpty()
 113        {
 0114            if (!left.HasStart || !left.HasEnd)
 0115                return false;
 116
 0117            var comparison = left.Start!.Value.Value.CompareTo(left.End!.Value.Value);
 118
 0119            return comparison == 0 && (!left.Start.Value.IsInclusive || !left.End.Value.IsInclusive);
 120        }
 121    }
 122
 123    extension<T>(IBoundedInterval<T> interval)
 124        where T : struct, IComparable<T>
 125    {
 126        /// <summary>
 127        /// Gets the start value of the bounded interval.
 128        /// </summary>
 129        /// <returns>The start value of the interval.</returns>
 21130        public T StartValue() => interval.Start.Value;
 131
 132        /// <summary>
 133        /// Gets the end value of the bounded interval.
 134        /// </summary>
 135        /// <returns>The end value of the interval.</returns>
 18136        public T EndValue() => interval.End.Value;
 137    }
 138}
 139