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