Example #1
0
        public void TestNext()
        {
            String text = "abc";
            CollationElementIterator iterator = coll
                                                .GetCollationElementIterator(text);

            int[] orders = new int[text.Length];
            int   order  = iterator.Next();
            int   i      = 0;

            while (order != CollationElementIterator.NULLORDER)
            {
                orders[i++] = order;
                order       = iterator.Next();
            }

            int offset = iterator.GetOffset();

            NUnit.Framework.Assert.AreEqual(text.Length, offset);
            order = iterator.Previous();

            while (order != CollationElementIterator.NULLORDER)
            {
                NUnit.Framework.Assert.AreEqual(orders[--i], order);
                order = iterator.Previous();
            }

            NUnit.Framework.Assert.AreEqual(0, iterator.GetOffset());
        }
Example #2
0
        public void TestSecondaryOrder()
        {
            RuleBasedCollator rbColl = (RuleBasedCollator)ILOG.J2CsMapping.Text.Collator
                                       .GetInstance(new Locale("fr", "FR"));
            String text = "a\u00e0";
            CollationElementIterator iterator = rbColl
                                                .GetCollationElementIterator(text);
            int order   = iterator.Next();
            int sOrder1 = CollationElementIterator.SecondaryOrder(order);

            order = iterator.Next();
            int sOrder2 = CollationElementIterator.SecondaryOrder(order);

            NUnit.Framework.Assert.AreEqual(sOrder1, sOrder2);
        }
Example #3
0
        public void TestGetMaxExpansion()
        {
            String            text   = "cha";
            RuleBasedCollator rbColl = (RuleBasedCollator)ILOG.J2CsMapping.Text.Collator
                                       .GetInstance(new Locale("es", "", "TRADITIONAL"));
            CollationElementIterator iterator = rbColl
                                                .GetCollationElementIterator(text);
            int order = iterator.Next();

            while (order != CollationElementIterator.NULLORDER)
            {
                NUnit.Framework.Assert.AreEqual(1, iterator.GetMaxExpansion(order));
                order = iterator.Next();
            }
        }
        public void TestGetCollationElementIteratorCharacterIterator()
        {
            {
                Locale            locale = new Locale("es", "", "TRADITIONAL");
                RuleBasedCollator coll   = (RuleBasedCollator)ILOG.J2CsMapping.Text.Collator
                                           .GetInstance(locale);
                String text = "cha";
                StringCharacterIterator source = new StringCharacterIterator(
                    text);
                CollationElementIterator iterator = coll
                                                    .GetCollationElementIterator(source);
                int[] e_offset = { 0, 1, 2, 3 };
                int   offset   = iterator.GetOffset();
                int   i        = 0;
                NUnit.Framework.Assert.AreEqual(e_offset[i++], offset);
                while (offset != text.Length)
                {
                    iterator.Next();
                    offset = iterator.GetOffset();
                    // System.out.println(offset);
                    NUnit.Framework.Assert.AreEqual(e_offset[i++], offset);
                }
            }

            {
                Locale            locale_0 = new Locale("de", "DE");
                RuleBasedCollator coll_1   = (RuleBasedCollator)ILOG.J2CsMapping.Text.Collator
                                             .GetInstance(locale_0);
                String text_2 = "\u00E6b";
                StringCharacterIterator source_3 = new StringCharacterIterator(
                    text_2);
                CollationElementIterator iterator_4 = coll_1
                                                      .GetCollationElementIterator(source_3);
                int[] e_offset_5 = { 0, 1, 1, 2 };
                int   offset_6   = iterator_4.GetOffset();
                int   i_7        = 0;
                NUnit.Framework.Assert.AreEqual(e_offset_5[i_7++], offset_6);
                while (offset_6 != text_2.Length)
                {
                    iterator_4.Next();
                    offset_6 = iterator_4.GetOffset();
                    NUnit.Framework.Assert.AreEqual(e_offset_5[i_7++], offset_6);
                }
            }
            // Regression for HARMONY-1352
            try
            {
                new RuleBasedCollator("< a< b< c< d")
                .GetCollationElementIterator((CharacterIterator)null);
                NUnit.Framework.Assert.Fail("NullPointerException expected");
            }
            catch (NullReferenceException e)
            {
                // expected
            }
        }
        public void TestGetCollationElementIteratorString()
        {
            {
                Locale            locale = new Locale("es", "", "TRADITIONAL");
                RuleBasedCollator coll   = (RuleBasedCollator)ILOG.J2CsMapping.Text.Collator
                                           .GetInstance(locale);
                String source = "cha";
                CollationElementIterator iterator = coll
                                                    .GetCollationElementIterator(source);
                int[] e_offset = { 0, 1, 2, 3 };
                int   offset   = iterator.GetOffset();
                int   i        = 0;
                NUnit.Framework.Assert.AreEqual(e_offset[i++], offset);
                while (offset != source.Length)
                {
                    iterator.Next();
                    offset = iterator.GetOffset();
                    NUnit.Framework.Assert.AreEqual(e_offset[i++], offset);
                }
            }

            {
                Locale            locale_0 = new Locale("de", "DE");
                RuleBasedCollator coll_1   = (RuleBasedCollator)ILOG.J2CsMapping.Text.Collator
                                             .GetInstance(locale_0);
                String source_2 = "\u00E6b";
                CollationElementIterator iterator_3 = coll_1
                                                      .GetCollationElementIterator(source_2);
                int[] e_offset_4 = { 0, 1, 1, 2 };
                int   offset_5   = iterator_3.GetOffset();
                int   i_6        = 0;
                NUnit.Framework.Assert.AreEqual(e_offset_4[i_6++], offset_5);
                while (offset_5 != source_2.Length)
                {
                    iterator_3.Next();
                    offset_5 = iterator_3.GetOffset();
                    NUnit.Framework.Assert.AreEqual(e_offset_4[i_6++], offset_5);
                }
            }
            // Regression for HARMONY-1352
            try
            {
                new RuleBasedCollator("< a< b< c< d")
                .GetCollationElementIterator((String)null);
                NUnit.Framework.Assert.Fail("NullPointerException expected");
            }
            catch (NullReferenceException e)
            {
                // expected
            }
        }
