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    *      https://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.apache.commons.lang3.LangAssertions.assertNullPointerException;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotEquals;
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  class MutableShortTest extends AbstractLangTest {
32  
33      @Test
34      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      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      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      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      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          assertNullPointerException(() -> mutNum.compareTo(null));
75      }
76  
77      @Test
78      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          assertNullPointerException(() -> new MutableShort((Number) null));
89      }
90  
91      @Test
92      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     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     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     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     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     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     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     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().get());
171         assertEquals(Short.valueOf((short) 0), new MutableShort().getValue());
172 
173         mutNum.setValue((short) 1);
174         assertEquals((short) 1, mutNum.shortValue());
175         assertEquals(Short.valueOf((short) 1), mutNum.get());
176         assertEquals(Short.valueOf((short) 1), mutNum.getValue());
177 
178         mutNum.setValue(Short.valueOf((short) 2));
179         assertEquals((short) 2, mutNum.shortValue());
180         assertEquals(Short.valueOf((short) 2), mutNum.get());
181         assertEquals(Short.valueOf((short) 2), mutNum.getValue());
182 
183         mutNum.setValue(new MutableShort((short) 3));
184         assertEquals((short) 3, mutNum.shortValue());
185         assertEquals(Short.valueOf((short) 3), mutNum.get());
186         assertEquals(Short.valueOf((short) 3), mutNum.getValue());
187         assertNullPointerException(() -> mutNum.setValue(null));
188     }
189 
190     @Test
191     void testHashCode() {
192         final MutableShort mutNumA = new MutableShort((short) 0);
193         final MutableShort mutNumB = new MutableShort((short) 0);
194         final MutableShort mutNumC = new MutableShort((short) 1);
195 
196         assertEquals(mutNumA.hashCode(), mutNumA.hashCode());
197         assertEquals(mutNumA.hashCode(), mutNumB.hashCode());
198         assertNotEquals(mutNumA.hashCode(), mutNumC.hashCode());
199         assertEquals(mutNumA.hashCode(), Short.valueOf((short) 0).hashCode());
200     }
201 
202     @Test
203     void testIncrement() {
204         final MutableShort mutNum = new MutableShort((short) 1);
205         mutNum.increment();
206 
207         assertEquals(2, mutNum.intValue());
208         assertEquals(2L, mutNum.longValue());
209     }
210 
211     @Test
212     void testIncrementAndGet() {
213         final MutableShort mutNum = new MutableShort((short) 1);
214         final short result = mutNum.incrementAndGet();
215 
216         assertEquals(2, result);
217         assertEquals(2, mutNum.intValue());
218         assertEquals(2L, mutNum.longValue());
219     }
220 
221     @Test
222     void testPrimitiveValues() {
223         final MutableShort mutNum = new MutableShort((short) 1);
224         assertEquals(1.0F, mutNum.floatValue());
225         assertEquals(1.0, mutNum.doubleValue());
226         assertEquals((byte) 1, mutNum.byteValue());
227         assertEquals((short) 1, mutNum.shortValue());
228         assertEquals(1, mutNum.intValue());
229         assertEquals(1L, mutNum.longValue());
230     }
231 
232     @Test
233     void testSubtractValueObject() {
234         final MutableShort mutNum = new MutableShort((short) 1);
235         mutNum.subtract(Short.valueOf((short) 1));
236 
237         assertEquals((short) 0, mutNum.shortValue());
238     }
239 
240     @Test
241     void testSubtractValuePrimitive() {
242         final MutableShort mutNum = new MutableShort((short) 1);
243         mutNum.subtract((short) 1);
244 
245         assertEquals((short) 0, mutNum.shortValue());
246     }
247 
248     @Test
249     void testToShort() {
250         assertEquals(Short.valueOf((short) 0), new MutableShort((short) 0).toShort());
251         assertEquals(Short.valueOf((short) 123), new MutableShort((short) 123).toShort());
252     }
253 
254     @Test
255     void testToString() {
256         assertEquals("0", new MutableShort((short) 0).toString());
257         assertEquals("10", new MutableShort((short) 10).toString());
258         assertEquals("-123", new MutableShort((short) -123).toString());
259     }
260 
261 }