View Javadoc

1   // Copyright (C) 2000 - 2009 Philip Aston
2   // Copyright (C) 2005 Martin Wagner
3   // All rights reserved.
4   //
5   // This file is part of The Grinder software distribution. Refer to
6   // the file LICENSE which is part of The Grinder distribution for
7   // licensing details. The Grinder distribution is available on the
8   // Internet at http://grinder.sourceforge.net/
9   //
10  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
11  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
12  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
14  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
15  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
21  // OF THE POSSIBILITY OF SUCH DAMAGE.
22  
23  package net.grinder.util;
24  
25  import java.util.Locale;
26  
27  import junit.framework.TestCase;
28  
29  import static net.grinder.testutility.AssertUtilities.assertNotEquals;
30  
31  
32  /**
33   * Unit tests for {@link SignificantFigureFormat}.
34   *
35   * @author Philip Aston
36   */
37  public class TestSignificantFigureFormat extends TestCase {
38  
39    private final Locale m_originalDefaultLocale = Locale.getDefault();
40  
41    protected void setUp() {
42      Locale.setDefault(Locale.US);
43    }
44  
45    protected void tearDown() throws Exception {
46      Locale.setDefault(m_originalDefaultLocale);
47    }
48  
49    public void testSignificantFigureFormat() throws Exception {
50      java.util.Locale.setDefault(java.util.Locale.US);
51      final SignificantFigureFormat f = new SignificantFigureFormat(4);
52  
53      assertEquals("1.000", f.format(1d));
54      assertEquals("1.000", f.format(1));
55      assertEquals("-1.000", f.format(-1d));
56      assertEquals("0.1000", f.format(0.1));
57      assertEquals("123.0", f.format(123d));
58      assertEquals("123.0", f.format(123));
59      assertEquals("10.00", f.format(10d));
60      assertEquals("10.00", f.format(10));
61      assertEquals("0.9900", f.format(.99d));
62      assertEquals("0.002320", f.format(.00232));
63      assertEquals("12350", f.format(12345d));
64      assertEquals("12350", f.format(12345));
65      assertEquals("1235", f.format(1234.5));
66      assertEquals("1234", f.format(1234));
67      assertEquals("12.35", f.format(12.345));
68      assertEquals("0.1235", f.format(0.12345));
69      // Interestingly .012345 -> 0.01234, but I think this is a
70      // floating point thing.
71      assertEquals("0.01234", f.format(0.012345));
72      assertEquals("0.001235", f.format(0.0012345));
73      assertEquals("0.000", f.format(0));
74      assertEquals("0.000", f.format(-0));
75      assertEquals("0.000", f.format(0.0));
76      assertEquals("0.000", f.format(-0.0));
77      assertEquals("\u221e", f.format(Double.POSITIVE_INFINITY));
78      assertEquals("-\u221e", f.format(Double.NEGATIVE_INFINITY));
79      assertEquals("\ufffd", f.format(Double.NaN));
80    }
81  
82    public void testEquality() {
83      final SignificantFigureFormat f1 = new SignificantFigureFormat(4);
84  
85      assertNotEquals(f1, null);
86      assertEquals(f1, f1);
87  
88      final SignificantFigureFormat f2 = new SignificantFigureFormat(4);
89      assertEquals(f1, f2);
90      assertEquals(f1.hashCode(), f2.hashCode());
91  
92      final SignificantFigureFormat f3 = new SignificantFigureFormat(3);
93      assertNotEquals(f3, f2);
94      assertNotEquals(f2, f3);
95    }
96  }