JAL-2326 added setup method for JvOptionPane in all Jalveiw test classes to enable...
[jalview.git] / test / jalview / ext / android / SparseIntArrayTest.java
1 package jalview.ext.android;
2
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.fail;
5
6 import jalview.gui.JvOptionPane;
7
8 import org.testng.annotations.BeforeClass;
9 import org.testng.annotations.Test;
10
11 /*
12  * Tests for SparseIntArray. Unlike SparseShortArray, SparseIntArray does not throw
13  * any exception for overflow.
14  */
15 public class SparseIntArrayTest
16 {
17
18   @BeforeClass(alwaysRun = true)
19   public void setUpJvOptionPane()
20   {
21     JvOptionPane.setInteractiveMode(false);
22     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
23   }
24
25   @Test(groups = "Functional")
26   public void testPut()
27   {
28     SparseIntArray counter = new SparseIntArray();
29
30     /*
31      * either key or value may be in the range of int
32      */
33     counter.put(Integer.MAX_VALUE, Integer.MIN_VALUE);
34     counter.put(Integer.MIN_VALUE, Integer.MAX_VALUE);
35     assertEquals(counter.get(Integer.MAX_VALUE), Integer.MIN_VALUE);
36     assertEquals(counter.get(Integer.MIN_VALUE), Integer.MAX_VALUE);
37   }
38
39   @Test(groups = "Functional")
40   public void testAdd()
41   {
42     SparseIntArray counter = new SparseIntArray();
43   
44     assertEquals(counter.add('P', 2), 2);
45     assertEquals(counter.add('P', 3), 5);
46     counter.put('Q', 7);
47     assertEquals(counter.add('Q', 4), 11);
48
49     counter.put('x', Integer.MAX_VALUE);
50     try
51     {
52       counter.add('x', 1);
53       fail("expected exception");
54     } catch (ArithmeticException e)
55     {
56       // expected
57     }
58   
59     counter.put('y', Integer.MIN_VALUE);
60     try
61     {
62       counter.add('y', -1);
63       fail("expected exception");
64     } catch (ArithmeticException e)
65     {
66       // expected
67     }
68   }
69
70   @Test(groups = "Functional")
71   public void testCheckOverflow()
72   {
73     // things that don't overflow:
74     SparseIntArray.checkOverflow(Integer.MAX_VALUE, 0);
75     SparseIntArray.checkOverflow(Integer.MAX_VALUE, -1);
76     SparseIntArray.checkOverflow(Integer.MAX_VALUE, Integer.MIN_VALUE);
77     SparseIntArray.checkOverflow(Integer.MAX_VALUE, -Integer.MAX_VALUE);
78     SparseIntArray.checkOverflow(0, -Integer.MAX_VALUE);
79     SparseIntArray.checkOverflow(0, Integer.MIN_VALUE);
80     SparseIntArray.checkOverflow(Integer.MIN_VALUE, 0);
81     SparseIntArray.checkOverflow(Integer.MIN_VALUE, 1);
82     SparseIntArray.checkOverflow(Integer.MIN_VALUE, Integer.MAX_VALUE);
83
84     // and some that do
85     try
86     {
87       SparseIntArray.checkOverflow(Integer.MAX_VALUE, 1);
88       fail("expected exception");
89     } catch (ArithmeticException e)
90     {
91       // expected
92     }
93     try
94     {
95       SparseIntArray.checkOverflow(Integer.MAX_VALUE - 1, 2);
96       fail("expected exception");
97     } catch (ArithmeticException e)
98     {
99       // expected
100     }
101     try
102     {
103       SparseIntArray.checkOverflow(1, Integer.MAX_VALUE);
104       fail("expected exception");
105     } catch (ArithmeticException e)
106     {
107       // expected
108     }
109     try
110     {
111       SparseIntArray.checkOverflow(Integer.MIN_VALUE, -1);
112       fail("expected exception");
113     } catch (ArithmeticException e)
114     {
115       // expected
116     }
117     try
118     {
119       SparseIntArray.checkOverflow(Integer.MIN_VALUE + 1, -2);
120       fail("expected exception");
121     } catch (ArithmeticException e)
122     {
123       // expected
124     }
125     try
126     {
127       SparseIntArray.checkOverflow(-1, Integer.MIN_VALUE);
128       fail("expected exception");
129     } catch (ArithmeticException e)
130     {
131       // expected
132     }
133   }
134
135 }