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