using System; using Glass.Text; using UnitTestSharp; namespace Glass.UnitTests.Text { public class ShortNotationTests : TestFixture { public static string SuffixOf(ShortNotation notation, int group) { var buffer = new char[notation.MaxSuffixLength + 10]; int end = notation.WriteSuffix(group, buffer, 2); return new string(buffer, 2, end - 2); } public class EnglishTests : TestFixture { public void IsKMBTThenLetters_InGroupsOfThreeDigits() { CheckEqual(4, ShortNotation.English.NamedCount); CheckEqual("abcdefghijklmnopqrstuvwxyz", ShortNotation.English.Letters); CheckEqual(3, ShortNotation.English.Step); } } public class CtorTests : TestFixture { public void KeepsTheNamedSuffixesLettersAndStep() { var notation = new ShortNotation(new[] { "x", "yy" }, "ab", 4); CheckEqual(2, notation.NamedCount); CheckEqual("x", notation.NamedAt(0)); CheckEqual("yy", notation.NamedAt(1)); CheckEqual("ab", notation.Letters); CheckEqual(4, notation.Step); } public void ChangingTheArrayAfterwards_ChangesNothing() { var names = new[] { "x" }; var notation = new ShortNotation(names, "", 3); names[0] = "changed"; CheckEqual("x", notation.NamedAt(0)); } public void NoLettersAndNoNames_IsAllowed() { var notation = new ShortNotation(new string[0], "", 3); CheckEqual(0, notation.NamedCount); CheckEqual(0, notation.MaxSuffixLength); } public void ANullNamedList_Throws() { CheckThrow(typeof(ArgumentNullException)); new ShortNotation(null, "ab", 3); } public void ANullName_Throws() { CheckThrow(typeof(ArgumentNullException)); new ShortNotation(new string[] { "K", null }, "ab", 3); } public void NullLetters_Throw() { CheckThrow(typeof(ArgumentNullException)); new ShortNotation(new string[0], null, 3); } public void OneLetter_Throws() { CheckThrow(typeof(ArgumentException)); new ShortNotation(new string[0], "a", 3); } public void AStepOfZero_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new ShortNotation(new string[0], "", 0); } public void AStepPastTheMaximum_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new ShortNotation(new string[0], "", ShortNotation.MaxStep + 1); } public void TheMaximumStep_IsAllowed() { CheckEqual(ShortNotation.MaxStep, new ShortNotation(new string[0], "", ShortNotation.MaxStep).Step); } } public class MaxSuffixLengthTests : TestFixture { public void English_IsTwoLetters() { CheckEqual(2, ShortNotation.English.MaxSuffixLength); } public void ALongNamedSuffix_Counts() { CheckEqual(5, new ShortNotation(new[] { "K", "MEGAS" }, "", 3).MaxSuffixLength); } public void LettersThatReachPastTwoWithinADoublesRange_Count() { var notation = new ShortNotation(new string[0], "ab", 3); CheckEqual(6, notation.MaxSuffixLength); } } public class HasSuffixTests : TestFixture { public void NamedGroups_HaveOne() { CheckTrue(ShortNotation.English.HasSuffix(1)); CheckTrue(ShortNotation.English.HasSuffix(4)); } public void WithLetters_EveryGroupHasOne() { CheckTrue(ShortNotation.English.HasSuffix(5)); CheckTrue(ShortNotation.English.HasSuffix(100000)); } public void WithoutLetters_GroupsPastTheNamedOnesHaveNone() { var notation = new ShortNotation(new[] { "a", "b" }, "", 4); CheckTrue(notation.HasSuffix(2)); CheckFalse(notation.HasSuffix(3)); } public void GroupZero_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ShortNotation.English.HasSuffix(0); } } public class SuffixLengthTests : TestFixture { public void ANamedSuffix_IsItsLength() { CheckEqual(1, ShortNotation.English.SuffixLength(1)); CheckEqual(3, new ShortNotation(new[] { "abc" }, "", 3).SuffixLength(1)); } public void ALetterSuffix_IsItsTier() { CheckEqual(2, ShortNotation.English.SuffixLength(5)); CheckEqual(2, ShortNotation.English.SuffixLength(680)); CheckEqual(3, ShortNotation.English.SuffixLength(681)); } public void AGroupWithNoSuffix_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new ShortNotation(new string[0], "", 3).SuffixLength(1); } } public class WriteSuffixTests : TestFixture { public void NamedGroups_WriteTheirNames() { CheckEqual("K", SuffixOf(ShortNotation.English, 1)); CheckEqual("M", SuffixOf(ShortNotation.English, 2)); CheckEqual("B", SuffixOf(ShortNotation.English, 3)); CheckEqual("T", SuffixOf(ShortNotation.English, 4)); } public void GeneratedLetters_StartAtAAAfterTheNamedOnes() { CheckEqual("aa", SuffixOf(ShortNotation.English, 5)); CheckEqual("ab", SuffixOf(ShortNotation.English, 6)); CheckEqual("az", SuffixOf(ShortNotation.English, 30)); } public void TheLastLetterRollsOver_IntoTheNextFirstLetter() { CheckEqual("ba", SuffixOf(ShortNotation.English, 31)); CheckEqual("bz", SuffixOf(ShortNotation.English, 56)); CheckEqual("ca", SuffixOf(ShortNotation.English, 57)); } public void TheLastTwoLetterSuffix_IsZZ() { CheckEqual("zz", SuffixOf(ShortNotation.English, 680)); } public void PastTheTwoLetterOnes_ThreeLettersStartAtAAA() { CheckEqual("aaa", SuffixOf(ShortNotation.English, 681)); CheckEqual("aab", SuffixOf(ShortNotation.English, 682)); CheckEqual("aba", SuffixOf(ShortNotation.English, 707)); CheckEqual("zzz", SuffixOf(ShortNotation.English, 680 + 17576)); CheckEqual("aaaa", SuffixOf(ShortNotation.English, 681 + 17576)); } public void ADoublesLastGroup_IsTwoLetters() { CheckEqual("dt", SuffixOf(ShortNotation.English, 102)); } public void FewLetters_MakeLongerTiersSooner() { var notation = new ShortNotation(new[] { "K" }, "ab", 3); CheckEqual("K", SuffixOf(notation, 1)); CheckEqual("aa", SuffixOf(notation, 2)); CheckEqual("ab", SuffixOf(notation, 3)); CheckEqual("ba", SuffixOf(notation, 4)); CheckEqual("bb", SuffixOf(notation, 5)); CheckEqual("aaa", SuffixOf(notation, 6)); CheckEqual("bbb", SuffixOf(notation, 13)); CheckEqual("aaaa", SuffixOf(notation, 14)); } public void WritesAtThePositionAndReturnsTheEnd() { var buffer = new char[6]; for (int i = 0; i < buffer.Length; ++i) { buffer[i] = '#'; } int end = ShortNotation.English.WriteSuffix(5, buffer, 1); CheckEqual(3, end); CheckEqual("#aa###", new string(buffer)); } public void ANamedSuffixWritesAtThePosition() { var buffer = new char[6]; for (int i = 0; i < buffer.Length; ++i) { buffer[i] = '#'; } int end = new ShortNotation(new[] { "abc" }, "", 3).WriteSuffix(1, buffer, 2); CheckEqual(5, end); CheckEqual("##abc#", new string(buffer)); } public void AnEmptyNamedSuffix_WritesNothing() { var buffer = new char[3]; CheckEqual(1, new ShortNotation(new[] { "" }, "", 3).WriteSuffix(1, buffer, 1)); } public void AGroupWithNoSuffix_Throws() { var buffer = new char[10]; CheckThrow(typeof(ArgumentOutOfRangeException)); new ShortNotation(new string[0], "", 3).WriteSuffix(1, buffer, 0); } } } }