Пример #1
0
        public void TestReorderRunsOnly2()
        {
            Bidi   bidi = new Bidi();
            Bidi   bidiL2V = new Bidi();
            String src, dest, visual1, visual2;
            String srcU16, destU16, visual1U16, visual2U16;
            int    option, i, j, nCases;
            sbyte  level;

            Logln("\nEntering TestReorderRunsOnly\n");
            bidi.SetReorderingMode(IBM.ICU.Text.Bidi.REORDER_RUNS_ONLY);
            bidiL2V.SetReorderingOptions(IBM.ICU.Text.Bidi.OPTION_REMOVE_CONTROLS);

            for (option = 0; option < 2; option++)
            {
                bidi.SetReorderingOptions((option == 0) ? IBM.ICU.Text.Bidi.OPTION_REMOVE_CONTROLS
                            : IBM.ICU.Text.Bidi.OPTION_INSERT_MARKS);
                for (i = 0, nCases = testCases.Length; i < nCases; i++)
                {
                    src    = testCases[i].textIn;
                    srcU16 = IBM.ICU.Charset.BidiTest.PseudoToU16(src);
                    for (j = 0; j < 2; j++)
                    {
                        Logln("Now doing test for option " + option + ", case " + i
                              + ", level " + j);
                        level = (sbyte)j;
                        bidi.SetPara(srcU16, level, null);
                        destU16 = bidi.WriteReordered(IBM.ICU.Text.Bidi.DO_MIRRORING);
                        dest    = IBM.ICU.Charset.BidiTest.U16ToPseudo(destU16);
                        CheckWhatYouCan(bidi, src, dest);
                        AssertEquals("Reorder runs only failed for case " + i,
                                     testCases[i].textOut[option][level], dest, src,
                                     null, null, level.ToString());

                        if ((option == 0) && (testCases[i].noroundtrip[level] > 0))
                        {
                            continue;
                        }
                        bidiL2V.SetPara(srcU16, level, null);
                        visual1U16 = bidiL2V.WriteReordered(IBM.ICU.Text.Bidi.DO_MIRRORING);
                        visual1    = IBM.ICU.Charset.BidiTest.U16ToPseudo(visual1U16);
                        CheckWhatYouCan(bidiL2V, src, visual1);
                        bidiL2V.SetPara(destU16, (sbyte)(level ^ 1), null);
                        visual2U16 = bidiL2V.WriteReordered(IBM.ICU.Text.Bidi.DO_MIRRORING);
                        visual2    = IBM.ICU.Charset.BidiTest.U16ToPseudo(visual2U16);
                        CheckWhatYouCan(bidiL2V, dest, visual2);
                        AssertEquals("Round trip failed for case " + i, visual1,
                                     visual2, src, "REORDER_RUNS_ONLY (2)",
                                     (option == 0) ? "0" : "OPTION_INSERT_MARKS",
                                     level.ToString());
                    }
                }
            }

            /* test with null or empty text */
            int paras;

            bidi.SetPara((String)null, IBM.ICU.Text.Bidi.LTR, null);
            paras = bidi.CountParagraphs();
            AssertEquals("\nInvalid number of paras #1 (should be 0): ", 0, paras);
            bidi.SetPara((char[])null, IBM.ICU.Text.Bidi.LTR, null);
            paras = bidi.CountParagraphs();
            AssertEquals("\nInvalid number of paras #2 (should be 0): ", 0, paras);
            bidi.SetPara("", IBM.ICU.Text.Bidi.LTR, null);
            paras = bidi.CountParagraphs();
            AssertEquals("\nInvalid number of paras #3 (should be 0): ", 0, paras);
            bidi.SetPara(new char[0], IBM.ICU.Text.Bidi.LTR, null);
            paras = bidi.CountParagraphs();
            AssertEquals("\nInvalid number of paras #4 (should be 0): ", 0, paras);

            Logln("\nExiting TestReorderRunsOnly\n");
        }
