Migrate yang-common to JUnit5
[yangtools.git] / common / yang-common / src / test / java / org / opendaylight / yangtools / yang / common / Decimal64Test.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.common;
9
10
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12 import static org.junit.jupiter.api.Assertions.assertFalse;
13 import static org.junit.jupiter.api.Assertions.assertThrows;
14 import static org.junit.jupiter.api.Assertions.assertTrue;
15
16 import java.math.BigDecimal;
17 import java.math.RoundingMode;
18 import org.junit.jupiter.api.Test;
19
20 public class Decimal64Test {
21     @Test
22     public void testParseEmpty() {
23         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf(""));
24     }
25
26     @Test
27     public void testParseSingleIllegal() {
28         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("a"));
29     }
30
31     @Test
32     public void testParseSingleHighIllegal() {
33         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf(":"));
34     }
35
36     @Test
37     public void testParseZeroIllegal() {
38         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0a"));
39     }
40
41     @Test
42     public void testParseZeroHighIllegal() {
43         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0:"));
44     }
45
46     @Test
47     public void testParseZeroPointIllegal() {
48         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.a"));
49     }
50
51     @Test
52     public void testParseZeroPointHighIllegal() {
53         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.:"));
54     }
55
56     @Test
57     public void testParsePointIllegal() {
58         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf(".a"));
59     }
60
61     @Test
62     public void testParseMinus() {
63         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("-"));
64     }
65
66     @Test
67     public void testParsePlus() {
68         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("+"));
69     }
70
71     @Test
72     public void testParsePeriod() {
73         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("."));
74     }
75
76     @Test
77     public void testParseTwoPeriods() {
78         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf(".."));
79     }
80
81     @Test
82     public void testParseTrailingPeriod() {
83         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0."));
84     }
85
86     @Test
87     public void testParseMultiplePeriods() {
88         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.1."));
89     }
90
91     @Test
92     public void testParseLongString() {
93         Decimal64.valueOf("123456789012345678");
94     }
95
96     @Test
97     public void testParseLongDecimal() {
98         Decimal64.valueOf("0.12345678901234568");
99     }
100
101     @Test
102     public void testFractionLimits() {
103         Decimal64.valueOf("922337203685477580.7");
104         Decimal64.valueOf("9.223372036854775807");
105
106         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("922337203685477580.71"));
107         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("9.2233720368547758071"));
108     }
109
110     @Test
111     public void testParseTooLongString() {
112         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("1234567890123456789"));
113     }
114
115     @Test
116     public void testParseTooLongDecimal() {
117         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.1234567890123456789"));
118     }
119
120     @Test
121     public void testParse() {
122         assertParsedVariants("0", 0, 0, 1);
123         assertParsedVariants("0.00", 0, 0, 1);
124         assertParsedVariants("00.0", 0, 0, 1);
125         assertParsedVariants("000.0", 0, 0, 1);
126         assertCanonicalVariants("10.0", 10, 0, 1);
127         assertCanonicalVariants("10.09", 10, 9, 2);
128         assertParsedVariants("10.0900900", 10, 9009, 5);
129         assertParsedVariants("0002210.09", 2210, 9, 2);
130
131         Decimal64 parsed = assertParsedString("0.0", 0, 0, 1, false);
132         parsed = assertParsedString("+0.0", 0, 0, 1, false);
133         assertEquals("0.0", parsed.toString());
134         assertEquals("0.0", parsed.toString());
135         parsed = assertParsedString("-0.0", 0, 0, 1, true);
136         assertEquals("0.0", parsed.toString());
137
138         assertCanonicalVariants("1.0", 1, 0, 1);
139         assertCanonicalVariants("2.3", 2, 3, 1);
140     }
141
142     @Test
143     public void testCompare() {
144         final Decimal64 one = Decimal64.valueOf("1");
145         final Decimal64 two = Decimal64.valueOf("2");
146         final Decimal64 three = Decimal64.valueOf("3");
147         final Decimal64 negOne = Decimal64.valueOf("-1");
148         final Decimal64 anotherOne = Decimal64.valueOf("1");
149
150         assertEquals(0, one.compareTo(one));
151         assertEquals(0, one.compareTo(anotherOne));
152         assertEquals(-1, one.compareTo(two));
153         assertEquals(-1, one.compareTo(three));
154         assertEquals(1, one.compareTo(negOne));
155
156         assertEquals(1, two.compareTo(one));
157         assertEquals(1, two.compareTo(anotherOne));
158         assertEquals(-1, two.compareTo(three));
159         assertEquals(1, one.compareTo(negOne));
160     }
161
162     @Test
163     public void testEquals() {
164         final Decimal64 one = Decimal64.valueOf("1");
165         final Decimal64 two = Decimal64.valueOf("2");
166         final Decimal64 anotherOne = Decimal64.valueOf("1");
167
168         assertTrue(one.equals(one));
169         assertTrue(one.equals(anotherOne));
170         assertFalse(one.equals(two));
171         assertTrue(two.equals(two));
172         assertFalse(two.equals(one));
173
174         assertFalse(one.equals(new Object()));
175     }
176
177     @Test
178     public void testConversions() {
179         assertEquals(new BigDecimal("0.12"), Decimal64.valueOf("0.12").decimalValue());
180         assertEquals(new BigDecimal("-0.12"), Decimal64.valueOf("-0.12").decimalValue());
181         assertEquals(new BigDecimal("0.12"), Decimal64.valueOf("+0.12").decimalValue());
182         assertEquals(new BigDecimal("123.456"), Decimal64.valueOf("123.456").decimalValue());
183         assertEquals(new BigDecimal("-123.456"), Decimal64.valueOf("-123.456").decimalValue());
184
185         assertEquals(0.12, Decimal64.valueOf("0.12").doubleValue(), 0);
186         assertEquals(-0.12, Decimal64.valueOf("-0.12").doubleValue(), 0);
187
188         assertEquals((float) 0.12, Decimal64.valueOf("0.12").floatValue(), 0);
189         assertEquals((float) -0.12, Decimal64.valueOf("-0.12").floatValue(), 0);
190
191         assertEquals(12345678901L, Decimal64.valueOf("12345678901").longValue());
192         assertEquals(-12345678901L, Decimal64.valueOf("-12345678901").longValue());
193     }
194
195     @Test
196     public void testFactories() {
197         assertEquals("0.0", Decimal64.valueOf(1, (byte) 0).toString());
198         assertEquals("1.0", Decimal64.valueOf(1, (byte) 1).toString());
199         assertEquals("-1.0", Decimal64.valueOf(1, (byte) -1).toString());
200
201         assertEquals("0.0", Decimal64.valueOf(1, (short) 0).toString());
202         assertEquals("1.0", Decimal64.valueOf(1, (short) 1).toString());
203         assertEquals("-1.0", Decimal64.valueOf(1, (short) -1).toString());
204
205         assertEquals("0.0", Decimal64.valueOf(1, 0).toString());
206         assertEquals("1.0", Decimal64.valueOf(1, 1).toString());
207         assertEquals("-1.0", Decimal64.valueOf(1, -1).toString());
208
209         assertEquals("0.0", Decimal64.valueOf(1, 0L).toString());
210         assertEquals("1.0", Decimal64.valueOf(1, 1L).toString());
211         assertEquals("-1.0", Decimal64.valueOf(1, -1L).toString());
212
213         assertEquals("0.0", Decimal64.valueOf(0.0F, RoundingMode.UNNECESSARY).toString());
214         assertEquals("1.0", Decimal64.valueOf(1.0F, RoundingMode.UNNECESSARY).toString());
215         assertEquals("-1.0", Decimal64.valueOf(-1.0F, RoundingMode.UNNECESSARY).toString());
216
217         assertEquals("0.0", Decimal64.valueOf(0.0D, RoundingMode.UNNECESSARY).toString());
218         assertEquals("1.0", Decimal64.valueOf(1.0D, RoundingMode.UNNECESSARY).toString());
219         assertEquals("-1.0", Decimal64.valueOf(-1.0D, RoundingMode.UNNECESSARY).toString());
220
221         assertEquals("0.0", Decimal64.valueOf(BigDecimal.ZERO).toString());
222         assertEquals("1.0", Decimal64.valueOf(BigDecimal.ONE).toString());
223         assertEquals("-1.0", Decimal64.valueOf(BigDecimal.ONE.negate()).toString());
224     }
225
226     @Test
227     public void testBoundaries() {
228         assertEquals(-128L, Decimal64.valueOf(1, Byte.MIN_VALUE).longValue());
229         assertEquals(127L, Decimal64.valueOf(1, Byte.MAX_VALUE).longValue());
230         assertEquals(-32768L, Decimal64.valueOf(2, Short.MIN_VALUE).longValue());
231         assertEquals(32767L, Decimal64.valueOf(2, Short.MAX_VALUE).longValue());
232         assertEquals(-2147483648L, Decimal64.valueOf(3, Integer.MIN_VALUE).longValue());
233         assertEquals(2147483647L, Decimal64.valueOf(3, Integer.MAX_VALUE).longValue());
234     }
235
236     @Test
237     public void testByteValueExact() {
238         assertEquals(Byte.MIN_VALUE, Decimal64.valueOf(1, Byte.MIN_VALUE).byteValueExact());
239         assertEquals(Byte.MAX_VALUE, Decimal64.valueOf(1, Byte.MAX_VALUE).byteValueExact());
240     }
241
242     @Test
243     public void testByteValueExactFrac() {
244         final Decimal64 dec = Decimal64.valueOf("1.1");
245         assertThrows(ArithmeticException.class, () -> dec.byteValueExact());
246     }
247
248     @Test
249     public void testByteValueExactRange() {
250         final Decimal64 dec = Decimal64.valueOf(1, Byte.MAX_VALUE + 1);
251         assertThrows(ArithmeticException.class, () -> dec.byteValueExact());
252     }
253
254     @Test
255     public void testShortValueExact() {
256         assertEquals(Short.MIN_VALUE, Decimal64.valueOf(1, Short.MIN_VALUE).shortValueExact());
257         assertEquals(Short.MAX_VALUE, Decimal64.valueOf(1, Short.MAX_VALUE).shortValueExact());
258     }
259
260     @Test
261     public void testShortValueExactFrac() {
262         final Decimal64 dec = Decimal64.valueOf("1.1");
263         assertThrows(ArithmeticException.class, () -> dec.shortValueExact());
264     }
265
266     @Test
267     public void testShortValueExactRange() {
268         final Decimal64 dec = Decimal64.valueOf(1, Short.MAX_VALUE + 1);
269         assertThrows(ArithmeticException.class, () -> dec.shortValueExact());
270     }
271
272     @Test
273     public void testIntValueExact() {
274         assertEquals(Integer.MIN_VALUE, Decimal64.valueOf(1, Integer.MIN_VALUE).intValueExact());
275         assertEquals(Integer.MAX_VALUE, Decimal64.valueOf(1, Integer.MAX_VALUE).intValueExact());
276     }
277
278     @Test
279     public void testIntValueExactFrac() {
280         final Decimal64 dec = Decimal64.valueOf("1.1");
281         assertThrows(ArithmeticException.class, () -> dec.intValueExact());
282     }
283
284     @Test
285     public void testIntValueExactRange() {
286         final Decimal64 dec = Decimal64.valueOf(1, Integer.MAX_VALUE + 1L);
287         assertThrows(ArithmeticException.class, () -> dec.intValueExact());
288     }
289
290     @Test
291     public void testLongValueExactFrac() {
292         final Decimal64 dec = Decimal64.valueOf("1.1");
293         assertThrows(ArithmeticException.class, () -> dec.longValueExact());
294     }
295
296     @Test
297     public void testLongValueOfBits() {
298         final Decimal64 dec = Decimal64.valueOf(2, 25552555555L);
299         assertEquals(2, dec.scale());
300         assertEquals(2555255555500L, dec.unscaledValue());
301     }
302
303     @Test
304     public void testLongValueOfNegativeBits() {
305         final Decimal64 dec = Decimal64.valueOf(2, -25552555555L);
306         assertEquals(2, dec.scale());
307         assertEquals(-2555255555500L, dec.unscaledValue());
308     }
309
310     @Test
311     public void testByteRange() {
312         for (int i = 1; i <= 16; ++i) {
313             assertEquals(Byte.MIN_VALUE, Decimal64.valueOf(i, Byte.MIN_VALUE).byteValueExact());
314             assertEquals(Byte.MAX_VALUE, Decimal64.valueOf(i, Byte.MAX_VALUE).byteValueExact());
315         }
316         for (int i = 17; i <= 18; ++i) {
317             int scale = i;
318             assertThrows(IllegalArgumentException.class, () -> Decimal64.valueOf(scale, Byte.MIN_VALUE));
319             assertThrows(IllegalArgumentException.class, () -> Decimal64.valueOf(scale, Byte.MAX_VALUE));
320         }
321     }
322
323     @Test
324     public void testShortRange() {
325         for (int i = 1; i <= 14; ++i) {
326             assertEquals(Short.MIN_VALUE, Decimal64.valueOf(i, Short.MIN_VALUE).shortValueExact());
327             assertEquals(Short.MAX_VALUE, Decimal64.valueOf(i, Short.MAX_VALUE).shortValueExact());
328         }
329         for (int i = 15; i <= 18; ++i) {
330             int scale = i;
331             assertThrows(IllegalArgumentException.class, () -> Decimal64.valueOf(scale, Short.MIN_VALUE));
332             assertThrows(IllegalArgumentException.class, () -> Decimal64.valueOf(scale, Short.MAX_VALUE));
333         }
334     }
335
336     @Test
337     public void testIntRange() {
338         for (int i = 1; i <= 9; ++i) {
339             assertEquals(Integer.MIN_VALUE, Decimal64.valueOf(i, Integer.MIN_VALUE).intValueExact());
340             assertEquals(Integer.MAX_VALUE, Decimal64.valueOf(i, Integer.MAX_VALUE).intValueExact());
341         }
342         for (int i = 10; i <= 18; ++i) {
343             int scale = i;
344             assertThrows(IllegalArgumentException.class, () -> Decimal64.valueOf(scale, Integer.MIN_VALUE));
345             assertThrows(IllegalArgumentException.class, () -> Decimal64.valueOf(scale, Integer.MAX_VALUE));
346         }
347     }
348
349     @Test
350     public void testLongRange() {
351         for (int i = 1; i <= 18; ++i) {
352             int scale = i;
353             assertThrows(IllegalArgumentException.class, () -> Decimal64.valueOf(scale, Long.MIN_VALUE));
354             assertThrows(IllegalArgumentException.class, () -> Decimal64.valueOf(scale, Long.MAX_VALUE));
355         }
356     }
357
358     private static void assertCanonicalVariants(final String str, final long intPart, final long fracPart,
359             final int digits) {
360         assertCanonicalString(str, intPart, fracPart, digits, false);
361         assertCanonicalString("-" + str, intPart, fracPart, digits, true);
362
363         final Decimal64 parsed = assertParsedString("+" + str, intPart, fracPart, digits, false);
364         assertEquals(str, parsed.toString());
365     }
366
367     private static void assertParsedVariants(final String str, final long intPart, final long fracPart,
368             final int digits) {
369         assertParsedString(str, intPart, fracPart, digits, false);
370         assertParsedString("-" + str, intPart, fracPart, digits, true);
371         assertParsedString("+" + str, intPart, fracPart, digits, false);
372     }
373
374     private static void assertCanonicalString(final String str, final long intPart, final long fracPart,
375             final int digits, final boolean negative) {
376         final Decimal64 parsed = assertParsedString(str, intPart, fracPart, digits, negative);
377         assertEquals(str, parsed.toString());
378     }
379
380     private static Decimal64 assertParsedString(final String str, final long intPart, final long fracPart,
381             final int digits, final boolean negative) {
382         final Decimal64 parsed = Decimal64.valueOf(str);
383         assertEquals(new Decimal64((byte) digits, intPart, fracPart, negative), parsed);
384         return parsed;
385     }
386 }