JAL-2326 added setup method for JvOptionPane in all Jalveiw test classes to enable...
[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 jalview.gui.JvOptionPane;
8
9 import org.testng.annotations.BeforeClass;
10 import org.testng.annotations.Test;
11 public class SparseCountTest
12 {
13
14   @BeforeClass(alwaysRun = true)
15   public void setUpJvOptionPane()
16   {
17     JvOptionPane.setInteractiveMode(false);
18     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
19   }
20
21   @Test(groups = "Functional")
22   public void testAdd()
23   {
24     SparseCount p = new SparseCount(8);
25     p.add('a', 1);
26     p.add('b', 2);
27     p.add('a', 3);
28     p.add('b', -4);
29     assertEquals(p.size(), 2);
30     assertEquals(p.get('a'), 4);
31     assertEquals(p.get('b'), -2);
32   }
33
34   @Test(groups = "Functional")
35   public void testPut()
36   {
37     SparseCount p = new SparseCount(8);
38     p.put('a', 3);
39     p.add('b', 2);
40     p.put('b', 4);
41     assertEquals(p.size(), 2);
42     assertEquals(p.get('a'), 3);
43     assertEquals(p.get('b'), 4);
44   }
45
46   /**
47    * Test handling overflow of short by switching to counting ints
48    */
49   @Test(groups = "Functional")
50   public void testOverflow()
51   {
52     SparseCount p = new SparseCount(8);
53     p.put('a', Short.MAX_VALUE - 1);
54     p.add('a', 1);
55     assertFalse(p.isUsingInt());
56     p.add('a', 1);
57     assertTrue(p.isUsingInt());
58   }
59
60   /**
61    * Test handling underflow of short by switching to counting ints
62    */
63   @Test(groups = "Functional")
64   public void testUnderflow()
65   {
66     SparseCount p = new SparseCount(8);
67     p.put('a', Short.MIN_VALUE + 1);
68     p.add('a', -1);
69     assertFalse(p.isUsingInt());
70     p.add('a', -1);
71     assertTrue(p.isUsingInt());
72   }
73
74   @Test(groups = "Functional")
75   public void testKeyAt_ValueAt()
76   {
77     SparseCount p = new SparseCount(8);
78     p.put('W', 12);
79     p.put('K', 9);
80     p.put('R', 6);
81     assertEquals(p.size(), 3);
82     assertEquals(p.keyAt(0), 'K');
83     assertEquals(p.valueAt(0), 9);
84     assertEquals(p.keyAt(1), 'R');
85     assertEquals(p.valueAt(1), 6);
86     assertEquals(p.keyAt(2), 'W');
87     assertEquals(p.valueAt(2), 12);
88   }
89
90 }