Example #6
0
        public void TestPrimaryOrder()
        {
            RuleBasedCollator rbColl = (RuleBasedCollator)ILOG.J2CsMapping.Text.Collator
                                       .GetInstance(new Locale("de", "DE"));
            String text = "\u00e6";
            CollationElementIterator iterator = rbColl
                                                .GetCollationElementIterator(text);
            int order  = iterator.Next();
            int pOrder = CollationElementIterator.PrimaryOrder(order);
            CollationElementIterator iterator2 = rbColl
                                                 .GetCollationElementIterator("ae");
            int order2  = iterator2.Next();
            int pOrder2 = CollationElementIterator.PrimaryOrder(order2);

            NUnit.Framework.Assert.AreEqual(pOrder, pOrder2);
        }
Example #7
0
        public void TestGetOffset()
        {
            String text = "abc";
            CollationElementIterator iterator = coll
                                                .GetCollationElementIterator(text);

            int[] offsets = { 0, 1, 2, 3 };
            int   offset  = iterator.GetOffset();
            int   i       = 0;

            NUnit.Framework.Assert.AreEqual(offsets[i++], offset);
            while (offset != text.Length)
            {
                iterator.Next();
                offset = iterator.GetOffset();
                NUnit.Framework.Assert.AreEqual(offsets[i++], offset);
            }
        }
Example #8
0
        void assertEqual(CollationElementIterator i1, CollationElementIterator i2)
        {
            int c1, c2, count = 0;

            do
            {
                c1 = i1.Next();
                c2 = i2.Next();
                if (c1 != c2)
                {
                    Errln("    " + count + ": strength(0x" +
                          (c1).ToHexString() + ") != strength(0x" + (c2).ToHexString() + ")");
                    break;
                }
                count += 1;
            } while (c1 != CollationElementIterator.NULLORDER);
            CollationTest.BackAndForth(this, i1);
            CollationTest.BackAndForth(this, i2);
        }
Example #9
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");
                }
            }
        }
