001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.math.fraction;
018:
019: import java.text.ParseException;
020: import java.util.Locale;
021:
022: import junit.framework.TestCase;
023:
024: public class FractionFormatTest extends TestCase {
025:
026: FractionFormat properFormat = null;
027: FractionFormat improperFormat = null;
028:
029: protected Locale getLocale() {
030: return Locale.getDefault();
031: }
032:
033: protected void setUp() throws Exception {
034: properFormat = FractionFormat.getProperInstance(getLocale());
035: improperFormat = FractionFormat
036: .getImproperInstance(getLocale());
037: }
038:
039: public void testFormat() {
040: Fraction c = new Fraction(1, 2);
041: String expected = "1 / 2";
042:
043: String actual = properFormat.format(c);
044: assertEquals(expected, actual);
045:
046: actual = improperFormat.format(c);
047: assertEquals(expected, actual);
048: }
049:
050: public void testFormatNegative() {
051: Fraction c = new Fraction(-1, 2);
052: String expected = "-1 / 2";
053:
054: String actual = properFormat.format(c);
055: assertEquals(expected, actual);
056:
057: actual = improperFormat.format(c);
058: assertEquals(expected, actual);
059: }
060:
061: public void testFormatZero() {
062: Fraction c = new Fraction(0, 1);
063: String expected = "0 / 1";
064:
065: String actual = properFormat.format(c);
066: assertEquals(expected, actual);
067:
068: actual = improperFormat.format(c);
069: assertEquals(expected, actual);
070: }
071:
072: public void testFormatImproper() {
073: Fraction c = new Fraction(5, 3);
074:
075: String actual = properFormat.format(c);
076: assertEquals("1 2 / 3", actual);
077:
078: actual = improperFormat.format(c);
079: assertEquals("5 / 3", actual);
080: }
081:
082: public void testFormatImproperNegative() {
083: Fraction c = new Fraction(-5, 3);
084:
085: String actual = properFormat.format(c);
086: assertEquals("-1 2 / 3", actual);
087:
088: actual = improperFormat.format(c);
089: assertEquals("-5 / 3", actual);
090: }
091:
092: public void testParse() {
093: String source = "1 / 2";
094:
095: try {
096: Fraction c = properFormat.parse(source);
097: assertNotNull(c);
098: assertEquals(1, c.getNumerator());
099: assertEquals(2, c.getDenominator());
100:
101: c = improperFormat.parse(source);
102: assertNotNull(c);
103: assertEquals(1, c.getNumerator());
104: assertEquals(2, c.getDenominator());
105: } catch (ParseException ex) {
106: fail(ex.getMessage());
107: }
108: }
109:
110: public void testParseNegative() {
111:
112: try {
113: String source = "-1 / 2";
114: Fraction c = properFormat.parse(source);
115: assertNotNull(c);
116: assertEquals(-1, c.getNumerator());
117: assertEquals(2, c.getDenominator());
118:
119: c = improperFormat.parse(source);
120: assertNotNull(c);
121: assertEquals(-1, c.getNumerator());
122: assertEquals(2, c.getDenominator());
123:
124: source = "1 / -2";
125: c = properFormat.parse(source);
126: assertNotNull(c);
127: assertEquals(-1, c.getNumerator());
128: assertEquals(2, c.getDenominator());
129:
130: c = improperFormat.parse(source);
131: assertNotNull(c);
132: assertEquals(-1, c.getNumerator());
133: assertEquals(2, c.getDenominator());
134: } catch (ParseException ex) {
135: fail(ex.getMessage());
136: }
137: }
138:
139: public void testParseProper() {
140: String source = "1 2 / 3";
141:
142: try {
143: Fraction c = properFormat.parse(source);
144: assertNotNull(c);
145: assertEquals(5, c.getNumerator());
146: assertEquals(3, c.getDenominator());
147: } catch (ParseException ex) {
148: fail(ex.getMessage());
149: }
150:
151: try {
152: improperFormat.parse(source);
153: fail("invalid improper fraction.");
154: } catch (ParseException ex) {
155: // success
156: }
157: }
158:
159: public void testParseProperNegative() {
160: String source = "-1 2 / 3";
161: try {
162: Fraction c = properFormat.parse(source);
163: assertNotNull(c);
164: assertEquals(-5, c.getNumerator());
165: assertEquals(3, c.getDenominator());
166: } catch (ParseException ex) {
167: fail(ex.getMessage());
168: }
169:
170: try {
171: improperFormat.parse(source);
172: fail("invalid improper fraction.");
173: } catch (ParseException ex) {
174: // success
175: }
176: }
177: }
|