Пример #2
0
        public void TestMultipleParagraphs2()
        {
            sbyte gotLevel;

            sbyte[] gotLevels;
            bool    orderParagraphsLTR;
            String  src;
            Bidi    bidi = new Bidi();
            Bidi    bidiLine;
            int     count, paraStart, paraLimit, paraIndex, length;
            int     i, j, k;

            Logln("\nEntering TestMultipleParagraphs\n");
            try {
                bidi.SetPara(text, IBM.ICU.Text.Bidi.LTR, null);
            } catch (ArgumentException e) {
                Errln("1st Bidi.setPara failed, paraLevel = " + IBM.ICU.Text.Bidi.LTR);
            }

            /* check paragraph count and boundaries */
            if (paraCount != (count = bidi.CountParagraphs()))
            {
                Errln("1st Bidi.countParagraphs returned " + count + ", should be "
                      + paraCount);
            }
            BidiRun run;

            for (i = 0; i < paraCount; i++)
            {
                run       = bidi.GetParagraphByIndex(i);
                paraStart = run.GetStart();
                paraLimit = run.GetLimit();
                if ((paraStart != paraBounds[i]) ||
                    (paraLimit != paraBounds[i + 1]))
                {
                    Errln("Found boundaries of paragraph " + i + ": " + paraStart
                          + "-" + paraLimit + "; expected: " + paraBounds[i]
                          + "-" + paraBounds[i + 1]);
                }
            }

            /* check with last paragraph not terminated by B */
            char[] chars = text.ToCharArray();
            chars[chars.Length - 1] = 'L';
            src = ILOG.J2CsMapping.Util.StringUtil.NewString(chars);
            try {
                bidi.SetPara(src, IBM.ICU.Text.Bidi.LTR, null);
            } catch (ArgumentException e_0) {
                Errln("2nd Bidi.setPara failed, paraLevel = " + IBM.ICU.Text.Bidi.LTR);
            }
            if (paraCount != (count = bidi.CountParagraphs()))
            {
                Errln("2nd Bidi.countParagraphs returned " + count + ", should be "
                      + paraCount);
            }
            i         = paraCount - 1;
            run       = bidi.GetParagraphByIndex(i);
            paraStart = run.GetStart();
            paraLimit = run.GetLimit();
            if ((paraStart != paraBounds[i]) || (paraLimit != paraBounds[i + 1]))
            {
                Errln("2nd Found boundaries of paragraph " + i + ": " + paraStart
                      + "-" + paraLimit + "; expected: " + paraBounds[i] + "-"
                      + paraBounds[i + 1]);
            }

            /* check paraLevel for all paragraphs under various paraLevel specs */
            for (k = 0; k < 6; k++)
            {
                try {
                    bidi.SetPara(src, paraLevels[k], null);
                } catch (ArgumentException e_1) {
                    Errln("3nd Bidi.setPara failed, paraLevel = " + paraLevels[k]);
                }
                for (i = 0; i < paraCount; i++)
                {
                    paraIndex = bidi.GetParagraphIndex(paraBounds[i]);
                    run       = bidi.GetParagraph(paraBounds[i]);
                    if (paraIndex != i)
                    {
                        Errln("#1 For paraLevel = " + paraLevels[k]
                              + " paragraph = " + i + ", found paragraph"
                              + " index = " + paraIndex + " expected = " + i);
                    }
                    gotLevel = run.GetEmbeddingLevel();
                    if (gotLevel != multiLevels[k][i])
                    {
                        Errln("#2 For paraLevel = " + paraLevels[k]
                              + " paragraph = " + i + ", found level = "
                              + gotLevel + ", expected = " + multiLevels[k][i]);
                    }
                }
                gotLevel = bidi.GetParaLevel();
                if (gotLevel != multiLevels[k][0])
                {
                    Errln("#3 For paraLevel = " + paraLevels[k]
                          + " getParaLevel = " + gotLevel + ", expected "
                          + multiLevels[k][0]);
                }
            }

            /*
             * check that the result of Bidi.getParaLevel changes if the first
             * paragraph has a different level
             */
            chars[0] = '\u05d2';     /* Hebrew letter Gimel */
            src      = ILOG.J2CsMapping.Util.StringUtil.NewString(chars);
            try {
                bidi.SetPara(src, IBM.ICU.Text.Bidi.LEVEL_DEFAULT_LTR, null);
            } catch (ArgumentException e_2) {
                Errln("Bidi.setPara failed, paraLevel = " + IBM.ICU.Text.Bidi.LEVEL_DEFAULT_LTR);
            }
            gotLevel = bidi.GetParaLevel();
            if (gotLevel != IBM.ICU.Text.Bidi.RTL)
            {
                Errln("#4 For paraLevel = Bidi.LEVEL_DEFAULT_LTR getParaLevel = "
                      + gotLevel + ", expected = " + IBM.ICU.Text.Bidi.RTL);
            }

            /* check that line cannot overlap paragraph boundaries */
            bidiLine = new Bidi();
            i        = paraBounds[1];
            k        = paraBounds[2] + 1;
            try {
                bidiLine = bidi.SetLine(i, k);
                Errln("For line limits " + i + "-" + k
                      + " got success, while expected failure");
            } catch (Exception e_3) {
            }

            i = paraBounds[1];
            k = paraBounds[2];
            try {
                bidiLine = bidi.SetLine(i, k);
            } catch (Exception e_4) {
                Errln("For line limits " + i + "-" + k + " got failure");
            }

            /*
             * check level of block separator at end of paragraph when
             * orderParagraphsLTR==FALSE
             */
            try {
                bidi.SetPara(src, IBM.ICU.Text.Bidi.RTL, null);
            } catch (ArgumentException e_5) {
                Errln("Bidi.setPara failed, paraLevel = " + IBM.ICU.Text.Bidi.RTL);
            }
            /* get levels through para Bidi block */
            try {
                gotLevels = bidi.GetLevels();
            } catch (Exception e_6) {
                Errln("Error on Bidi.getLevels");
                gotLevels = new sbyte[bidi.GetLength()];
                ILOG.J2CsMapping.Collections.Arrays.Fill(gotLevels, (sbyte)-1);
            }
            for (i = 26; i < 32; i++)
            {
                if (gotLevels[i] != IBM.ICU.Text.Bidi.RTL)
                {
                    Errln("For char " + i + "(0x" + IBM.ICU.Impl.Utility.Hex(chars[i])
                          + "), level = " + gotLevels[i] + ", expected = "
                          + IBM.ICU.Text.Bidi.RTL);
                }
            }
            /* get levels through para Line block */
            i = paraBounds[1];
            k = paraBounds[2];
            try {
                bidiLine = bidi.SetLine(i, k);
            } catch (Exception e_7) {
                Errln("For line limits " + i + "-" + k + " got failure");
                return;
            }
            paraIndex = bidiLine.GetParagraphIndex(i);
            run       = bidiLine.GetParagraph(i);
            try {
                gotLevels = bidiLine.GetLevels();
            } catch (Exception e_8) {
                Errln("Error on bidiLine.getLevels");
                gotLevels = new sbyte[bidiLine.GetLength()];
                ILOG.J2CsMapping.Collections.Arrays.Fill(gotLevels, (sbyte)-1);
            }
            length   = bidiLine.GetLength();
            gotLevel = run.GetEmbeddingLevel();
            if ((gotLevel != IBM.ICU.Text.Bidi.RTL) || (gotLevels[length - 1] != IBM.ICU.Text.Bidi.RTL))
            {
                Errln("For paragraph " + paraIndex + " with limits "
                      + run.GetStart() + "-" + run.GetLimit() + ", paraLevel = "
                      + gotLevel + "expected = " + IBM.ICU.Text.Bidi.RTL
                      + ", level of separator = " + gotLevels[length - 1]
                      + " expected = " + IBM.ICU.Text.Bidi.RTL);
            }
            orderParagraphsLTR = bidi.IsOrderParagraphsLTR();
            AssertFalse("orderParagraphsLTR is true", orderParagraphsLTR);
            bidi.OrderParagraphsLTR(true);
            orderParagraphsLTR = bidi.IsOrderParagraphsLTR();
            AssertTrue("orderParagraphsLTR is false", orderParagraphsLTR);

            /*
             * check level of block separator at end of paragraph when
             * orderParagraphsLTR==TRUE
             */
            try {
                bidi.SetPara(src, IBM.ICU.Text.Bidi.RTL, null);
            } catch (ArgumentException e_9) {
                Errln("Bidi.setPara failed, paraLevel = " + IBM.ICU.Text.Bidi.RTL);
            }
            /* get levels through para Bidi block */
            try {
                gotLevels = bidi.GetLevels();
            } catch (Exception e_10) {
                Errln("Error on Bidi.getLevels");
                gotLevels = new sbyte[bidi.GetLength()];
                ILOG.J2CsMapping.Collections.Arrays.Fill(gotLevels, (sbyte)-1);
            }
            for (i = 26; i < 32; i++)
            {
                if (gotLevels[i] != 0)
                {
                    Errln("For char " + i + "(0x" + IBM.ICU.Impl.Utility.Hex(chars[i])
                          + "), level = " + gotLevels[i] + ", expected = 0");
                }
            }
            /* get levels through para Line block */
            i         = paraBounds[1];
            k         = paraBounds[2];
            paraStart = run.GetStart();
            paraLimit = run.GetLimit();
            try {
                bidiLine = bidi.SetLine(paraStart, paraLimit);
            } catch (Exception e_11) {
                Errln("For line limits " + paraStart + "-" + paraLimit
                      + " got failure");
            }
            paraIndex = bidiLine.GetParagraphIndex(i);
            run       = bidiLine.GetParagraph(i);
            try {
                gotLevels = bidiLine.GetLevels();
            } catch (Exception e_12) {
                Errln("Error on bidiLine.getLevels");
                gotLevels = new sbyte[bidiLine.GetLength()];
                ILOG.J2CsMapping.Collections.Arrays.Fill(gotLevels, (sbyte)-1);
            }
            length   = bidiLine.GetLength();
            gotLevel = run.GetEmbeddingLevel();
            if ((gotLevel != IBM.ICU.Text.Bidi.RTL) || (gotLevels[length - 1] != 0))
            {
                Err("\nFor paragraph " + paraIndex + " with limits "
                    + run.GetStart() + "-" + run.GetLimit() + ", paraLevel = "
                    + gotLevel + "expected = " + IBM.ICU.Text.Bidi.RTL
                    + ", level of separator = " + gotLevels[length - 1]
                    + " expected = 0\nlevels = ");
                for (count = 0; count < length; count++)
                {
                    Errcont(gotLevels[count] + "  ");
                }
                Errcont("\n");
            }

            /*
             * test that the concatenation of separate invocations of the bidi code
             * on each individual paragraph in order matches the levels array that
             * results from invoking bidi once over the entire multiparagraph tests
             * (with orderParagraphsLTR false, of course)
             */
            src = text;     /* restore original content */
            bidi.OrderParagraphsLTR(false);
            try {
                bidi.SetPara(src, IBM.ICU.Text.Bidi.LEVEL_DEFAULT_RTL, null);
            } catch (ArgumentException e_13) {
                Errln("Bidi.setPara failed, paraLevel = " + IBM.ICU.Text.Bidi.LEVEL_DEFAULT_RTL);
            }
            try {
                gotLevels = bidi.GetLevels();
            } catch (Exception e_14) {
                Errln("Error on bidiLine.getLevels");
                gotLevels = new sbyte[bidi.GetLength()];
                ILOG.J2CsMapping.Collections.Arrays.Fill(gotLevels, (sbyte)-1);
            }
            for (i = 0; i < paraCount; i++)
            {
                /* use pLine for individual paragraphs */
                paraStart = paraBounds[i];
                length    = paraBounds[i + 1] - paraStart;
                try {
                    bidiLine.SetPara(src.Substring(paraStart, (paraStart + length) - (paraStart)),
                                     IBM.ICU.Text.Bidi.LEVEL_DEFAULT_RTL, null);
                } catch (ArgumentException e_15) {
                    Errln("Bidi.setPara failed, paraLevel = "
                          + IBM.ICU.Text.Bidi.LEVEL_DEFAULT_RTL);
                }
                for (j = 0; j < length; j++)
                {
                    if ((k = bidiLine.GetLevelAt(j)) != (gotLevel = gotLevels[paraStart
                                                                              + j]))
                    {
                        Errln("Checking paragraph concatenation: for paragraph["
                              + i + "], char[" + j + "] = 0x"
                              + IBM.ICU.Impl.Utility.Hex(src[paraStart + j])
                              + ", level = " + k + ", expected = " + gotLevel);
                    }
                }
            }

            /*
             * ensure that leading numerics in a paragraph are not treated as arabic
             * numerals because of arabic text in a preceding paragraph
             */
            src = text2;
            bidi.OrderParagraphsLTR(true);
            try {
                bidi.SetPara(src, IBM.ICU.Text.Bidi.RTL, null);
            } catch (ArgumentException e_16) {
                Errln("Bidi.setPara failed, paraLevel = " + IBM.ICU.Text.Bidi.RTL);
            }
            try {
                gotLevels = bidi.GetLevels();
            } catch (Exception e_17) {
                Errln("Error on Bidi.getLevels");
                gotLevels = new sbyte[bidi.GetLength()];
                ILOG.J2CsMapping.Collections.Arrays.Fill(gotLevels, (sbyte)-1);
            }
            for (i = 0, length = src.Length; i < length; i++)
            {
                if (gotLevels[i] != levels2[i])
                {
                    Errln("Checking leading numerics: for char " + i + "(0x"
                          + IBM.ICU.Impl.Utility.Hex(src[i]) + "), level = "
                          + gotLevels[i] + ", expected = " + levels2[i]);
                }
            }

            /*
             * check handling of whitespace before end of paragraph separator when
             * orderParagraphsLTR==TRUE, when last paragraph has, and lacks, a
             * terminating B
             */
            chars = src.ToCharArray();
            ILOG.J2CsMapping.Collections.Arrays.Fill(chars, '\u0020');
            bidi.OrderParagraphsLTR(true);
            for (i = 0x001c; i <= 0x0020; i += (0x0020 - 0x001c))
            {
                chars[4] = (char)i;      /* with and without terminating B */
                for (j = 0x0041; j <= 0x05d0; j += (0x05d0 - 0x0041))
                {
                    chars[0] = (char)j;      /* leading 'A' or Alef */
                    src      = ILOG.J2CsMapping.Util.StringUtil.NewString(chars);
                    for (gotLevel = 4; gotLevel <= 5; gotLevel++)
                    {
                        /* test even and odd paraLevel */
                        try {
                            bidi.SetPara(src, gotLevel, null);
                        } catch (ArgumentException e_18) {
                            Errln("Bidi.setPara failed, paraLevel = " + gotLevel);
                        }
                        try {
                            gotLevels = bidi.GetLevels();
                        } catch (Exception e_19) {
                            Errln("Error on Bidi.getLevels");
                            gotLevels = new sbyte[bidi.GetLength()];
                            ILOG.J2CsMapping.Collections.Arrays.Fill(gotLevels, (sbyte)-1);
                        }
                        for (k = 1; k <= 3; k++)
                        {
                            if (gotLevels[k] != gotLevel)
                            {
                                Errln("Checking trailing spaces for leading char 0x"
                                      + IBM.ICU.Impl.Utility.Hex(chars[0])
                                      + ", last_char = "
                                      + IBM.ICU.Impl.Utility.Hex(chars[4])
                                      + ", index = "
                                      + k
                                      + "level = "
                                      + gotLevels[k]
                                      + ", expected = " + gotLevel);
                            }
                        }
                    }
                }
            }

            /*
             * check default orientation when inverse bidi and paragraph starts with
             * LTR strong char and ends with RTL strong char, with and without a
             * terminating B
             */
            bidi.SetReorderingMode(IBM.ICU.Text.Bidi.REORDER_INVERSE_LIKE_DIRECT);
            bidi.SetPara("abc \u05d2\u05d1\n", IBM.ICU.Text.Bidi.LEVEL_DEFAULT_LTR, null);
            String xout = bidi.WriteReordered(0);

            AssertEquals("\nInvalid output", "\u05d1\u05d2 abc\n", xout);
            bidi.SetPara("abc \u05d2\u05d1", IBM.ICU.Text.Bidi.LEVEL_DEFAULT_LTR, null);
            xout = bidi.WriteReordered(0);
            AssertEquals("\nInvalid output #1", "\u05d1\u05d2 abc", xout);

            /*
             * check multiple paragraphs together with explicit levels
             */
            bidi.SetReorderingMode(IBM.ICU.Text.Bidi.REORDER_DEFAULT);
            gotLevels = new sbyte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            bidi.SetPara("ab\u05d1\u05d2\n\u05d3\u05d4123", IBM.ICU.Text.Bidi.LTR, gotLevels);
            xout = bidi.WriteReordered(0);
            AssertEquals("\nInvalid output #2", "ab\u05d2\u05d1\n123\u05d4\u05d3",
                         xout);
            AssertEquals("\nInvalid number of paras", 2, bidi.CountParagraphs());

            Logln("\nExiting TestMultipleParagraphs\n");
        }