Example #10
0
        public void TestDiscontiguous()
        {
            String rulestr = "&z < AB < X\u0300 < ABC < X\u0300\u0315";

            String[] src = { "ADB",                 "ADBC",                 "A\u0315B",       "A\u0315BC",
                             // base character blocked
                             "XD\u0300",            "XD\u0300\u0315",
                             // non blocking combining character
                             "X\u0319\u0300",       "X\u0319\u0300\u0315",
                             // blocking combining character
                             "X\u0314\u0300",       "X\u0314\u0300\u0315",
                             // contraction prefix
                             "ABDC",                "AB\u0315C",            "X\u0300D\u0315",
                             "X\u0300\u0319\u0315", "X\u0300\u031A\u0315",
                             // ends not with a contraction character
                             "X\u0319\u0300D",      "X\u0319\u0300\u0315D",
                             "X\u0300D\u0315D",     "X\u0300\u0319\u0315D",
                             "X\u0300\u031A\u0315D" };
            String[] tgt =  // non blocking combining character
            {
                "A D B",                "A D BC",                "A \u0315 B",       "A \u0315 BC",
                // base character blocked
                "X D \u0300",           "X D \u0300\u0315",
                // non blocking combining character
                "X\u0300 \u0319",       "X\u0300\u0315 \u0319",
                // blocking combining character
                "X \u0314 \u0300",      "X \u0314 \u0300\u0315",
                // contraction prefix
                "AB DC",                "AB \u0315 C",           "X\u0300 D \u0315",
                "X\u0300\u0315 \u0319", "X\u0300 \u031A \u0315",
                // ends not with a contraction character
                "X\u0300 \u0319D",      "X\u0300\u0315 \u0319D",
                "X\u0300 D\u0315D",     "X\u0300\u0315 \u0319D",
                "X\u0300 \u031A\u0315D"
            };
            int count = 0;

            try
            {
                RuleBasedCollator        coll = new RuleBasedCollator(rulestr);
                CollationElementIterator iter
                    = coll.GetCollationElementIterator("");
                CollationElementIterator resultiter
                    = coll.GetCollationElementIterator("");
                while (count < src.Length)
                {
                    iter.SetText(src[count]);
                    int s = 0;
                    while (s < tgt[count].Length)
                    {
                        int e = tgt[count].IndexOf(' ', s);
                        if (e < 0)
                        {
                            e = tgt[count].Length;
                        }
                        String resultstr = tgt[count].Substring(s, e - s); // ICU4N: Corrected 2nd parameter
                        resultiter.SetText(resultstr);
                        int ce = resultiter.Next();
                        while (ce != CollationElementIterator.NULLORDER)
                        {
                            if (ce != iter.Next())
                            {
                                Errln("Discontiguos contraction test mismatch at"
                                      + count);
                                return;
                            }
                            ce = resultiter.Next();
                        }
                        s = e + 1;
                    }
                    iter.Reset();
                    CollationTest.BackAndForth(this, iter);
                    count++;
                }
            }
            catch (Exception e)
            {
                Warnln("Error running discontiguous tests " + e.ToString());
            }
        }
Example #11
0
        public void TestSetText(/* char* par */)
        {
            RuleBasedCollator        en_us = (RuleBasedCollator)Collator.GetInstance(new CultureInfo("en-US"));
            CollationElementIterator iter1 = en_us.GetCollationElementIterator(test1);
            CollationElementIterator iter2 = en_us.GetCollationElementIterator(test2);

            // Run through the second iterator just to exercise it
            int c = iter2.Next();
            int i = 0;

            while (++i < 10 && c != CollationElementIterator.NULLORDER)
            {
                try
                {
                    c = iter2.Next();
                }
                catch (Exception e)
                {
                    Errln("iter2.Next() returned an error.");
                    break;
                }
            }

            // Now set it to point to the same string as the first iterator
            try
            {
                iter2.SetText(test1);
            }
            catch (Exception e)
            {
                Errln("call to iter2->setText(test1) failed.");
                return;
            }
            assertEqual(iter1, iter2);

            iter1.Reset();
            //now use the overloaded setText(ChracterIterator&, UErrorCode) function to set the text
            CharacterIterator chariter = new StringCharacterIterator(test1);

            try
            {
                iter2.SetText(chariter);
            }
            catch (Exception e)
            {
                Errln("call to iter2->setText(chariter(test1)) failed.");
                return;
            }
            assertEqual(iter1, iter2);

            iter1.Reset();
            //now use the overloaded setText(ChracterIterator&, UErrorCode) function to set the text
            UCharacterIterator uchariter = UCharacterIterator.GetInstance(test1);

            try
            {
                iter2.SetText(uchariter);
            }
            catch (Exception e)
            {
                Errln("call to iter2->setText(uchariter(test1)) failed.");
                return;
            }
            assertEqual(iter1, iter2);
        }
Example #12
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());
                }
            }
        }
Example #13
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.
        }