Debian Codegraph @

proton-j/src/test/java/org/apache/qpid/proton/amqp/UnsignedLongTest.java

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21 package org.apache.qpid.proton.amqp;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
25
26 import java.math.BigInteger;
27
28 import org.junit.Test;
29
30 public class UnsignedLongTest
31 {
32 private static final byte[] TWO_TO_64_PLUS_ONE_BYTES = new byte[] { (byte) 1, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 1 };
33 private static final byte[] TWO_TO_64_MINUS_ONE_BYTES = new byte[] {(byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 1 };
34
35 @Test
36 public void testValueOfStringWithNegativeNumberThrowsNFE() throws Exception
37 {
38 try
39 {
40 UnsignedLong.valueOf("-1");
41 fail("Expected exception was not thrown");
42 }
43 catch(NumberFormatException nfe)
44 {
45 //expected
46 }
47 }
48
49 @Test
50 public void testValueOfBigIntegerWithNegativeNumberThrowsNFE() throws Exception
51 {
52 try
53 {
54 UnsignedLong.valueOf(BigInteger.valueOf(-1L));
55 fail("Expected exception was not thrown");
56 }
57 catch(NumberFormatException nfe)
58 {
59 //expected
60 }
61 }
62
63 @Test
64 public void testValuesOfStringWithinRangeSucceed() throws Exception
65 {
66 //check 0 (min) to confirm success
67 UnsignedLong min = UnsignedLong.valueOf("0");
68 assertEquals("unexpected value", 0, min.longValue());
69
70 //check 2^64 -1 (max) to confirm success
71 BigInteger onLimit = new BigInteger(TWO_TO_64_MINUS_ONE_BYTES);
72 String onlimitString = onLimit.toString();
73 UnsignedLong max = UnsignedLong.valueOf(onlimitString);
74 assertEquals("unexpected value", onLimit, max.bigIntegerValue());
75 }
76
77 @Test
78 public void testValuesOfBigIntegerWithinRangeSucceed() throws Exception
79 {
80 //check 0 (min) to confirm success
81 UnsignedLong min = UnsignedLong.valueOf(BigInteger.ZERO);
82 assertEquals("unexpected value", 0, min.longValue());
83
84 //check 2^64 -1 (max) to confirm success
85 BigInteger onLimit = new BigInteger(TWO_TO_64_MINUS_ONE_BYTES);
86 UnsignedLong max = UnsignedLong.valueOf(onLimit);
87 assertEquals("unexpected value", onLimit, max.bigIntegerValue());
88
89 //check Long.MAX_VALUE to confirm success
90 UnsignedLong longMax = UnsignedLong.valueOf(BigInteger.valueOf(Long.MAX_VALUE));
91 assertEquals("unexpected value", Long.MAX_VALUE, longMax.longValue());
92 }
93
94 @Test
95 public void testValueOfStringAboveMaxValueThrowsNFE() throws Exception
96 {
97 //2^64 + 1 (value 2 over max)
98 BigInteger aboveLimit = new BigInteger(TWO_TO_64_PLUS_ONE_BYTES);
99 try
100 {
101 UnsignedLong.valueOf(aboveLimit.toString());
102 fail("Expected exception was not thrown");
103 }
104 catch(NumberFormatException nfe)
105 {
106 //expected
107 }
108
109 //2^64 (value 1 over max)
110 aboveLimit = aboveLimit.subtract(BigInteger.ONE);
111 try
112 {
113 UnsignedLong.valueOf(aboveLimit.toString());
114 fail("Expected exception was not thrown");
115 }
116 catch(NumberFormatException nfe)
117 {
118 //expected
119 }
120 }
121
122 @Test
123 public void testValueOfBigIntegerAboveMaxValueThrowsNFE() throws Exception
124 {
125 //2^64 + 1 (value 2 over max)
126 BigInteger aboveLimit = new BigInteger(TWO_TO_64_PLUS_ONE_BYTES);
127 try
128 {
129 UnsignedLong.valueOf(aboveLimit);
130 fail("Expected exception was not thrown");
131 }
132 catch(NumberFormatException nfe)
133 {
134 //expected
135 }
136
137 //2^64 (value 1 over max)
138 aboveLimit = aboveLimit.subtract(BigInteger.ONE);
139 try
140 {
141 UnsignedLong.valueOf(aboveLimit);
142 fail("Expected exception was not thrown");
143 }
144 catch(NumberFormatException nfe)
145 {
146 //expected
147 }
148 }
149 }