コード例 #1
0
        public void TestSetTextCharacterIterator()
        {
            // Failed in java too
            RuleBasedCollator rbColl = (RuleBasedCollator)ILOG.J2CsMapping.Text.Collator
                                       .GetInstance(new Locale("es", "", "TRADITIONAL"));
            String text = "caa";
            CollationElementIterator iterator = rbColl
                                                .GetCollationElementIterator(text);

            iterator.SetOffset(1);
            NUnit.Framework.Assert.AreEqual(1, iterator.GetOffset());
            iterator.SetText(new StringCharacterIterator("cha"));
            iterator.SetOffset(1);
            NUnit.Framework.Assert.AreEqual(1, iterator.GetOffset());
        }
コード例 #2
0
        public void TestSearchCollatorElements()
        {
            String tsceText =
                " \uAC00" +              // simple LV Hangul
                " \uAC01" +              // simple LVT Hangul
                " \uAC0F" +              // LVTT, last jamo expands for search
                " \uAFFF" +              // LLVVVTT, every jamo expands for search
                " \u1100\u1161\u11A8" +  // 0xAC01 as conjoining jamo
                " \u3131\u314F\u3131" +  // 0xAC01 as compatibility jamo
                " \u1100\u1161\u11B6" +  // 0xAC0F as conjoining jamo; last expands for search
                " \u1101\u1170\u11B6" +  // 0xAFFF as conjoining jamo; all expand for search
                " \u00E6" +              // small letter ae, expands
                " \u1E4D" +              // small letter o with tilde and acute, decomposes
                " ";

            int[] rootStandardOffsets =
            {
                0,   1,  2,
                2,   3,  4,  4,
                4,   5,  6,  6,
                6,   7,  8,  8,
                8,   9, 10, 11,
                12, 13, 14, 15,
                16, 17, 18, 19,
                20, 21, 22, 23,
                24, 25, 26,/* plus another 1-2 offset=26 if ae-ligature maps to three CEs */
                26, 27, 28, 28,
                28,
                29
            };

            int[] rootSearchOffsets =
            {
                0,   1,  2,
                2,   3,  4,  4,
                4,   5,  6,  6,  6,
                6,   7,  8,  8,  8,  8,  8,  8,
                8,   9, 10, 11,
                12, 13, 14, 15,
                16, 17, 18, 19, 20,
                20, 21, 22, 22, 23, 23, 23, 24,
                24, 25, 26,/* plus another 1-2 offset=26 if ae-ligature maps to three CEs */
                26, 27, 28, 28,
                28,
                29
            };


            TSCEItem[] tsceItems =
            {
                new TSCEItem("root",                  rootStandardOffsets),
                new TSCEItem("root@collation=search", rootSearchOffsets),
            };

            foreach (TSCEItem tsceItem in tsceItems)
            {
                String            localeString = tsceItem.LocaleString;
                ULocale           uloc         = new ULocale(localeString);
                RuleBasedCollator col          = null;
                try
                {
                    col = (RuleBasedCollator)Collator.GetInstance(uloc);
                }
                catch (Exception e)
                {
                    Errln("Error: in locale " + localeString + ", err in Collator.getInstance");
                    continue;
                }
                CollationElementIterator uce = col.GetCollationElementIterator(tsceText);
                int[] offsets = tsceItem.GetOffsets();
                int   ioff, noff = offsets.Length;
                int   offset, element;

                ioff = 0;
                do
                {
                    offset  = uce.GetOffset();
                    element = uce.Next();
                    Logln(String.Format("({0}) offset={1:d2}  ce={2:x8}\n", tsceItem.LocaleString, offset, element));
                    if (element == 0)
                    {
                        Errln("Error: in locale " + localeString + ", CEIterator next() returned element 0");
                    }
                    if (ioff < noff)
                    {
                        if (offset != offsets[ioff])
                        {
                            Errln("Error: in locale " + localeString + ", expected CEIterator next()->getOffset " + offsets[ioff] + ", got " + offset);
                            //ioff = noff;
                            //break;
                        }
                        ioff++;
                    }
                    else
                    {
                        Errln("Error: in locale " + localeString + ", CEIterator next() returned more elements than expected");
                    }
                } while (element != CollationElementIterator.NULLORDER);
                if (ioff < noff)
                {
                    Errln("Error: in locale " + localeString + ", CEIterator next() returned fewer elements than expected");
                }

                // backwards test
                uce.SetOffset(tsceText.Length);
                ioff = noff;
                do
                {
                    offset  = uce.GetOffset();
                    element = uce.Previous();
                    if (element == 0)
                    {
                        Errln("Error: in locale " + localeString + ", CEIterator previous() returned element 0");
                    }
                    if (ioff > 0)
                    {
                        ioff--;
                        if (offset != offsets[ioff])
                        {
                            Errln("Error: in locale " + localeString + ", expected CEIterator previous()->getOffset " + offsets[ioff] + ", got " + offset);
                            //ioff = 0;
                            //break;
                        }
                    }
                    else
                    {
                        Errln("Error: in locale " + localeString + ", CEIterator previous() returned more elements than expected");
                    }
                } while (element != CollationElementIterator.NULLORDER);
                if (ioff > 0)
                {
                    Errln("Error: in locale " + localeString + ", CEIterator previous() returned fewer elements than expected");
                }
            }
        }
