JAL-2325 apply license to new source files
[jalview.git] / test / jalview / ext / android / SparseIntArrayTest.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.ext.android;
22
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.fail;
25
26 import org.testng.annotations.Test;
27
28 /*
29  * Tests for SparseIntArray. Unlike SparseShortArray, SparseIntArray does not throw
30  * any exception for overflow.
31  */
32 public class SparseIntArrayTest
33 {
34   @Test(groups = "Functional")
35   public void testPut()
36   {
37     SparseIntArray counter = new SparseIntArray();
38
39     /*
40      * either key or value may be in the range of int
41      */
42     counter.put(Integer.MAX_VALUE, Integer.MIN_VALUE);
43     counter.put(Integer.MIN_VALUE, Integer.MAX_VALUE);
44     assertEquals(counter.get(Integer.MAX_VALUE), Integer.MIN_VALUE);
45     assertEquals(counter.get(Integer.MIN_VALUE), Integer.MAX_VALUE);
46   }
47
48   @Test(groups = "Functional")
49   public void testAdd()
50   {
51     SparseIntArray counter = new SparseIntArray();
52   
53     assertEquals(counter.add('P', 2), 2);
54     assertEquals(counter.add('P', 3), 5);
55     counter.put('Q', 7);
56     assertEquals(counter.add('Q', 4), 11);
57
58     counter.put('x', Integer.MAX_VALUE);
59     try
60     {
61       counter.add('x', 1);
62       fail("expected exception");
63     } catch (ArithmeticException e)
64     {
65       // expected
66     }
67   
68     counter.put('y', Integer.MIN_VALUE);
69     try
70     {
71       counter.add('y', -1);
72       fail("expected exception");
73     } catch (ArithmeticException e)
74     {
75       // expected
76     }
77   }
78
79   @Test(groups = "Functional")
80   public void testCheckOverflow()
81   {
82     // things that don't overflow:
83     SparseIntArray.checkOverflow(Integer.MAX_VALUE, 0);
84     SparseIntArray.checkOverflow(Integer.MAX_VALUE, -1);
85     SparseIntArray.checkOverflow(Integer.MAX_VALUE, Integer.MIN_VALUE);
86     SparseIntArray.checkOverflow(Integer.MAX_VALUE, -Integer.MAX_VALUE);
87     SparseIntArray.checkOverflow(0, -Integer.MAX_VALUE);
88     SparseIntArray.checkOverflow(0, Integer.MIN_VALUE);
89     SparseIntArray.checkOverflow(Integer.MIN_VALUE, 0);
90     SparseIntArray.checkOverflow(Integer.MIN_VALUE, 1);
91     SparseIntArray.checkOverflow(Integer.MIN_VALUE, Integer.MAX_VALUE);
92
93     // and some that do
94     try
95     {
96       SparseIntArray.checkOverflow(Integer.MAX_VALUE, 1);
97       fail("expected exception");
98     } catch (ArithmeticException e)
99     {
100       // expected
101     }
102     try
103     {
104       SparseIntArray.checkOverflow(Integer.MAX_VALUE - 1, 2);
105       fail("expected exception");
106     } catch (ArithmeticException e)
107     {
108       // expected
109     }
110     try
111     {
112       SparseIntArray.checkOverflow(1, Integer.MAX_VALUE);
113       fail("expected exception");
114     } catch (ArithmeticException e)
115     {
116       // expected
117     }
118     try
119     {
120       SparseIntArray.checkOverflow(Integer.MIN_VALUE, -1);
121       fail("expected exception");
122     } catch (ArithmeticException e)
123     {
124       // expected
125     }
126     try
127     {
128       SparseIntArray.checkOverflow(Integer.MIN_VALUE + 1, -2);
129       fail("expected exception");
130     } catch (ArithmeticException e)
131     {
132       // expected
133     }
134     try
135     {
136       SparseIntArray.checkOverflow(-1, Integer.MIN_VALUE);
137       fail("expected exception");
138     } catch (ArithmeticException e)
139     {
140       // expected
141     }
142   }
143
144 }