mirror of
https://github.com/Radarr/Radarr
synced 2026-05-08 14:40:51 +02:00
Prevent overflow exception for big numbers in SizeSuffix and Fluent.Round
(cherry picked from commit be4a564456818fc18138bded9d981c93bb2cdd14)
This commit is contained in:
parent
7dd5365ccd
commit
0134fdedca
4 changed files with 18 additions and 7 deletions
|
|
@ -17,6 +17,8 @@ public class NumberExtensionFixture
|
||||||
[TestCase(-1000000, "-976.6 KB")]
|
[TestCase(-1000000, "-976.6 KB")]
|
||||||
[TestCase(-377487360, "-360.0 MB")]
|
[TestCase(-377487360, "-360.0 MB")]
|
||||||
[TestCase(-1255864686, "-1.2 GB")]
|
[TestCase(-1255864686, "-1.2 GB")]
|
||||||
|
[TestCase(long.MinValue, "-8.0 EB")]
|
||||||
|
[TestCase(long.MaxValue, "8.0 EB")]
|
||||||
public void should_calculate_string_correctly(long bytes, string expected)
|
public void should_calculate_string_correctly(long bytes, string expected)
|
||||||
{
|
{
|
||||||
bytes.SizeSuffix().Should().Be(expected);
|
bytes.SizeSuffix().Should().Be(expected);
|
||||||
|
|
|
||||||
|
|
@ -11,16 +11,21 @@ public static string SizeSuffix(this long bytes)
|
||||||
{
|
{
|
||||||
const int bytesInKb = 1024;
|
const int bytesInKb = 1024;
|
||||||
|
|
||||||
if (bytes < 0)
|
|
||||||
{
|
|
||||||
return "-" + SizeSuffix(-bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bytes == 0)
|
if (bytes == 0)
|
||||||
{
|
{
|
||||||
return "0 B";
|
return "0 B";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (bytes == long.MinValue)
|
||||||
|
{
|
||||||
|
return "-" + SizeSuffix(long.MaxValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bytes < 0)
|
||||||
|
{
|
||||||
|
return "-" + SizeSuffix(Math.Abs(bytes));
|
||||||
|
}
|
||||||
|
|
||||||
var mag = (int)Math.Log(bytes, bytesInKb);
|
var mag = (int)Math.Log(bytes, bytesInKb);
|
||||||
var adjustedSize = bytes / (decimal)Math.Pow(bytesInKb, mag);
|
var adjustedSize = bytes / (decimal)Math.Pow(bytesInKb, mag);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -175,7 +175,9 @@ public void MinOrDefault_should_return_zero_when_collection_is_null()
|
||||||
[TestCase(199, 100, 100)]
|
[TestCase(199, 100, 100)]
|
||||||
[TestCase(1000, 100, 1000)]
|
[TestCase(1000, 100, 1000)]
|
||||||
[TestCase(0, 100, 0)]
|
[TestCase(0, 100, 0)]
|
||||||
public void round_to_level(long number, int level, int result)
|
[TestCase(long.MinValue, 1000, -9223372036854775000L)]
|
||||||
|
[TestCase(long.MaxValue, 1000, 9223372036854775000L)]
|
||||||
|
public void round_to_level(long number, int level, long result)
|
||||||
{
|
{
|
||||||
number.Round(level).Should().Be(result);
|
number.Round(level).Should().Be(result);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,9 @@ public static string WithDefault(this string actual, object defaultValue)
|
||||||
|
|
||||||
public static long Round(this long number, long level)
|
public static long Round(this long number, long level)
|
||||||
{
|
{
|
||||||
return Convert.ToInt64(Math.Floor((decimal)number / level) * level);
|
return number < 0
|
||||||
|
? Convert.ToInt64(Math.Ceiling((decimal)number / level) * level)
|
||||||
|
: Convert.ToInt64(Math.Floor((decimal)number / level) * level);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string ToBestDateString(this DateTime dateTime)
|
public static string ToBestDateString(this DateTime dateTime)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue