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()); }
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"); } } }
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. }
public void TestMaxExpansion(/* char* par */) { int unassigned = 0xEFFFD; String rule = "&a < ab < c/aba < d < z < ch"; RuleBasedCollator coll = null; try { coll = new RuleBasedCollator(rule); } catch (Exception e) { Warnln("Fail to create RuleBasedCollator"); return; } char ch = (char)0; String str = ch + ""; CollationElementIterator iter = coll.GetCollationElementIterator(str); while (ch < 0xFFFF) { int count = 1; ch++; str = ch + ""; iter.SetText(str); int order = iter.Previous(); // thai management if (order == 0) { order = iter.Previous(); } while (iter.Previous() != CollationElementIterator.NULLORDER) { count++; } if (iter.GetMaxExpansion(order) < count) { Errln("Failure at codepoint " + ch + ", maximum expansion count < " + count); } } // testing for exact max expansion ch = (char)0; while (ch < 0x61) { str = ch + ""; iter.SetText(str); int order = iter.Previous(); if (iter.GetMaxExpansion(order) != 1) { Errln("Failure at codepoint 0x" + (ch).ToHexString() + " maximum expansion count == 1"); } ch++; } ch = (char)0x63; str = ch + ""; iter.SetText(str); int temporder = iter.Previous(); if (iter.GetMaxExpansion(temporder) != 3) { Errln("Failure at codepoint 0x" + (ch).ToHexString() + " maximum expansion count == 3"); } ch = (char)0x64; str = ch + ""; iter.SetText(str); temporder = iter.Previous(); if (iter.GetMaxExpansion(temporder) != 1) { Errln("Failure at codepoint 0x" + (ch).ToHexString() + " maximum expansion count == 1"); } str = UChar.ToString(unassigned); iter.SetText(str); temporder = iter.Previous(); if (iter.GetMaxExpansion(temporder) != 2) { Errln("Failure at codepoint 0x" + (ch).ToHexString() + " maximum expansion count == 2"); } // testing jamo ch = (char)0x1165; str = ch + ""; iter.SetText(str); temporder = iter.Previous(); if (iter.GetMaxExpansion(temporder) > 3) { Errln("Failure at codepoint 0x" + (ch).ToHexString() + " maximum expansion count < 3"); } // testing special jamo &a<\u1165 rule = "\u0026\u0071\u003c\u1165\u002f\u0071\u0071\u0071\u0071"; try { coll = new RuleBasedCollator(rule); } catch (Exception e) { Errln("Fail to create RuleBasedCollator"); return; } iter = coll.GetCollationElementIterator(str); temporder = iter.Previous(); if (iter.GetMaxExpansion(temporder) != 6) { Errln("Failure at codepoint 0x" + (ch).ToHexString() + " maximum expansion count == 6"); } }