2c5cf8b9c678df5cd6529d5ecb2ff241ffb6d664
[yangtools.git] / model / yang-model-ri / src / test / java / org / opendaylight / yangtools / yang / model / ri / type / NumberUtilTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.model.ri.type;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
12 import static org.junit.Assert.assertTrue;
13
14 import java.math.BigDecimal;
15 import java.math.BigInteger;
16 import java.util.function.Function;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.Uint16;
19 import org.opendaylight.yangtools.yang.common.Uint32;
20 import org.opendaylight.yangtools.yang.common.Uint64;
21
22 public class NumberUtilTest {
23
24     @Test
25     public void testRangeCoveredForShort() {
26         final short min = 100;
27         final short superMin = 50;
28         final short max = 200;
29         final short superMax = 300;
30
31         assertTrue(NumberUtil.isRangeCovered(min, max, superMin, superMax));
32     }
33
34     @Test
35     public void testRangeCoveredForLong() {
36         final long min = 100L;
37         final long superMin = 50L;
38         final long max = 200L;
39         final long superMax = 300L;
40
41         assertTrue(NumberUtil.isRangeCovered(min, max, superMin, superMax));
42     }
43
44     @Test
45     public void testRangeCoveredForBigDecimal() {
46         final BigDecimal min = new BigDecimal(100.0);
47         final BigDecimal superMin = new BigDecimal(50.0);
48         final BigDecimal max = new BigDecimal(200.0);
49         final BigDecimal superMax = new BigDecimal(300.0);
50
51         assertTrue(NumberUtil.isRangeCovered(min, max, superMin, superMax));
52     }
53
54     @Test
55     public void testRangeCoveredForUint8() {
56         final Uint64 min = Uint64.valueOf("100");
57         final Uint64 superMin = Uint64.valueOf("50");
58         final Uint64 max = Uint64.valueOf("200");
59         final Uint64 superMax = Uint64.valueOf("250");
60
61         assertTrue(NumberUtil.isRangeCovered(min, max, superMin, superMax));
62     }
63
64     @Test
65     public void testRangeCoveredForUint16() {
66         final Uint16 min = Uint16.valueOf("100");
67         final Uint16 superMin = Uint16.valueOf("50");
68         final Uint16 max = Uint16.valueOf("200");
69         final Uint16 superMax = Uint16.valueOf("300");
70
71         assertTrue(NumberUtil.isRangeCovered(min, max, superMin, superMax));
72     }
73
74     @Test
75     public void testRangeCoveredForUint32() {
76         final Uint32 min = Uint32.valueOf("100");
77         final Uint32 superMin = Uint32.valueOf("50");
78         final Uint32 max = Uint32.valueOf("200");
79         final Uint32 superMax = Uint32.valueOf("300");
80
81         assertTrue(NumberUtil.isRangeCovered(min, max, superMin, superMax));
82     }
83
84     @Test
85     public void testRangeCoveredForUint64() {
86         final Uint64 min = Uint64.valueOf("100");
87         final Uint64 superMin = Uint64.valueOf("50");
88         final Uint64 max = Uint64.valueOf("200");
89         final Uint64 superMax = Uint64.valueOf("300");
90
91         assertTrue(NumberUtil.isRangeCovered(min, max, superMin, superMax));
92     }
93
94     @Test
95     public void testRangeCoveredForUnsupportedNumberType() {
96         final double min = 100.0;
97         final double superMin = 50.0;
98         final double max = 200.0;
99         final double superMax = 300.0;
100
101         assertThrows(IllegalArgumentException.class, () -> NumberUtil.isRangeCovered(min, max, superMin, superMax));
102     }
103
104     @Test
105     public void testConverterToShort() {
106         final Short shortNum = 20;
107         final Function<Number, Short> numberFunction = NumberUtil.converterTo(Short.class);
108         assertEquals(shortNum, numberFunction.apply(shortNum));
109
110         final byte byteNum = 20;
111         assertEquals(shortNum, numberFunction.apply(byteNum));
112
113         final int intNum = 20;
114         assertEquals(shortNum, numberFunction.apply(intNum));
115     }
116
117     @Test
118     public void testConverterToInteger() {
119         final Integer intNum = 20;
120         final byte byteNum = 20;
121         final Function<Number, Integer> numberFunction = NumberUtil.converterTo(Integer.class);
122         assertEquals(intNum, numberFunction.apply(byteNum));
123     }
124
125     @Test
126     public void testConverterToLong() {
127         final Long longNum = 20L;
128         final Function<Number, Long> numberFunction = NumberUtil.converterTo(Long.class);
129         assertEquals(longNum, numberFunction.apply(longNum));
130
131         final byte byteNum = 20;
132         assertEquals(longNum, numberFunction.apply(byteNum));
133
134         final BigInteger bigIntNum = new BigInteger("20");
135         assertEquals(longNum, numberFunction.apply(bigIntNum));
136     }
137
138     @Test
139     public void testConverterToBigDecimal() {
140         BigDecimal bigDecNum = new BigDecimal(20.0);
141         final Function<Number, BigDecimal> numberFunction = NumberUtil.converterTo(BigDecimal.class);
142         assertEquals(bigDecNum, numberFunction.apply(bigDecNum));
143
144         int intNum = 20;
145         assertEquals(bigDecNum, numberFunction.apply(intNum));
146
147         double doubleNum = 20.0;
148         bigDecNum = new BigDecimal("20.0");
149         assertEquals(bigDecNum, numberFunction.apply(doubleNum));
150     }
151
152     @Test
153     public void testConverterToUint64() {
154         final Uint64 bigIntNum = Uint64.valueOf("20");
155         final Function<Number, Uint64> numberFunction = NumberUtil.converterTo(Uint64.class);
156         assertEquals(bigIntNum, numberFunction.apply(bigIntNum));
157
158         final int intNum = 20;
159         assertEquals(bigIntNum, numberFunction.apply(intNum));
160
161         final BigDecimal bigDecNum = new BigDecimal(20.0);
162         assertEquals(bigIntNum, numberFunction.apply(bigDecNum));
163     }
164 }