JAL-98 use Profile to store consensus, ResidueCount for fast compact
[jalview.git] / test / jalview / util / SparseCountTest.java
1 package jalview.util;
2
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertFalse;
5 import static org.testng.Assert.assertTrue;
6
7 import org.testng.annotations.Test;
8 public class SparseCountTest
9 {
10   @Test(groups = "Functional")
11   public void testAdd()
12   {
13     SparseCount p = new SparseCount(8);
14     p.add('a', 1);
15     p.add('b', 2);
16     p.add('a', 3);
17     p.add('b', -4);
18     assertEquals(p.size(), 2);
19     assertEquals(p.get('a'), 4);
20     assertEquals(p.get('b'), -2);
21   }
22
23   @Test(groups = "Functional")
24   public void testPut()
25   {
26     SparseCount p = new SparseCount(8);
27     p.put('a', 3);
28     p.add('b', 2);
29     p.put('b', 4);
30     assertEquals(p.size(), 2);
31     assertEquals(p.get('a'), 3);
32     assertEquals(p.get('b'), 4);
33   }
34
35   /**
36    * Test handling overflow of short by switching to counting ints
37    */
38   @Test(groups = "Functional")
39   public void testOverflow()
40   {
41     SparseCount p = new SparseCount(8);
42     p.put('a', Short.MAX_VALUE - 1);
43     p.add('a', 1);
44     assertFalse(p.isUsingInt());
45     p.add('a', 1);
46     assertTrue(p.isUsingInt());
47   }
48
49   /**
50    * Test handling underflow of short by switching to counting ints
51    */
52   @Test(groups = "Functional")
53   public void testUnderflow()
54   {
55     SparseCount p = new SparseCount(8);
56     p.put('a', Short.MIN_VALUE + 1);
57     p.add('a', -1);
58     assertFalse(p.isUsingInt());
59     p.add('a', -1);
60     assertTrue(p.isUsingInt());
61   }
62
63   @Test(groups = "Functional")
64   public void testKeyAt_ValueAt()
65   {
66     SparseCount p = new SparseCount(8);
67     p.put('W', 12);
68     p.put('K', 9);
69     p.put('R', 6);
70     assertEquals(p.size(), 3);
71     assertEquals(p.keyAt(0), 'K');
72     assertEquals(p.valueAt(0), 9);
73     assertEquals(p.keyAt(1), 'R');
74     assertEquals(p.valueAt(1), 6);
75     assertEquals(p.keyAt(2), 'W');
76     assertEquals(p.valueAt(2), 12);
77   }
78
79 }