public void TestBasic() { // This is not interesting anymore as the data is already built // beforehand // check build // UnicodeSet ss = CanonicalIterator.getSafeStart(); // Logln("Safe Start: " + ss.toPattern(true)); // ss = CanonicalIterator.getStarts('a'); // expectEqual("Characters with 'a' at the start of their decomposition: ", "", CanonicalIterator.getStarts('a'), // new UnicodeSet("[\u00E0-\u00E5\u0101\u0103\u0105\u01CE\u01DF\u01E1\u01FB" // + "\u0201\u0203\u0227\u1E01\u1EA1\u1EA3\u1EA5\u1EA7\u1EA9\u1EAB\u1EAD\u1EAF\u1EB1\u1EB3\u1EB5\u1EB7]") // ); // check permute // NOTE: we use a TreeSet below to sort the output, which is not guaranteed to be sorted! ISet <string> results = new SortedSet <string>(StringComparer.Ordinal); CanonicalIterator.Permute("ABC", false, results); expectEqual("Simple permutation ", "", CollectionToString(results), "ABC, ACB, BAC, BCA, CAB, CBA"); // try samples ISet <string> set = new SortedSet <string>(StringComparer.Ordinal); for (int i = 0; i < testArray.Length; ++i) { //Logln("Results for: " + name.transliterate(testArray[i])); CanonicalIterator it = new CanonicalIterator(testArray[i][0]); // int counter = 0; set.Clear(); string first = null; while (true) { String result = it.Next(); if (first == null) { first = result; } if (result == null) { break; } set.Add(result); // sort them //Logln(++counter + ": " + hex.transliterate(result)); //Logln(" = " + name.transliterate(result)); } expectEqual(i + ": ", testArray[i][0], CollectionToString(set), testArray[i][1]); it.Reset(); if (!it.Next().Equals(first)) { Errln("CanonicalIterator.reset() failed"); } if (!it.Source.Equals(Normalizer.Normalize(testArray[i][0], Normalizer.NFD))) { Errln("CanonicalIterator.getSource() does not return NFD of input source"); } } }
public int TestSpeed() { // skip unless verbose if (!IsVerbose()) { return(0); } string s = "\uAC01\u0345"; CanonicalIterator it = new CanonicalIterator(s); double start, end; int x = 0; // just to keep code from optimizing away. int iterations = 10000; double slowDelta = 0; /* * CanonicalIterator slowIt = new CanonicalIterator(s); * slowIt.SKIP_ZEROS = false; * * start = System.currentTimeMillis(); * for (int i = 0; i < iterations; ++i) { * slowIt.setSource(s); * while (true) { * String item = slowIt.next(); * if (item == null) break; * x += item.length(); * } * } * end = System.currentTimeMillis(); * double slowDelta = (end-start) / iterations; * Logln("Slow iteration: " + slowDelta); */ start = Time.CurrentTimeMilliseconds(); for (int i = 0; i < iterations; ++i) { it.SetSource(s); while (true) { string item = it.Next(); if (item == null) { break; } x += item.Length; } } end = Time.CurrentTimeMilliseconds(); double fastDelta = (end - start) / iterations; Logln("Fast iteration: " + fastDelta + (slowDelta != 0 ? ", " + (fastDelta / slowDelta) : "")); return(x); }
private void CharacterTest(string s, int ch, CanonicalIterator it) { int mixedCounter = 0; int lastMixedCounter = -1; bool gotDecomp = false; bool gotComp = false; bool gotSource = false; string decomp = Normalizer.Decompose(s, false); string comp = Normalizer.Compose(s, false); // skip characters that don't have either decomp. // need quick test for this! if (s.Equals(decomp) && s.Equals(comp)) { return; } it.SetSource(s); while (true) { string item = it.Next(); if (item == null) { break; } if (item.Equals(s)) { gotSource = true; } if (item.Equals(decomp)) { gotDecomp = true; } if (item.Equals(comp)) { gotComp = true; } if ((mixedCounter & 0x7F) == 0 && (ch < 0xAD00 || ch > 0xAC00 + 11172)) { if (lastMixedCounter != mixedCounter) { Logln(""); lastMixedCounter = mixedCounter; } Logln("\t" + mixedCounter + "\t" + Hex(item) + (item.Equals(s) ? "\t(*original*)" : "") + (item.Equals(decomp) ? "\t(*decomp*)" : "") + (item.Equals(comp) ? "\t(*comp*)" : "") ); } } // check that zeros optimization doesn't mess up. /* * if (true) { * it.reset(); * itSet.clear(); * while (true) { * String item = it.next(); * if (item == null) break; * itSet.add(item); * } * slowIt.setSource(s); * slowItSet.clear(); * while (true) { * String item = slowIt.next(); * if (item == null) break; * slowItSet.add(item); * } * if (!itSet.equals(slowItSet)) { * errln("Zero optimization failure with " + getReadable(s)); * } * } */ mixedCounter++; if (!gotSource || !gotDecomp || !gotComp) { Errln("FAIL CanonicalIterator: " + s + " decomp: " + decomp + " comp: " + comp); it.Reset(); for (string item = it.Next(); item != null; item = it.Next()) { Err(item + " "); } Errln(""); } }