using System; using Tessera.Text; using UnitTestSharp; namespace Tessera.UnitTests.Text { public class CounterSpecTests : TestFixture { private static CounterSpec CountOf(int digits, bool negative = false, CounterAlignment alignment = CounterAlignment.Right) { return new CounterSpec(NumberFormat.Invariant, NumberKind.Count, digits, precision: 0, negative, alignment); } public static CounterSpec Scientific(int exponentDigits, int significant, bool negative = false, NumberFormat format = null) { return new CounterSpec(format ?? NumberFormat.Invariant, NumberKind.Scientific, exponentDigits, significant, negative, CounterAlignment.Right); } public static CounterSpec ShortNumber(int digits, int significant, bool negative = false, NumberFormat format = null) { return new CounterSpec(format ?? NumberFormat.Invariant, NumberKind.ShortExponent, digits, significant, negative, CounterAlignment.Right); } public static CounterSpec Duration(int digits, DurationFormat durationFormat, bool negative = false, NumberFormat format = null) { return new CounterSpec(format ?? NumberFormat.Invariant, NumberKind.Duration, digits, durationFormat.Pack(), negative, CounterAlignment.Right); } public static CounterSpec Clock(int digits, bool negative, ClockFormat clockFormat, NumberFormat format = null) { return new CounterSpec(format ?? NumberFormat.Invariant, NumberKind.Clock, digits, clockFormat.Pack(), negative, CounterAlignment.Right); } public class CtorTests : TestFixture { public void KeepsEveryValue() { var spec = new CounterSpec(NumberFormat.Invariant, NumberKind.Fixed, 4, 2, negative: true, CounterAlignment.Digit); CheckTrue(ReferenceEquals(NumberFormat.Invariant, spec.Format)); CheckEqual(NumberKind.Fixed, spec.Kind); CheckEqual(4, spec.Digits); CheckEqual(2, spec.Precision); CheckTrue(spec.Negative); CheckEqual(CounterAlignment.Digit, spec.Alignment); } public void ANullFormat_Throws() { CheckThrow(typeof(ArgumentNullException)); new CounterSpec(null, NumberKind.Count, 3, 0, negative: false, CounterAlignment.Right); } public void AKindThatCantBeACounter_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CounterSpec(NumberFormat.Invariant, NumberKind.Plain, 3, 0, negative: false, CounterAlignment.Right); } public void AShortNumberWithDigits_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ShortNumber(digits: 3, significant: 3); } public void AShortNumberWithNoSignificantDigits_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ShortNumber(digits: 0, significant: 0); } public void AShortNumberWithMoreSignificantDigitsThanItCanWrite_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ShortNumber(digits: 0, significant: NumberFormat.MaxSignificantDigits + 1); } public void AShortNumberWithTheMostSignificantDigits_Works() { CheckEqual(NumberFormat.MaxSignificantDigits, ShortNumber(digits: 0, significant: NumberFormat.MaxSignificantDigits).Precision); } public void AScientificNumberWithNoSignificantDigits_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Scientific(exponentDigits: 2, significant: 0); } public void AScientificNumberWithMoreSignificantDigitsThanItCanWrite_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Scientific(exponentDigits: 2, significant: NumberFormat.MaxSignificantDigits + 1); } public void AScientificNumberWithNoExponentDigits_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Scientific(exponentDigits: 0, significant: 3); } public void AScientificNumberWithMoreExponentDigitsThanADoubleHas_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Scientific(exponentDigits: WidestNumber.MaxExponentDigits + 1, significant: 3); } public void AScientificNumberWithTheMostOfEach_Works() { CheckEqual(WidestNumber.MaxExponentDigits, Scientific(WidestNumber.MaxExponentDigits, NumberFormat.MaxSignificantDigits).Digits); } public void ADurationWithNoDigits_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Duration(digits: 0, new DurationFormat()); } public void ADurationLongerThanADurationCanHold_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Duration(digits: 11, new DurationFormat { Largest = DurationUnit.Days }); } public void ADurationOfTheMostDigitsItsUnitAllows_Works() { CheckEqual(10, Duration(digits: 10, new DurationFormat { Largest = DurationUnit.Days }).Digits); } public void ADurationInSecondsOfTheMostDigitsItAllows_Works() { CheckEqual(15, Duration(digits: 15, new DurationFormat { Largest = DurationUnit.Seconds }).Digits); } public void ADurationInSecondsOfMoreDigitsThanItAllows_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Duration(digits: 16, new DurationFormat { Largest = DurationUnit.Seconds }); } public void ADurationWhoseLargestUnitIsSmallerThanItsSmallest_Throws() { CheckThrow(typeof(ArgumentException)); Duration(digits: 2, new DurationFormat { Largest = DurationUnit.Seconds, Smallest = DurationUnit.Days }); } public void AClockWithDigits_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Clock(digits: 2, negative: false, new ClockFormat()); } public void ANegativeClock_Throws() { CheckThrow(typeof(ArgumentException)); Clock(digits: 0, negative: true, new ClockFormat()); } public void AClockWithNoDigitsAndNoSign_Works() { CheckEqual(0, Clock(digits: 0, negative: false, new ClockFormat()).Digits); } public void ACountWithNoDigits_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); CountOf(0); } public void ACountWithMoreDigitsThanItCanWrite_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); CountOf(WidestNumber.MaxCountDigits + 1); } public void ACountWithTheMostDigits_Works() { CheckEqual(WidestNumber.MaxCountDigits, CountOf(WidestNumber.MaxCountDigits).Digits); } public void AFixedNumberWithNoDigits_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CounterSpec(NumberFormat.Invariant, NumberKind.Fixed, 0, 2, negative: false, CounterAlignment.Right); } public void AFixedNumberWithNegativePlaces_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CounterSpec(NumberFormat.Invariant, NumberKind.Fixed, 3, -1, negative: false, CounterAlignment.Right); } public void AFixedNumberWithMorePlacesThanItCanWrite_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CounterSpec(NumberFormat.Invariant, NumberKind.Fixed, 3, NumberFormat.MaxDecimals + 1, negative: false, CounterAlignment.Right); } public void AFixedNumberWithMoreDigitsThanItCanHold_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CounterSpec(NumberFormat.Invariant, NumberKind.Fixed, WidestNumber.MaxFixedDigits, 1, negative: false, CounterAlignment.Right); } public void APercentWithMorePlacesThanItCanWrite_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CounterSpec(NumberFormat.Invariant, NumberKind.Percent, 2, NumberFormat.MaxPercentDecimals + 1, negative: false, CounterAlignment.Right); } public void APercentWithMoreDigitsThanItCanHold_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CounterSpec(NumberFormat.Invariant, NumberKind.Percent, WidestNumber.MaxPercentDigits, 1, negative: false, CounterAlignment.Right); } } public class IsSetTests : TestFixture { public void ADefaultSpec_IsNotACounter() { CheckFalse(default(CounterSpec).IsSet); } public void ASpecMadeWithAFormat_Is() { CheckTrue(CountOf(3).IsSet); } } public class WriteWidestTests : TestFixture { private static string Written(CounterSpec spec) { var buffer = new char[64]; int end = spec.WriteWidest(buffer, 0, variant: 0); return new string(buffer, 0, end); } public void ACount_IsWrittenAsTheWidestCount() { CheckEqual("-99,999", Written(CountOf(5, negative: true))); } public void AFixedNumber_IsWrittenAsTheWidestFixedNumber() { var spec = new CounterSpec(NumberFormat.Invariant, NumberKind.Fixed, 3, 2, negative: false, CounterAlignment.Left); CheckEqual("999.99", Written(spec)); } public void APercent_IsWrittenAsTheWidestPercent() { var spec = new CounterSpec(NumberFormat.Invariant, NumberKind.Percent, 2, 1, negative: false, CounterAlignment.Left); CheckEqual("99.9%", Written(spec)); } private static string WrittenVariant(CounterSpec spec, int variant) { var buffer = new char[64]; int end = spec.WriteWidest(buffer, 0, variant); return new string(buffer, 0, end); } public void AScientificNumber_IsWrittenWithAMinusSignedExponentOfTheDigitsAsked() { CheckEqual("9.99e-99", Written(Scientific(exponentDigits: 2, significant: 3))); } public void AScientificNumberWithOneExponentDigit_HasOne() { CheckEqual("9.99e-9", Written(Scientific(exponentDigits: 1, significant: 3))); } public void AScientificNumberWithThreeExponentDigits_HasThree() { CheckEqual("9.99e-300", Written(Scientific(exponentDigits: 3, significant: 3))); } public void AScientificNumberOfOneSignificantDigit_HasNoSeparator() { CheckEqual("9e-99", Written(Scientific(exponentDigits: 2, significant: 1))); } public void ANegativeScientificNumber_HasAMinusSignBeforeItsMantissa() { CheckEqual("-9.99e-99", Written(Scientific(exponentDigits: 2, significant: 3, negative: true))); } public void AScientificNumber_IsWrittenInTheLanguagesOwnSymbols() { NumberFormat german = NumberFormat.For("de-DE"); CheckEqual("9,99" + german.ExponentSymbol + german.MinusSign + "99", Written(Scientific(exponentDigits: 2, significant: 3, format: german))); } public void ADuration_IsWrittenWithEverySmallerUnitAtItsMost() { CheckEqual("99h 59m", Written(Duration(digits: 2, new DurationFormat()))); } public void ADurationFromDaysToSeconds_IsWrittenInAllFourUnits() { var format = new DurationFormat { Largest = DurationUnit.Days, Smallest = DurationUnit.Seconds }; CheckEqual("999d 23h 59m 59s", Written(Duration(digits: 3, format))); } public void ADurationThatStopsAtItsLargestUnit_IsWrittenInOne() { var format = new DurationFormat { Largest = DurationUnit.Hours, Smallest = DurationUnit.Hours }; CheckEqual("99h", Written(Duration(digits: 2, format))); } public void ADurationInSeconds_HasNoSmallerUnit() { var format = new DurationFormat { Largest = DurationUnit.Seconds, Smallest = DurationUnit.Seconds }; CheckEqual("999s", Written(Duration(digits: 3, format))); } public void ADurationWithAPad_IsWrittenAsTheUnpaddedOne() { var format = new DurationFormat { Pad = DurationPad.Zero }; CheckEqual("99h 59m", Written(Duration(digits: 2, format))); } public void ANegativeDuration_HasAMinusSignInFront() { CheckEqual("-99h 59m", Written(Duration(digits: 2, new DurationFormat(), negative: true))); } public void AClockOfTwelveHours_IsWrittenAtTheLastMinuteOfTheMorningFirst() { CheckEqual("11:59 AM", WrittenVariant(Clock(0, false, ClockFormat.Hours12), variant: 0)); } public void AClockOfTwelveHours_IsWrittenAtTheLastMinuteOfTheDaySecond() { CheckEqual("11:59 PM", WrittenVariant(Clock(0, false, ClockFormat.Hours12), variant: 1)); } public void AClockOfTwentyFourHours_IsWrittenAsTheHourBeforeNoonThenTheHourBeforeMidnight() { CheckEqual("11:59", WrittenVariant(Clock(0, false, ClockFormat.Hours24), variant: 0)); CheckEqual("23:59", WrittenVariant(Clock(0, false, ClockFormat.Hours24), variant: 1)); } public void AClockWithSeconds_HasThem() { var format = ClockFormat.Hours12 with { Seconds = true }; CheckEqual("11:59:59 PM", WrittenVariant(Clock(0, false, format), variant: 1)); } public void AShortNumberOfVariantZero_IsTheScientificForm() { CheckEqual("9.99e-300", WrittenVariant(ShortNumber(0, 3), variant: 0)); } public void AShortNumberOfVariantFour_IsTheFirstTiersFewestWholeDigits() { CheckEqual("-9.99K", WrittenVariant(ShortNumber(0, 3, negative: true), variant: 4)); } public void AShortNumberOfTheLastVariant_IsTheLastTiersMostWholeDigits() { CounterSpec spec = ShortNumber(0, 3); string suffix = ShortNotation.English.TierOf(NumberFormat.LargestExponent) .PatternFor(PluralCategory.Other).After; CheckEqual("999" + suffix, WrittenVariant(spec, spec.VariantCount - 1)); } public void AVariantThatTheSpecDoesNotHave_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); WrittenVariant(CountOf(3), variant: 1); } public void AShortNumberVariantPastTheLast_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); CounterSpec spec = ShortNumber(0, 3); WrittenVariant(spec, spec.VariantCount); } public void ANegativeVariant_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); WrittenVariant(CountOf(3), variant: -1); } public void ADefaultSpec_Throws() { CheckThrow(typeof(InvalidOperationException)); default(CounterSpec).WriteWidest(new char[8], 0, variant: 0); } } public class VariantCountTests : TestFixture { public void ACount_HasOne() { CheckEqual(1, CountOf(3).VariantCount); } public void AScientificNumber_HasOne() { CheckEqual(1, Scientific(2, 3).VariantCount); } public void ADuration_HasOne() { CheckEqual(1, Duration(2, new DurationFormat()).VariantCount); } public void AClock_HasTwo_OneForEachMarker() { CheckEqual(2, Clock(0, false, ClockFormat.Hours12).VariantCount); } public void AShortNumber_HasTheScientificFormTheWrittenOutFormAndEveryRoom() { CheckEqual(2 + ShortNotation.English.RoomCount, ShortNumber(0, 3).VariantCount); } } public class MaxLengthTests : TestFixture { public void ACount_IsTheFormatsLongestCount() { CheckEqual(NumberFormat.Invariant.MaxCountLength, CountOf(3).MaxLength); } public void AFixedNumber_IsTheFormatsLongestFixedNumber() { var spec = new CounterSpec(NumberFormat.Invariant, NumberKind.Fixed, 3, 2, negative: false, CounterAlignment.Left); CheckEqual(NumberFormat.Invariant.MaxFixedLength, spec.MaxLength); } public void APercent_IsTheFormatsLongestPercent() { var spec = new CounterSpec(NumberFormat.Invariant, NumberKind.Percent, 2, 1, negative: false, CounterAlignment.Left); CheckEqual(NumberFormat.Invariant.MaxPercentLength, spec.MaxLength); } public void AScientificNumber_IsTheFormatsLongestScientificNumber() { CheckEqual(NumberFormat.Invariant.MaxScientificLength, Scientific(2, 3).MaxLength); } public void AShortNumber_IsTheFormatsLongestShortNumber() { CheckEqual(NumberFormat.Invariant.MaxShortLength, ShortNumber(0, 3).MaxLength); } public void AClock_IsTheFormatsLongestClock() { CheckEqual(NumberFormat.Invariant.MaxClockLength, Clock(0, false, ClockFormat.Hours12).MaxLength); } public void ADuration_IsTheFormatsLongestDuration() { CheckEqual(NumberFormat.Invariant.MaxDurationLength, Duration(2, new DurationFormat()).MaxLength); } public void EveryKindsLongestNumberInEveryLocale_FitsInIt() { var everything = new DurationFormat { Largest = DurationUnit.Days, Smallest = DurationUnit.Seconds }; var buffer = new char[512]; foreach (string tag in NumberFormat.LocaleTags()) { NumberFormat format = NumberFormat.For(tag); CounterSpec[] specs = { Scientific(WidestNumber.MaxExponentDigits, NumberFormat.MaxSignificantDigits, true, format), Duration(10, everything, negative: true, format), ShortNumber(0, NumberFormat.MaxSignificantDigits, negative: true, format), Clock(0, false, ClockFormat.Hours12 with { Seconds = true }, format), }; foreach (CounterSpec spec in specs) { for (int variant = 0; variant < spec.VariantCount; ++variant) { CheckTrue(spec.WriteWidest(buffer, 0, variant) <= spec.MaxLength); } } } } } public class EqualityTests : TestFixture { public void SpecsMadeTheSameWay_AreEqual() { CheckEqual(CountOf(4), CountOf(4)); } public void SpecsThatDifferInAlignment_AreNot() { CheckFalse(CountOf(4, alignment: CounterAlignment.Left).Equals(CountOf(4))); } public void SpecsThatDifferInDigits_AreNot() { CheckFalse(CountOf(4).Equals(CountOf(5))); } } } }