999e45616dbf28049a5dcf40e97c653897b381bc
[jalview.git] / test / jalview / util / FormatTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.util;
22
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertTrue;
25
26 import jalview.gui.JvOptionPane;
27
28 import org.testng.annotations.BeforeClass;
29 import org.testng.annotations.Test;
30
31 public class FormatTest
32 {
33
34   @BeforeClass(alwaysRun = true)
35   public void setUpJvOptionPane()
36   {
37     JvOptionPane.setInteractiveMode(false);
38     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
39   }
40
41   @Test(groups = "Functional")
42   public void testAppendPercentage()
43   {
44     StringBuilder sb = new StringBuilder();
45     Format.appendPercentage(sb, 123.436f, 0);
46     assertEquals(sb.toString(), "123");
47
48     sb.setLength(0);
49     Format.appendPercentage(sb, 123.536f, 0);
50     assertEquals(sb.toString(), "124");
51
52     sb.setLength(0);
53     Format.appendPercentage(sb, 799.536f, 0);
54     assertEquals(sb.toString(), "800");
55
56     sb.setLength(0);
57     Format.appendPercentage(sb, 123.436f, 1);
58     assertEquals(sb.toString(), "123.4");
59
60     sb.setLength(0);
61     Format.appendPercentage(sb, 123.436f, 2);
62     assertEquals(sb.toString(), "123.44");
63
64     sb.setLength(0);
65     Format.appendPercentage(sb, 123.436f, 3);
66     assertEquals(sb.toString(), "123.436");
67
68     sb.setLength(0);
69     Format.appendPercentage(sb, 123.436f, 4);
70     assertEquals(sb.toString(), "123.4360");
71   }
72
73   @Test(groups = "Functional")
74   public void testForm_float()
75   {
76     Format f = new Format("%3.2f");
77     assertEquals(f.form(123f), "123.00");
78     assertEquals(f.form(123.1f), "123.10");
79     assertEquals(f.form(123.12f), "123.12");
80     assertEquals(f.form(123.124f), "123.12");
81     assertEquals(f.form(123.125f), "123.13");
82     assertEquals(f.form(123.126f), "123.13");
83
84     f = new Format("%3.0f");
85     assertEquals(f.form(123f), "123.");
86     assertEquals(f.form(12f), "12.");
87     assertEquals(f.form(123.4f), "123.");
88     assertEquals(f.form(123.5f), "124.");
89     assertEquals(f.form(123.6f), "124.");
90     assertEquals(f.form(129.6f), "130.");
91   }
92
93   @Test(groups = "Functional")
94   public void testRepeat()
95   {
96     assertEquals(Format.repeat('a', 3), "aaa");
97     assertEquals(Format.repeat('b', 0), "");
98     assertEquals(Format.repeat('c', -1), "");
99   }
100
101   @Test(groups = "Functional")
102   public void testFormat_scientific()
103   {
104     Format f = new Format("%3.4e");
105     double d = 1d;
106     assertEquals(f.form(d), "1.0000e+000");
107     assertEquals(String.format("%3.4e", d), "1.0000e+00");
108
109     d = 12345678.12345678d;
110     assertEquals(f.form(d), "1.2346e+007");
111     assertEquals(String.format("%3.4e", d), "1.2346e+07");
112   }
113
114   /**
115    * Test that fails (in 2.10.1) with timeout as there is an infinite loop in
116    * Format.exp_format()
117    */
118   @Test(groups = "Functional", timeOut = 500)
119   public void testFormat_scientific_overflow()
120   {
121     Format f = new Format("%3.4e");
122     double d = 1.12E-310;
123     /*
124      * problem: exp_format() scales up 'd' to the range 1-10
125      * while computing a scaling factor, but this factor acquires
126      * the value Double.POSITIVE_INFINITY instead of 1.0E+309
127      * the value to be formatted is multipled by factor and becomes Infinity
128      * resulting in an infinite loop in the recursive call to exp_format()
129      */
130     assertEquals(f.form(d), "1.1200e-310");
131   }
132
133   /**
134    * This test shows that Format.form() is faster for this case than
135    * String.format()
136    */
137   @Test(groups = "Timing")
138   public void testFormat_scientificTiming()
139   {
140     Format f = new Format("%3.4e");
141     double d = 12345678.12345678d;
142
143     int iterations = 1000;
144     long start = System.currentTimeMillis();
145     for (int i = 0; i < iterations; i++)
146     {
147       f.form(d);
148     }
149     long stop = System.currentTimeMillis();
150     long elapsed1 = stop - start;
151     System.out.println(iterations + " x Format.form took " + elapsed1
152             + "ms");
153
154     start = System.currentTimeMillis();
155     for (int i = 0; i < iterations; i++)
156     {
157       String.format("%3.4e", d);
158     }
159     stop = System.currentTimeMillis();
160     long elapsed2 = stop - start;
161     System.out.println(iterations + " x String.format took " + elapsed2
162             + "ms");
163     assertTrue(elapsed2 > elapsed1);
164   }
165 }