2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertTrue;
26 import jalview.gui.JvOptionPane;
28 import java.awt.Color;
30 import org.testng.annotations.BeforeClass;
31 import org.testng.annotations.Test;
33 public class FormatTest
36 @BeforeClass(alwaysRun = true)
37 public void setUpJvOptionPane()
39 JvOptionPane.setInteractiveMode(false);
40 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
43 @Test(groups = "Functional")
44 public void testAppendPercentage()
46 StringBuilder sb = new StringBuilder();
47 Format.appendPercentage(sb, 123.436f, 0);
48 assertEquals(sb.toString(), "123");
51 Format.appendPercentage(sb, 123.536f, 0);
52 assertEquals(sb.toString(), "124");
55 Format.appendPercentage(sb, 799.536f, 0);
56 assertEquals(sb.toString(), "800");
59 Format.appendPercentage(sb, 123.436f, 1);
60 assertEquals(sb.toString(), "123.4");
63 Format.appendPercentage(sb, 123.436f, 2);
64 assertEquals(sb.toString(), "123.44");
67 Format.appendPercentage(sb, 123.436f, 3);
68 assertEquals(sb.toString(), "123.436");
71 Format.appendPercentage(sb, 123.436f, 4);
72 assertEquals(sb.toString(), "123.4360");
75 @Test(groups = "Functional")
76 public void testForm_float()
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");
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.");
95 @Test(groups = "Functional")
96 public void testRepeat()
98 assertEquals(Format.repeat('a', 3), "aaa");
99 assertEquals(Format.repeat('b', 0), "");
100 assertEquals(Format.repeat('c', -1), "");
103 @Test(groups = "Functional")
104 public void testFormat_scientific()
106 Format f = new Format("%3.4e");
108 assertEquals(f.form(d), "1.0000e+000");
109 assertEquals(String.format("%3.4e", d), "1.0000e+00");
111 d = 12345678.12345678d;
112 assertEquals(f.form(d), "1.2346e+007");
113 assertEquals(String.format("%3.4e", d), "1.2346e+07");
117 * Test that fails (in 2.10.1) with timeout as there is an infinite loop in
118 * Format.exp_format()
120 @Test(groups = "Functional", timeOut = 500)
121 public void testFormat_scientific_overflow()
123 Format f = new Format("%3.4e");
124 double d = 1.12E-310;
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()
132 assertEquals(f.form(d), "1.1200e-310");
136 * This test shows that Format.form() is faster for this case than
139 @Test(groups = "Timing")
140 public void testFormat_scientificTiming()
142 Format f = new Format("%3.4e");
143 double d = 12345678.12345678d;
145 int iterations = 1000;
146 long start = System.currentTimeMillis();
147 for (int i = 0; i < iterations; i++)
151 long stop = System.currentTimeMillis();
152 long elapsed1 = stop - start;
153 System.out.println(iterations + " x Format.form took " + elapsed1
156 start = System.currentTimeMillis();
157 for (int i = 0; i < iterations; i++)
159 String.format("%3.4e", d);
161 stop = System.currentTimeMillis();
162 long elapsed2 = stop - start;
163 System.out.println(iterations + " x String.format took " + elapsed2
165 assertTrue(elapsed2 > elapsed1);
168 @Test(groups = "Functional")
169 public void testGetHexString()
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
176 Color c = new Color(13, 103, 219);
177 assertEquals(Format.getHexString(c), "0d67db");
179 assertEquals(Format.getHexString(Color.black), "000000");
181 assertEquals(Format.getHexString(Color.white), "ffffff");