コード例 #1
0
        public void TestExhaustive()
        {
            int counter            = 0;
            CanonicalEnumerator it = new CanonicalEnumerator("");

            /*
             * CanonicalIterator slowIt = new CanonicalIterator("");
             * slowIt.SKIP_ZEROS = false;
             */
            //Transliterator name = Transliterator.getInstance("[^\\u0020-\\u007F] name");
            //Set itSet = new TreeSet();
            //Set slowItSet = new TreeSet();


            for (int i = 0; i < 0x10FFFF; ++i)
            {
                // skip characters we know don't have decomps
                UUnicodeCategory type = UChar.GetUnicodeCategory(i);
                if (type == UUnicodeCategory.OtherNotAssigned || type == UUnicodeCategory.PrivateUse ||
                    type == UUnicodeCategory.Surrogate)
                {
                    continue;
                }

                if ((++counter % 5000) == 0)
                {
                    Logln("Testing " + Utility.Hex(i, 0));
                }

                string s = UTF16.ValueOf(i);
                CharacterTest(s, i, it);

                CharacterTest(s + "\u0345", i, it);
            }
        }
コード例 #2
0
        public int TestSpeed()
        {
            // skip unless verbose
            if (!IsVerbose())
            {
                return(0);
            }

            string s = "\uAC01\u0345";

            CanonicalEnumerator it = new CanonicalEnumerator(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 (it.MoveNext())
                {
                    string item = it.Current;
                    x += item.Length;
                }
            }
            end = Time.CurrentTimeMilliseconds();
            double fastDelta = (end - start) / iterations;

            Logln("Fast iteration: " + fastDelta + (slowDelta != 0 ? ", " + (fastDelta / slowDelta) : ""));


            return(x);
        }
コード例 #3
0
        private void CharacterTest(string s, int ch, CanonicalEnumerator 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 (it.MoveNext())
            {
                string item = it.Current;
                //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())
                while (it.MoveNext())
                {
                    string item = it.Current;
                    Err(item + "    ");
                }
                Errln("");
            }
        }
コード例 #4
0
        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);

            CanonicalEnumerator.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]));
                CanonicalEnumerator it = new CanonicalEnumerator(testArray[i][0]);
                // int counter = 0;
                set.Clear();
                string first = null;
                while (it.MoveNext())
                {
                    string result = it.Current;
                    if (first == null)
                    {
                        first = result;
                    }
                    set.Add(result); // sort them
                                     //Logln(++counter + ": " + hex.transliterate(result));
                                     //Logln(" = " + name.transliterate(result));
                }
                //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();
                it.MoveNext();
                if (!it.Current.Equals(first))
                {
                    Errln("CanonicalIterator.reset() failed");
                }
                if (!it.Source.Equals(Normalizer.Normalize(testArray[i][0], NormalizerMode.NFD)))
                {
                    Errln("CanonicalIterator.getSource() does not return NFD of input source");
                }
            }
        }