< Summary

Information
Class: MyNet.Primitives.Conversion.DataSizeConverter
Assembly: MyNet.Primitives
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Conversion/DataSizeConverter.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 11
Uncovered lines: 0
Coverable lines: 11
Total lines: 49
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Convert(...)100%44100%
Pow1024(...)100%22100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Conversion/DataSizeConverter.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="DataSizeConverter.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7namespace MyNet.Primitives.Conversion;
 8
 9/// <summary>
 10/// Provides a converter for file size units, allowing conversion between different file size units such as bytes, kilob
 11/// </summary>
 12public sealed class DataSizeConverter : IUnitConverter<DataSizeUnit>
 13{
 14    /// <summary>
 15    /// Converts a file size value from one unit to another. The conversion is performed by calculating the difference i
 16    /// </summary>
 17    /// <param name="value">The file size value to convert.</param>
 18    /// <param name="from">The unit of the input value.</param>
 19    /// <param name="to">The unit to convert the value to.</param>
 20    /// <returns>The converted file size value.</returns>
 21    public double Convert(double value, DataSizeUnit from, DataSizeUnit to)
 22    {
 10523        var diff = to - from;
 24
 10525        return diff switch
 10526        {
 927            0 => value,
 7228            > 0 => value / Pow1024(diff),
 2429            _ => value * Pow1024(-diff)
 10530        };
 31    }
 32
 33    /// <summary>
 34    /// Calculates the power of 1024 for a given exponent. This method multiplies 1024 by itself the specified number of
 35    /// </summary>
 36    /// <param name="exp">The exponent to raise 1024 to.</param>
 37    /// <returns>The result of 1024 raised to the specified exponent.</returns>
 38    private static double Pow1024(int exp)
 39    {
 9640        double result = 1;
 41        const double baseValue = 1024;
 42
 60043        for (var i = 0; i < exp; i++)
 20444            result *= baseValue;
 45
 9646        return result;
 47    }
 48}
 49