public void TestDummyIntTrie() { IntTrie trie; int initialValue = 0x01234567, leadUnitValue = -1985229329; int value_ren; int c; trie = new IntTrie(initialValue, leadUnitValue, new TrieTest.DummyGetFoldingOffset()); /* test that all code points have initialValue */ for (c = 0; c <= 0x10ffff; ++c) { value_ren = trie.GetCodePointValue(c); if (value_ren != initialValue) { Errln("IntTrie/dummy.getCodePointValue(c)(U+" + IBM.ICU.Charset.TestFmwk.Hex(c) + ")=0x" + IBM.ICU.Charset.TestFmwk.Hex(value_ren) + " instead of 0x" + IBM.ICU.Charset.TestFmwk.Hex(initialValue)); } } /* test that the lead surrogate code units have leadUnitValue */ for (c = 0xd800; c <= 0xdbff; ++c) { value_ren = trie.GetLeadValue((char)c); if (value_ren != leadUnitValue) { Errln("IntTrie/dummy.getLeadValue(c)(U+" + IBM.ICU.Charset.TestFmwk.Hex(c) + ")=0x" + IBM.ICU.Charset.TestFmwk.Hex(value_ren) + " instead of 0x" + IBM.ICU.Charset.TestFmwk.Hex(leadUnitValue)); } } }
private void _testTrieRanges(TrieTest.SetRange[] setRanges, int countSetRanges, TrieTest.CheckRange[] checkRanges, int countCheckRanges, bool latin1Linear) { IntTrieBuilder newTrie = new IntTrieBuilder(null, 2000, checkRanges[0].value_ren, checkRanges[0].value_ren, latin1Linear); // set values from setRanges[] bool ok = true; for (int i = 0; i < countSetRanges; ++i) { int start_0 = setRanges[i].start; int limit_1 = setRanges[i].limit; int value_ren = setRanges[i].value_ren; bool overwrite_2 = setRanges[i].overwrite; if ((limit_1 - start_0) == 1 && overwrite_2) { ok &= newTrie.SetValue(start_0, value_ren); } else { ok &= newTrie.SetRange(start_0, limit_1, value_ren, overwrite_2); } } if (!ok) { Errln("setting values into a trie failed"); return; } // verify that all these values are in the new Trie int start_3 = 0; for (int i_4 = 0; i_4 < countCheckRanges; ++i_4) { int limit_5 = checkRanges[i_4].limit; int value_6 = checkRanges[i_4].value_ren; while (start_3 < limit_5) { if (value_6 != newTrie.GetValue(start_3)) { Errln("newTrie [U+" + ILOG.J2CsMapping.Util.IlNumber.ToString(start_3, 16) + "]==0x" + ILOG.J2CsMapping.Util.IlNumber.ToString(newTrie.GetValue(start_3), 16) + " instead of 0x" + ILOG.J2CsMapping.Util.IlNumber.ToString(value_6, 16)); } ++start_3; } } IntTrie trie = newTrie.Serialize(new TrieTest._testFoldedValue(newTrie), new TrieTest._testFoldingOffset()); // test linear Latin-1 range from utrie_getData() if (latin1Linear) { start_3 = 0; for (int i_7 = 0; i_7 < countCheckRanges && start_3 <= 0xff; ++i_7) { int limit_8 = checkRanges[i_7].limit; int value_9 = checkRanges[i_7].value_ren; while (start_3 < limit_8 && start_3 <= 0xff) { if (value_9 != trie.GetLatin1LinearValue((char)start_3)) { Errln("IntTrie.getLatin1LinearValue[U+" + ILOG.J2CsMapping.Util.IlNumber.ToString(start_3, 16) + "]==0x" + ILOG.J2CsMapping.Util.IlNumber.ToString(trie .GetLatin1LinearValue((char)start_3), 16) + " instead of 0x" + ILOG.J2CsMapping.Util.IlNumber.ToString(value_9, 16)); } ++start_3; } } } if (latin1Linear != trie.IsLatin1Linear()) { Errln("trie serialization did not preserve " + "Latin-1-linearity"); } // verify that all these values are in the serialized Trie start_3 = 0; for (int i_10 = 0; i_10 < countCheckRanges; ++i_10) { int limit_11 = checkRanges[i_10].limit; int value_12 = checkRanges[i_10].value_ren; if (start_3 == 0xd800) { // skip surrogates start_3 = limit_11; continue; } while (start_3 < limit_11) { if (start_3 <= 0xffff) { int value2 = trie.GetBMPValue((char)start_3); if (value_12 != value2) { Errln("serialized trie.getBMPValue(U+" + ILOG.J2CsMapping.Util.IlNumber.ToString(start_3, 16) + " == 0x" + ILOG.J2CsMapping.Util.IlNumber.ToString(value2, 16) + " instead of 0x" + ILOG.J2CsMapping.Util.IlNumber.ToString(value_12, 16)); } if (!IBM.ICU.Text.UTF16.IsLeadSurrogate((char)start_3)) { value2 = trie.GetLeadValue((char)start_3); if (value_12 != value2) { Errln("serialized trie.getLeadValue(U+" + ILOG.J2CsMapping.Util.IlNumber.ToString(start_3, 16) + " == 0x" + ILOG.J2CsMapping.Util.IlNumber.ToString(value2, 16) + " instead of 0x" + ILOG.J2CsMapping.Util.IlNumber.ToString(value_12, 16)); } } } int value2_13 = trie.GetCodePointValue(start_3); if (value_12 != value2_13) { Errln("serialized trie.getCodePointValue(U+" + ILOG.J2CsMapping.Util.IlNumber.ToString(start_3, 16) + ")==0x" + ILOG.J2CsMapping.Util.IlNumber.ToString(value2_13, 16) + " instead of 0x" + ILOG.J2CsMapping.Util.IlNumber.ToString(value_12, 16)); } ++start_3; } } // enumerate and verify all ranges int enumRanges = 1; TrieIterator iter = new TrieTest._testEnumValue(trie); RangeValueIterator_Constants.Element result = new RangeValueIterator_Constants.Element(); while (iter.Next(result)) { if (result.start != checkRanges[enumRanges - 1].limit || result.limit != checkRanges[enumRanges].limit || (result.value_ren ^ 0x5555) != checkRanges[enumRanges].value_ren) { Errln("utrie_enum() delivers wrong range [U+" + ILOG.J2CsMapping.Util.IlNumber.ToString(result.start, 16) + "..U+" + ILOG.J2CsMapping.Util.IlNumber.ToString(result.limit, 16) + "].0x" + ILOG.J2CsMapping.Util.IlNumber.ToString(result.value_ren ^ 0x5555, 16) + " instead of [U+" + ILOG.J2CsMapping.Util.IlNumber.ToString(checkRanges[enumRanges - 1].limit, 16) + "..U+" + ILOG.J2CsMapping.Util.IlNumber.ToString(checkRanges[enumRanges].limit, 16) + "].0x" + ILOG.J2CsMapping.Util.IlNumber.ToString(checkRanges[enumRanges].value_ren, 16)); } enumRanges++; } // test linear Latin-1 range if (trie.IsLatin1Linear()) { for (start_3 = 0; start_3 < 0x100; ++start_3) { if (trie.GetLatin1LinearValue((char)start_3) != trie .GetLeadValue((char)start_3)) { Errln("trie.getLatin1LinearValue[U+" + ILOG.J2CsMapping.Util.IlNumber.ToString(start_3, 16) + "]=0x" + ILOG.J2CsMapping.Util.IlNumber.ToString(trie .GetLatin1LinearValue((char)start_3), 16) + " instead of 0x" + ILOG.J2CsMapping.Util.IlNumber.ToString(trie .GetLeadValue((char)start_3), 16)); } } } _testTrieIteration(trie, checkRanges, countCheckRanges); }
private void _testTrieIteration(IntTrie trie, TrieTest.CheckRange[] checkRanges, int countCheckRanges) { // write a string int countValues = 0; StringBuilder s = new StringBuilder(); int[] values = new int[30]; for (int i = 0; i < countCheckRanges; ++i) { int c = checkRanges[i].limit; if (c != 0) { --c; IBM.ICU.Text.UTF16.Append(s, c); values[countValues++] = checkRanges[i].value_ren; } } int limit_0 = s.Length; // try forward int p = 0; int i_1 = 0; while (p < limit_0) { int c_2 = IBM.ICU.Text.UTF16.CharAt(s, p); p += IBM.ICU.Text.UTF16.GetCharCount(c_2); int value_ren = trie.GetCodePointValue(c_2); if (value_ren != values[i_1]) { Errln("wrong value from UTRIE_NEXT(U+" + ILOG.J2CsMapping.Util.IlNumber.ToString(c_2, 16) + "): 0x" + ILOG.J2CsMapping.Util.IlNumber.ToString(value_ren, 16) + " instead of 0x" + ILOG.J2CsMapping.Util.IlNumber.ToString(values[i_1], 16)); } // unlike the c version lead is 0 if c is non-supplementary char lead = IBM.ICU.Text.UTF16.GetLeadSurrogate(c_2); char trail = IBM.ICU.Text.UTF16.GetTrailSurrogate(c_2); if ((lead == 0) ? trail != s[p - 1] : !IBM.ICU.Text.UTF16 .IsLeadSurrogate(lead) || !IBM.ICU.Text.UTF16.IsTrailSurrogate(trail) || lead != s[p - 2] || trail != s[p - 1]) { Errln("wrong (lead, trail) from UTRIE_NEXT(U+" + ILOG.J2CsMapping.Util.IlNumber.ToString(c_2, 16)); continue; } if (lead != 0) { value_ren = trie.GetLeadValue(lead); value_ren = trie.GetTrailValue(value_ren, trail); if (value_ren != trie.GetSurrogateValue(lead, trail) && value_ren != values[i_1]) { Errln("wrong value from getting supplementary " + "values (U+" + ILOG.J2CsMapping.Util.IlNumber.ToString(c_2, 16) + "): 0x" + ILOG.J2CsMapping.Util.IlNumber.ToString(value_ren, 16) + " instead of 0x" + ILOG.J2CsMapping.Util.IlNumber.ToString(values[i_1], 16)); } } ++i_1; } }