< Summary

Information
Class: DirectSight.Common.EnumerableExtensions
Assembly DirectSight
File(s): /home/runner/work/DirectSight/DirectSight/DirectSight/Common/EnumerableExtensions.cs
Line coverage
40%
Covered lines: 29
Uncovered lines: 43
Coverable lines: 72
Total lines: 188
Line coverage: 40.2%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
SafeSum(...)100%11100%
SafeSum(...)100%1162.5%
SafeSumNullable(...)0%220%
SafeSum(...)100%11100%
SafeSum(...)100%110%
SafeSum(...)100%11100%
SafeSum(...)100%110%
KeepElementByCount(...)0%440%
TakeLast()0%220%

File(s)

/home/runner/work/DirectSight/DirectSight/DirectSight/Common/EnumerableExtensions.cs

#LineLine coverage
 1#if !NETSTANDARD2_1_OR_GREATER && !NETCOREAPP_2_0_OR_GREATER
 2
 3using System;
 4using System.Collections.Generic;
 5using System.Linq;
 6
 7namespace DirectSight.Common;
 8
 9/// <summary>
 10/// Extension methods on <see cref="IEnumerable{T}"/>.
 11/// </summary>
 12internal static class EnumerableExtensions
 13{
 14    /// <summary>
 15    /// Computes the sum of the sequence of System.Int32 values that are obtained by invoking a transform function on ea
 16    /// </summary>
 17    /// <typeparam name="TSource">The type of the elements of source.</typeparam>
 18    /// <param name="source">A sequence of values that are used to calculate a sum.</param>
 19    /// <param name="selector">A transform function to apply to each element.</param>
 20    /// <returns>The sum of the projected values.</returns>
 21    public static int SafeSum<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector)
 915622    {
 23        checked
 915624        {
 25            try
 915626            {
 915627                return source.Sum(selector);
 28            }
 129            catch (OverflowException)
 130            {
 131                return int.MaxValue;
 32            }
 33        }
 915634    }
 35
 36    /// <summary>
 37    /// Computes the sum of the sequence of System.Int32 values that are obtained by invoking a transform function on ea
 38    /// </summary>
 39    /// <typeparam name="TSource">The type of the elements of source.</typeparam>
 40    /// <param name="source">A sequence of values that are used to calculate a sum.</param>
 41    /// <param name="selector">A transform function to apply to each element.</param>
 42    /// <returns>The sum of the projected values.</returns>
 43    public static int? SafeSum<TSource>(this IEnumerable<TSource> source, Func<TSource, int?> selector)
 361844    {
 45        checked
 361846        {
 47            try
 361848            {
 361849                return source.Sum(selector);
 50            }
 051            catch (OverflowException)
 052            {
 053                return int.MaxValue;
 54            }
 55        }
 361856    }
 57
 58    /// <summary>
 59    /// Computes the sum of the sequence of System.Int32 values that are obtained by invoking a transform function on ea
 60    /// </summary>
 61    /// <typeparam name="TSource">The type of the elements of source.</typeparam>
 62    /// <param name="source">A sequence of values that are used to calculate a sum.</param>
 63    /// <param name="selector">A transform function to apply to each element.</param>
 64    /// <returns>The sum of the projected values.</returns>
 65    public static int? SafeSumNullable<TSource>(this IEnumerable<TSource> source, Func<TSource, int?> selector)
 066    {
 067        if (source.All(x => !selector(x).HasValue))
 068        {
 069            return null;
 70        }
 71
 072        return source.SafeSum(selector);
 073    }
 74
 75    /// <summary>
 76    /// Computes the sum of the sequence of System.Int64 values that are obtained by invoking a transform function on ea
 77    /// </summary>
 78    /// <typeparam name="TSource">The type of the elements of source.</typeparam>
 79    /// <param name="source">A sequence of values that are used to calculate a sum.</param>
 80    /// <param name="selector">A transform function to apply to each element.</param>
 81    /// <returns>The sum of the projected values.</returns>
 82    public static long SafeSum<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector)
 283    {
 84        checked
 285        {
 86            try
 287            {
 288                return source.Sum(selector);
 89            }
 190            catch (OverflowException)
 191            {
 192                return long.MaxValue;
 93            }
 94        }
 295    }
 96
 97    /// <summary>
 98    /// Computes the sum of the sequence of System.Int64 values that are obtained by invoking a transform function on ea
 99    /// </summary>
 100    /// <typeparam name="TSource">The type of the elements of source.</typeparam>
 101    /// <param name="source">A sequence of values that are used to calculate a sum.</param>
 102    /// <param name="selector">A transform function to apply to each element.</param>
 103    /// <returns>The sum of the projected values.</returns>
 104    public static long? SafeSum<TSource>(this IEnumerable<TSource> source, Func<TSource, long?> selector)
 0105    {
 106        checked
 0107        {
 108            try
 0109            {
 0110                return source.Sum(selector);
 111            }
 0112            catch (OverflowException)
 0113            {
 0114                return long.MaxValue;
 115            }
 116        }
 0117    }
 118
 119    /// <summary>
 120    /// Computes the sum of the sequence of decimal values that are obtained by invoking a transform function on each el
 121    /// </summary>
 122    /// <typeparam name="TSource">The type of the elements of source.</typeparam>
 123    /// <param name="source">A sequence of values that are used to calculate a sum.</param>
 124    /// <param name="selector">A transform function to apply to each element.</param>
 125    /// <returns>The sum of the projected values.</returns>
 126    public static decimal SafeSum<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector)
 2127    {
 128        checked
 2129        {
 130            try
 2131            {
 2132                return source.Sum(selector);
 133            }
 1134            catch (OverflowException)
 1135            {
 1136                return decimal.MaxValue;
 137            }
 138        }
 2139    }
 140
 141    /// <summary>
 142    /// Computes the sum of the sequence of decimal values that are obtained by invoking a transform function on each el
 143    /// </summary>
 144    /// <typeparam name="TSource">The type of the elements of source.</typeparam>
 145    /// <param name="source">A sequence of values that are used to calculate a sum.</param>
 146    /// <param name="selector">A transform function to apply to each element.</param>
 147    /// <returns>The sum of the projected values.</returns>
 148    public static decimal? SafeSum<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
 0149    {
 150        checked
 0151        {
 152            try
 0153            {
 0154                return source.Sum(selector);
 155            }
 0156            catch (OverflowException)
 0157            {
 0158                return decimal.MaxValue;
 159            }
 160        }
 0161    }
 162
 163    private static IEnumerable<T> KeepElementByCount<T>(this IEnumerable<T> source, int count)
 0164    {
 0165        var queue = new Queue<T>(count);
 0166        foreach (var item in source)
 0167        {
 0168            if (queue.Count == count)
 0169            {
 0170                queue.Dequeue();
 0171            }
 172
 0173            queue.Enqueue(item);
 0174        }
 175
 0176        return queue;
 0177    }
 178
 179    private static IEnumerable<T> TakeLast<T>(IList<T> list, int count)
 0180    {
 0181        for (int i = list.Count - count; i < list.Count; i++)
 0182        {
 0183            yield return list[i];
 0184        }
 0185    }
 186}
 187
 188#endif