e2b01871d7909a840348c9a68692a094f84f20c9
[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 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
14
15 import java.math.BigDecimal;
16 import org.junit.Test;
17
18 public class Decimal64Test {
19     @Test
20     public void testParseEmpty() {
21         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf(""));
22     }
23
24     @Test
25     public void testParseSingleIllegal() {
26         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("a"));
27     }
28
29     @Test
30     public void testParseSingleHighIllegal() {
31         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf(":"));
32     }
33
34     @Test
35     public void testParseZeroIllegal() {
36         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0a"));
37     }
38
39     @Test
40     public void testParseZeroHighIllegal() {
41         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0:"));
42     }
43
44     @Test
45     public void testParseZeroPointIllegal() {
46         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.a"));
47     }
48
49     @Test
50     public void testParseZeroPointHighIllegal() {
51         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.:"));
52     }
53
54     @Test
55     public void testParsePointIllegal() {
56         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf(".a"));
57     }
58
59     @Test
60     public void testParseMinus() {
61         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("-"));
62     }
63
64     @Test
65     public void testParsePlus() {
66         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("+"));
67     }
68
69     @Test
70     public void testParsePeriod() {
71         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("."));
72     }
73
74     @Test
75     public void testParseTwoPeriods() {
76         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf(".."));
77     }
78
79     @Test
80     public void testParseTrailingPeriod() {
81         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0."));
82     }
83
84     @Test
85     public void testParseMultiplePeriods() {
86         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.1."));
87     }
88
89     @Test
90     public void testParseLongString() {
91         Decimal64.valueOf("123456789012345678");
92     }
93
94     @Test
95     public void testParseLongDecimal() {
96         Decimal64.valueOf("0.12345678901234568");
97     }
98
99     @Test
100     public void testFractionLimits() {
101         Decimal64.valueOf("922337203685477580.7");
102         Decimal64.valueOf("9.223372036854775807");
103
104         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("922337203685477580.71"));
105         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("9.2233720368547758071"));
106     }
107
108     @Test
109     public void testParseTooLongString() {
110         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("1234567890123456789"));
111     }
112
113     @Test
114     public void testParseTooLongDecimal() {
115         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.1234567890123456789"));
116     }
117
118     @Test
119     public void testParse() {
120         assertParsedVariants("0", 0, 0, 1);
121         assertParsedVariants("0.00", 0, 0, 1);
122         assertParsedVariants("00.0", 0, 0, 1);
123         assertParsedVariants("000.0", 0, 0, 1);
124         assertCanonicalVariants("10.0", 10, 0, 1);
125         assertCanonicalVariants("10.09", 10, 9, 2);
126         assertParsedVariants("10.0900900", 10, 9009, 5);
127         assertParsedVariants("0002210.09", 2210, 9, 2);
128
129         Decimal64 parsed = assertParsedString("0.0", 0, 0, 1, false);
130         parsed = assertParsedString("+0.0", 0, 0, 1, false);
131         assertEquals("0.0", parsed.toString());
132         assertEquals("0.0", parsed.toString());
133         parsed = assertParsedString("-0.0", 0, 0, 1, true);
134         assertEquals("0.0", parsed.toString());
135
136         assertCanonicalVariants("1.0", 1, 0, 1);
137         assertCanonicalVariants("2.3", 2, 3, 1);
138     }
139
140     @Test
141     public void testCompare() {
142         final Decimal64 one = Decimal64.valueOf("1");
143         final Decimal64 two = Decimal64.valueOf("2");
144         final Decimal64 three = Decimal64.valueOf("3");
145         final Decimal64 negOne = Decimal64.valueOf("-1");
146         final Decimal64 anotherOne = Decimal64.valueOf("1");
147
148         assertEquals(0, one.compareTo(one));
149         assertEquals(0, one.compareTo(anotherOne));
150         assertEquals(-1, one.compareTo(two));
151         assertEquals(-1, one.compareTo(three));
152         assertEquals(1, one.compareTo(negOne));
153
154         assertEquals(1, two.compareTo(one));
155         assertEquals(1, two.compareTo(anotherOne));
156         assertEquals(-1, two.compareTo(three));
157         assertEquals(1, one.compareTo(negOne));
158     }
159
160     @Test
161     public void testEquals() {
162         final Decimal64 one = Decimal64.valueOf("1");
163         final Decimal64 two = Decimal64.valueOf("2");
164         final Decimal64 anotherOne = Decimal64.valueOf("1");
165
166         assertTrue(one.equals(one));
167         assertTrue(one.equals(anotherOne));
168         assertFalse(one.equals(two));
169         assertTrue(two.equals(two));
170         assertFalse(two.equals(one));
171
172         assertFalse(one.equals(new Object()));
173     }
174
175     @Test
176     public void testConversions() {
177         assertEquals(new BigDecimal("0.12"), Decimal64.valueOf("0.12").decimalValue());
178         assertEquals(new BigDecimal("-0.12"), Decimal64.valueOf("-0.12").decimalValue());
179         assertEquals(new BigDecimal("0.12"), Decimal64.valueOf("+0.12").decimalValue());
180         assertEquals(new BigDecimal("123.456"), Decimal64.valueOf("123.456").decimalValue());
181         assertEquals(new BigDecimal("-123.456"), Decimal64.valueOf("-123.456").decimalValue());
182
183         assertEquals(0.12, Decimal64.valueOf("0.12").doubleValue(), 0);
184         assertEquals(-0.12, Decimal64.valueOf("-0.12").doubleValue(), 0);
185
186         assertEquals((float) 0.12, Decimal64.valueOf("0.12").floatValue(), 0);
187         assertEquals((float) -0.12, Decimal64.valueOf("-0.12").floatValue(), 0);
188
189         assertEquals(12345678901L, Decimal64.valueOf("12345678901").longValue());
190         assertEquals(-12345678901L, Decimal64.valueOf("-12345678901").longValue());
191     }
192
193     @Test
194     public void testFactories() {
195         assertEquals("0.0", Decimal64.valueOf((byte) 0).toString());
196         assertEquals("1.0", Decimal64.valueOf((byte) 1).toString());
197         assertEquals("-1.0", Decimal64.valueOf((byte) -1).toString());
198
199         assertEquals("0.0", Decimal64.valueOf((short) 0).toString());
200         assertEquals("1.0", Decimal64.valueOf((short) 1).toString());
201         assertEquals("-1.0", Decimal64.valueOf((short) -1).toString());
202
203         assertEquals("0.0", Decimal64.valueOf(0).toString());
204         assertEquals("1.0", Decimal64.valueOf(1).toString());
205         assertEquals("-1.0", Decimal64.valueOf(-1).toString());
206
207         assertEquals("0.0", Decimal64.valueOf(0L).toString());
208         assertEquals("1.0", Decimal64.valueOf(1L).toString());
209         assertEquals("-1.0", Decimal64.valueOf(-1L).toString());
210
211         assertEquals("0.0", Decimal64.valueOf(0.0).toString());
212         assertEquals("1.0", Decimal64.valueOf(1.0).toString());
213         assertEquals("-1.0", Decimal64.valueOf(-1.0).toString());
214
215         assertEquals("0.0", Decimal64.valueOf(BigDecimal.ZERO).toString());
216         assertEquals("1.0", Decimal64.valueOf(BigDecimal.ONE).toString());
217         assertEquals("-1.0", Decimal64.valueOf(BigDecimal.ONE.negate()).toString());
218     }
219
220     @Test
221     public void testBoundaries() {
222         assertEquals(-128L, Decimal64.valueOf(Byte.MIN_VALUE).longValue());
223         assertEquals(127L, Decimal64.valueOf(Byte.MAX_VALUE).longValue());
224         assertEquals(-32768L, Decimal64.valueOf(Short.MIN_VALUE).longValue());
225         assertEquals(32767L, Decimal64.valueOf(Short.MAX_VALUE).longValue());
226         assertEquals(-2147483648L, Decimal64.valueOf(Integer.MIN_VALUE).longValue());
227         assertEquals(2147483647L, Decimal64.valueOf(Integer.MAX_VALUE).longValue());
228     }
229
230     @Test
231     public void testByteValueExact() {
232         assertEquals(Byte.MIN_VALUE, Decimal64.valueOf(Byte.MIN_VALUE).byteValueExact());
233         assertEquals(Byte.MAX_VALUE, Decimal64.valueOf(Byte.MAX_VALUE).byteValueExact());
234     }
235
236     @Test
237     public void testByteValueExactFrac() {
238         final Decimal64 dec = Decimal64.valueOf("1.1");
239         assertThrows(ArithmeticException.class, () -> dec.byteValueExact());
240     }
241
242     @Test
243     public void testByteValueExactRange() {
244         final Decimal64 dec = Decimal64.valueOf(Byte.MAX_VALUE + 1);
245         assertThrows(ArithmeticException.class, () -> dec.byteValueExact());
246     }
247
248     @Test
249     public void testShortValueExact() {
250         assertEquals(Short.MIN_VALUE, Decimal64.valueOf(Short.MIN_VALUE).shortValueExact());
251         assertEquals(Short.MAX_VALUE, Decimal64.valueOf(Short.MAX_VALUE).shortValueExact());
252     }
253
254     @Test
255     public void testShortValueExactFrac() {
256         final Decimal64 dec = Decimal64.valueOf("1.1");
257         assertThrows(ArithmeticException.class, () -> dec.shortValueExact());
258     }
259
260     @Test
261     public void testShortValueExactRange() {
262         final Decimal64 dec = Decimal64.valueOf(Short.MAX_VALUE + 1);
263         assertThrows(ArithmeticException.class, () -> dec.shortValueExact());
264     }
265
266     @Test
267     public void testIntValueExact() {
268         assertEquals(Integer.MIN_VALUE, Decimal64.valueOf(Integer.MIN_VALUE).intValueExact());
269         assertEquals(Integer.MAX_VALUE, Decimal64.valueOf(Integer.MAX_VALUE).intValueExact());
270     }
271
272     @Test
273     public void testIntValueExactFrac() {
274         final Decimal64 dec = Decimal64.valueOf("1.1");
275         assertThrows(ArithmeticException.class, () -> dec.intValueExact());
276     }
277
278     @Test
279     public void testIntValueExactRange() {
280         final Decimal64 dec = Decimal64.valueOf(Integer.MAX_VALUE + 1L);
281         assertThrows(ArithmeticException.class, () -> dec.intValueExact());
282     }
283
284     @Test
285     public void testLongValueExactFrac() {
286         final Decimal64 dec = Decimal64.valueOf("1.1");
287         assertThrows(ArithmeticException.class, () -> dec.longValueExact());
288     }
289
290     private static void assertCanonicalVariants(final String str, final long intPart, final long fracPart,
291             final int digits) {
292         assertCanonicalString(str, intPart, fracPart, digits, false);
293         assertCanonicalString("-" + str, intPart, fracPart, digits, true);
294
295         final Decimal64 parsed = assertParsedString("+" + str, intPart, fracPart, digits, false);
296         assertEquals(str, parsed.toString());
297     }
298
299     private static void assertParsedVariants(final String str, final long intPart, final long fracPart,
300             final int digits) {
301         assertParsedString(str, intPart, fracPart, digits, false);
302         assertParsedString("-" + str, intPart, fracPart, digits, true);
303         assertParsedString("+" + str, intPart, fracPart, digits, false);
304     }
305
306     private static void assertCanonicalString(final String str, final long intPart, final long fracPart,
307             final int digits, final boolean negative) {
308         final Decimal64 parsed = assertParsedString(str, intPart, fracPart, digits, negative);
309         assertEquals(str, parsed.toString());
310     }
311
312     private static Decimal64 assertParsedString(final String str, final long intPart, final long fracPart,
313             final int digits, final boolean negative) {
314         final Decimal64 parsed = Decimal64.valueOf(str);
315         assertEquals(new Decimal64((byte) digits, intPart, fracPart, negative), parsed);
316         return parsed;
317     }
318 }