JAL-3261 tidy up get/set properties in Cache, Javadoc, tests
[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 java.awt.Color;
29
30 import org.testng.annotations.BeforeClass;
31 import org.testng.annotations.Test;
32
33 public class FormatTest
34 {
35
36   @BeforeClass(alwaysRun = true)
37   public void setUpJvOptionPane()
38   {
39     JvOptionPane.setInteractiveMode(false);
40     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
41   }
42
43   @Test(groups = "Functional")
44   public void testAppendPercentage()
45   {
46     StringBuilder sb = new StringBuilder();
47     Format.appendPercentage(sb, 123.436f, 0);
48     assertEquals(sb.toString(), "123");
49
50     sb.setLength(0);
51     Format.appendPercentage(sb, 123.536f, 0);
52     assertEquals(sb.toString(), "124");
53
54     sb.setLength(0);
55     Format.appendPercentage(sb, 799.536f, 0);
56     assertEquals(sb.toString(), "800");
57
58     sb.setLength(0);
59     Format.appendPercentage(sb, 123.436f, 1);
60     assertEquals(sb.toString(), "123.4");
61
62     sb.setLength(0);
63     Format.appendPercentage(sb, 123.436f, 2);
64     assertEquals(sb.toString(), "123.44");
65
66     sb.setLength(0);
67     Format.appendPercentage(sb, 123.436f, 3);
68     assertEquals(sb.toString(), "123.436");
69
70     sb.setLength(0);
71     Format.appendPercentage(sb, 123.436f, 4);
72     assertEquals(sb.toString(), "123.4360");
73   }
74
75   @Test(groups = "Functional")
76   public void testForm_float()
77   {
78     Format f = new Format("%3.2f");
79     assertEquals(f.form(123f), "123.00");
80     assertEquals(f.form(123.1f), "123.10");
81     assertEquals(f.form(123.12f), "123.12");
82     assertEquals(f.form(123.124f), "123.12");
83     assertEquals(f.form(123.125f), "123.13");
84     assertEquals(f.form(123.126f), "123.13");
85
86     f = new Format("%3.0f");
87     assertEquals(f.form(123f), "123.");
88     assertEquals(f.form(12f), "12.");
89     assertEquals(f.form(123.4f), "123.");
90     assertEquals(f.form(123.5f), "124.");
91     assertEquals(f.form(123.6f), "124.");
92     assertEquals(f.form(129.6f), "130.");
93   }
94
95   @Test(groups = "Functional")
96   public void testRepeat()
97   {
98     assertEquals(Format.repeat('a', 3), "aaa");
99     assertEquals(Format.repeat('b', 0), "");
100     assertEquals(Format.repeat('c', -1), "");
101   }
102
103   @Test(groups = "Functional")
104   public void testFormat_scientific()
105   {
106     Format f = new Format("%3.4e");
107     double d = 1d;
108     assertEquals(f.form(d), "1.0000e+000");
109     assertEquals(String.format("%3.4e", d), "1.0000e+00");
110
111     d = 12345678.12345678d;
112     assertEquals(f.form(d), "1.2346e+007");
113     assertEquals(String.format("%3.4e", d), "1.2346e+07");
114   }
115
116   /**
117    * Test that fails (in 2.10.1) with timeout as there is an infinite loop in
118    * Format.exp_format()
119    */
120   @Test(groups = "Functional", timeOut = 500)
121   public void testFormat_scientific_overflow()
122   {
123     Format f = new Format("%3.4e");
124     double d = 1.12E-310;
125     /*
126      * problem: exp_format() scales up 'd' to the range 1-10
127      * while computing a scaling factor, but this factor acquires
128      * the value Double.POSITIVE_INFINITY instead of 1.0E+309
129      * the value to be formatted is multipled by factor and becomes Infinity
130      * resulting in an infinite loop in the recursive call to exp_format()
131      */
132     assertEquals(f.form(d), "1.1200e-310");
133   }
134
135   /**
136    * This test shows that Format.form() is faster for this case than
137    * String.format()
138    */
139   @Test(groups = "Timing")
140   public void testFormat_scientificTiming()
141   {
142     Format f = new Format("%3.4e");
143     double d = 12345678.12345678d;
144
145     int iterations = 1000;
146     long start = System.currentTimeMillis();
147     for (int i = 0; i < iterations; i++)
148     {
149       f.form(d);
150     }
151     long stop = System.currentTimeMillis();
152     long elapsed1 = stop - start;
153     System.out.println(iterations + " x Format.form took " + elapsed1
154             + "ms");
155
156     start = System.currentTimeMillis();
157     for (int i = 0; i < iterations; i++)
158     {
159       String.format("%3.4e", d);
160     }
161     stop = System.currentTimeMillis();
162     long elapsed2 = stop - start;
163     System.out.println(iterations + " x String.format took " + elapsed2
164             + "ms");
165     assertTrue(elapsed2 > elapsed1);
166   }
167
168   @Test(groups = "Functional")
169   public void testGetHexString()
170   {
171     /*
172      * r = 13 = d base 16, gets padded to 0d
173      * g = 103 = (16x6) + 7 = 67 base 16
174      * b = 219 = (16x13) + 11 = db base 16
175      */
176     Color c = new Color(13, 103, 219);
177     assertEquals(Format.getHexString(c), "0d67db");
178
179     assertEquals(Format.getHexString(Color.black), "000000");
180
181     assertEquals(Format.getHexString(Color.white), "ffffff");
182   }
183 }