|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.common; |
| 21 | + |
| 22 | +import org.elasticsearch.test.ESTestCase; |
| 23 | + |
| 24 | +import java.math.BigDecimal; |
| 25 | +import java.math.BigInteger; |
| 26 | +import java.util.concurrent.atomic.AtomicInteger; |
| 27 | + |
| 28 | +public class NumbersTests extends ESTestCase { |
| 29 | + |
| 30 | + public void testToLongExact() { |
| 31 | + assertEquals(3L, Numbers.toLongExact(Long.valueOf(3L))); |
| 32 | + assertEquals(3L, Numbers.toLongExact(Integer.valueOf(3))); |
| 33 | + assertEquals(3L, Numbers.toLongExact(Short.valueOf((short) 3))); |
| 34 | + assertEquals(3L, Numbers.toLongExact(Byte.valueOf((byte) 3))); |
| 35 | + assertEquals(3L, Numbers.toLongExact(3d)); |
| 36 | + assertEquals(3L, Numbers.toLongExact(3f)); |
| 37 | + assertEquals(3L, Numbers.toLongExact(BigInteger.valueOf(3L))); |
| 38 | + assertEquals(3L, Numbers.toLongExact(BigDecimal.valueOf(3L))); |
| 39 | + |
| 40 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, |
| 41 | + () -> Numbers.toLongExact(3.1d)); |
| 42 | + assertEquals("3.1 is not an integer value", e.getMessage()); |
| 43 | + e = expectThrows(IllegalArgumentException.class, |
| 44 | + () -> Numbers.toLongExact(Double.NaN)); |
| 45 | + assertEquals("NaN is not an integer value", e.getMessage()); |
| 46 | + e = expectThrows(IllegalArgumentException.class, |
| 47 | + () -> Numbers.toLongExact(Double.POSITIVE_INFINITY)); |
| 48 | + assertEquals("Infinity is not an integer value", e.getMessage()); |
| 49 | + e = expectThrows(IllegalArgumentException.class, |
| 50 | + () -> Numbers.toLongExact(3.1f)); |
| 51 | + assertEquals("3.1 is not an integer value", e.getMessage()); |
| 52 | + e = expectThrows(IllegalArgumentException.class, |
| 53 | + () -> Numbers.toLongExact(new AtomicInteger(3))); // not supported |
| 54 | + assertEquals("Cannot check whether [3] of class [java.util.concurrent.atomic.AtomicInteger] is actually a long", e.getMessage()); |
| 55 | + } |
| 56 | + |
| 57 | + public void testToIntExact() { |
| 58 | + assertEquals(3L, Numbers.toIntExact(Long.valueOf(3L))); |
| 59 | + assertEquals(3L, Numbers.toIntExact(Integer.valueOf(3))); |
| 60 | + assertEquals(3L, Numbers.toIntExact(Short.valueOf((short) 3))); |
| 61 | + assertEquals(3L, Numbers.toIntExact(Byte.valueOf((byte) 3))); |
| 62 | + assertEquals(3L, Numbers.toIntExact(3d)); |
| 63 | + assertEquals(3L, Numbers.toIntExact(3f)); |
| 64 | + assertEquals(3L, Numbers.toIntExact(BigInteger.valueOf(3L))); |
| 65 | + assertEquals(3L, Numbers.toIntExact(BigDecimal.valueOf(3L))); |
| 66 | + |
| 67 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, |
| 68 | + () -> Numbers.toIntExact(3.1d)); |
| 69 | + assertEquals("3.1 is not an integer value", e.getMessage()); |
| 70 | + e = expectThrows(IllegalArgumentException.class, |
| 71 | + () -> Numbers.toLongExact(Double.NaN)); |
| 72 | + assertEquals("NaN is not an integer value", e.getMessage()); |
| 73 | + e = expectThrows(IllegalArgumentException.class, |
| 74 | + () -> Numbers.toLongExact(Double.POSITIVE_INFINITY)); |
| 75 | + assertEquals("Infinity is not an integer value", e.getMessage()); |
| 76 | + e = expectThrows(IllegalArgumentException.class, |
| 77 | + () -> Numbers.toIntExact(3.1f)); |
| 78 | + assertEquals("3.1 is not an integer value", e.getMessage()); |
| 79 | + ArithmeticException ae = expectThrows(ArithmeticException.class, |
| 80 | + () -> Numbers.toIntExact(1L << 40)); |
| 81 | + assertEquals("integer overflow", ae.getMessage()); |
| 82 | + e = expectThrows(IllegalArgumentException.class, |
| 83 | + () -> Numbers.toIntExact(new AtomicInteger(3))); // not supported |
| 84 | + assertEquals("Cannot check whether [3] of class [java.util.concurrent.atomic.AtomicInteger] is actually a long", e.getMessage()); |
| 85 | + } |
| 86 | + |
| 87 | + public void testToShortExact() { |
| 88 | + assertEquals(3L, Numbers.toShortExact(Long.valueOf(3L))); |
| 89 | + assertEquals(3L, Numbers.toShortExact(Integer.valueOf(3))); |
| 90 | + assertEquals(3L, Numbers.toShortExact(Short.valueOf((short) 3))); |
| 91 | + assertEquals(3L, Numbers.toShortExact(Byte.valueOf((byte) 3))); |
| 92 | + assertEquals(3L, Numbers.toShortExact(3d)); |
| 93 | + assertEquals(3L, Numbers.toShortExact(3f)); |
| 94 | + assertEquals(3L, Numbers.toShortExact(BigInteger.valueOf(3L))); |
| 95 | + assertEquals(3L, Numbers.toShortExact(BigDecimal.valueOf(3L))); |
| 96 | + |
| 97 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, |
| 98 | + () -> Numbers.toShortExact(3.1d)); |
| 99 | + assertEquals("3.1 is not an integer value", e.getMessage()); |
| 100 | + e = expectThrows(IllegalArgumentException.class, |
| 101 | + () -> Numbers.toLongExact(Double.NaN)); |
| 102 | + assertEquals("NaN is not an integer value", e.getMessage()); |
| 103 | + e = expectThrows(IllegalArgumentException.class, |
| 104 | + () -> Numbers.toLongExact(Double.POSITIVE_INFINITY)); |
| 105 | + assertEquals("Infinity is not an integer value", e.getMessage()); |
| 106 | + e = expectThrows(IllegalArgumentException.class, |
| 107 | + () -> Numbers.toShortExact(3.1f)); |
| 108 | + assertEquals("3.1 is not an integer value", e.getMessage()); |
| 109 | + ArithmeticException ae = expectThrows(ArithmeticException.class, |
| 110 | + () -> Numbers.toShortExact(100000)); |
| 111 | + assertEquals("short overflow: " + 100000, ae.getMessage()); |
| 112 | + e = expectThrows(IllegalArgumentException.class, |
| 113 | + () -> Numbers.toShortExact(new AtomicInteger(3))); // not supported |
| 114 | + assertEquals("Cannot check whether [3] of class [java.util.concurrent.atomic.AtomicInteger] is actually a long", e.getMessage()); |
| 115 | + } |
| 116 | + |
| 117 | + public void testToByteExact() { |
| 118 | + assertEquals(3L, Numbers.toByteExact(Long.valueOf(3L))); |
| 119 | + assertEquals(3L, Numbers.toByteExact(Integer.valueOf(3))); |
| 120 | + assertEquals(3L, Numbers.toByteExact(Short.valueOf((short) 3))); |
| 121 | + assertEquals(3L, Numbers.toByteExact(Byte.valueOf((byte) 3))); |
| 122 | + assertEquals(3L, Numbers.toByteExact(3d)); |
| 123 | + assertEquals(3L, Numbers.toByteExact(3f)); |
| 124 | + assertEquals(3L, Numbers.toByteExact(BigInteger.valueOf(3L))); |
| 125 | + assertEquals(3L, Numbers.toByteExact(BigDecimal.valueOf(3L))); |
| 126 | + |
| 127 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, |
| 128 | + () -> Numbers.toByteExact(3.1d)); |
| 129 | + assertEquals("3.1 is not an integer value", e.getMessage()); |
| 130 | + e = expectThrows(IllegalArgumentException.class, |
| 131 | + () -> Numbers.toLongExact(Double.NaN)); |
| 132 | + assertEquals("NaN is not an integer value", e.getMessage()); |
| 133 | + e = expectThrows(IllegalArgumentException.class, |
| 134 | + () -> Numbers.toLongExact(Double.POSITIVE_INFINITY)); |
| 135 | + assertEquals("Infinity is not an integer value", e.getMessage()); |
| 136 | + e = expectThrows(IllegalArgumentException.class, |
| 137 | + () -> Numbers.toByteExact(3.1f)); |
| 138 | + assertEquals("3.1 is not an integer value", e.getMessage()); |
| 139 | + ArithmeticException ae = expectThrows(ArithmeticException.class, |
| 140 | + () -> Numbers.toByteExact(300)); |
| 141 | + assertEquals("byte overflow: " + 300, ae.getMessage()); |
| 142 | + e = expectThrows(IllegalArgumentException.class, |
| 143 | + () -> Numbers.toByteExact(new AtomicInteger(3))); // not supported |
| 144 | + assertEquals("Cannot check whether [3] of class [java.util.concurrent.atomic.AtomicInteger] is actually a long", e.getMessage()); |
| 145 | + } |
| 146 | +} |
0 commit comments