internal void PrintCaseInfo(Bidi bidi, String src, String dst) { int length = bidi.GetProcessedLength(); sbyte[] levels = bidi.GetLevels(); char[] levelChars = new char[length]; sbyte lev; int runCount = bidi.CountRuns(); Errcontln("========================================"); Errcontln("Processed length: " + length); for (int i = 0; i < length; i++) { lev = levels[i]; if (lev < 0) { levelChars[i] = '-'; } else if (lev < columns.Length) { levelChars[i] = columns[lev]; } else { levelChars[i] = '+'; } } Errcontln("Levels: " + ILOG.J2CsMapping.Util.StringUtil.NewString(levelChars)); Errcontln("Source: " + src); Errcontln("Result: " + dst); Errcontln("Direction: " + bidi.GetDirection()); Errcontln("paraLevel: " + bidi.GetParaLevel().ToString()); Errcontln("reorderingMode: " + ModeToString(bidi.GetReorderingMode())); Errcontln("reorderingOptions: " + SpOptionsToString(bidi.GetReorderingOptions())); Errcont("Runs: " + runCount + " => logicalStart.length/level: "); for (int i_0 = 0; i_0 < runCount; i_0++) { BidiRun run; run = bidi.GetVisualRun(i_0); Errcont(" " + run.GetStart() + "." + run.GetLength() + "/" + run.GetEmbeddingLevel()); } Errcont("\n"); }
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"); }
private void DoMisc() { /* Miscellaneous tests to exercize less popular code paths */ Bidi bidi = new Bidi(120, 66), bidiLine; AssertEquals("\nwriteReverse should return an empty string", "", IBM.ICU.Text.Bidi.WriteReverse("", 0)); bidi.SetPara("", IBM.ICU.Text.Bidi.LTR, null); AssertEquals("\nwriteReordered should return an empty string", "", bidi.WriteReordered(0)); bidi.SetPara("abc", IBM.ICU.Text.Bidi.LTR, null); AssertEquals("\ngetRunStart should return 0", 0, bidi.GetRunStart(0)); AssertEquals("\ngetRunLimit should return 3", 3, bidi.GetRunLimit(0)); bidi.SetPara("abc ", IBM.ICU.Text.Bidi.RTL, null); bidiLine = bidi.SetLine(0, 6); for (int i = 3; i < 6; i++) { AssertEquals("\nTrailing space at " + i + " should get paragraph level", IBM.ICU.Text.Bidi.RTL, bidiLine.GetLevelAt(i)); } bidi.SetPara("abc def", IBM.ICU.Text.Bidi.RTL, null); bidiLine = bidi.SetLine(0, 6); for (int i_0 = 3; i_0 < 6; i_0++) { AssertEquals("\nTrailing space at " + i_0 + " should get paragraph level", IBM.ICU.Text.Bidi.RTL, bidiLine.GetLevelAt(i_0)); } bidi.SetPara("abcdefghi ", IBM.ICU.Text.Bidi.RTL, null); bidiLine = bidi.SetLine(0, 6); for (int i_1 = 3; i_1 < 6; i_1++) { AssertEquals("\nTrailing char at " + i_1 + " should get level 2", 2, bidiLine.GetLevelAt(i_1)); } bidi.SetReorderingOptions(IBM.ICU.Text.Bidi.OPTION_REMOVE_CONTROLS); bidi.SetPara("\u200eabc def", IBM.ICU.Text.Bidi.RTL, null); bidiLine = bidi.SetLine(0, 6); AssertEquals("\nWrong result length", 5, bidiLine.GetResultLength()); bidi.SetPara("abcdefghi", IBM.ICU.Text.Bidi.LTR, null); bidiLine = bidi.SetLine(0, 6); AssertEquals("\nWrong direction #1", IBM.ICU.Text.Bidi.LTR, bidiLine.GetDirection()); bidi.SetPara("", IBM.ICU.Text.Bidi.LTR, null); sbyte[] levels = bidi.GetLevels(); AssertEquals("\nWrong number of level elements", 0, levels.Length); AssertEquals("\nWrong number of runs #1", 0, bidi.CountRuns()); bidi.SetPara(" ", IBM.ICU.Text.Bidi.RTL, null); bidiLine = bidi.SetLine(0, 6); AssertEquals("\nWrong number of runs #2", 1, bidiLine.CountRuns()); bidi.SetPara("a\u05d0 bc", IBM.ICU.Text.Bidi.RTL, null); bidiLine = bidi.SetLine(0, 6); AssertEquals("\nWrong direction #2", IBM.ICU.Text.Bidi.MIXED, bidi.GetDirection()); AssertEquals("\nWrong direction #3", IBM.ICU.Text.Bidi.MIXED, bidiLine.GetDirection()); AssertEquals("\nWrong number of runs #3", 2, bidiLine.CountRuns()); int[] map = IBM.ICU.Text.Bidi.ReorderLogical(null); AssertTrue("\nWe should have got a null map #1", map == null); map = IBM.ICU.Text.Bidi.ReorderLogical(new sbyte[] { 0, 99, 99 }); AssertTrue("\nWe should have got a null map #2", map == null); map = IBM.ICU.Text.Bidi.ReorderVisual(null); AssertTrue("\nWe should have got a null map #3", map == null); map = IBM.ICU.Text.Bidi.InvertMap(null); AssertTrue("\nWe should have got a null map #4", map == null); map = IBM.ICU.Text.Bidi.InvertMap(new int[] { 0, 1, -1, 5, 4 }); AssertTrue("\nUnexpected inverted Map", ILOG.J2CsMapping.Collections.Arrays.Equals(map, new int[] { 0, 1, -1, -1, 4, 3 })); bidi.SetPara("", IBM.ICU.Text.Bidi.LTR, null); map = bidi.GetLogicalMap(); AssertTrue("\nMap should have length==0 #1", map.Length == 0); map = bidi.GetVisualMap(); AssertTrue("\nMap should have length==0 #2", map.Length == 0); /* test BidiRun.toString and allocation of run memory > 1 */ bidi.SetPara("abc", IBM.ICU.Text.Bidi.LTR, null); AssertEquals("\nWrong run display", "BidiRun 0 - 3 @ 0", bidi .GetLogicalRun(0).ToString()); /* test REMOVE_BIDI_CONTROLS together with DO_MIRRORING */ bidi.SetPara("abc\u200e", IBM.ICU.Text.Bidi.LTR, null); String xout = bidi.WriteReordered(IBM.ICU.Text.Bidi.REMOVE_BIDI_CONTROLS | IBM.ICU.Text.Bidi.DO_MIRRORING); AssertEquals("\nWrong result #1", "abc", xout); /* test inverse Bidi with marks and contextual orientation */ bidi.SetReorderingMode(IBM.ICU.Text.Bidi.REORDER_INVERSE_LIKE_DIRECT); bidi.SetReorderingOptions(IBM.ICU.Text.Bidi.OPTION_INSERT_MARKS); bidi.SetPara("", IBM.ICU.Text.Bidi.LEVEL_DEFAULT_RTL, null); xout = bidi.WriteReordered(0); AssertEquals("\nWrong result #2", "", xout); bidi.SetPara(" ", IBM.ICU.Text.Bidi.LEVEL_DEFAULT_RTL, null); xout = bidi.WriteReordered(0); AssertEquals("\nWrong result #3", " ", xout); bidi.SetPara("abc", IBM.ICU.Text.Bidi.LEVEL_DEFAULT_RTL, null); xout = bidi.WriteReordered(0); AssertEquals("\nWrong result #4", "abc", xout); bidi.SetPara("\u05d0\u05d1", IBM.ICU.Text.Bidi.LEVEL_DEFAULT_RTL, null); xout = bidi.WriteReordered(0); AssertEquals("\nWrong result #5", "\u05d1\u05d0", xout); bidi.SetPara("abc \u05d0\u05d1", IBM.ICU.Text.Bidi.LEVEL_DEFAULT_RTL, null); xout = bidi.WriteReordered(0); AssertEquals("\nWrong result #6", "\u05d1\u05d0 abc", xout); bidi.SetPara("\u05d0\u05d1 abc", IBM.ICU.Text.Bidi.LEVEL_DEFAULT_RTL, null); xout = bidi.WriteReordered(0); AssertEquals("\nWrong result #7", "\u200fabc \u05d1\u05d0", xout); bidi.SetPara("\u05d0\u05d1 abc .-=", IBM.ICU.Text.Bidi.LEVEL_DEFAULT_RTL, null); xout = bidi.WriteReordered(0); AssertEquals("\nWrong result #8", "\u200f=-. abc \u05d1\u05d0", xout); bidi.OrderParagraphsLTR(true); bidi.SetPara("\n\r \n\rabc\n\u05d0\u05d1\rabc \u05d2\u05d3\n\r" + "\u05d4\u05d5 abc\n\u05d6\u05d7 abc .-=\r\n" + "-* \u05d8\u05d9 abc .-=", IBM.ICU.Text.Bidi.LEVEL_DEFAULT_RTL, null); xout = bidi.WriteReordered(0); AssertEquals( "\nWrong result #9", "\n\r \n\rabc\n\u05d1\u05d0\r\u05d3\u05d2 abc\n\r" + "\u200fabc \u05d5\u05d4\n\u200f=-. abc \u05d7\u05d6\r\n" + "\u200f=-. abc \u05d9\u05d8 *-", xout); bidi.SetPara("\u05d0 \t", IBM.ICU.Text.Bidi.LTR, null); xout = bidi.WriteReordered(0); AssertEquals("\nWrong result #10", "\u05D0\u200e \t", xout); bidi.SetPara("\u05d0 123 \t\u05d1 123 \u05d2", IBM.ICU.Text.Bidi.LTR, null); xout = bidi.WriteReordered(0); AssertEquals("\nWrong result #11", "\u05d0 \u200e123\u200e \t\u05d2 123 \u05d1", xout); bidi.SetPara("\u05d0 123 \u0660\u0661 ab", IBM.ICU.Text.Bidi.LTR, null); xout = bidi.WriteReordered(0); AssertEquals("\nWrong result #12", "\u05d0 \u200e123 \u200e\u0660\u0661 ab", xout); bidi.SetPara("ab \t", IBM.ICU.Text.Bidi.RTL, null); xout = bidi.WriteReordered(0); AssertEquals("\nWrong result #13", "\u200f\t ab", xout); /* check exceeding para level */ bidi = new Bidi(); bidi.SetPara("A\u202a\u05d0\u202aC\u202c\u05d1\u202cE", (sbyte)(IBM.ICU.Text.Bidi.MAX_EXPLICIT_LEVEL - 1), null); AssertEquals("\nWrong level at index 2", 61, bidi.GetLevelAt(2)); /* check 1-char runs with RUNS_ONLY */ bidi.SetReorderingMode(IBM.ICU.Text.Bidi.REORDER_RUNS_ONLY); bidi.SetPara("a \u05d0 b \u05d1 c \u05d2 d ", IBM.ICU.Text.Bidi.LTR, null); AssertEquals("\nWrong number of runs #4", 14, bidi.CountRuns()); }
private void _testReordering(Bidi bidi, int testNumber) { int[] logicalMap1; int[] logicalMap2; int[] logicalMap3; int[] visualMap1; int[] visualMap2; int[] visualMap3; int[] visualMap4 = new int[MAXLEN]; sbyte[] levels; int i, length = bidi.GetLength(), destLength = bidi.GetResultLength(); int runCount, visualIndex, logicalIndex = -1, logicalStart, runLength; bool odd; if (length <= 0) { return; } /* get the logical and visual maps from the object */ logicalMap1 = bidi.GetLogicalMap(); if (logicalMap1 == null) { Errln("getLogicalMap in test " + testNumber + " is null"); logicalMap1 = new int[0]; } visualMap1 = bidi.GetVisualMap(); if (visualMap1 == null) { Errln("getVisualMap() in test " + testNumber + " is null"); visualMap1 = new int[0]; } /* invert them both */ visualMap2 = IBM.ICU.Text.Bidi.InvertMap(logicalMap1); logicalMap2 = IBM.ICU.Text.Bidi.InvertMap(visualMap1); /* get them from the levels array, too */ levels = bidi.GetLevels(); if (levels == null || levels.Length != length) { Errln("getLevels() in test " + testNumber + " failed"); } logicalMap3 = IBM.ICU.Text.Bidi.ReorderLogical(levels); visualMap3 = IBM.ICU.Text.Bidi.ReorderVisual(levels); /* get the visual map from the runs, too */ try { runCount = bidi.CountRuns(); } catch (InvalidOperationException e) { Errln("countRuns() in test " + testNumber + " failed"); runCount = 0; } Logln("\n---- " + runCount + " runs"); visualIndex = 0; BidiRun run; for (i = 0; i < runCount; ++i) { run = bidi.GetVisualRun(i); if (run == null) { Errln("null visual run encountered at index " + i + ", in test " + testNumber); continue; } odd = run.IsOddRun(); logicalStart = run.GetStart(); runLength = run.GetLength(); Log("(" + ((run.IsOddRun()) ? "R" : "L")); Log(" @" + run.GetStart() + '[' + run.GetLength() + "])\n"); if (!odd) { do /* LTR */ { visualMap4[visualIndex++] = logicalStart++; } while (--runLength > 0); } else { logicalStart += runLength; /* logicalLimit */ do /* RTL */ { visualMap4[visualIndex++] = --logicalStart; } while (--runLength > 0); } } Log("\n"); /* print all the maps */ Logln("logical maps:"); for (i = 0; i < length; ++i) { Log(logicalMap1[i] + " "); } Log("\n"); for (i = 0; i < length; ++i) { Log(logicalMap2[i] + " "); } Log("\n"); for (i = 0; i < length; ++i) { Log(logicalMap3[i] + " "); } Log("\nvisual maps:\n"); for (i = 0; i < destLength; ++i) { Log(visualMap1[i] + " "); } Log("\n"); for (i = 0; i < destLength; ++i) { Log(visualMap2[i] + " "); } Log("\n"); for (i = 0; i < length; ++i) { Log(visualMap3[i] + " "); } Log("\n"); for (i = 0; i < length; ++i) { Log(visualMap4[i] + " "); } Log("\n"); /* * check that the indexes are the same between these and * Bidi.getLogical/VisualIndex() */ for (i = 0; i < length; ++i) { if (logicalMap1[i] != logicalMap2[i]) { Errln("Error in tests[" + testNumber + "]: (logicalMap1[" + i + "] == " + logicalMap1[i] + ") != (logicalMap2[" + i + "] == " + logicalMap2[i] + ")"); } if (logicalMap1[i] != logicalMap3[i]) { Errln("Error in tests[" + testNumber + "]: (logicalMap1[" + i + "] == " + logicalMap1[i] + ") != (logicalMap3[" + i + "] == " + logicalMap3[i] + ")"); } if (visualMap1[i] != visualMap2[i]) { Errln("Error in tests[" + testNumber + "]: (visualMap1[" + i + "] == " + visualMap1[i] + ") != (visualMap2[" + i + "] == " + visualMap2[i] + ")"); } if (visualMap1[i] != visualMap3[i]) { Errln("Error in tests[" + testNumber + "]: (visualMap1[" + i + "] == " + visualMap1[i] + ") != (visualMap3[" + i + "] == " + visualMap3[i] + ")"); } if (visualMap1[i] != visualMap4[i]) { Errln("Error in tests[" + testNumber + "]: (visualMap1[" + i + "] == " + visualMap1[i] + ") != (visualMap4[" + i + "] == " + visualMap4[i] + ")"); } try { visualIndex = bidi.GetVisualIndex(i); } catch (Exception e_0) { Errln("Bidi.getVisualIndex(" + i + ") failed in tests[" + testNumber + "]"); } if (logicalMap1[i] != visualIndex) { Errln("Error in tests[" + testNumber + "]: (logicalMap1[" + i + "] == " + logicalMap1[i] + ") != (Bidi.getVisualIndex(" + i + ") == " + visualIndex + ")"); } try { logicalIndex = bidi.GetLogicalIndex(i); } catch (Exception e_1) { Errln("Bidi.getLogicalIndex(" + i + ") failed in tests[" + testNumber + "]"); } if (visualMap1[i] != logicalIndex) { Errln("Error in tests[" + testNumber + "]: (visualMap1[" + i + "] == " + visualMap1[i] + ") != (Bidi.getLogicalIndex(" + i + ") == " + logicalIndex + ")"); } } }
private void _testInverseBidi(Bidi bidi, String src, int direction) { String visualLTR, logicalDest, visualDest; try { if (direction == IBM.ICU.Text.Bidi.DIRECTION_LEFT_TO_RIGHT) { Log("inverse Bidi: testInverse(L)\n"); /* convert visual to logical */ bidi.SetInverse(true); if (!bidi.IsInverse()) { Err("Error while doing setInverse(true)\n"); } bidi.SetPara(src, IBM.ICU.Text.Bidi.LTR, null); if (!ILOG.J2CsMapping.Collections.Arrays.Equals(src.ToCharArray(), bidi.GetText())) { Err("Wrong value returned by getText\n"); } if (!src.Equals(bidi.GetTextAsString())) { Err("Wrong value returned by getTextAsString\n"); } logicalDest = bidi.WriteReordered(IBM.ICU.Text.Bidi.DO_MIRRORING | IBM.ICU.Text.Bidi.INSERT_LRM_FOR_NUMERIC); Log(" v "); PrintUnicode(src.ToCharArray(), bidi.GetLevels()); Log("\n"); /* convert back to visual LTR */ bidi.SetInverse(false); if (bidi.IsInverse()) { Err("Error while doing setInverse(false)\n"); } bidi.SetPara(logicalDest, IBM.ICU.Text.Bidi.LTR, null); visualDest = bidi.WriteReordered(IBM.ICU.Text.Bidi.DO_MIRRORING | IBM.ICU.Text.Bidi.REMOVE_BIDI_CONTROLS); } else { Logln("inverse Bidi: testInverse(R)\n"); /* reverse visual from RTL to LTR */ visualLTR = IBM.ICU.Text.Bidi.WriteReverse(src, 0); Log(" vr"); PrintUnicode(src.ToCharArray(), null); Log("\n"); /* convert visual RTL to logical */ bidi.SetInverse(true); bidi.SetPara(visualLTR, IBM.ICU.Text.Bidi.LTR, null); logicalDest = bidi.WriteReordered(IBM.ICU.Text.Bidi.DO_MIRRORING | IBM.ICU.Text.Bidi.INSERT_LRM_FOR_NUMERIC); Log(" vl"); PrintUnicode(visualLTR.ToCharArray(), bidi.GetLevels()); Log("\n"); /* convert back to visual RTL */ bidi.SetInverse(false); bidi.SetPara(logicalDest, IBM.ICU.Text.Bidi.LTR, null); visualDest = bidi.WriteReordered(IBM.ICU.Text.Bidi.DO_MIRRORING | IBM.ICU.Text.Bidi.REMOVE_BIDI_CONTROLS | IBM.ICU.Text.Bidi.OUTPUT_REVERSE); } Log(" l "); PrintUnicode(logicalDest.ToCharArray(), bidi.GetLevels()); Log("\n"); Log(" v "); PrintUnicode(visualDest.ToCharArray(), null); Log("\n"); } catch (Exception e) { Errln("inverse Bidi: *** failed"); visualDest = null; } /* check and print results */ if (src.Equals(visualDest)) { ++countRoundtrips; Log(" + roundtripped\n"); } else { ++countNonRoundtrips; Log(" * did not roundtrip\n"); } }