Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-common / src / test / java / org / opendaylight / yangtools / yang / common / Uint64Test.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.assertSame;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.primitives.UnsignedLong;
16 import java.io.ByteArrayInputStream;
17 import java.io.ByteArrayOutputStream;
18 import java.io.IOException;
19 import java.io.ObjectInputStream;
20 import java.io.ObjectOutputStream;
21 import java.math.BigInteger;
22 import org.junit.Test;
23
24 public class Uint64Test {
25     @Test
26     public void testValueOf() {
27         assertEquals(127, Uint64.valueOf(Byte.MAX_VALUE).byteValue());
28         assertEquals(32767, Uint64.valueOf(Short.MAX_VALUE).shortValue());
29         assertEquals(2147483647, Uint64.valueOf(Integer.MAX_VALUE).intValue());
30         assertEquals(9223372036854775807L, Uint64.valueOf(Long.MAX_VALUE).longValue());
31         assertEquals(0, Uint64.valueOf("0").intValue());
32         assertEquals(2170205184637009920L, Uint64.valueOf(2170205184637009920L).longValue());
33         assertEquals(2170205184637009920L, Uint64.valueOf(new BigInteger("2170205184637009920")).longValue());
34     }
35
36     @Test
37     public void testCompareTo() {
38         final Uint64 five = Uint64.valueOf(5);
39         final Uint64 zero = Uint64.valueOf(0);
40         final Uint64 max = Uint64.valueOf(4294967295L);
41
42         assertEquals(0, zero.compareTo(zero));
43         assertEquals(-1, zero.compareTo(five));
44         assertEquals(-1, zero.compareTo(max));
45
46         assertEquals(1, five.compareTo(zero));
47         assertEquals(0, five.compareTo(five));
48         assertEquals(-1, five.compareTo(max));
49
50         assertEquals(1, max.compareTo(zero));
51         assertEquals(1, max.compareTo(five));
52         assertEquals(0, max.compareTo(max));
53     }
54
55     @Test
56     public void testEquals() {
57         final Uint64 five = Uint64.valueOf(5);
58         final Uint64 zero = Uint64.valueOf(0);
59         final Uint64 max = Uint64.valueOf(4294967295L);
60
61         final Uint64 test = new Uint64(five);
62         assertFalse(test.equals(zero));
63         assertFalse(test.equals(new Object()));
64         assertFalse(test.equals(max));
65         assertTrue(test.equals(test));
66         assertTrue(test.equals(five));
67         assertTrue(five.equals(test));
68     }
69
70     @Test
71     public void testToString() {
72         assertEquals("0", Uint64.valueOf(0).toString());
73         assertEquals("2147483647", Uint64.valueOf(2147483647L).toString());
74         assertEquals("2147483648", Uint64.valueOf(2147483648L).toString());
75         assertEquals("4294967295", Uint64.valueOf(4294967295L).toString());
76     }
77
78     @Test
79     public void testHashCode() {
80         assertEquals(Long.hashCode(-63), Uint64.fromLongBits(-63L).hashCode());
81     }
82
83     @Test
84     public void testFloatValue() {
85         assertEquals(0, Uint64.valueOf(0).floatValue(), 0);
86     }
87
88     @Test
89     public void testDoubleValue() {
90         assertEquals(0, Uint64.valueOf(0).doubleValue(), 0);
91     }
92
93     @Test
94     public void testConversions() {
95         assertSame(Uint64.valueOf(5), Uint64.valueOf(Uint8.valueOf(5)));
96         assertSame(Uint64.valueOf(10), Uint64.valueOf(Uint16.valueOf(10)));
97         assertSame(Uint64.valueOf(20), Uint64.valueOf(Uint32.valueOf(20)));
98         assertEquals(Uint64.valueOf(30), Uint64.valueOf(new BigInteger("30")));
99
100         assertSame(Uint64.valueOf(5), Uint64.valueOf(UnsignedLong.fromLongBits(5)));
101         assertEquals(UnsignedLong.fromLongBits(5), Uint64.valueOf(5).toGuava());
102
103         assertEquals(Uint8.TEN, Uint64.TEN.toUint8());
104         assertEquals(Uint16.TEN, Uint64.TEN.toUint16());
105         assertEquals(Uint32.TEN, Uint64.TEN.toUint32());
106     }
107
108     @Test(expected = IllegalArgumentException.class)
109     public void testToUint8() {
110         Uint64.MAX_VALUE.toUint8();
111     }
112
113     @Test(expected = IllegalArgumentException.class)
114     public void testToUint16() {
115         Uint64.MAX_VALUE.toUint16();
116     }
117
118     @Test(expected = IllegalArgumentException.class)
119     public void testToUint32() {
120         Uint64.MAX_VALUE.toUint32();
121     }
122
123     @Test
124     public void testSerialization() throws IOException, ClassNotFoundException {
125         final Uint64 source = Uint64.valueOf(255);
126         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
127         try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
128             oos.writeObject(source);
129         }
130
131         final Object read;
132         try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
133             read = ois.readObject();
134         }
135
136         assertSame(source, read);
137     }
138
139     @Test(expected = IllegalArgumentException.class)
140     public void testNegativeByte() {
141         Uint64.valueOf((byte)-1);
142     }
143
144     @Test(expected = IllegalArgumentException.class)
145     public void testNegativeShort() {
146         Uint64.valueOf((short)-1);
147     }
148
149     @Test(expected = IllegalArgumentException.class)
150     public void testNegativeInt() {
151         Uint64.valueOf(-1);
152     }
153
154     @Test(expected = IllegalArgumentException.class)
155     public void testNegativeLong() {
156         Uint64.valueOf(-1L);
157     }
158
159     @Test(expected = IllegalArgumentException.class)
160     public void testNegativeBigInteger() {
161         Uint64.valueOf(new BigInteger("-1"));
162     }
163
164     @Test(expected = IllegalArgumentException.class)
165     public void testBigBigInteger() {
166         Uint64.valueOf(new BigInteger("0x10000000000000000"));
167     }
168
169     @Test(expected = NullPointerException.class)
170     public void testNullValueOfString() {
171         Uint64.valueOf((String) null);
172     }
173
174     @Test(expected = NullPointerException.class)
175     public void testNullValueOfBigInteger() {
176         Uint64.valueOf((BigInteger) null);
177     }
178 }