001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.math.fraction; 019 020 import java.text.NumberFormat; 021 import java.text.ParseException; 022 import java.util.Locale; 023 024 import junit.framework.TestCase; 025 026 public class FractionFormatTest extends TestCase { 027 028 FractionFormat properFormat = null; 029 FractionFormat improperFormat = null; 030 031 protected Locale getLocale() { 032 return Locale.getDefault(); 033 } 034 035 @Override 036 protected void setUp() throws Exception { 037 properFormat = FractionFormat.getProperInstance(getLocale()); 038 improperFormat = FractionFormat.getImproperInstance(getLocale()); 039 } 040 041 public void testFormat() { 042 Fraction c = new Fraction(1, 2); 043 String expected = "1 / 2"; 044 045 String actual = properFormat.format(c); 046 assertEquals(expected, actual); 047 048 actual = improperFormat.format(c); 049 assertEquals(expected, actual); 050 } 051 052 public void testFormatNegative() { 053 Fraction c = new Fraction(-1, 2); 054 String expected = "-1 / 2"; 055 056 String actual = properFormat.format(c); 057 assertEquals(expected, actual); 058 059 actual = improperFormat.format(c); 060 assertEquals(expected, actual); 061 } 062 063 public void testFormatZero() { 064 Fraction c = new Fraction(0, 1); 065 String expected = "0 / 1"; 066 067 String actual = properFormat.format(c); 068 assertEquals(expected, actual); 069 070 actual = improperFormat.format(c); 071 assertEquals(expected, actual); 072 } 073 074 public void testFormatImproper() { 075 Fraction c = new Fraction(5, 3); 076 077 String actual = properFormat.format(c); 078 assertEquals("1 2 / 3", actual); 079 080 actual = improperFormat.format(c); 081 assertEquals("5 / 3", actual); 082 } 083 084 public void testFormatImproperNegative() { 085 Fraction c = new Fraction(-5, 3); 086 087 String actual = properFormat.format(c); 088 assertEquals("-1 2 / 3", actual); 089 090 actual = improperFormat.format(c); 091 assertEquals("-5 / 3", actual); 092 } 093 094 public void testParse() { 095 String source = "1 / 2"; 096 097 try { 098 Fraction c = properFormat.parse(source); 099 assertNotNull(c); 100 assertEquals(1, c.getNumerator()); 101 assertEquals(2, c.getDenominator()); 102 103 c = improperFormat.parse(source); 104 assertNotNull(c); 105 assertEquals(1, c.getNumerator()); 106 assertEquals(2, c.getDenominator()); 107 } catch (ParseException ex) { 108 fail(ex.getMessage()); 109 } 110 } 111 112 public void testParseInteger() { 113 String source = "10"; 114 try { 115 Fraction c = properFormat.parse(source); 116 assertNotNull(c); 117 assertEquals(10, c.getNumerator()); 118 assertEquals(1, c.getDenominator()); 119 } catch (ParseException ex) { 120 fail(ex.getMessage()); 121 } 122 try { 123 Fraction c = improperFormat.parse(source); 124 assertNotNull(c); 125 assertEquals(10, c.getNumerator()); 126 assertEquals(1, c.getDenominator()); 127 } catch (ParseException ex) { 128 fail(ex.getMessage()); 129 } 130 } 131 132 public void testParseInvalid() { 133 String source = "a"; 134 String msg = "should not be able to parse '10 / a'."; 135 try { 136 properFormat.parse(source); 137 fail(msg); 138 } catch (ParseException ex) { 139 // success 140 } 141 try { 142 improperFormat.parse(source); 143 fail(msg); 144 } catch (ParseException ex) { 145 // success 146 } 147 } 148 149 public void testParseInvalidDenominator() { 150 String source = "10 / a"; 151 String msg = "should not be able to parse '10 / a'."; 152 try { 153 properFormat.parse(source); 154 fail(msg); 155 } catch (ParseException ex) { 156 // success 157 } 158 try { 159 improperFormat.parse(source); 160 fail(msg); 161 } catch (ParseException ex) { 162 // success 163 } 164 } 165 166 public void testParseNegative() { 167 168 try { 169 String source = "-1 / 2"; 170 Fraction c = properFormat.parse(source); 171 assertNotNull(c); 172 assertEquals(-1, c.getNumerator()); 173 assertEquals(2, c.getDenominator()); 174 175 c = improperFormat.parse(source); 176 assertNotNull(c); 177 assertEquals(-1, c.getNumerator()); 178 assertEquals(2, c.getDenominator()); 179 180 source = "1 / -2"; 181 c = properFormat.parse(source); 182 assertNotNull(c); 183 assertEquals(-1, c.getNumerator()); 184 assertEquals(2, c.getDenominator()); 185 186 c = improperFormat.parse(source); 187 assertNotNull(c); 188 assertEquals(-1, c.getNumerator()); 189 assertEquals(2, c.getDenominator()); 190 } catch (ParseException ex) { 191 fail(ex.getMessage()); 192 } 193 } 194 195 public void testParseProper() { 196 String source = "1 2 / 3"; 197 198 try { 199 Fraction c = properFormat.parse(source); 200 assertNotNull(c); 201 assertEquals(5, c.getNumerator()); 202 assertEquals(3, c.getDenominator()); 203 } catch (ParseException ex) { 204 fail(ex.getMessage()); 205 } 206 207 try { 208 improperFormat.parse(source); 209 fail("invalid improper fraction."); 210 } catch (ParseException ex) { 211 // success 212 } 213 } 214 215 public void testParseProperNegative() { 216 String source = "-1 2 / 3"; 217 try { 218 Fraction c = properFormat.parse(source); 219 assertNotNull(c); 220 assertEquals(-5, c.getNumerator()); 221 assertEquals(3, c.getDenominator()); 222 } catch (ParseException ex) { 223 fail(ex.getMessage()); 224 } 225 226 try { 227 improperFormat.parse(source); 228 fail("invalid improper fraction."); 229 } catch (ParseException ex) { 230 // success 231 } 232 } 233 234 public void testParseProperInvalidMinus() { 235 String source = "2 -2 / 3"; 236 try { 237 properFormat.parse(source); 238 fail("invalid minus in improper fraction."); 239 } catch (ParseException ex) { 240 // expected 241 } 242 source = "2 2 / -3"; 243 try { 244 properFormat.parse(source); 245 fail("invalid minus in improper fraction."); 246 } catch (ParseException ex) { 247 // expected 248 } 249 } 250 251 public void testNumeratorFormat() { 252 NumberFormat old = properFormat.getNumeratorFormat(); 253 NumberFormat nf = NumberFormat.getInstance(); 254 nf.setParseIntegerOnly(true); 255 properFormat.setNumeratorFormat(nf); 256 assertEquals(nf, properFormat.getNumeratorFormat()); 257 properFormat.setNumeratorFormat(old); 258 259 old = improperFormat.getNumeratorFormat(); 260 nf = NumberFormat.getInstance(); 261 nf.setParseIntegerOnly(true); 262 improperFormat.setNumeratorFormat(nf); 263 assertEquals(nf, improperFormat.getNumeratorFormat()); 264 improperFormat.setNumeratorFormat(old); 265 } 266 267 public void testDenominatorFormat() { 268 NumberFormat old = properFormat.getDenominatorFormat(); 269 NumberFormat nf = NumberFormat.getInstance(); 270 nf.setParseIntegerOnly(true); 271 properFormat.setDenominatorFormat(nf); 272 assertEquals(nf, properFormat.getDenominatorFormat()); 273 properFormat.setDenominatorFormat(old); 274 275 old = improperFormat.getDenominatorFormat(); 276 nf = NumberFormat.getInstance(); 277 nf.setParseIntegerOnly(true); 278 improperFormat.setDenominatorFormat(nf); 279 assertEquals(nf, improperFormat.getDenominatorFormat()); 280 improperFormat.setDenominatorFormat(old); 281 } 282 283 public void testWholeFormat() { 284 ProperFractionFormat format = (ProperFractionFormat)properFormat; 285 286 NumberFormat old = format.getWholeFormat(); 287 NumberFormat nf = NumberFormat.getInstance(); 288 nf.setParseIntegerOnly(true); 289 format.setWholeFormat(nf); 290 assertEquals(nf, format.getWholeFormat()); 291 format.setWholeFormat(old); 292 } 293 294 public void testLongFormat() { 295 assertEquals("10 / 1", improperFormat.format(10l)); 296 } 297 298 public void testDoubleFormat() { 299 assertEquals("355 / 113", improperFormat.format(Math.PI)); 300 } 301 }