Example #1
0
        public void TestDummyIntTrie()
        {
            Int32Trie trie;
            int       initialValue = 0x01234567, leadUnitValue = unchecked ((int)0x89abcdef);
            int       value;
            int       c;

            trie = new Int32Trie(initialValue, leadUnitValue, new DummyGetFoldingOffset());

            /* test that all code points have initialValue */
            for (c = 0; c <= 0x10ffff; ++c)
            {
                value = trie.GetCodePointValue(c);
                if (value != initialValue)
                {
                    Errln("IntTrie/dummy.getCodePointValue(c)(U+" + Hex(c) + ")=0x" + Hex(value) + " instead of 0x" + Hex(initialValue));
                }
            }

            /* test that the lead surrogate code units have leadUnitValue */
            for (c = 0xd800; c <= 0xdbff; ++c)
            {
                value = trie.GetLeadValue((char)c);
                if (value != leadUnitValue)
                {
                    Errln("IntTrie/dummy.getLeadValue(c)(U+" + Hex(c) + ")=0x" + Hex(value) + " instead of 0x" + Hex(leadUnitValue));
                }
            }
        }
Example #2
0
        private void _testTrieRanges(SetRange[] setRanges, int countSetRanges,
                                     CheckRange[] checkRanges, int countCheckRanges,
                                     bool latin1Linear)
        {
            Int32TrieBuilder newTrie = new Int32TrieBuilder(null, 2000,
                                                            checkRanges[0].Value,
                                                            checkRanges[0].Value,
                                                            latin1Linear);

            // set values from setRanges[]
            bool ok = true;

            for (int i = 0; i < countSetRanges; ++i)
            {
                int  start     = setRanges[i].start;
                int  limit     = setRanges[i].limit;
                int  value     = setRanges[i].value;
                bool overwrite = setRanges[i].overwrite;
                if ((limit - start) == 1 && overwrite)
                {
                    ok &= newTrie.SetValue(start, value);
                }
                else
                {
                    ok &= newTrie.SetRange(start, limit, value, overwrite);
                }
            }
            if (!ok)
            {
                Errln("setting values into a trie failed");
                return;
            }

            {
                // verify that all these values are in the new Trie
                int start = 0;
                for (int i = 0; i < countCheckRanges; ++i)
                {
                    int limit = checkRanges[i].Limit;
                    int value = checkRanges[i].Value;

                    while (start < limit)
                    {
                        if (value != newTrie.GetValue(start))
                        {
                            Errln("newTrie [U+"
                                  + (start).ToHexString() + "]==0x"
                                  + (newTrie.GetValue(start).ToHexString())
                                  + " instead of 0x" + (value).ToHexString());
                        }
                        ++start;
                    }
                }

                Int32Trie trie = newTrie.Serialize(new _testFoldedValue(newTrie),
                                                   new _testFoldingOffset());

                // test linear Latin-1 range from utrie_getData()
                if (latin1Linear)
                {
                    start = 0;
                    for (int i = 0; i < countCheckRanges && start <= 0xff; ++i)
                    {
                        int limit = checkRanges[i].Limit;
                        int value = checkRanges[i].Value;

                        while (start < limit && start <= 0xff)
                        {
                            if (value != trie.GetLatin1LinearValue((char)start))
                            {
                                Errln("IntTrie.getLatin1LinearValue[U+"
                                      + (start).ToHexString() + "]==0x"
                                      + (
                                          trie.GetLatin1LinearValue((char)start).ToHexString())
                                      + " instead of 0x" + (value).ToHexString());
                            }
                            ++start;
                        }
                    }
                }

                if (latin1Linear != trie.IsLatin1Linear)
                {
                    Errln("trie serialization did not preserve "
                          + "Latin-1-linearity");
                }

                // verify that all these values are in the serialized Trie
                start = 0;
                for (int i = 0; i < countCheckRanges; ++i)
                {
                    int limit = checkRanges[i].Limit;
                    int value = checkRanges[i].Value;

                    if (start == 0xd800)
                    {
                        // skip surrogates
                        start = limit;
                        continue;
                    }

                    while (start < limit)
                    {
                        if (start <= 0xffff)
                        {
                            int value2 = trie.GetBMPValue((char)start);
                            if (value != value2)
                            {
                                Errln("serialized trie.getBMPValue(U+"
                                      + (start).ToHexString() + " == 0x"
                                      + (value2).ToHexString() + " instead of 0x"
                                      + (value).ToHexString());
                            }
                            if (!UTF16.IsLeadSurrogate((char)start))
                            {
                                value2 = trie.GetLeadValue((char)start);
                                if (value != value2)
                                {
                                    Errln("serialized trie.getLeadValue(U+"
                                          + (start).ToHexString() + " == 0x"
                                          + (value2).ToHexString() + " instead of 0x"
                                          + (value).ToHexString());
                                }
                            }
                        }

                        {
                            int value2 = trie.GetCodePointValue(start);
                            if (value != value2)
                            {
                                Errln("serialized trie.getCodePointValue(U+"
                                      + (start).ToHexString() + ")==0x"
                                      + (value2).ToHexString() + " instead of 0x"
                                      + (value).ToHexString());
                            }
                            ++start;
                        }
                    }
                }


                // enumerate and verify all ranges

                int            enumRanges = 1;
                TrieEnumerator iter       = new _testEnumValue(trie);
                while (iter.MoveNext())
                {
                    RangeValueEnumeratorElement result = iter.Current;
                    if (result.Start != checkRanges[enumRanges - 1].Limit ||
                        result.Limit != checkRanges[enumRanges].Limit ||
                        (result.Value ^ 0x5555) != checkRanges[enumRanges].Value)
                    {
                        Errln("utrie_enum() delivers wrong range [U+"
                              + (result.Start).ToHexString() + "..U+"
                              + (result.Limit).ToHexString() + "].0x"
                              + (result.Value ^ 0x5555).ToHexString()
                              + " instead of [U+"
                              + (checkRanges[enumRanges - 1].Limit).ToHexString()
                              + "..U+"
                              + (checkRanges[enumRanges].Limit).ToHexString()
                              + "].0x"
                              + (checkRanges[enumRanges].Value).ToHexString());
                    }
                    enumRanges++;
                }

                // test linear Latin-1 range
                if (trie.IsLatin1Linear)
                {
                    for (start = 0; start < 0x100; ++start)
                    {
                        if (trie.GetLatin1LinearValue((char)start)
                            != trie.GetLeadValue((char)start))
                        {
                            Errln("trie.getLatin1LinearValue[U+"
                                  + (start).ToHexString() + "]=0x"
                                  + (
                                      trie.GetLatin1LinearValue((char)start).ToHexString())
                                  + " instead of 0x"
                                  + (
                                      trie.GetLeadValue((char)start)).ToHexString());
                        }
                    }
                }

                _testTrieIteration(trie, checkRanges, countCheckRanges);
            }
        }
