JAL-2325 apply license to new source files
[jalview.git] / test / jalview / util / SparseCountTest.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.assertFalse;
25 import static org.testng.Assert.assertTrue;
26
27 import org.testng.annotations.Test;
28 public class SparseCountTest
29 {
30   @Test(groups = "Functional")
31   public void testAdd()
32   {
33     SparseCount p = new SparseCount(8);
34     p.add('a', 1);
35     p.add('b', 2);
36     p.add('a', 3);
37     p.add('b', -4);
38     assertEquals(p.size(), 2);
39     assertEquals(p.get('a'), 4);
40     assertEquals(p.get('b'), -2);
41   }
42
43   @Test(groups = "Functional")
44   public void testPut()
45   {
46     SparseCount p = new SparseCount(8);
47     p.put('a', 3);
48     p.add('b', 2);
49     p.put('b', 4);
50     assertEquals(p.size(), 2);
51     assertEquals(p.get('a'), 3);
52     assertEquals(p.get('b'), 4);
53   }
54
55   /**
56    * Test handling overflow of short by switching to counting ints
57    */
58   @Test(groups = "Functional")
59   public void testOverflow()
60   {
61     SparseCount p = new SparseCount(8);
62     p.put('a', Short.MAX_VALUE - 1);
63     p.add('a', 1);
64     assertFalse(p.isUsingInt());
65     p.add('a', 1);
66     assertTrue(p.isUsingInt());
67   }
68
69   /**
70    * Test handling underflow of short by switching to counting ints
71    */
72   @Test(groups = "Functional")
73   public void testUnderflow()
74   {
75     SparseCount p = new SparseCount(8);
76     p.put('a', Short.MIN_VALUE + 1);
77     p.add('a', -1);
78     assertFalse(p.isUsingInt());
79     p.add('a', -1);
80     assertTrue(p.isUsingInt());
81   }
82
83   @Test(groups = "Functional")
84   public void testKeyAt_ValueAt()
85   {
86     SparseCount p = new SparseCount(8);
87     p.put('W', 12);
88     p.put('K', 9);
89     p.put('R', 6);
90     assertEquals(p.size(), 3);
91     assertEquals(p.keyAt(0), 'K');
92     assertEquals(p.valueAt(0), 9);
93     assertEquals(p.keyAt(1), 'R');
94     assertEquals(p.valueAt(1), 6);
95     assertEquals(p.keyAt(2), 'W');
96     assertEquals(p.valueAt(2), 12);
97   }
98
99 }