LEFT | RIGHT |
(no file at all) | |
| 1 // © 2017 and later: Unicode, Inc. and others. |
| 2 // License & terms of use: http://www.unicode.org/copyright.html#License |
| 3 package com.ibm.icu.dev.test.number; |
| 4 |
| 5 import static org.junit.Assert.assertEquals; |
| 6 import static org.junit.Assert.assertNotNull; |
| 7 |
| 8 import org.junit.Test; |
| 9 |
| 10 import com.ibm.icu.impl.number.parse.NumberParserImpl; |
| 11 import com.ibm.icu.impl.number.parse.ParsedNumber; |
| 12 |
| 13 /** |
| 14 * @author sffc |
| 15 * |
| 16 */ |
| 17 public class NumberParserTest { |
| 18 @Test |
| 19 public void testBasic() { |
| 20 Object[][] cases = new Object[][] { |
| 21 // Fields: |
| 22 // a) Flags: |
| 23 // --- Bit 0x01 => Test greedy implementation |
| 24 // --- Bit 0x02 => Test slow implementation |
| 25 // --- Bit 0x04 => Test strict grouping separators |
| 26 // b) Input string |
| 27 // c) Pattern |
| 28 // d) Expected chars consumed |
| 29 // e) Expected double result |
| 30 { 3, "51423", "0", 5, 51423. }, |
| 31 { 3, "51423x", "0", 5, 51423. }, |
| 32 { 3, " 51423", "0", 6, 51423. }, |
| 33 { 3, "51423 ", "0", 5, 51423. }, |
| 34 { 3, "𝟱𝟭𝟰𝟮𝟯", "0", 10, 51423. }, |
| 35 { 3, "𝟱𝟭𝟰𝟮𝟯x", "0", 10, 51423. }, |
| 36 { 3, " 𝟱𝟭𝟰𝟮𝟯", "0", 11, 51423. }, |
| 37 { 3, "𝟱𝟭𝟰𝟮𝟯 ", "0", 10, 51423. }, |
| 38 { 7, "𝟱𝟭,𝟰𝟮𝟯", "#,##,##0", 11, 51423. }, |
| 39 { 7, "𝟳,𝟴𝟵,𝟱𝟭,𝟰𝟮𝟯", "#,##,##0", 19, 78951423. }, |
| 40 { 4, "𝟳𝟴,𝟵𝟱𝟭,𝟰𝟮𝟯", "#,##,##0", 11, 78951. }, |
| 41 { 7, "𝟳𝟴,𝟵𝟱𝟭.𝟰𝟮𝟯", "#,##,##0", 18, 78951.423 }, |
| 42 { 7, "𝟳𝟴,𝟬𝟬𝟬", "#,##,##0", 11, 78000. }, |
| 43 { 7, "𝟳𝟴,𝟬𝟬𝟬.𝟬𝟬𝟬", "#,##,##0", 18, 78000. }, |
| 44 { 7, "𝟳𝟴,𝟬𝟬𝟬.𝟬𝟮𝟯", "#,##,##0", 18, 78000.023 }, |
| 45 { 7, "𝟳𝟴.𝟬𝟬𝟬.𝟬𝟮𝟯", "#,##,##0", 11, 78. }, |
| 46 { 3, "-𝟱𝟭𝟰𝟮𝟯", "0", 11, -51423. }, |
| 47 { 3, "-𝟱𝟭𝟰𝟮𝟯-", "0", 11, -51423. }, |
| 48 { 3, "a51423US dollars", "a0¤¤¤", 16, 51423. }, |
| 49 { 3, "a 51423 US dollars", "a0¤¤¤", 18, 51423. }, |
| 50 { 3, "514.23 USD", "0", 10, 514.23 }, |
| 51 { 3, "514.23 GBP", "0", 10, 514.23 }, |
| 52 { 3, "a 𝟱𝟭𝟰𝟮𝟯 b", "a0b", 14, 51423. }, |
| 53 { 3, "-a 𝟱𝟭𝟰𝟮𝟯 b", "a0b", 15, -51423. }, |
| 54 { 3, "a -𝟱𝟭𝟰𝟮𝟯 b", "a0b", 15, -51423. }, |
| 55 { 3, "𝟱𝟭𝟰𝟮𝟯", "[0];(0)", 10, 51423. }, |
| 56 { 3, "[𝟱𝟭𝟰𝟮𝟯", "[0];(0)", 11, 51423. }, |
| 57 { 3, "𝟱𝟭𝟰𝟮𝟯]", "[0];(0)", 11, 51423. }, |
| 58 { 3, "[𝟱𝟭𝟰𝟮𝟯]", "[0];(0)", 12, 51423. }, |
| 59 { 3, "(𝟱𝟭𝟰𝟮𝟯", "[0];(0)", 11, -51423. }, |
| 60 { 3, "𝟱𝟭𝟰𝟮𝟯)", "[0];(0)", 11, -51423. }, |
| 61 { 3, "(𝟱𝟭𝟰𝟮𝟯)", "[0];(0)", 12, -51423. }, |
| 62 { 3, "𝟱𝟭𝟰𝟮𝟯", "{0};{0}", 10, 51423. }, |
| 63 { 3, "{𝟱𝟭𝟰𝟮𝟯", "{0};{0}", 11, 51423. }, |
| 64 { 3, "𝟱𝟭𝟰𝟮𝟯}", "{0};{0}", 11, 51423. }, |
| 65 { 3, "{𝟱𝟭𝟰𝟮𝟯}", "{0};{0}", 12, 51423. }, |
| 66 { 1, "a40b", "a0'0b'", 3, 40. }, // greedy code path thinks "40"
is the number |
| 67 { 2, "a40b", "a0'0b'", 4, 4. }, // slow code path finds the suff
ix "0b" |
| 68 { 3, "𝟱.𝟭𝟰𝟮E𝟯", "0", 12, 5142. }, |
| 69 { 3, "𝟱.𝟭𝟰𝟮E-𝟯", "0", 13, 0.005142 }, |
| 70 { 3, "𝟱.𝟭𝟰𝟮e-𝟯", "0", 13, 0.005142 }, |
| 71 { 7, "5,142.50 Canadian dollars", "#,##,##0", 25, 5142.5 }, |
| 72 // { 3, "a$ b5", "a ¤ b0", 6, 5.0 }, // TODO: Does not work |
| 73 { 3, "📺1.23", "📺0;📻0", 6, 1.23 }, |
| 74 { 3, "📻1.23", "📺0;📻0", 6, -1.23 }, |
| 75 { 3, ".00", "0", 3, 0.0 }, |
| 76 { 3, "0", "0", 1, 0.0 } }; |
| 77 |
| 78 for (Object[] cas : cases) { |
| 79 int flags = (Integer) cas[0]; |
| 80 String input = (String) cas[1]; |
| 81 String pattern = (String) cas[2]; |
| 82 int expectedCharsConsumed = (Integer) cas[3]; |
| 83 double resultDouble = (Double) cas[4]; |
| 84 NumberParserImpl parser = NumberParserImpl.createParserFromPattern(p
attern, false); |
| 85 String message = "Input <" + input + "> Parser " + parser; |
| 86 |
| 87 if (0 != (flags & 0x01)) { |
| 88 // Test greedy code path |
| 89 ParsedNumber resultObject = new ParsedNumber(); |
| 90 parser.parse(input, true, resultObject); |
| 91 assertNotNull(message, resultObject.quantity); |
| 92 assertEquals(message, expectedCharsConsumed, resultObject.charsC
onsumed); |
| 93 assertEquals(message, resultDouble, resultObject.getNumber().dou
bleValue(), 0.0); |
| 94 } |
| 95 |
| 96 if (0 != (flags & 0x02)) { |
| 97 // Test slow code path |
| 98 ParsedNumber resultObject = new ParsedNumber(); |
| 99 parser.parse(input, false, resultObject); |
| 100 assertNotNull(message, resultObject.quantity); |
| 101 assertEquals(message, expectedCharsConsumed, resultObject.charsC
onsumed); |
| 102 assertEquals(message, resultDouble, resultObject.getNumber().dou
bleValue(), 0.0); |
| 103 } |
| 104 |
| 105 if (0 != (flags & 0x04)) { |
| 106 // Test with strict separators |
| 107 parser = NumberParserImpl.createParserFromPattern(pattern, true)
; |
| 108 ParsedNumber resultObject = new ParsedNumber(); |
| 109 parser.parse(input, true, resultObject); |
| 110 assertNotNull(message, resultObject.quantity); |
| 111 assertEquals(message, expectedCharsConsumed, resultObject.charsC
onsumed); |
| 112 assertEquals(message, resultDouble, resultObject.getNumber().dou
bleValue(), 0.0); |
| 113 } |
| 114 } |
| 115 } |
| 116 } |
LEFT | RIGHT |