diff --git a/UnicodeCharacterInspector/UnicodeCharacterInspector.csproj b/UnicodeCharacterInspector/UnicodeCharacterInspector.csproj index 7dc17f9..4428878 100644 --- a/UnicodeCharacterInspector/UnicodeCharacterInspector.csproj +++ b/UnicodeCharacterInspector/UnicodeCharacterInspector.csproj @@ -2,7 +2,7 @@ - Debug + Release AnyCPU {04E97F21-EF04-441F-83CF-2E71F3AAB089} WinExe diff --git a/UnicodeInformation.Builder/UnicodeCharacterDataBuilder.cs b/UnicodeInformation.Builder/UnicodeCharacterDataBuilder.cs index d818f73..aed085c 100644 --- a/UnicodeInformation.Builder/UnicodeCharacterDataBuilder.cs +++ b/UnicodeInformation.Builder/UnicodeCharacterDataBuilder.cs @@ -10,7 +10,7 @@ namespace System.Unicode.Builder { public sealed class UnicodeCharacterDataBuilder { - private readonly UnicodeCharacterRange codePointRange; + private readonly UnicodeCodePointRange codePointRange; private string name; private UnicodeCategory category = UnicodeCategory.OtherNotAssigned; private CanonicalCombiningClass canonicalCombiningClass; @@ -29,7 +29,7 @@ namespace System.Unicode.Builder private readonly List nameAliases = new List(); private readonly List crossRerefences = new List(); - public UnicodeCharacterRange CodePointRange { get { return codePointRange; } } + public UnicodeCodePointRange CodePointRange { get { return codePointRange; } } public string Name { @@ -127,11 +127,11 @@ namespace System.Unicode.Builder public IList CrossRerefences { get { return crossRerefences; } } public UnicodeCharacterDataBuilder(int codePoint) - : this(new UnicodeCharacterRange(codePoint)) + : this(new UnicodeCodePointRange(codePoint)) { } - public UnicodeCharacterDataBuilder(UnicodeCharacterRange codePointRange) + public UnicodeCharacterDataBuilder(UnicodeCodePointRange codePointRange) { this.codePointRange = codePointRange; this.category = UnicodeCategory.OtherNotAssigned; diff --git a/UnicodeInformation.Builder/UnicodeDataProcessor.cs b/UnicodeInformation.Builder/UnicodeDataProcessor.cs index fb8ccd7..27693de 100644 --- a/UnicodeInformation.Builder/UnicodeDataProcessor.cs +++ b/UnicodeInformation.Builder/UnicodeDataProcessor.cs @@ -61,7 +61,7 @@ namespace System.Unicode.Builder while (reader.MoveToNextLine()) { - var codePoint = new UnicodeCharacterRange(int.Parse(reader.ReadField(), NumberStyles.HexNumber)); + var codePoint = new UnicodeCodePointRange(int.Parse(reader.ReadField(), NumberStyles.HexNumber)); string name = reader.ReadField(); @@ -79,7 +79,7 @@ namespace System.Unicode.Builder { if (rangeStartCodePoint < 0) throw new InvalidDataException("Invalid range data in UnicodeData.txt."); - codePoint = new UnicodeCharacterRange(rangeStartCodePoint, codePoint.LastCodePoint); + codePoint = new UnicodeCodePointRange(rangeStartCodePoint, codePoint.LastCodePoint); name = name.Substring(1, name.Length - 8).ToUpperInvariant(); // Upper-case the name in order to respect unicode naming scheme. (Spec says all names are uppercase ASCII) @@ -178,7 +178,7 @@ namespace System.Unicode.Builder { ContributoryProperties property; - var range = UnicodeCharacterRange.Parse(reader.ReadTrimmedField()); + var range = UnicodeCodePointRange.Parse(reader.ReadTrimmedField()); if (EnumHelper.TryGetNamedValue(reader.ReadTrimmedField(), out property)) { builder.SetProperties(property, range); @@ -195,7 +195,7 @@ namespace System.Unicode.Builder { CoreProperties property; - var range = UnicodeCharacterRange.Parse(reader.ReadTrimmedField()); + var range = UnicodeCodePointRange.Parse(reader.ReadTrimmedField()); if (EnumHelper.TryGetNamedValue(reader.ReadTrimmedField(), out property)) { builder.SetProperties(property, range); @@ -364,7 +364,7 @@ namespace System.Unicode.Builder { while (reader.MoveToNextLine()) { - builder.AddBlockEntry(new UnicodeBlock(UnicodeCharacterRange.Parse(reader.ReadField()), reader.ReadTrimmedField())); + builder.AddBlockEntry(new UnicodeBlock(UnicodeCodePointRange.Parse(reader.ReadField()), reader.ReadTrimmedField())); } } } diff --git a/UnicodeInformation.Builder/UnicodeInfoBuilder.cs b/UnicodeInformation.Builder/UnicodeInfoBuilder.cs index 5baf51d..6d4cf2b 100644 --- a/UnicodeInformation.Builder/UnicodeInfoBuilder.cs +++ b/UnicodeInformation.Builder/UnicodeInfoBuilder.cs @@ -179,7 +179,7 @@ namespace System.Unicode.Builder } } - public void SetProperties(ContributoryProperties property, UnicodeCharacterRange codePointRange) + public void SetProperties(ContributoryProperties property, UnicodeCodePointRange codePointRange) { int firstIndex = FindUcdCodePoint(codePointRange.FirstCodePoint); int lastIndex = FindUcdCodePoint(codePointRange.LastCodePoint); @@ -210,7 +210,7 @@ namespace System.Unicode.Builder } } - public void SetProperties(CoreProperties property, UnicodeCharacterRange codePointRange) + public void SetProperties(CoreProperties property, UnicodeCodePointRange codePointRange) { int firstIndex = FindUcdCodePoint(codePointRange.FirstCodePoint); int lastIndex = FindUcdCodePoint(codePointRange.LastCodePoint); diff --git a/UnicodeInformation.Builder/UnicodeInformation.Builder.csproj b/UnicodeInformation.Builder/UnicodeInformation.Builder.csproj index 43bd7a4..07d5be3 100644 --- a/UnicodeInformation.Builder/UnicodeInformation.Builder.csproj +++ b/UnicodeInformation.Builder/UnicodeInformation.Builder.csproj @@ -2,7 +2,7 @@ - Debug + Release AnyCPU {8DFDEE6C-4F0D-4DE1-B346-574CB56D2B8B} Exe diff --git a/UnicodeInformation.Tests/AssertEx.cs b/UnicodeInformation.Tests/AssertEx.cs index 3bf5b74..f01de91 100644 --- a/UnicodeInformation.Tests/AssertEx.cs +++ b/UnicodeInformation.Tests/AssertEx.cs @@ -9,7 +9,7 @@ namespace UnicodeInformation.Tests { public static class AssertEx { - public static void ThrowsExactly(Action action, string methodName) + public static void ThrowsExactly(Action action, string methodName = null, string message = null) where TException : Exception { try @@ -19,12 +19,12 @@ namespace UnicodeInformation.Tests catch (TException ex) { if (ex.GetType() != typeof(TException)) - Assert.Fail("The " + methodName + " method should throw an exception of type " + typeof(TException).Name + " but got " + ex.GetType().Name + "."); + Assert.Fail(message ?? (methodName != null ? "The " + methodName + " method should throw an exception of type " : "Expected an exception of type ") + typeof(TException).Name + " but got " + ex.GetType().Name + "."); else return; } - Assert.Fail("The " + methodName + " method should throw an exception of type " + typeof(TException).Name + "."); + Assert.Fail(message ?? (methodName != null ? "The " + methodName + " method should throw an exception of type " : "Expected an exception of type ") + typeof(TException).Name + "."); } } } diff --git a/UnicodeInformation.Tests/UnicodeInfoBuilderTests.cs b/UnicodeInformation.Tests/UnicodeInfoBuilderTests.cs index 8cd95c6..0f904f7 100644 --- a/UnicodeInformation.Tests/UnicodeInfoBuilderTests.cs +++ b/UnicodeInformation.Tests/UnicodeInfoBuilderTests.cs @@ -148,7 +148,8 @@ namespace System.Unicode.Tests { var data = (await UnicodeDataProcessor.BuildDataAsync(ucdSource, unihanSource)); - using (var stream = new DeflateStream(File.Create("ucd.dat"), CompressionLevel.Optimal, false)) + //using (var stream = new DeflateStream(File.Create("ucd.dat"), CompressionLevel.Optimal, false)) + using (var stream = File.Create("ucd.dat")) { data.WriteToStream(stream); } diff --git a/UnicodeInformation.Tests/UnicodeInfoTests.cs b/UnicodeInformation.Tests/UnicodeInfoTests.cs index d13d361..c936c1b 100644 --- a/UnicodeInformation.Tests/UnicodeInfoTests.cs +++ b/UnicodeInformation.Tests/UnicodeInfoTests.cs @@ -68,7 +68,7 @@ namespace UnicodeInformation.Tests { var info = UnicodeInfo.GetCharInfo(codePoint); Assert.AreEqual(codePoint, info.CodePoint); - Assert.AreEqual(category, info.Category); + Assert.AreEqual(category, info.Category); Assert.AreEqual(name, info.Name); Assert.AreEqual(block, UnicodeInfo.GetBlockName(codePoint)); Assert.AreEqual(block, info.Block); @@ -192,7 +192,64 @@ namespace UnicodeInformation.Tests AssertEx.ThrowsExactly(() => UnicodeInfo.GetCjkRadicalInfo(0), nameof(UnicodeInfo.GetCjkRadicalInfo)); AssertEx.ThrowsExactly(() => UnicodeInfo.GetCjkRadicalInfo(215), nameof(UnicodeInfo.GetCjkRadicalInfo)); - } + } + + [TestMethod] + public void CodePointRangeTest() + { + var fullRange = new UnicodeCodePointRange(0, 0x10FFFF); + + Assert.AreEqual(0, fullRange.FirstCodePoint); + Assert.AreEqual(0x10FFFF, fullRange.LastCodePoint); + Assert.AreEqual(false, fullRange.IsSingleCodePoint); + + var letterA = new UnicodeCodePointRange('A'); + + Assert.AreEqual('A', letterA.FirstCodePoint); + Assert.AreEqual('A', letterA.LastCodePoint); + Assert.AreEqual(true, letterA.IsSingleCodePoint); + + AssertEx.ThrowsExactly(() => new UnicodeCodePointRange(-1)); + AssertEx.ThrowsExactly(() => new UnicodeCodePointRange(0x110000)); + AssertEx.ThrowsExactly(() => new UnicodeCodePointRange(int.MaxValue)); + AssertEx.ThrowsExactly(() => new UnicodeCodePointRange(-1, 10)); + AssertEx.ThrowsExactly(() => new UnicodeCodePointRange(10, 0x110000)); + AssertEx.ThrowsExactly(() => new UnicodeCodePointRange(-1, 0x110000)); + } + + [TestMethod] + public void CodePointRangeEnumerationTest() + { + const int start = 0xA3F; + const int end = 0x105F; + + // Generic test + { + int i = start; + + foreach (int n in new UnicodeCodePointRange(start, end)) + { + Assert.AreEqual(i++, n); + } + } + + // Nongeneric test + { + int i = start; + + var enumerator = (IEnumerator)new UnicodeCodePointRange(start, end).GetEnumerator(); + + while (enumerator.MoveNext()) + { + Assert.AreEqual(i++, enumerator.Current); + } + + enumerator.Reset(); + + Assert.AreEqual(true, enumerator.MoveNext()); + Assert.AreEqual(start, enumerator.Current); + } + } #if DEBUG [TestMethod] @@ -209,41 +266,15 @@ namespace UnicodeInformation.Tests for (int i = 0x2F800; i < 0x30000; ++i) Assert.AreEqual(i, UnihanCharacterData.UnpackCodePoint(UnihanCharacterData.PackCodePoint(i))); - try - { - UnihanCharacterData.PackCodePoint(0xA000); - Assert.Fail("The PackCodePoint method should fail for code points outside of the valid range."); - } - catch (ArgumentOutOfRangeException) - { - } + const string packCodePointErrorMessage = "The PackCodePoint method should fail for code points outside of the valid range."; + const string unpackCodePointErrorMessage = "The PackCodePoint method should fail for values outside of the valid range."; - try - { - UnihanCharacterData.PackCodePoint(0xFB00); - Assert.Fail("The PackCodePoint method should fail for code points outside of the valid range."); - } - catch (ArgumentOutOfRangeException) - { - } + AssertEx.ThrowsExactly(() => UnihanCharacterData.PackCodePoint(0xA000), nameof(UnihanCharacterData.PackCodePoint), packCodePointErrorMessage); + AssertEx.ThrowsExactly(() => UnihanCharacterData.PackCodePoint(0xFB00), nameof(UnihanCharacterData.PackCodePoint), packCodePointErrorMessage); + AssertEx.ThrowsExactly(() => UnihanCharacterData.PackCodePoint(0x30000), nameof(UnihanCharacterData.PackCodePoint), packCodePointErrorMessage); - try - { - UnihanCharacterData.PackCodePoint(0x30000); - Assert.Fail("The PackCodePoint method should fail for code points outside of the valid range."); - } - catch (ArgumentOutOfRangeException) - { - } - - try - { - UnihanCharacterData.UnpackCodePoint(0x20000); - Assert.Fail("The UnpackCodePoint method should fail for code points outside of the valid range."); - } - catch (ArgumentOutOfRangeException) - { - } + AssertEx.ThrowsExactly(() => UnihanCharacterData.UnpackCodePoint(-1), nameof(UnihanCharacterData.UnpackCodePoint), unpackCodePointErrorMessage); + AssertEx.ThrowsExactly(() => UnihanCharacterData.UnpackCodePoint(0x20000), nameof(UnihanCharacterData.UnpackCodePoint), unpackCodePointErrorMessage); } #endif } diff --git a/UnicodeInformation.Tests/UnicodeInformation.Tests.csproj b/UnicodeInformation.Tests/UnicodeInformation.Tests.csproj index 1da1343..2e7b43b 100644 --- a/UnicodeInformation.Tests/UnicodeInformation.Tests.csproj +++ b/UnicodeInformation.Tests/UnicodeInformation.Tests.csproj @@ -1,7 +1,7 @@  - Debug + Release AnyCPU {50337426-E884-4394-9E1A-F6F7A555F5D9} Library diff --git a/UnicodeInformation/UnicodeBlock.cs b/UnicodeInformation/UnicodeBlock.cs index 971fdaa..ed37f87 100644 --- a/UnicodeInformation/UnicodeBlock.cs +++ b/UnicodeInformation/UnicodeBlock.cs @@ -8,10 +8,10 @@ namespace System.Unicode { public struct UnicodeBlock { - public readonly UnicodeCharacterRange CodePointRange; + public readonly UnicodeCodePointRange CodePointRange; public readonly string Name; - internal UnicodeBlock(UnicodeCharacterRange codePointRange, string name) + internal UnicodeBlock(UnicodeCodePointRange codePointRange, string name) { this.CodePointRange = codePointRange; this.Name = name; diff --git a/UnicodeInformation/UnicodeCharacterData.cs b/UnicodeInformation/UnicodeCharacterData.cs index c021b6c..82e0577 100644 --- a/UnicodeInformation/UnicodeCharacterData.cs +++ b/UnicodeInformation/UnicodeCharacterData.cs @@ -9,7 +9,7 @@ namespace System.Unicode { internal sealed class UnicodeCharacterData { - public readonly UnicodeCharacterRange CodePointRange; + public readonly UnicodeCodePointRange CodePointRange; public readonly string Name; public readonly UnicodeNameAlias[] NameAliases; public readonly UnicodeCategory Category; @@ -31,7 +31,7 @@ namespace System.Unicode internal UnicodeCharacterData ( - UnicodeCharacterRange codePointRange, + UnicodeCodePointRange codePointRange, string name, UnicodeNameAlias[] nameAliases, UnicodeCategory category, diff --git a/UnicodeInformation/UnicodeCharacterRange.cs b/UnicodeInformation/UnicodeCodePointRange.cs similarity index 90% rename from UnicodeInformation/UnicodeCharacterRange.cs rename to UnicodeInformation/UnicodeCodePointRange.cs index 093c7f5..fe1a331 100644 --- a/UnicodeInformation/UnicodeCharacterRange.cs +++ b/UnicodeInformation/UnicodeCodePointRange.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace System.Unicode { - public struct UnicodeCharacterRange : IEnumerable + public struct UnicodeCodePointRange : IEnumerable { public struct Enumerator : IEnumerator { @@ -39,7 +39,7 @@ namespace System.Unicode public bool IsSingleCodePoint { get { return FirstCodePoint == LastCodePoint; } } - public UnicodeCharacterRange(int codePoint) + public UnicodeCodePointRange(int codePoint) { if (codePoint < 0 || codePoint > 0x10FFFF) throw new ArgumentOutOfRangeException(nameof(codePoint)); @@ -47,7 +47,7 @@ namespace System.Unicode LastCodePoint = codePoint; } - public UnicodeCharacterRange(int firstCodePoint, int lastCodePoint) + public UnicodeCodePointRange(int firstCodePoint, int lastCodePoint) { if (firstCodePoint < 0 || firstCodePoint > 0x10FFFF) throw new ArgumentOutOfRangeException(nameof(firstCodePoint)); if (lastCodePoint < firstCodePoint || lastCodePoint > 0x10FFFF) throw new ArgumentOutOfRangeException(nameof(lastCodePoint)); @@ -71,7 +71,7 @@ namespace System.Unicode return FirstCodePoint == LastCodePoint ? FirstCodePoint.ToString("X4") : FirstCodePoint.ToString("X4") + ".." + LastCodePoint.ToString("X4"); } - public static UnicodeCharacterRange Parse(string s) + public static UnicodeCodePointRange Parse(string s) { int start, end; @@ -88,7 +88,7 @@ namespace System.Unicode end = int.Parse(s.Substring(rangeSeparatorOffset + 2), NumberStyles.HexNumber); } - return new UnicodeCharacterRange(start, end); + return new UnicodeCodePointRange(start, end); } public Enumerator GetEnumerator() { return new Enumerator(FirstCodePoint, LastCodePoint); } diff --git a/UnicodeInformation/UnicodeInfo.cs b/UnicodeInformation/UnicodeInfo.cs index fed813a..a319e9a 100644 --- a/UnicodeInformation/UnicodeInfo.cs +++ b/UnicodeInformation/UnicodeInfo.cs @@ -100,7 +100,7 @@ namespace System.Unicode { var fields = (UcdFields)reader.ReadUInt16(); - var codePointRange = (fields & UcdFields.CodePointRange) != 0 ? new UnicodeCharacterRange(ReadCodePoint(reader), ReadCodePoint(reader)) : new UnicodeCharacterRange(ReadCodePoint(reader)); + var codePointRange = (fields & UcdFields.CodePointRange) != 0 ? new UnicodeCodePointRange(ReadCodePoint(reader), ReadCodePoint(reader)) : new UnicodeCodePointRange(ReadCodePoint(reader)); string name = null; UnicodeNameAlias[] nameAliases = UnicodeNameAlias.EmptyArray; @@ -249,7 +249,7 @@ namespace System.Unicode private static UnicodeBlock ReadBlockEntry(BinaryReader reader) { - return new UnicodeBlock(new UnicodeCharacterRange(ReadCodePoint(reader), ReadCodePoint(reader)), reader.ReadString()); + return new UnicodeBlock(new UnicodeCodePointRange(ReadCodePoint(reader), ReadCodePoint(reader)), reader.ReadString()); } private static int ReadInt24(BinaryReader reader) diff --git a/UnicodeInformation/UnicodeInformation.csproj b/UnicodeInformation/UnicodeInformation.csproj index 7b8e294..f567271 100644 --- a/UnicodeInformation/UnicodeInformation.csproj +++ b/UnicodeInformation/UnicodeInformation.csproj @@ -3,7 +3,7 @@ 11.0 - Debug + Release AnyCPU {CB722958-A1C4-4121-804B-7D5A671491B1} Library @@ -83,7 +83,7 @@ - +