JAL-2325 apply license to new source files
[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 org.testng.annotations.Test;
26
27 public class FormatTest
28 {
29   @Test(groups = "Functional")
30   public void testAppendPercentage()
31   {
32     StringBuilder sb = new StringBuilder();
33     Format.appendPercentage(sb, 123.436f, 0);
34     assertEquals(sb.toString(), "123");
35
36     sb.setLength(0);
37     Format.appendPercentage(sb, 123.536f, 0);
38     assertEquals(sb.toString(), "124");
39
40     sb.setLength(0);
41     Format.appendPercentage(sb, 799.536f, 0);
42     assertEquals(sb.toString(), "800");
43
44     sb.setLength(0);
45     Format.appendPercentage(sb, 123.436f, 1);
46     assertEquals(sb.toString(), "123.4");
47
48     sb.setLength(0);
49     Format.appendPercentage(sb, 123.436f, 2);
50     assertEquals(sb.toString(), "123.44");
51
52     sb.setLength(0);
53     Format.appendPercentage(sb, 123.436f, 3);
54     assertEquals(sb.toString(), "123.436");
55
56     sb.setLength(0);
57     Format.appendPercentage(sb, 123.436f, 4);
58     assertEquals(sb.toString(), "123.4360");
59   }
60
61   @Test(groups = "Functional")
62   public void testForm_float()
63   {
64     Format f = new Format("%3.2f");
65     assertEquals(f.form(123f), "123.00");
66     assertEquals(f.form(123.1f), "123.10");
67     assertEquals(f.form(123.12f), "123.12");
68     assertEquals(f.form(123.124f), "123.12");
69     assertEquals(f.form(123.125f), "123.13");
70     assertEquals(f.form(123.126f), "123.13");
71
72     f = new Format("%3.0f");
73     assertEquals(f.form(123f), "123.");
74     assertEquals(f.form(12f), "12.");
75     assertEquals(f.form(123.4f), "123.");
76     assertEquals(f.form(123.5f), "124.");
77     assertEquals(f.form(123.6f), "124.");
78     assertEquals(f.form(129.6f), "130.");
79   }
80
81   @Test(groups = "Functional")
82   public void testRepeat()
83   {
84     assertEquals(Format.repeat('a', 3), "aaa");
85     assertEquals(Format.repeat('b', 0), "");
86     assertEquals(Format.repeat('c', -1), "");
87   }
88 }