using System; using Tessera.Text; using UnitTestSharp; using static Tessera.UnitTests.Text.ShortNotationSamples; namespace Tessera.UnitTests.Text { public class ShortNotationTests : TestFixture { public static string SuffixOf(ShortNotation notation, int exponent) { return notation.TierOf(exponent).PatternFor(PluralCategory.Other).After; } public class EnglishTests : TestFixture { public void StartsAtTheThousandsAndShiftsByGroupsOfThree() { CheckEqual(3, ShortNotation.English.FirstExponent); CheckEqual(3, ShortNotation.English.TierOf(3).Shift); CheckEqual(3, ShortNotation.English.TierOf(5).Shift); CheckEqual(6, ShortNotation.English.TierOf(6).Shift); } public void HasNoPluralForms() { CheckTrue(ReferenceEquals(PluralRules.None, ShortNotation.English.Plurals)); } public void ReachesTheLargestExponentOfADouble() { CheckTrue(ShortNotation.English.HasTier(NumberFormat.LargestExponent)); CheckFalse(ShortNotation.English.HasTier(NumberFormat.LargestExponent + 1)); } public void WritesNoTextBeforeTheNumber() { CheckEqual("", ShortNotation.English.TierOf(3).PatternFor(PluralCategory.Other).Before); } } public class FromGroupsTests : TestFixture { public void EachNamedSuffix_CoversAGroupOfExponents() { ShortNotation notation = ShortNotation.FromGroups(new[] { "x", "yy" }, "", 4); CheckEqual(4, notation.FirstExponent); CheckEqual(8, notation.TierCount); CheckEqual("x", SuffixOf(notation, 4)); CheckEqual("x", SuffixOf(notation, 7)); CheckEqual("yy", SuffixOf(notation, 8)); CheckEqual("yy", SuffixOf(notation, 11)); CheckEqual(4, notation.TierOf(7).Shift); CheckEqual(8, notation.TierOf(8).Shift); } public void ChangingTheArrayAfterwards_ChangesNothing() { var names = new[] { "x" }; ShortNotation notation = ShortNotation.FromGroups(names, "", 3); names[0] = "changed"; CheckEqual("x", SuffixOf(notation, 3)); } public void NoLettersAndNoNames_HasNoTiers() { ShortNotation notation = ShortNotation.FromGroups(new string[0], "", 3); CheckEqual(0, notation.TierCount); CheckEqual(0, notation.MaxTextLength); CheckEqual(3, notation.FirstExponent); } public void ANullNamedList_Throws() { CheckThrow(typeof(ArgumentNullException)); ShortNotation.FromGroups(null, "ab", 3); } public void ANullName_Throws() { CheckThrow(typeof(ArgumentNullException)); ShortNotation.FromGroups(new string[] { "K", null }, "ab", 3); } public void NullLetters_Throw() { CheckThrow(typeof(ArgumentNullException)); ShortNotation.FromGroups(new string[0], null, 3); } public void OneLetter_Throws() { CheckThrow(typeof(ArgumentException)); ShortNotation.FromGroups(new string[0], "a", 3); } public void AStepOfZero_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ShortNotation.FromGroups(new string[0], "", 0); } public void AStepPastTheMaximum_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ShortNotation.FromGroups(new string[0], "", ShortNotation.MaxWholeDigits + 1); } public void TheMaximumStep_IsAllowed() { CheckEqual(ShortNotation.MaxWholeDigits, ShortNotation.FromGroups(new string[0], "", ShortNotation.MaxWholeDigits).FirstExponent); } public void NamedSuffixes_AreKFollowedByThemselves() { CheckEqual("K", SuffixOf(ShortNotation.English, 3)); CheckEqual("M", SuffixOf(ShortNotation.English, 6)); CheckEqual("B", SuffixOf(ShortNotation.English, 9)); CheckEqual("T", SuffixOf(ShortNotation.English, 12)); } public void GeneratedLetters_StartAtAAAfterTheNamedOnes() { CheckEqual("aa", SuffixOf(ShortNotation.English, 15)); CheckEqual("ab", SuffixOf(ShortNotation.English, 18)); CheckEqual("az", SuffixOf(ShortNotation.English, 90)); } public void TheLastLetterRollsOver_IntoTheNextFirstLetter() { CheckEqual("ba", SuffixOf(ShortNotation.English, 93)); CheckEqual("bz", SuffixOf(ShortNotation.English, 168)); CheckEqual("ca", SuffixOf(ShortNotation.English, 171)); } public void ADoublesLastGroup_IsTwoLetters() { CheckEqual("dt", SuffixOf(ShortNotation.English, 306)); CheckEqual("dt", SuffixOf(ShortNotation.English, NumberFormat.LargestExponent)); } public void FewLetters_MakeLongerTiersSooner() { ShortNotation notation = ShortNotation.FromGroups(new[] { "K" }, "ab", 3); CheckEqual("K", SuffixOf(notation, 3)); CheckEqual("aa", SuffixOf(notation, 6)); CheckEqual("ab", SuffixOf(notation, 9)); CheckEqual("ba", SuffixOf(notation, 12)); CheckEqual("bb", SuffixOf(notation, 15)); CheckEqual("aaa", SuffixOf(notation, 18)); CheckEqual("bbb", SuffixOf(notation, 39)); CheckEqual("aaaa", SuffixOf(notation, 42)); } public void WithoutLetters_ExponentsPastTheNamedOnesHaveNoTier() { ShortNotation notation = ShortNotation.FromGroups(new[] { "a", "b" }, "", 4); CheckTrue(notation.HasTier(11)); CheckFalse(notation.HasTier(12)); } public void ANamedSuffixThatIsEmpty_IsAllowed() { CheckEqual("", SuffixOf(ShortNotation.FromGroups(new[] { "" }, "", 3), 3)); } } public class CtorTests : TestFixture { public void KeepsTheTiersFirstExponentAndPlurals() { var tiers = new[] { Tier(3, "{0}k"), Tier(3, "{0}m") }; PluralRules plurals = PluralRules.For("en"); var notation = new ShortNotation(3, tiers, plurals); CheckEqual(3, notation.FirstExponent); CheckEqual(2, notation.TierCount); CheckTrue(ReferenceEquals(tiers[0], notation.TierOf(3))); CheckTrue(ReferenceEquals(tiers[1], notation.TierOf(4))); CheckTrue(ReferenceEquals(plurals, notation.Plurals)); } public void ChangingTheArrayAfterwards_ChangesNothing() { var tiers = new[] { Tier(3, "{0}k") }; var notation = new ShortNotation(3, tiers, PluralRules.None); tiers[0] = Tier(3, "{0}changed"); CheckEqual("k", SuffixOf(notation, 3)); } public void ATierMayShiftByNothing() { var notation = new ShortNotation(3, new[] { Tier(0, "{0}") }, PluralRules.None); CheckEqual(4, notation.WholeDigitsOf(3)); } public void ANullTierList_Throws() { CheckThrow(typeof(ArgumentNullException)); new ShortNotation(3, null, PluralRules.None); } public void ANullTier_Throws() { CheckThrow(typeof(ArgumentNullException)); new ShortNotation(3, new ShortTier[] { Tier(3, "{0}k"), null }, PluralRules.None); } public void NullPlurals_Throw() { CheckThrow(typeof(ArgumentNullException)); new ShortNotation(3, new ShortTier[0], null); } public void AFirstExponentOfZero_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new ShortNotation(0, new ShortTier[0], PluralRules.None); } public void AFirstExponentPastTheMaximum_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new ShortNotation(ShortNotation.MaxWholeDigits + 1, new ShortTier[0], PluralRules.None); } public void ATierThatShiftsPastItsExponent_Throws() { CheckThrow(typeof(ArgumentException)); new ShortNotation(3, new[] { Tier(4, "{0}k") }, PluralRules.None); } public void ATierWithTooManyWholeDigits_Throws() { CheckThrow(typeof(ArgumentException)); new ShortNotation(9, new[] { Tier(0, "{0}") }, PluralRules.None); } public void ATierWithTheMostWholeDigits_IsAllowed() { var notation = new ShortNotation(8, new[] { Tier(0, "{0}") }, PluralRules.None); CheckEqual(ShortNotation.MaxWholeDigits, notation.WholeDigitsOf(8)); } } public class LongestWholePartTests : TestFixture { public void English_IsThreeDigits() { CheckEqual(3, ShortNotation.English.LongestWholePart); } public void ATierThatShiftsLessThanItsExponent_Counts() { var notation = new ShortNotation(3, new[] { Tier(3, "{0}k"), Tier(0, "{0}"), Tier(1, "{0}x") }, PluralRules.None); CheckEqual(5, notation.LongestWholePart); } public void TheNumbersBelowTheFirstTier_Count() { CheckEqual(5, new ShortNotation(5, new ShortTier[0], PluralRules.None).LongestWholePart); } } public class MaxTextLengthTests : TestFixture { public void English_IsTwoLetters() { CheckEqual(2, ShortNotation.English.MaxTextLength); } public void ALongNamedSuffix_Counts() { CheckEqual(5, ShortNotation.FromGroups(new[] { "K", "MEGAS" }, "", 3).MaxTextLength); } public void LettersThatReachPastTwoWithinADoublesRange_Count() { CheckEqual(6, ShortNotation.FromGroups(new string[0], "ab", 3).MaxTextLength); } public void TextBeforeTheNumber_Counts() { var notation = new ShortNotation(3, new[] { Tier(3, "ab{0}c") }, PluralRules.None); CheckEqual(3, notation.MaxTextLength); } public void ThePluralFormWithTheMostText_Counts() { var forms = new UnitPattern[6]; forms[(int)PluralCategory.Other] = UnitPattern.Parse("{0}a"); forms[(int)PluralCategory.Few] = UnitPattern.Parse("{0}abcd"); var notation = new ShortNotation(3, new[] { new ShortTier(3, forms) }, PluralRules.None); CheckEqual(4, notation.MaxTextLength); } } public class RoomTests : TestFixture { public void ASingleTier_HasTheNumberWrittenOutAtBothEndsAndItsOwnWay() { var notation = new ShortNotation(3, new[] { Tier(3, "{0}k") }, PluralRules.None); CheckEqual(3, notation.RoomCount); CheckEqual(1, notation.RoomAt(0).WholeDigits); CheckEqual(3, notation.RoomAt(1).WholeDigits); CheckTrue(notation.RoomAt(0).Pattern == null); CheckTrue(notation.RoomAt(1).Pattern == null); CheckEqual("k", notation.RoomAt(2).Pattern.After); CheckEqual(1, notation.RoomAt(2).WholeDigits); } public void AFirstExponentOfOne_WritesOutOnlyOneDigitNumbers() { var notation = new ShortNotation(1, new[] { Tier(1, "{0}x") }, PluralRules.None); CheckEqual(2, notation.RoomCount); CheckEqual(1, notation.RoomAt(0).WholeDigits); CheckEqual("x", notation.RoomAt(1).Pattern.After); } public void ATierSharedByManyPowersOfTen_HasItsFewestAndItsMostWholeDigits() { ShortTier thousands = Tier(3, "{0}k"); var notation = new ShortNotation(3, new[] { thousands, thousands, thousands }, PluralRules.None); CheckEqual(4, notation.RoomCount); CheckEqual(1, notation.RoomAt(2).WholeDigits); CheckEqual(3, notation.RoomAt(3).WholeDigits); } public void TwoTiersWithTheSameTextAndWholeDigits_AreOneRoom() { var notation = new ShortNotation(3, new[] { Tier(3, "{0}k"), Tier(4, "{0}k") }, PluralRules.None); CheckEqual(3, notation.RoomCount); } public void SeparateTiersWithTheSameTextAndDifferentWholeDigits_AreARoomEach() { var notation = new ShortNotation(3, new[] { Tier(3, "{0}k"), Tier(3, "{0}k"), Tier(3, "{0}k") }, PluralRules.None); CheckEqual(5, notation.RoomCount); } public void EachPluralFormOfATier_IsARoom() { var forms = new UnitPattern[6]; forms[(int)PluralCategory.Other] = UnitPattern.Parse("{0}a"); forms[(int)PluralCategory.Few] = UnitPattern.Parse("{0}bc"); var notation = new ShortNotation(3, new[] { new ShortTier(3, forms) }, PluralRules.None); CheckEqual(4, notation.RoomCount); } public void PluralFormsThatWriteTheSame_AreOneRoom() { var forms = new UnitPattern[6]; forms[(int)PluralCategory.Other] = UnitPattern.Parse("{0}a"); forms[(int)PluralCategory.One] = UnitPattern.Parse("{0}a"); var notation = new ShortNotation(3, new[] { new ShortTier(3, forms) }, PluralRules.None); CheckEqual(3, notation.RoomCount); } public void ASignAfterTheText_IsKept() { var tier = new ShortTier(3, UnitPattern.Parse("x{0}"), signAfterText: true); var notation = new ShortNotation(3, new[] { tier }, PluralRules.None); CheckTrue(notation.RoomAt(2).SignAfterText); CheckFalse(notation.RoomAt(0).SignAfterText); } public void English_HasTheTwoEndsOfEveryGroupAndOfTheWholeNumbers() { CheckEqual(2 + 2 * 102, ShortNotation.English.RoomCount); } public void ARoomBeforeTheFirstOrAfterTheLast_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ShortNotation.English.RoomAt(ShortNotation.English.RoomCount); } public void ANegativeRoom_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ShortNotation.English.RoomAt(-1); } } public class HasTierTests : TestFixture { public void ExponentsFromTheFirstThroughTheLast_HaveOne() { var notation = new ShortNotation(3, new[] { Tier(3, "{0}k"), Tier(3, "{0}m") }, PluralRules.None); CheckTrue(notation.HasTier(3)); CheckTrue(notation.HasTier(4)); } public void ExponentsBelowTheFirst_HaveNone() { CheckFalse(ShortNotation.English.HasTier(2)); CheckFalse(ShortNotation.English.HasTier(0)); CheckFalse(ShortNotation.English.HasTier(-1)); } public void ExponentsPastTheLast_HaveNone() { var notation = new ShortNotation(3, new[] { Tier(3, "{0}k") }, PluralRules.None); CheckFalse(notation.HasTier(4)); } } public class TierOfTests : TestFixture { public void AnExponentWithoutOne_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ShortNotation.English.TierOf(2); } } public class WholeDigitsOfTests : TestFixture { public void BelowTheFirstTier_IsEveryDigit() { CheckEqual(1, ShortNotation.English.WholeDigitsOf(0)); CheckEqual(3, ShortNotation.English.WholeDigitsOf(2)); } public void InATier_IsTheDigitsLeftAfterTheShift() { CheckEqual(1, ShortNotation.English.WholeDigitsOf(3)); CheckEqual(3, ShortNotation.English.WholeDigitsOf(5)); CheckEqual(1, ShortNotation.English.WholeDigitsOf(6)); } public void PastTheLastTier_IsZero() { var notation = new ShortNotation(3, new[] { Tier(3, "{0}k") }, PluralRules.None); CheckEqual(0, notation.WholeDigitsOf(4)); } } } }