public void UnihanCodePointPackingShouldHaveExpectedResults() { for (int i = 0x3400; i < 0x4E00; ++i) { Assert.Equal(i, UnihanCharacterData.UnpackCodePoint(UnihanCharacterData.PackCodePoint(i))); } for (int i = 0x4E00; i < 0xA000; ++i) { Assert.Equal(i, UnihanCharacterData.UnpackCodePoint(UnihanCharacterData.PackCodePoint(i))); } for (int i = 0xF900; i < 0xFB00; ++i) { Assert.Equal(i, UnihanCharacterData.UnpackCodePoint(UnihanCharacterData.PackCodePoint(i))); } for (int i = 0x20000; i < 0x2F800; ++i) { Assert.Equal(i, UnihanCharacterData.UnpackCodePoint(UnihanCharacterData.PackCodePoint(i))); } for (int i = 0x2F800; i < 0x30000; ++i) { Assert.Equal(i, UnihanCharacterData.UnpackCodePoint(UnihanCharacterData.PackCodePoint(i))); } // The PackCodePoint method should fail for code points outside of the valid range. Assert.Throws <ArgumentOutOfRangeException>(() => UnihanCharacterData.PackCodePoint(0xA000)); Assert.Throws <ArgumentOutOfRangeException>(() => UnihanCharacterData.PackCodePoint(0xFB00)); Assert.Throws <ArgumentOutOfRangeException>(() => UnihanCharacterData.PackCodePoint(0x30000)); // The UnpackCodePoint method should fail for values outside of the valid range. Assert.Throws <ArgumentOutOfRangeException>(() => UnihanCharacterData.UnpackCodePoint(-1)); Assert.Throws <ArgumentOutOfRangeException>(() => UnihanCharacterData.UnpackCodePoint(0x20000)); }
public void CodePointPackingShouldRoundTrip(string blockName) { var block = Blocks.Single(b => b.Name == blockName); foreach (int codePoint in block.CodePointRange) { Assert.Equal(codePoint, UnihanCharacterData.UnpackCodePoint(UnihanCharacterData.PackCodePoint(codePoint))); } }
internal void WriteToFile(BinaryWriter writer) { UnihanFields fields = default(UnihanFields); fields |= (UnihanFields)NumericType; // For now, we have enough bits to encode the length of the array in the field specifier, so we'll do that. // (NB: A quick analysis of the files revealed thare there are almost always exactly one Radical/Stroke count, and occasionally two, yet never more.) if (unicodeRadicalStrokeCounts.Count > 0) { if (unicodeRadicalStrokeCounts.Count == 1) { fields |= UnihanFields.UnicodeRadicalStrokeCount; } else if (unicodeRadicalStrokeCounts.Count == 2) { fields |= UnihanFields.UnicodeRadicalStrokeCountTwice; } else { fields |= UnihanFields.UnicodeRadicalStrokeCountMore; } } if (Definition != null) { fields |= UnihanFields.Definition; } if (MandarinReading != null) { fields |= UnihanFields.MandarinReading; } if (CantoneseReading != null) { fields |= UnihanFields.CantoneseReading; } if (JapaneseKunReading != null) { fields |= UnihanFields.JapaneseKunReading; } if (JapaneseOnReading != null) { fields |= UnihanFields.JapaneseOnReading; } if (KoreanReading != null) { fields |= UnihanFields.KoreanReading; } if (HangulReading != null) { fields |= UnihanFields.HangulReading; } if (VietnameseReading != null) { fields |= UnihanFields.VietnameseReading; } if (SimplifiedVariant != null) { fields |= UnihanFields.SimplifiedVariant; } if (TraditionalVariant != null) { fields |= UnihanFields.TraditionalVariant; } writer.Write((ushort)fields); writer.WriteCodePoint(UnihanCharacterData.PackCodePoint(codePoint)); if ((fields & UnihanFields.OtherNumeric) != 0) { writer.Write(numericValue); } if ((fields & UnihanFields.UnicodeRadicalStrokeCountMore) != 0) { if ((fields & (UnihanFields.UnicodeRadicalStrokeCountMore)) == UnihanFields.UnicodeRadicalStrokeCountMore) { writer.Write(checked ((byte)(unicodeRadicalStrokeCounts.Count - 3))); } foreach (var radicalStrokeCount in unicodeRadicalStrokeCounts) { writer.Write(radicalStrokeCount.Radical); writer.Write(radicalStrokeCount.RawStrokeCount); } } if ((fields & UnihanFields.Definition) != 0) { writer.Write(Definition); } if ((fields & UnihanFields.MandarinReading) != 0) { writer.Write(MandarinReading); } if ((fields & UnihanFields.CantoneseReading) != 0) { writer.Write(CantoneseReading); } if ((fields & UnihanFields.JapaneseKunReading) != 0) { writer.Write(JapaneseKunReading); } if ((fields & UnihanFields.JapaneseOnReading) != 0) { writer.Write(JapaneseOnReading); } if ((fields & UnihanFields.KoreanReading) != 0) { writer.Write(KoreanReading); } if ((fields & UnihanFields.HangulReading) != 0) { writer.Write(HangulReading); } if ((fields & UnihanFields.VietnameseReading) != 0) { writer.Write(VietnameseReading); } if ((fields & UnihanFields.SimplifiedVariant) != 0) { writer.Write(SimplifiedVariant); } if ((fields & UnihanFields.TraditionalVariant) != 0) { writer.Write(TraditionalVariant); } }