LEFT | RIGHT |
(no file at all) | |
1 // © 2017 and later: Unicode, Inc. and others. | 1 // © 2017 and later: Unicode, Inc. and others. |
2 // License & terms of use: http://www.unicode.org/copyright.html#License | 2 // License & terms of use: http://www.unicode.org/copyright.html#License |
3 package com.ibm.icu.dev.test.number; | 3 package com.ibm.icu.dev.test.number; |
4 | 4 |
5 import static org.junit.Assert.assertEquals; | 5 import static org.junit.Assert.assertEquals; |
6 import static org.junit.Assert.assertTrue; | 6 import static org.junit.Assert.assertTrue; |
7 | 7 |
8 import org.junit.Test; | 8 import org.junit.Test; |
9 | 9 |
10 import com.ibm.icu.impl.SimpleFormatterImpl; | 10 import com.ibm.icu.impl.SimpleFormatterImpl; |
(...skipping 28 matching lines...) Expand all Loading... |
39 Modifier mod2 = new ConstantMultiFieldModifier(prefix, suffix, true); | 39 Modifier mod2 = new ConstantMultiFieldModifier(prefix, suffix, true); |
40 assertModifierEquals(mod2, 3, true, "a📻|b", "%%%n$"); | 40 assertModifierEquals(mod2, 3, true, "a📻|b", "%%%n$"); |
41 | 41 |
42 // Make sure the first modifier is still the same (that it stayed consta
nt) | 42 // Make sure the first modifier is still the same (that it stayed consta
nt) |
43 assertModifierEquals(mod1, 0, true, "|", "n"); | 43 assertModifierEquals(mod1, 0, true, "|", "n"); |
44 } | 44 } |
45 | 45 |
46 @Test | 46 @Test |
47 public void testSimpleModifier() { | 47 public void testSimpleModifier() { |
48 String[] patterns = { "{0}", "X{0}Y", "XX{0}YYY", "{0}YY", "XX📺XX{0}" }; | 48 String[] patterns = { "{0}", "X{0}Y", "XX{0}YYY", "{0}YY", "XX📺XX{0}" }; |
49 Object[][] outputs = { { "", 0, 0 }, { "a📻bcde", 0, 0 }, { "a📻bcde", 4,
4 }, { "a📻bcde", 3, 5 } }; | 49 Object[][] outputs = { |
| 50 { "", 0, 0 }, |
| 51 { "a📻bcde", 0, 0 }, |
| 52 { "a📻bcde", 4, 4 }, |
| 53 { "a📻bcde", 3, 5 } }; |
50 int[] prefixLens = { 0, 1, 2, 0, 6 }; | 54 int[] prefixLens = { 0, 1, 2, 0, 6 }; |
51 String[][] expectedCharFields = { { "|", "n" }, { "X|Y", "%n%" }, { "XX|
YYY", "%%n%%%" }, { "|YY", "n%%" }, | 55 String[][] expectedCharFields = { |
| 56 { "|", "n" }, |
| 57 { "X|Y", "%n%" }, |
| 58 { "XX|YYY", "%%n%%%" }, |
| 59 { "|YY", "n%%" }, |
52 { "XX📺XX|", "%%%%%%n" } }; | 60 { "XX📺XX|", "%%%%%%n" } }; |
53 String[][] expecteds = { { "", "XY", "XXYYY", "YY", "XX📺XX" }, | 61 String[][] expecteds = { |
| 62 { "", "XY", "XXYYY", "YY", "XX📺XX" }, |
54 { "a📻bcde", "XYa📻bcde", "XXYYYa📻bcde", "YYa📻bcde", "XX📺XXa📻bcde"
}, | 63 { "a📻bcde", "XYa📻bcde", "XXYYYa📻bcde", "YYa📻bcde", "XX📺XXa📻bcde"
}, |
55 { "a📻bcde", "a📻bXYcde", "a📻bXXYYYcde", "a📻bYYcde", "a📻bXX📺XXcde"
}, | 64 { "a📻bcde", "a📻bXYcde", "a📻bXXYYYcde", "a📻bYYcde", "a📻bXX📺XXcde"
}, |
56 { "a📻bcde", "a📻XbcYde", "a📻XXbcYYYde", "a📻bcYYde", "a📻XX📺XXbcde"
} }; | 65 { "a📻bcde", "a📻XbcYde", "a📻XXbcYYYde", "a📻bcYYde", "a📻XX📺XXbcde"
} }; |
57 for (int i = 0; i < patterns.length; i++) { | 66 for (int i = 0; i < patterns.length; i++) { |
58 String pattern = patterns[i]; | 67 String pattern = patterns[i]; |
59 String compiledPattern = SimpleFormatterImpl | 68 String compiledPattern = SimpleFormatterImpl |
60 .compileToStringMinMaxArguments(pattern, new StringBuilder()
, 1, 1); | 69 .compileToStringMinMaxArguments(pattern, new StringBuilder()
, 1, 1); |
61 Modifier mod = new SimpleModifier(compiledPattern, NumberFormat.Fiel
d.PERCENT, false); | 70 Modifier mod = new SimpleModifier(compiledPattern, NumberFormat.Fiel
d.PERCENT, false); |
62 assertModifierEquals(mod, prefixLens[i], false, expectedCharFields[i
][0], expectedCharFields[i][1]); | 71 assertModifierEquals(mod, |
| 72 prefixLens[i], |
| 73 false, |
| 74 expectedCharFields[i][0], |
| 75 expectedCharFields[i][1]); |
63 | 76 |
64 // Test strange insertion positions | 77 // Test strange insertion positions |
65 for (int j = 0; j < outputs.length; j++) { | 78 for (int j = 0; j < outputs.length; j++) { |
66 NumberStringBuilder output = new NumberStringBuilder(); | 79 NumberStringBuilder output = new NumberStringBuilder(); |
67 output.append((String) outputs[j][0], null); | 80 output.append((String) outputs[j][0], null); |
68 mod.apply(output, (Integer) outputs[j][1], (Integer) outputs[j][
2]); | 81 mod.apply(output, (Integer) outputs[j][1], (Integer) outputs[j][
2]); |
69 String expected = expecteds[j][i]; | 82 String expected = expecteds[j][i]; |
70 String actual = output.toString(); | 83 String actual = output.toString(); |
71 assertEquals(expected, actual); | 84 assertEquals(expected, actual); |
72 } | 85 } |
(...skipping 19 matching lines...) Expand all Loading... |
92 assertModifierEquals(mod2, sb1, 3, true, "USD\u00A0123", "$$$niii"); | 105 assertModifierEquals(mod2, sb1, 3, true, "USD\u00A0123", "$$$niii"); |
93 | 106 |
94 // Compare with the unsafe code path | 107 // Compare with the unsafe code path |
95 NumberStringBuilder sb2 = new NumberStringBuilder(sb); | 108 NumberStringBuilder sb2 = new NumberStringBuilder(sb); |
96 sb2.insert(0, "USD", NumberFormat.Field.CURRENCY); | 109 sb2.insert(0, "USD", NumberFormat.Field.CURRENCY); |
97 CurrencySpacingEnabledModifier.applyCurrencySpacing(sb2, 0, 3, 6, 0, sym
bols); | 110 CurrencySpacingEnabledModifier.applyCurrencySpacing(sb2, 0, 3, 6, 0, sym
bols); |
98 assertTrue(sb1.toDebugString() + " vs " + sb2.toDebugString(), sb1.conte
ntEquals(sb2)); | 111 assertTrue(sb1.toDebugString() + " vs " + sb2.toDebugString(), sb1.conte
ntEquals(sb2)); |
99 | 112 |
100 // Test custom patterns | 113 // Test custom patterns |
101 // The following line means that the last char of the number should be a
| (rather than a digit) | 114 // The following line means that the last char of the number should be a
| (rather than a digit) |
102 symbols.setPatternForCurrencySpacing(DecimalFormatSymbols.CURRENCY_SPC_S
URROUNDING_MATCH, true, "[|]"); | 115 symbols.setPatternForCurrencySpacing(DecimalFormatSymbols.CURRENCY_SPC_S
URROUNDING_MATCH, |
| 116 true, |
| 117 "[|]"); |
103 suffix.append("XYZ", NumberFormat.Field.CURRENCY); | 118 suffix.append("XYZ", NumberFormat.Field.CURRENCY); |
104 Modifier mod3 = new CurrencySpacingEnabledModifier(prefix, suffix, true,
symbols); | 119 Modifier mod3 = new CurrencySpacingEnabledModifier(prefix, suffix, true,
symbols); |
105 assertModifierEquals(mod3, 3, true, "USD|\u00A0XYZ", "$$$nn$$$"); | 120 assertModifierEquals(mod3, 3, true, "USD|\u00A0XYZ", "$$$nn$$$"); |
106 } | 121 } |
107 | 122 |
108 @Test | 123 @Test |
109 public void testCurrencySpacingPatternStability() { | 124 public void testCurrencySpacingPatternStability() { |
110 // This test checks for stability of the currency spacing patterns in CL
DR. | 125 // This test checks for stability of the currency spacing patterns in CL
DR. |
111 // For efficiency, ICU caches the most common currency spacing UnicodeSe
ts. | 126 // For efficiency, ICU caches the most common currency spacing UnicodeSe
ts. |
112 // If this test starts failing, please update the method #getUnicodeSet(
) in | 127 // If this test starts failing, please update the method #getUnicodeSet(
) in |
113 // BOTH CurrencySpacingEnabledModifier.java AND in C++. | 128 // BOTH CurrencySpacingEnabledModifier.java AND in C++. |
114 DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(new ULocale(
"en-US")); | 129 DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(new ULocale(
"en-US")); |
115 assertEquals( | 130 assertEquals("[:^S:]", |
116 "[:^S:]", | 131 dfs.getPatternForCurrencySpacing(DecimalFormatSymbols.CURRENCY_S
PC_CURRENCY_MATCH, |
117 dfs.getPatternForCurrencySpacing(DecimalFormatSymbols.CURRENCY_S
PC_CURRENCY_MATCH, true)); | 132 true)); |
118 assertEquals( | 133 assertEquals("[:^S:]", |
119 "[:^S:]", | 134 dfs.getPatternForCurrencySpacing(DecimalFormatSymbols.CURRENCY_S
PC_CURRENCY_MATCH, |
120 dfs.getPatternForCurrencySpacing(DecimalFormatSymbols.CURRENCY_S
PC_CURRENCY_MATCH, false)); | 135 false)); |
121 assertEquals( | 136 assertEquals("[:digit:]", |
122 "[:digit:]", | 137 dfs.getPatternForCurrencySpacing(DecimalFormatSymbols.CURRENCY_S
PC_SURROUNDING_MATCH, |
123 dfs.getPatternForCurrencySpacing(DecimalFormatSymbols.CURRENCY_S
PC_SURROUNDING_MATCH, true)); | 138 true)); |
124 assertEquals( | 139 assertEquals("[:digit:]", |
125 "[:digit:]", | 140 dfs.getPatternForCurrencySpacing(DecimalFormatSymbols.CURRENCY_S
PC_SURROUNDING_MATCH, |
126 dfs.getPatternForCurrencySpacing(DecimalFormatSymbols.CURRENCY_S
PC_SURROUNDING_MATCH, false)); | 141 false)); |
127 } | 142 } |
128 | 143 |
129 private void assertModifierEquals( | 144 private void assertModifierEquals( |
130 Modifier mod, | 145 Modifier mod, |
131 int expectedPrefixLength, | 146 int expectedPrefixLength, |
132 boolean expectedStrong, | 147 boolean expectedStrong, |
133 String expectedChars, | 148 String expectedChars, |
134 String expectedFields) { | 149 String expectedFields) { |
135 NumberStringBuilder sb = new NumberStringBuilder(); | 150 NumberStringBuilder sb = new NumberStringBuilder(); |
136 sb.appendCodePoint('|', null); | 151 sb.appendCodePoint('|', null); |
137 assertModifierEquals(mod, sb, expectedPrefixLength, expectedStrong, expe
ctedChars, expectedFields); | 152 assertModifierEquals(mod, |
| 153 sb, |
| 154 expectedPrefixLength, |
| 155 expectedStrong, |
| 156 expectedChars, |
| 157 expectedFields); |
138 } | 158 } |
139 | 159 |
140 private void assertModifierEquals( | 160 private void assertModifierEquals( |
141 Modifier mod, | 161 Modifier mod, |
142 NumberStringBuilder sb, | 162 NumberStringBuilder sb, |
143 int expectedPrefixLength, | 163 int expectedPrefixLength, |
144 boolean expectedStrong, | 164 boolean expectedStrong, |
145 String expectedChars, | 165 String expectedChars, |
146 String expectedFields) { | 166 String expectedFields) { |
147 int oldCount = sb.codePointCount(); | 167 int oldCount = sb.codePointCount(); |
148 mod.apply(sb, 0, oldCount); | 168 mod.apply(sb, 0, oldCount); |
149 assertEquals("Prefix length on " + sb, expectedPrefixLength, mod.getPref
ixLength()); | 169 assertEquals("Prefix length on " + sb, expectedPrefixLength, mod.getPref
ixLength()); |
150 assertEquals("Strong on " + sb, expectedStrong, mod.isStrong()); | 170 assertEquals("Strong on " + sb, expectedStrong, mod.isStrong()); |
151 if (!(mod instanceof CurrencySpacingEnabledModifier)) { | 171 if (!(mod instanceof CurrencySpacingEnabledModifier)) { |
152 assertEquals("Code point count equals actual code point count", | 172 assertEquals("Code point count equals actual code point count", |
153 sb.codePointCount() - oldCount, mod.getCodePointCount()); | 173 sb.codePointCount() - oldCount, |
| 174 mod.getCodePointCount()); |
154 } | 175 } |
155 assertEquals("<NumberStringBuilder [" + expectedChars + "] [" + expected
Fields + "]>", sb.toDebugString()); | 176 assertEquals("<NumberStringBuilder [" + expectedChars + "] [" + expected
Fields + "]>", |
| 177 sb.toDebugString()); |
156 } | 178 } |
157 } | 179 } |
LEFT | RIGHT |