using System; using System.Collections.Generic; using Amphora; using Tessera.Text; using Tessera.Unicode; using UnitTestSharp; using Tessera.Typesetting; namespace Tessera.UnitTests.Text { public class WidestNumberTests : TestFixture { public class WriteCountTests : TestFixture { public static string CountOf(NumberFormat format, int digits, bool negative = false) { var buffer = new char[64]; int end = WidestNumber.WriteCount(format, digits, negative, buffer, 0); return new string(buffer, 0, end); } public void OneDigit_IsANine() { CheckEqual("9", CountOf(NumberFormat.Invariant, 1)); } public void FiveDigits_AreGroupedByThree() { CheckEqual("99,999", CountOf(NumberFormat.Invariant, 5)); } public void ADigitCountThatFillsAGroupExactly_HasNoLeadingSeparator() { CheckEqual("999,999", CountOf(NumberFormat.Invariant, 6)); } public void Negative_PutsTheLanguagesMinusFirst() { CheckEqual("-9,999,999", CountOf(NumberFormat.Invariant, 7, negative: true)); } public void TheMostDigits_AreAllWritten() { CheckEqual("999,999,999,999,999,999", CountOf(NumberFormat.Invariant, WidestNumber.MaxCountDigits)); } public void ALanguageWithUnevenGroups_UsesThem() { CheckEqual("99,99,999", CountOf(NumberFormat.For("hi"), 7)); } public void ALanguageWithAnotherSeparator_UsesIt() { CheckEqual("99.999", CountOf(NumberFormat.For("de"), 5)); } public void ALanguageWithOtherDigits_WritesItsOwnNine() { NumberFormat arabic = NumberFormat.For("ar-EG"); CheckEqual(new string((char)(arabic.ZeroDigit + 9), 3), CountOf(arabic, 3)); } public void WritesAtThePosition_AndReturnsTheEnd() { var buffer = new char[16]; for (int i = 0; i < buffer.Length; ++i) { buffer[i] = 'x'; } int end = WidestNumber.WriteCount(NumberFormat.Invariant, 4, negative: false, buffer, 2); CheckEqual(7, end); CheckEqual("xx9,999xx", new string(buffer, 0, 9)); } public void ADestinationThatIsTooSmall_Throws() { CheckThrow(typeof(ArgumentException)); WidestNumber.WriteCount(NumberFormat.Invariant, 5, negative: false, new char[5], 0); } public void ANullFormat_Throws() { CheckThrow(typeof(ArgumentNullException)); WidestNumber.WriteCount(null, 3, negative: false, new char[8], 0); } public void NoDigits_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); WidestNumber.WriteCount(NumberFormat.Invariant, 0, negative: false, new char[8], 0); } public void MoreDigitsThanALongHolds_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); WidestNumber.WriteCount(NumberFormat.Invariant, WidestNumber.MaxCountDigits + 1, negative: false, new char[64], 0); } } public class WriteFixedTests : TestFixture { public static string FixedOf(NumberFormat format, int digits, int decimals, bool negative = false) { var buffer = new char[64]; int end = WidestNumber.WriteFixed(format, digits, decimals, negative, buffer, 0); return new string(buffer, 0, end); } public void WholeAndDecimalNines_AreWrittenWithTheSeparators() { CheckEqual("9,999.99", FixedOf(NumberFormat.Invariant, 4, 2)); } public void NoDecimals_WritesNoDecimalSeparator() { CheckEqual("999", FixedOf(NumberFormat.Invariant, 3, 0)); } public void Negative_PutsTheLanguagesMinusFirst() { CheckEqual("-99.9", FixedOf(NumberFormat.Invariant, 2, 1, negative: true)); } public void ALanguageWithAnotherDecimalSeparator_UsesIt() { CheckEqual("999,9", FixedOf(NumberFormat.For("de"), 3, 1)); } public void ALanguageWithUnevenGroups_GroupsTheWholePartOnly() { CheckEqual("99,99,999.99", FixedOf(NumberFormat.For("hi"), 7, 2)); } public void TheMostDigits_AreAllWritten() { CheckEqual("9,999,999,999,999,999,999.999999999", FixedOf(NumberFormat.Invariant, digits: 19, decimals: NumberFormat.MaxDecimals)); } public void ADestinationThatIsTooSmall_Throws() { CheckThrow(typeof(ArgumentException)); WidestNumber.WriteFixed(NumberFormat.Invariant, 4, 2, negative: false, new char[5], 0); } public void ANullFormat_Throws() { CheckThrow(typeof(ArgumentNullException)); WidestNumber.WriteFixed(null, 3, 1, negative: false, new char[8], 0); } public void NoWholeDigits_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); WidestNumber.WriteFixed(NumberFormat.Invariant, 0, 2, negative: false, new char[8], 0); } public void NegativeDecimals_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); WidestNumber.WriteFixed(NumberFormat.Invariant, 3, -1, negative: false, new char[8], 0); } public void MoreDigitsThanADecimalHolds_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); WidestNumber.WriteFixed(NumberFormat.Invariant, WidestNumber.MaxFixedDigits, 1, negative: false, new char[64], 0); } } public class WritePercentTests : TestFixture { public static string PercentOf(NumberFormat format, int digits, int decimals, bool negative = false) { var buffer = new char[64]; int end = WidestNumber.WritePercent(format, digits, decimals, negative, buffer, 0); return new string(buffer, 0, end); } public void WholeAndDecimalNines_EndWithThePercentSign() { CheckEqual("99.9%", PercentOf(NumberFormat.Invariant, 2, 1)); } public void Negative_PutsTheMinusFirst() { CheckEqual("-999%", PercentOf(NumberFormat.Invariant, 3, 0, negative: true)); } public void ALanguageThatPutsThePercentSignFirst_DoesSo() { CheckEqual("%99,9", PercentOf(NumberFormat.For("tr"), 2, 1)); } public void TheMostDigits_AreAllWritten() { CheckEqual("99.9999999%", PercentOf(NumberFormat.Invariant, 2, NumberFormat.MaxPercentDecimals)); } public void ANullFormat_Throws() { CheckThrow(typeof(ArgumentNullException)); WidestNumber.WritePercent(null, 2, 1, negative: false, new char[8], 0); } public void NoWholeDigits_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); WidestNumber.WritePercent(NumberFormat.Invariant, 0, 1, negative: false, new char[8], 0); } public void NegativeDecimals_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); WidestNumber.WritePercent(NumberFormat.Invariant, 2, -1, negative: false, new char[8], 0); } public void MoreDigitsThanADoubleHolds_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); WidestNumber.WritePercent(NumberFormat.Invariant, 9, 7, negative: false, new char[64], 0); } } public class WriteScientificTests : TestFixture { public static string ScientificOf(NumberFormat format, int significant, int exponentDigits, bool negative = false) { var buffer = new char[64]; int end = WidestNumber.WriteScientific(format, significant, exponentDigits, negative, buffer, 0); return new string(buffer, 0, end); } public void IsTheNinesWithAMinusSignedExponentOfTheDigitsAsked() { CheckEqual("9.99e-99", ScientificOf(NumberFormat.Invariant, 3, 2)); } public void NegativeAddsAMinusSignToTheMantissa() { CheckEqual("-9.99e-99", ScientificOf(NumberFormat.Invariant, 3, 2, negative: true)); } public void TheMostSignificantDigits_AreAllWritten() { CheckEqual("9.99999999999999e-99", ScientificOf(NumberFormat.Invariant, NumberFormat.MaxSignificantDigits, 2)); } public void ANullFormat_Throws() { CheckThrow(typeof(ArgumentNullException)); WidestNumber.WriteScientific(null, 3, 2, negative: false, new char[16], 0); } public void NoSignificantDigits_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ScientificOf(NumberFormat.Invariant, 0, 2); } public void NoExponentDigits_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ScientificOf(NumberFormat.Invariant, 3, 0); } public void MoreExponentDigitsThanADoubleHas_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ScientificOf(NumberFormat.Invariant, 3, WidestNumber.MaxExponentDigits + 1); } } public class RequireDurationDigitsTests : TestFixture { public void ADurationAUnitAllows_Passes() { WidestNumber.RequireDurationDigits(new DurationFormat { Largest = DurationUnit.Days }, 10); } public void ADurationLongerThanADurationCanHold_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); WidestNumber.RequireDurationDigits(new DurationFormat { Largest = DurationUnit.Days }, 11); } public void NoDigits_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); WidestNumber.RequireDurationDigits(new DurationFormat(), 0); } public void MoreDigitsThanALongHolds_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); WidestNumber.RequireDurationDigits(new DurationFormat { Largest = DurationUnit.Seconds }, WidestNumber.MaxCountDigits + 1); } public void TheDefaultUnitsAreHours_SoElevenDigitsPass() { WidestNumber.RequireDurationDigits(new DurationFormat(), 11); } public void TheDefaultUnitsAreHours_SoTwelveDigitsThrow() { CheckThrow(typeof(ArgumentOutOfRangeException)); WidestNumber.RequireDurationDigits(new DurationFormat(), 12); } } public class WriteDurationTests : TestFixture { public static string DurationOf(NumberFormat format, DurationFormat durationFormat, int digits, bool negative = false) { var buffer = new char[128]; int end = WidestNumber.WriteDuration(format, durationFormat, digits, negative, buffer, 0); return new string(buffer, 0, end); } public void TheDefaultUnits_AreHoursAndMinutes() { CheckEqual("99h 59m", DurationOf(NumberFormat.Invariant, new DurationFormat(), 2)); } public void ADurationInMinutesAndSeconds_HasSecondsAtFiftyNine() { var format = new DurationFormat { Largest = DurationUnit.Minutes, Smallest = DurationUnit.Seconds }; CheckEqual("999m 59s", DurationOf(NumberFormat.Invariant, format, 3)); } public void Negative_PutsTheLanguagesMinusFirst() { CheckEqual("-9h 59m", DurationOf(NumberFormat.Invariant, new DurationFormat(), 1, negative: true)); } public void ANullFormat_Throws() { CheckThrow(typeof(ArgumentNullException)); WidestNumber.WriteDuration(null, new DurationFormat(), 2, negative: false, new char[16], 0); } public void TooManyDigits_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); DurationOf(NumberFormat.Invariant, new DurationFormat { Largest = DurationUnit.Days }, 11); } } public class WriteClockTests : TestFixture { public static string ClockOf(NumberFormat format, ClockFormat clockFormat, bool afternoon) { var buffer = new char[64]; int end = WidestNumber.WriteClock(format, clockFormat, afternoon, buffer, 0); return new string(buffer, 0, end); } public void TheMorning_IsEleventyNineAm() { CheckEqual("11:59 AM", ClockOf(NumberFormat.Invariant, ClockFormat.Hours12, afternoon: false)); } public void TheAfternoon_IsElevenFiftyNinePm() { CheckEqual("11:59 PM", ClockOf(NumberFormat.Invariant, ClockFormat.Hours12, afternoon: true)); } public void ATwentyFourHourClock_ShowsTheHourAsItIs() { CheckEqual("23:59", ClockOf(NumberFormat.Invariant, ClockFormat.Hours24, afternoon: true)); } public void SecondsAreWrittenWhenTheFormatAsksForThem() { var format = ClockFormat.Hours24 with { Seconds = true }; CheckEqual("11:59:59", ClockOf(NumberFormat.Invariant, format, afternoon: false)); } public void ANullFormat_Throws() { CheckThrow(typeof(ArgumentNullException)); WidestNumber.WriteClock(null, ClockFormat.Hours12, afternoon: false, new char[16], 0); } } public class ShortVariantCountTests : TestFixture { public void IsTheScientificFormTheWrittenOutFormAndEveryRoom() { CheckEqual(2 + ShortNotation.English.RoomCount, WidestNumber.ShortVariantCount(NumberFormat.Invariant)); } public void ANullFormat_Throws() { CheckThrow(typeof(ArgumentNullException)); WidestNumber.ShortVariantCount(null); } } public class WriteShortTests : TestFixture { public static string ShortOf(NumberFormat format, int significant, int variant, bool negative = false) { var buffer = new char[128]; int end = WidestNumber.WriteShort(format, significant, negative, variant, buffer, 0); return new string(buffer, 0, end); } public void VariantZero_IsTheScientificFormWithAThreeDigitMinusSignedExponent() { CheckEqual("9.99e-300", ShortOf(NumberFormat.Invariant, 3, variant: 0)); } public void VariantOne_IsTheLongestNumberWrittenOutBelowOne() { CheckEqual("0.000999", ShortOf(NumberFormat.Invariant, 3, variant: 1)); } public void VariantOneOfOneSignificantDigit_HasNoFraction() { CheckEqual("0.0009", ShortOf(NumberFormat.Invariant, 1, variant: 1)); } public void ANegativeNumber_HasAMinusSignInEveryForm() { CheckEqual("-9.99e-300", ShortOf(NumberFormat.Invariant, 3, variant: 0, negative: true)); CheckEqual("-0.000999", ShortOf(NumberFormat.Invariant, 3, variant: 1, negative: true)); CheckEqual("-9.99", ShortOf(NumberFormat.Invariant, 3, variant: 2, negative: true)); CheckEqual("-9.99K", ShortOf(NumberFormat.Invariant, 3, variant: 4, negative: true)); } public void ANumberWrittenOutWithOneWholeDigit_HasTheSignificantDigits() { CheckEqual("9.99", ShortOf(NumberFormat.Invariant, 3, variant: 2)); CheckEqual("9", ShortOf(NumberFormat.Invariant, 1, variant: 2)); } public void ANumberWrittenOutWithAsManyWholeDigitsAsSignificantOnes_HasNoSeparator() { CheckEqual("999", ShortOf(NumberFormat.Invariant, 3, variant: 3)); } public void ANumberWithFewerWholeDigitsThanSignificantOnes_HasTheRestAsFraction() { CheckEqual("999.99", ShortOf(NumberFormat.Invariant, 5, variant: 3)); } public void ANumberWithMoreWholeDigitsThanSignificantOnes_KeepsThemAll() { CheckEqual("999", ShortOf(NumberFormat.Invariant, 1, variant: 3)); CheckEqual("999K", ShortOf(NumberFormat.Invariant, 2, variant: 5)); } public void ATiersRoom_HasItsTextAroundTheDigits() { CheckEqual("9.99K", ShortOf(NumberFormat.Invariant, 3, variant: 4)); CheckEqual("999K", ShortOf(NumberFormat.Invariant, 3, variant: 5)); CheckEqual("999.99K", ShortOf(NumberFormat.Invariant, 5, variant: 5)); CheckEqual("9.99M", ShortOf(NumberFormat.Invariant, 3, variant: 6)); } public void ATiersTextBeforeTheDigits_ComesBeforeThem() { var notation = new ShortNotation(3, new[] { new ShortTier(3, UnitPattern.Parse("x{0}y")) }, PluralRules.None); var format = new NumberFormat(",", 3, 3, "-", ".", "NaN", "Infinity", "e", notation); CheckEqual("x9.99y", ShortOf(format, 3, variant: 4)); } public void AMinusSignAfterATiersText_FollowsIt() { var notation = new ShortNotation(3, new[] { new ShortTier(3, UnitPattern.Parse("x{0}y"), signAfterText: true) }, PluralRules.None); var format = new NumberFormat(",", 3, 3, "-", ".", "NaN", "Infinity", "e", notation); CheckEqual("x-9.99y", ShortOf(format, 3, variant: 4, negative: true)); } public void ALanguageWithOtherSeparators_WritesItsOwn() { NumberFormat german = NumberFormat.For("de-DE"); CheckEqual("999,99", ShortOf(german, 5, variant: 3)); } public void TheLastVariant_IsTheLastTiersWidestForm() { string suffix = ShortNotation.English.TierOf(NumberFormat.LargestExponent) .PatternFor(PluralCategory.Other).After; int last = WidestNumber.ShortVariantCount(NumberFormat.Invariant) - 1; CheckEqual("999.999999999999" + suffix, ShortOf(NumberFormat.Invariant, 15, last)); } public void ANullFormat_Throws() { CheckThrow(typeof(ArgumentNullException)); WidestNumber.WriteShort(null, 3, negative: false, 0, new char[16], 0); } public void NoSignificantDigits_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ShortOf(NumberFormat.Invariant, 0, variant: 0); } public void MoreSignificantDigitsThanADoubleHolds_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ShortOf(NumberFormat.Invariant, NumberFormat.MaxSignificantDigits + 1, variant: 0); } public void ANegativeVariant_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ShortOf(NumberFormat.Invariant, 3, variant: -1); } public void AVariantPastTheLast_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ShortOf(NumberFormat.Invariant, 3, WidestNumber.ShortVariantCount(NumberFormat.Invariant)); } } } }