Example #3
0
        private void _testTrieIteration(Int32Trie trie, CheckRange[] checkRanges,
                                        int countCheckRanges)
        {
            // write a string
            int          countValues = 0;
            StringBuffer s           = new StringBuffer();

            int[] values = new int[30];
            for (int i = 0; i < countCheckRanges; ++i)
            {
                int c = checkRanges[i].Limit;
                if (c != 0)
                {
                    --c;
                    UTF16.Append(s, c);
                    values[countValues++] = checkRanges[i].Value;
                }
            }

            {
                int limit = s.Length;
                // try forward
                int p = 0;
                int i = 0;
                while (p < limit)
                {
                    int c = UTF16.CharAt(s, p);
                    p += UTF16.GetCharCount(c);
                    int value = trie.GetCodePointValue(c);
                    if (value != values[i])
                    {
                        Errln("wrong value from UTRIE_NEXT(U+"
                              + (c).ToHexString() + "): 0x"
                              + (value).ToHexString() + " instead of 0x"
                              + (values[i]).ToHexString());
                    }
                    // unlike the c version lead is 0 if c is non-supplementary
                    char lead  = UTF16.GetLeadSurrogate(c);
                    char trail = UTF16.GetTrailSurrogate(c);
                    if (lead == 0
                        ? trail != s[p - 1]
                        : !UTF16.IsLeadSurrogate(lead) ||
                        !UTF16.IsTrailSurrogate(trail) || lead != s[p - 2] ||
                        trail != s[p - 1])
                    {
                        Errln("wrong (lead, trail) from UTRIE_NEXT(U+"
                              + (c).ToHexString());
                        continue;
                    }
                    if (lead != 0)
                    {
                        value = trie.GetLeadValue(lead);
                        value = trie.GetTrailValue(value, trail);
                        if (value != trie.GetSurrogateValue(lead, trail) &&
                            value != values[i])
                        {
                            Errln("wrong value from getting supplementary "
                                  + "values (U+"
                                  + (c).ToHexString() + "): 0x"
                                  + (value).ToHexString() + " instead of 0x"
                                  + (values[i]).ToHexString());
                        }
                    }
                    ++i;
                }
            }
        }