コード例 #3
0
        public void TestClearBuffers(/* char* par */)
        {
            RuleBasedCollator c = null;

            try
            {
                c = new RuleBasedCollator("&a < b < c & ab = d");
            }
            catch (Exception e)
            {
                Warnln("Couldn't create a RuleBasedCollator.");
                return;
            }

            String source = "abcd";
            CollationElementIterator i = c.GetCollationElementIterator(source);
            int e0 = 0;

            try
            {
                e0 = i.Next();    // save the first collation element
            }
            catch (Exception e)
            {
                Errln("call to i.Next() failed.");
                return;
            }

            try
            {
                i.SetOffset(3);        // go to the expanding character
            }
            catch (Exception e)
            {
                Errln("call to i.setOffset(3) failed.");
                return;
            }

            try
            {
                i.Next();                // but only use up half of it
            }
            catch (Exception e)
            {
                Errln("call to i.Next() failed.");
                return;
            }

            try
            {
                i.SetOffset(0);        // go back to the beginning
            }
            catch (Exception e)
            {
                Errln("call to i.setOffset(0) failed. ");
            }

            {
                int e = 0;
                try
                {
                    e = i.Next();    // and get this one again
                }
                catch (Exception ee)
                {
                    Errln("call to i.Next() failed. ");
                    return;
                }

                if (e != e0)
                {
                    Errln("got 0x" + (e).ToHexString() + ", expected 0x" + (e0).ToHexString());
                }
            }
        }
コード例 #4
0
        public void TestOffset(/* char* par */)
        {
            RuleBasedCollator en_us;

            try
            {
                en_us = (RuleBasedCollator)Collator.GetInstance(new CultureInfo("en-US"));
            }
            catch (Exception e)
            {
                Warnln("ERROR: in creation of collator of ENGLISH locale");
                return;
            }

            CollationElementIterator iter = en_us.GetCollationElementIterator(test1);

            // testing boundaries
            iter.SetOffset(0);
            if (iter.Previous() != CollationElementIterator.NULLORDER)
            {
                Errln("Error: After setting offset to 0, we should be at the end "
                      + "of the backwards iteration");
            }
            iter.SetOffset(test1.Length);
            if (iter.Next() != CollationElementIterator.NULLORDER)
            {
                Errln("Error: After setting offset to the end of the string, we "
                      + "should be at the end of the forwards iteration");
            }

            // Run all the way through the iterator, then get the offset
            int[] orders = CollationTest.GetOrders(iter);
            Logln("orders.Length = " + orders.Length);

            int offset = iter.GetOffset();

            if (offset != test1.Length)
            {
                String msg1 = "offset at end != length: ";
                String msg2 = " vs ";
                Errln(msg1 + offset + msg2 + test1.Length);
            }

            // Now set the offset back to the beginning and see if it works
            CollationElementIterator pristine = en_us.GetCollationElementIterator(test1);

            try
            {
                iter.SetOffset(0);
            }
            catch (Exception e)
            {
                Errln("setOffset failed.");
            }
            assertEqual(iter, pristine);

            // setting offset in the middle of a contraction
            String            contraction = "change";
            RuleBasedCollator tailored    = null;

            try
            {
                tailored = new RuleBasedCollator("& a < ch");
            }
            catch (Exception e)
            {
                Errln("Error: in creation of Spanish collator");
                return;
            }
            iter = tailored.GetCollationElementIterator(contraction);
            int[] order = CollationTest.GetOrders(iter);
            iter.SetOffset(1); // sets offset in the middle of ch
            int[] order2 = CollationTest.GetOrders(iter);
            if (!Arrays.Equals(order, order2))
            {
                Errln("Error: setting offset in the middle of a contraction should be the same as setting it to the start of the contraction");
            }
            contraction = "peache";
            iter        = tailored.GetCollationElementIterator(contraction);
            iter.SetOffset(3);
            order = CollationTest.GetOrders(iter);
            iter.SetOffset(4); // sets offset in the middle of ch
            order2 = CollationTest.GetOrders(iter);
            if (!Arrays.Equals(order, order2))
            {
                Errln("Error: setting offset in the middle of a contraction should be the same as setting it to the start of the contraction");
            }
            // setting offset in the middle of a surrogate pair
            String surrogate = "\ud800\udc00str";

            iter  = tailored.GetCollationElementIterator(surrogate);
            order = CollationTest.GetOrders(iter);
            iter.SetOffset(1); // sets offset in the middle of surrogate
            order2 = CollationTest.GetOrders(iter);
            if (!Arrays.Equals(order, order2))
            {
                Errln("Error: setting offset in the middle of a surrogate pair should be the same as setting it to the start of the surrogate pair");
            }
            surrogate = "simple\ud800\udc00str";
            iter      = tailored.GetCollationElementIterator(surrogate);
            iter.SetOffset(6);
            order = CollationTest.GetOrders(iter);
            iter.SetOffset(7); // sets offset in the middle of surrogate
            order2 = CollationTest.GetOrders(iter);
            if (!Arrays.Equals(order, order2))
            {
                Errln("Error: setting offset in the middle of a surrogate pair should be the same as setting it to the start of the surrogate pair");
            }
            // TODO: try iterating halfway through a messy string.
        }