View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3.mutable;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import org.apache.commons.lang3.AbstractLangTest;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * JUnit tests.
28   *
29   * @see MutableShort
30   */
31  public class MutableShortTest extends AbstractLangTest {
32  
33      @Test
34      public void testAddAndGetValueObject() {
35          final MutableShort mutableShort = new MutableShort((short) 0);
36          final short result = mutableShort.addAndGet(Short.valueOf((short) 1));
37  
38          assertEquals((short) 1, result);
39          assertEquals((short) 1, mutableShort.shortValue());
40      }
41  
42      @Test
43      public void testAddAndGetValuePrimitive() {
44          final MutableShort mutableShort = new MutableShort((short) 0);
45          final short result = mutableShort.addAndGet((short) 1);
46  
47          assertEquals((short) 1, result);
48          assertEquals((short) 1, mutableShort.shortValue());
49      }
50  
51      @Test
52      public void testAddValueObject() {
53          final MutableShort mutNum = new MutableShort((short) 1);
54          mutNum.add(Short.valueOf((short) 1));
55  
56          assertEquals((short) 2, mutNum.shortValue());
57      }
58  
59      @Test
60      public void testAddValuePrimitive() {
61          final MutableShort mutNum = new MutableShort((short) 1);
62          mutNum.add((short) 1);
63  
64          assertEquals((short) 2, mutNum.shortValue());
65      }
66  
67      @Test
68      public void testCompareTo() {
69          final MutableShort mutNum = new MutableShort((short) 0);
70  
71          assertEquals((short) 0, mutNum.compareTo(new MutableShort((short) 0)));
72          assertEquals((short) +1, mutNum.compareTo(new MutableShort((short) -1)));
73          assertEquals((short) -1, mutNum.compareTo(new MutableShort((short) 1)));
74          assertThrows(NullPointerException.class, () -> mutNum.compareTo(null));
75      }
76  
77      @Test
78      public void testConstructors() {
79          assertEquals((short) 0, new MutableShort().shortValue());
80  
81          assertEquals((short) 1, new MutableShort((short) 1).shortValue());
82  
83          assertEquals((short) 2, new MutableShort(Short.valueOf((short) 2)).shortValue());
84          assertEquals((short) 3, new MutableShort(new MutableShort((short) 3)).shortValue());
85  
86          assertEquals((short) 2, new MutableShort("2").shortValue());
87  
88          assertThrows(NullPointerException.class, () -> new MutableShort((Number) null));
89      }
90  
91      @Test
92      public void testDecrement() {
93          final MutableShort mutNum = new MutableShort((short) 1);
94          mutNum.decrement();
95  
96          assertEquals(0, mutNum.intValue());
97          assertEquals(0L, mutNum.longValue());
98      }
99  
100     @Test
101     public void testDecrementAndGet() {
102         final MutableShort mutNum = new MutableShort((short) 1);
103         final short result = mutNum.decrementAndGet();
104 
105         assertEquals(0, result);
106         assertEquals(0, mutNum.intValue());
107         assertEquals(0L, mutNum.longValue());
108     }
109 
110     @Test
111     public void testEquals() {
112         final MutableShort mutNumA = new MutableShort((short) 0);
113         final MutableShort mutNumB = new MutableShort((short) 0);
114         final MutableShort mutNumC = new MutableShort((short) 1);
115 
116         assertEquals(mutNumA, mutNumA);
117         assertEquals(mutNumA, mutNumB);
118         assertEquals(mutNumB, mutNumA);
119         assertEquals(mutNumB, mutNumB);
120         assertNotEquals(mutNumA, mutNumC);
121         assertNotEquals(mutNumB, mutNumC);
122         assertEquals(mutNumC, mutNumC);
123         assertNotEquals(null, mutNumA);
124         assertNotEquals(mutNumA, Short.valueOf((short) 0));
125         assertNotEquals("0", mutNumA);
126     }
127 
128     @Test
129     public void testGetAndAddValueObject() {
130         final MutableShort mutableShort = new MutableShort((short) 0);
131         final short result = mutableShort.getAndAdd(Short.valueOf((short) 1));
132 
133         assertEquals((short) 0, result);
134         assertEquals((short) 1, mutableShort.shortValue());
135     }
136 
137     @Test
138     public void testGetAndAddValuePrimitive() {
139         final MutableShort mutableShort = new MutableShort((short) 0);
140         final short result = mutableShort.getAndAdd((short) 1);
141 
142         assertEquals((short) 0, result);
143         assertEquals((short) 1, mutableShort.shortValue());
144     }
145 
146     @Test
147     public void testGetAndDecrement() {
148         final MutableShort mutNum = new MutableShort((short) 1);
149         final short result = mutNum.getAndDecrement();
150 
151         assertEquals(1, result);
152         assertEquals(0, mutNum.intValue());
153         assertEquals(0L, mutNum.longValue());
154     }
155 
156     @Test
157     public void testGetAndIncrement() {
158         final MutableShort mutNum = new MutableShort((short) 1);
159         final short result = mutNum.getAndIncrement();
160 
161         assertEquals(1, result);
162         assertEquals(2, mutNum.intValue());
163         assertEquals(2L, mutNum.longValue());
164     }
165 
166     @Test
167     public void testGetSet() {
168         final MutableShort mutNum = new MutableShort((short) 0);
169         assertEquals((short) 0, new MutableShort().shortValue());
170         assertEquals(Short.valueOf((short) 0), new MutableShort().getValue());
171 
172         mutNum.setValue((short) 1);
173         assertEquals((short) 1, mutNum.shortValue());
174         assertEquals(Short.valueOf((short) 1), mutNum.getValue());
175 
176         mutNum.setValue(Short.valueOf((short) 2));
177         assertEquals((short) 2, mutNum.shortValue());
178         assertEquals(Short.valueOf((short) 2), mutNum.getValue());
179 
180         mutNum.setValue(new MutableShort((short) 3));
181         assertEquals((short) 3, mutNum.shortValue());
182         assertEquals(Short.valueOf((short) 3), mutNum.getValue());
183         assertThrows(NullPointerException.class, () -> mutNum.setValue(null));
184     }
185 
186     @Test
187     public void testHashCode() {
188         final MutableShort mutNumA = new MutableShort((short) 0);
189         final MutableShort mutNumB = new MutableShort((short) 0);
190         final MutableShort mutNumC = new MutableShort((short) 1);
191 
192         assertEquals(mutNumA.hashCode(), mutNumA.hashCode());
193         assertEquals(mutNumA.hashCode(), mutNumB.hashCode());
194         assertNotEquals(mutNumA.hashCode(), mutNumC.hashCode());
195         assertEquals(mutNumA.hashCode(), Short.valueOf((short) 0).hashCode());
196     }
197 
198     @Test
199     public void testIncrement() {
200         final MutableShort mutNum = new MutableShort((short) 1);
201         mutNum.increment();
202 
203         assertEquals(2, mutNum.intValue());
204         assertEquals(2L, mutNum.longValue());
205     }
206 
207     @Test
208     public void testIncrementAndGet() {
209         final MutableShort mutNum = new MutableShort((short) 1);
210         final short result = mutNum.incrementAndGet();
211 
212         assertEquals(2, result);
213         assertEquals(2, mutNum.intValue());
214         assertEquals(2L, mutNum.longValue());
215     }
216 
217     @Test
218     public void testPrimitiveValues() {
219         final MutableShort mutNum = new MutableShort( (short) 1 );
220         assertEquals(1.0F, mutNum.floatValue());
221         assertEquals(1.0, mutNum.doubleValue());
222         assertEquals( (byte) 1, mutNum.byteValue() );
223         assertEquals( (short) 1, mutNum.shortValue() );
224         assertEquals( 1, mutNum.intValue() );
225         assertEquals( 1L, mutNum.longValue() );
226     }
227 
228     @Test
229     public void testSubtractValueObject() {
230         final MutableShort mutNum = new MutableShort((short) 1);
231         mutNum.subtract(Short.valueOf((short) 1));
232 
233         assertEquals((short) 0, mutNum.shortValue());
234     }
235 
236     @Test
237     public void testSubtractValuePrimitive() {
238         final MutableShort mutNum = new MutableShort((short) 1);
239         mutNum.subtract((short) 1);
240 
241         assertEquals((short) 0, mutNum.shortValue());
242     }
243 
244     @Test
245     public void testToShort() {
246         assertEquals(Short.valueOf((short) 0), new MutableShort((short) 0).toShort());
247         assertEquals(Short.valueOf((short) 123), new MutableShort((short) 123).toShort());
248     }
249 
250     @Test
251     public void testToString() {
252         assertEquals("0", new MutableShort((short) 0).toString());
253         assertEquals("10", new MutableShort((short) 10).toString());
254         assertEquals("-123", new MutableShort((short) -123).toString());
255     }
256 
257 }