JAL-2325 apply license to new source files
[jalview.git] / test / jalview / ext / android / SparseShortArrayTest.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 public class SparseShortArrayTest
29 {
30   @Test(groups = "Functional")
31   public void testPut()
32   {
33     SparseShortArray counter = new SparseShortArray();
34
35     /*
36      * either key or value may be in the range of short
37      */
38     counter.put(Short.MAX_VALUE, Short.MIN_VALUE);
39     counter.put(Short.MIN_VALUE, Short.MAX_VALUE);
40
41     // put a too large value
42     try
43     {
44       counter.put(0, Short.MAX_VALUE + 1);
45       fail("Expected exception");
46     } catch (ArithmeticException e)
47     {
48       // expected;
49     }
50
51     // put a too small value
52     try
53     {
54       counter.put(1, Short.MIN_VALUE - 1);
55       fail("Expected exception");
56     } catch (ArithmeticException e)
57     {
58       // expected;
59     }
60
61     // put a too large key
62     try
63     {
64       counter.put(Short.MAX_VALUE + 1, 0);
65       fail("Expected exception");
66     } catch (ArithmeticException e)
67     {
68       // expected;
69     }
70
71     // put a too small key
72     try
73     {
74       counter.put(Short.MIN_VALUE - 1, 2);
75       fail("Expected exception");
76     } catch (ArithmeticException e)
77     {
78       // expected;
79     }
80   }
81
82   @Test(groups = "Functional")
83   public void testAdd()
84   {
85     SparseShortArray counter = new SparseShortArray();
86   
87     assertEquals(counter.add('P', 2), 2);
88     assertEquals(counter.add('P', 3), 5);
89     counter.put('Q', 7);
90     assertEquals(counter.add('Q', 4), 11);
91
92     // increment giving overflow
93     counter.put('x', Short.MAX_VALUE);
94     try
95     {
96       counter.add('x', 1);
97       fail("Expected exception");
98     } catch (ArithmeticException e)
99     {
100       // expected;
101     }
102   
103     // decrement giving underflow
104     counter.put('y', Short.MIN_VALUE);
105     try
106     {
107       counter.add('y', -1);
108       fail("Expected exception");
109     } catch (ArithmeticException e)
110     {
111       // expected;
112     }
113   }
114 }