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