JAL-2374 unit test for (modified) Alignment.findGroup()
[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
25 import jalview.gui.JvOptionPane;
26
27 import org.testng.annotations.BeforeClass;
28 import org.testng.annotations.Test;
29
30 public class FormatTest
31 {
32
33   @BeforeClass(alwaysRun = true)
34   public void setUpJvOptionPane()
35   {
36     JvOptionPane.setInteractiveMode(false);
37     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
38   }
39
40   @Test(groups = "Functional")
41   public void testAppendPercentage()
42   {
43     StringBuilder sb = new StringBuilder();
44     Format.appendPercentage(sb, 123.436f, 0);
45     assertEquals(sb.toString(), "123");
46
47     sb.setLength(0);
48     Format.appendPercentage(sb, 123.536f, 0);
49     assertEquals(sb.toString(), "124");
50
51     sb.setLength(0);
52     Format.appendPercentage(sb, 799.536f, 0);
53     assertEquals(sb.toString(), "800");
54
55     sb.setLength(0);
56     Format.appendPercentage(sb, 123.436f, 1);
57     assertEquals(sb.toString(), "123.4");
58
59     sb.setLength(0);
60     Format.appendPercentage(sb, 123.436f, 2);
61     assertEquals(sb.toString(), "123.44");
62
63     sb.setLength(0);
64     Format.appendPercentage(sb, 123.436f, 3);
65     assertEquals(sb.toString(), "123.436");
66
67     sb.setLength(0);
68     Format.appendPercentage(sb, 123.436f, 4);
69     assertEquals(sb.toString(), "123.4360");
70   }
71
72   @Test(groups = "Functional")
73   public void testForm_float()
74   {
75     Format f = new Format("%3.2f");
76     assertEquals(f.form(123f), "123.00");
77     assertEquals(f.form(123.1f), "123.10");
78     assertEquals(f.form(123.12f), "123.12");
79     assertEquals(f.form(123.124f), "123.12");
80     assertEquals(f.form(123.125f), "123.13");
81     assertEquals(f.form(123.126f), "123.13");
82
83     f = new Format("%3.0f");
84     assertEquals(f.form(123f), "123.");
85     assertEquals(f.form(12f), "12.");
86     assertEquals(f.form(123.4f), "123.");
87     assertEquals(f.form(123.5f), "124.");
88     assertEquals(f.form(123.6f), "124.");
89     assertEquals(f.form(129.6f), "130.");
90   }
91
92   @Test(groups = "Functional")
93   public void testRepeat()
94   {
95     assertEquals(Format.repeat('a', 3), "aaa");
96     assertEquals(Format.repeat('b', 0), "");
97     assertEquals(Format.repeat('c', -1), "");
98   }
99 }