1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package org.apache.harmony.text.tests.java.text; 18 19import dalvik.annotation.AndroidOnly; 20import dalvik.annotation.TestTargets; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTargetNew; 23import dalvik.annotation.TestTargetClass; 24 25import junit.framework.TestCase; 26 27import java.text.DecimalFormat; 28import java.text.NumberFormat; 29import java.util.Locale; 30 31/** 32 * Test for additional features introduced by icu. These tests fail on the RI 33 * but succeed on Android 1.0. The features are only accessible through the 34 * pattern. Api methods where not introduced to allow direct access of these 35 * features. This would have changed the api too much. 36 */ 37@TestTargetClass(DecimalFormat.class) 38public class DecimalFormatTestICU extends TestCase { 39 40 DecimalFormat format; 41 42 protected void setUp() { 43 format = (DecimalFormat) NumberFormat.getNumberInstance(); 44 } 45 46 @TestTargets({ 47 @TestTargetNew( 48 level = TestLevel.COMPLETE, 49 notes = "Regression test.", 50 method = "format", 51 args = {java.lang.Object.class} 52 ), 53 @TestTargetNew( 54 level = TestLevel.COMPLETE, 55 notes = "Regression test.", 56 method = "parse", 57 args = {java.lang.String.class} 58 ) 59 }) 60 @AndroidOnly("special feature of icu4c") 61 public void test_sigDigitPatterns() throws Exception { 62 DecimalFormat format = (DecimalFormat) NumberFormat 63 .getInstance(Locale.US); 64 65 format.applyPattern("@@@"); 66 assertEquals("sigDigit doesn't work", "12300", format.format(12345)); 67 assertEquals("sigDigit doesn't work", "0.123", format.format(0.12345)); 68 69 format.applyPattern("@@##"); 70 assertEquals("sigDigit doesn't work", "3.142", format.format(3.14159)); 71 assertEquals("sigDigit doesn't work", "1.23", format.format(1.23004)); 72 73 format.applyPattern("@@###E0"); 74 assertEquals("1.23E1", format.format(12.3)); 75 format.applyPattern("0.0###E0"); 76 assertEquals("1.23E1", format.format(12.3)); 77 78 try { 79 format.applyPattern("@00"); 80 fail("expected IllegalArgumentException was not thrown for " 81 + "pattern \"@00\"."); 82 } catch (IllegalArgumentException e) { 83 // expected 84 } 85 86 try { 87 format.applyPattern("@.###"); 88 fail("expected IllegalArgumentException was not thrown for " 89 + "pattern \"@.###\"."); 90 } catch (IllegalArgumentException e) { 91 // expected 92 } 93 } 94 95 @TestTargets({ 96 @TestTargetNew( 97 level = TestLevel.PARTIAL, 98 notes = "Regression test.", 99 method = "format", 100 args = {java.lang.Object.class} 101 ), 102 @TestTargetNew( 103 level = TestLevel.PARTIAL, 104 notes = "Regression test.", 105 method = "parse", 106 args = {java.lang.String.class} 107 ) 108 }) 109 @AndroidOnly("special feature of icu4c") 110 public void test_paddingPattern() throws Exception { 111 format.applyPattern("*x##,##,#,##0.0#"); 112 assertEquals("xxxxxxxxx123.0", format.format(123)); 113 assertEquals(123, format.parse("xxxxxxxxx123.0").intValue()); 114 115 format.applyPattern("$*x#,##0.00"); 116 assertEquals("$xx123.00", format.format(123)); 117 assertEquals("$1,234.00", format.format(1234)); 118 119 format.applyPattern("*\u00e7#0 o''clock"); 120 assertEquals("\u00e72 o'clock", format.format(2)); 121 assertEquals("12 o'clock", format.format(12)); 122 assertEquals(2, format.parse("\u00e72 o'clock").intValue()); 123 assertEquals(12, format.parse("12 o'clock").intValue()); 124 125 try { 126 format.applyPattern("#0.##*xE0"); 127 fail("expected IllegalArgumentException was not thrown for" 128 + "pattern \"#0.##*xE0\"."); 129 } catch (IllegalArgumentException e) { 130 // expected 131 } 132 133 try { 134 format.applyPattern("##0.## *"); 135 fail("expected IllegalArgumentException was not thrown for " 136 + "pattern \"##0.## *\"."); 137 } catch (IllegalArgumentException e) { 138 // expected 139 } 140 } 141 142 @TestTargets({ 143 @TestTargetNew( 144 level = TestLevel.PARTIAL, 145 notes = "Regression test.", 146 method = "format", 147 args = {java.lang.Object.class} 148 ), 149 @TestTargetNew( 150 level = TestLevel.PARTIAL, 151 notes = "Regression test.", 152 method = "parse", 153 args = {java.lang.String.class} 154 ) 155 }) 156 @AndroidOnly("special feature of icu4c") 157 public void test_positiveExponentSign() throws Exception { 158 format.applyPattern("0.###E+0"); 159 assertEquals("1E+2", format.format(100)); 160 assertEquals("1E-2", format.format(0.01)); 161 assertEquals(100, format.parse("1E+2").intValue()); 162 assertEquals(0.01f, format.parse("1E-2").floatValue()); 163 164 format.applyPattern("0.###E0 m/s"); 165 assertEquals("1E2 m/s", format.format(100)); 166 assertEquals(100, format.parse("1E2 m/s").intValue()); 167 168 format.applyPattern("00.###E0"); 169 assertEquals("12.3E-4", format.format(0.00123)); 170 assertEquals(0.00123f, format.parse("12.3E-4").floatValue()); 171 172 format.applyPattern("##0.####E0"); 173 assertEquals("12.345E3", format.format(12345)); 174 assertEquals(12345, format.parse("12.345E3").intValue()); 175 176 try { 177 format.applyPattern("#,##0.##E0"); 178 fail("expected IllegalArgumentException was not thrown for " 179 + "pattern \"#,##0.##E0\"."); 180 } catch (IllegalArgumentException e) { 181 // expected 182 } 183 } 184 185 @TestTargets({ 186 @TestTargetNew( 187 level = TestLevel.PARTIAL, 188 notes = "Verifies the grouping size.", 189 method = "format", 190 args = {java.lang.Object.class} 191 ), 192 @TestTargetNew( 193 level = TestLevel.PARTIAL, 194 notes = "Verifies the grouping size.", 195 method = "parse", 196 args = {java.lang.String.class} 197 ) 198 }) 199 @AndroidOnly("special feature of icu4c") 200 public void test_secondaryGroupingSize() throws Exception { 201 format.applyPattern("#,##,###,####"); 202 assertEquals("123,456,7890", format.format(1234567890)); 203 assertEquals(1234567890, format.parse("123,456,7890").intValue()); 204 format.applyPattern("##,#,###,####"); 205 assertEquals("123,456,7890", format.format(1234567890)); 206 assertEquals(1234567890, format.parse("123,456,7890").intValue()); 207 format.applyPattern("###,###,####"); 208 assertEquals("123,456,7890", format.format(1234567890)); 209 assertEquals(1234567890, format.parse("123,456,7890").intValue()); 210 211 format.applyPattern("###,##,###.#"); 212 assertEquals("12,34,567.8", format.format(1234567.8)); 213 format.applyPattern("##,#,##,###.#"); 214 assertEquals("12,34,567.8", format.format(1234567.8)); 215 format.applyPattern("#,##,##,###.#"); 216 assertEquals("12,34,567.8", format.format(1234567.8)); 217 } 218} 219