using System; using System.Linq; using Glass.Typesetting; using UnitTestSharp; namespace Glass.UnitTests.Typesetting { public class GlyphMetricsTableTests : TestFixture { public const int CodePoints = char.MaxValue + 1; /// /// A distinct instance for every code point, so no two pages are the same. /// public static GlyphMetrics[] DistinctMetrics() { var returnMe = new GlyphMetrics[CodePoints]; for (int i = 0; i < returnMe.Length; ++i) { returnMe[i] = new GlyphMetrics { Width = i }; } return returnMe; } public class CtorTests : TestFixture { public void NullMetricsFor_Throws() { CheckThrow(typeof(ArgumentNullException)); new GlyphMetricsTable(null); } public void AsksForEachCodePointOnce() { var asked = new int[CodePoints]; var shared = new GlyphMetrics(); new GlyphMetricsTable(glyph => { ++asked[glyph]; return shared; }); CheckEqual(-1, Array.FindIndex(asked, count => count != 1)); } } public class PageCountTests : TestFixture { public void SameMetricsEverywhere_IsOnePage() { var shared = new GlyphMetrics(); CheckEqual(1, new GlyphMetricsTable(glyph => shared).PageCount); } public void OneDifferentEntry_SetsItsPageApart() { var shared = new GlyphMetrics(); var different = new GlyphMetrics(); CheckEqual(2, new GlyphMetricsTable(glyph => glyph == 'A' ? different : shared).PageCount); } public void RepeatedPages_AreStoredOnce() { GlyphMetrics[] metrics = DistinctMetrics(); CheckEqual(1, new GlyphMetricsTable(glyph => metrics[glyph % GlyphMetricsTable.PageSize]).PageCount); } public void EqualButDistinctMetrics_AreNotShared() { CheckEqual(CodePoints / GlyphMetricsTable.PageSize, new GlyphMetricsTable(glyph => new GlyphMetrics()).PageCount); } } public class AsciiTests : TestFixture { public void HoldsWhatTheIndexerFindsForEachAsciiCodePoint() { GlyphMetrics[] metrics = DistinctMetrics(); var table = new GlyphMetricsTable(glyph => metrics[glyph]); CheckEqual(metrics.Take(GlyphMetricsTable.AsciiLength), table.Ascii); } } public class IndexerTests : TestFixture { public void EveryCodePoint_FindsItsOwnMetrics() { GlyphMetrics[] metrics = DistinctMetrics(); var table = new GlyphMetricsTable(glyph => metrics[glyph]); CheckEqual(-1, Array.FindIndex(metrics, entry => !ReferenceEquals(entry, table[(char)entry.Width]))); } public void SharedPage_FindsTheEntryAtEachPosition() { GlyphMetrics[] metrics = DistinctMetrics(); var table = new GlyphMetricsTable(glyph => metrics[glyph % GlyphMetricsTable.PageSize]); CheckEqual(metrics[1], table[(char)1]); CheckEqual(metrics[1], table[(char)(GlyphMetricsTable.PageSize + 1)]); CheckEqual(metrics[GlyphMetricsTable.PageSize - 1], table[char.MaxValue]); } } } }