Skip to content

Commit

Permalink
feat(SignificantDigitsNumberRounder): implement RoundDouble method
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadHadi2031 committed Sep 8, 2021
1 parent 59422ee commit e30b006
Show file tree
Hide file tree
Showing 6 changed files with 429 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.Globalization.NumberFormatting;
using WS = Windows.Globalization.NumberFormatting;

namespace Uno.UI.Tests.Windows_Globalization
{
[TestClass]
public class Given_SignificantDigitsNumberRounder
{
[DataTestMethod]
[DataRow(123.456, (uint)8, 123.456)]
[DataRow(123.456, (uint)6, 123.456)]
[DataRow(123.456, (uint)4, 123.5)]
[DataRow(123.456, (uint)2, 120)]
[DataRow(123.456, (uint)1, 100)]
[DataRow(123, (uint)5, 123)]
public void Test_SignificantDigitsNumberRounder(double value, uint significantDigits, double expected)
{
WS.SignificantDigitsNumberRounder rounder = new WS.SignificantDigitsNumberRounder();
rounder.SignificantDigits = significantDigits;

var rounded = rounder.RoundDouble(value);
Assert.AreEqual(expected, rounded);
}

[DataTestMethod]
[DataRow(1.25, 1.3)]
[DataRow(1.27, 1.3)]
[DataRow(1.23, 1.3)]
[DataRow(-1.25, -1.3)]
[DataRow(-1.27, -1.3)]
[DataRow(-1.23, -1.3)]
public void When_RoundingAlgorithm_Is_RoundAwayFromZero(double value, double expected)
{
TestRoundingAlgorithm(value, RoundingAlgorithm.RoundAwayFromZero, expected);
}

[DataTestMethod]
[DataRow(1.25, 1.3)]
[DataRow(1.27, 1.3)]
[DataRow(1.23, 1.2)]
[DataRow(-1.25, -1.3)]
[DataRow(-1.27, -1.3)]
[DataRow(-1.23, -1.2)]
public void When_RoundingAlgorithm_Is_RoundHalfAwayFromZero(double value, double expected)
{
TestRoundingAlgorithm(value, RoundingAlgorithm.RoundHalfAwayFromZero, expected);
}

[DataTestMethod]
[DataRow(1.25, 1.3)]
[DataRow(1.27, 1.3)]
[DataRow(1.23, 1.3)]
[DataRow(-1.25, -1.2)]
[DataRow(-1.27, -1.2)]
[DataRow(-1.23, -1.2)]
public void When_RoundingAlgorithm_Is_RoundUp(double value, double expected)
{
TestRoundingAlgorithm(value, RoundingAlgorithm.RoundUp, expected);
}

[DataTestMethod]
[DataRow(1.25, 1.3)]
[DataRow(1.27, 1.3)]
[DataRow(1.23, 1.2)]
[DataRow(-1.25, -1.2)]
[DataRow(-1.27, -1.3)]
[DataRow(-1.23, -1.2)]
public void When_RoundingAlgorithm_Is_RoundHalfUp(double value, double expected)
{
TestRoundingAlgorithm(value, RoundingAlgorithm.RoundHalfUp, expected);
}

[DataTestMethod]
[DataRow(1.25, 1.2)]
[DataRow(1.27, 1.2)]
[DataRow(1.23, 1.2)]
[DataRow(-1.25, -1.3)]
[DataRow(-1.27, -1.3)]
[DataRow(-1.23, -1.3)]
public void When_RoundingAlgorithm_Is_RoundDown(double value, double expected)
{
TestRoundingAlgorithm(value, RoundingAlgorithm.RoundDown, expected);
}

[DataTestMethod]
[DataRow(1.25, 1.2)]
[DataRow(1.27, 1.3)]
[DataRow(1.23, 1.2)]
[DataRow(-1.25, -1.3)]
[DataRow(-1.27, -1.3)]
[DataRow(-1.23, -1.2)]
public void When_RoundingAlgorithm_Is_RoundHalfDown(double value, double expected)
{
TestRoundingAlgorithm(value, RoundingAlgorithm.RoundHalfDown, expected);
}

[DataTestMethod]
[DataRow(1.25, 1.2)]
[DataRow(1.27, 1.2)]
[DataRow(1.23, 1.2)]
[DataRow(-1.25, -1.2)]
[DataRow(-1.27, -1.2)]
[DataRow(-1.23, -1.2)]
public void When_RoundingAlgorithm_Is_RoundTowardsZero(double value, double expected)
{
TestRoundingAlgorithm(value, RoundingAlgorithm.RoundTowardsZero, expected);
}

[DataTestMethod]
[DataRow(1.25, 1.2)]
[DataRow(1.27, 1.3)]
[DataRow(1.23, 1.2)]
[DataRow(-1.25, -1.2)]
[DataRow(-1.27, -1.3)]
[DataRow(-1.23, -1.2)]
public void When_RoundingAlgorithm_Is_RoundHalfTowardsZero(double value, double expected)
{
TestRoundingAlgorithm(value, RoundingAlgorithm.RoundHalfTowardsZero, expected);
}

[DataTestMethod]
[DataRow(1.25, 1.2)]
[DataRow(1.27, 1.3)]
[DataRow(1.23, 1.2)]
[DataRow(-1.25, -1.2)]
[DataRow(-1.27, -1.3)]
[DataRow(-1.23, -1.2)]
[DataRow(1.35, 1.4)]
[DataRow(1.37, 1.4)]
[DataRow(1.33, 1.3)]
[DataRow(-1.35, -1.4)]
[DataRow(-1.37, -1.4)]
[DataRow(-1.33, -1.3)]
public void When_RoundingAlgorithm_Is_RoundHalfToEven(double value, double expected)
{
TestRoundingAlgorithm(value, RoundingAlgorithm.RoundHalfToEven, expected);
}

[DataTestMethod]
[DataRow(1.25, 1.3)]
[DataRow(1.27, 1.3)]
[DataRow(1.23, 1.2)]
[DataRow(-1.25, -1.3)]
[DataRow(-1.27, -1.3)]
[DataRow(-1.23, -1.2)]
[DataRow(1.35, 1.3)]
[DataRow(1.37, 1.4)]
[DataRow(1.33, 1.3)]
[DataRow(-1.35, -1.3)]
[DataRow(-1.37, -1.4)]
[DataRow(-1.33, -1.3)]
public void When_RoundingAlgorithm_Is_RoundHalfToOdd(double value, double expected)
{
TestRoundingAlgorithm(value, RoundingAlgorithm.RoundHalfToOdd, expected);
}

[DataTestMethod]
[DataRow(double.NaN, double.NaN)]
[DataRow(double.NegativeInfinity, double.NaN)]
[DataRow(double.PositiveInfinity, double.NaN)]
public void When_Value_Is_Special(double value, double expected)
{
TestRoundingAlgorithm(value, RoundingAlgorithm.RoundHalfUp, expected);
}

private void TestRoundingAlgorithm(double value, RoundingAlgorithm roundingAlgorithm, double expected)
{
WS.SignificantDigitsNumberRounder rounder = new WS.SignificantDigitsNumberRounder();
rounder.SignificantDigits = 2;
rounder.RoundingAlgorithm = roundingAlgorithm;

var rounded = rounder.RoundDouble(value);
Assert.AreEqual(expected, rounded);
}


[TestMethod]
public void Should_Throw_When_RoundingAlgorithm_Is_None()
{
WS.SignificantDigitsNumberRounder rounder = new WS.SignificantDigitsNumberRounder();
Assert.ThrowsException<System.ArgumentException>(() => rounder.RoundingAlgorithm = RoundingAlgorithm.None);
}

[TestMethod]
public void Should_Throw_When_SignificantDigits_Is_Zero()
{
WS.SignificantDigitsNumberRounder rounder = new WS.SignificantDigitsNumberRounder();
Assert.ThrowsException<System.ArgumentException>(() => rounder.SignificantDigits = 0);
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,11 @@
#pragma warning disable 114 // new keyword hiding
namespace Windows.Globalization.NumberFormatting
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented]
#endif
public partial interface INumberRounder
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
int RoundInt32( int value);
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
uint RoundUInt32( uint value);
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
long RoundInt64( long value);
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
ulong RoundUInt64( ulong value);
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
float RoundSingle( float value);
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
double RoundDouble( double value);
#endif

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,13 @@
#pragma warning disable 114 // new keyword hiding
namespace Windows.Globalization.NumberFormatting
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented]
#endif
public enum RoundingAlgorithm
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
None,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
RoundDown,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
RoundUp,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
RoundTowardsZero,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
RoundAwayFromZero,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
RoundHalfDown,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
RoundHalfUp,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
RoundHalfTowardsZero,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
RoundHalfAwayFromZero,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
RoundHalfToEven,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
RoundHalfToOdd,
#endif

}
#endif
}
12 changes: 12 additions & 0 deletions src/Uno.UWP/Globalization/NumberFormatting/INumberRounder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Windows.Globalization.NumberFormatting
{
public partial interface INumberRounder
{
int RoundInt32( int value);
uint RoundUInt32( uint value);
long RoundInt64( long value);
ulong RoundUInt64( ulong value);
float RoundSingle( float value);
double RoundDouble( double value);
}
}
17 changes: 17 additions & 0 deletions src/Uno.UWP/Globalization/NumberFormatting/RoundingAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Windows.Globalization.NumberFormatting
{
public enum RoundingAlgorithm
{
None,
RoundDown,
RoundUp,
RoundTowardsZero,
RoundAwayFromZero,
RoundHalfDown,
RoundHalfUp,
RoundHalfTowardsZero,
RoundHalfAwayFromZero,
RoundHalfToEven,
RoundHalfToOdd,
}
}
Loading

0 comments on commit e30b006

Please sign in to comment.