-
Notifications
You must be signed in to change notification settings - Fork 742
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SignificantDigitsNumberRounder): implement RoundDouble method
- Loading branch information
1 parent
59422ee
commit e30b006
Showing
6 changed files
with
429 additions
and
61 deletions.
There are no files selected for viewing
199 changes: 199 additions & 0 deletions
199
src/Uno.UI.Tests/Windows_Globalization/Given_SignificantDigitsNumberRounder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/Uno.UWP/Globalization/NumberFormatting/INumberRounder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
src/Uno.UWP/Globalization/NumberFormatting/RoundingAlgorithm.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
Oops, something went wrong.