|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | + |
| 42 | +package com.oracle.graal.python.nodes.util; |
| 43 | + |
| 44 | +import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
| 45 | +import com.oracle.graal.python.builtins.objects.ints.PInt; |
| 46 | +import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile; |
| 47 | +import com.oracle.graal.python.runtime.exception.PException; |
| 48 | +import com.oracle.graal.python.runtime.object.PythonObjectFactory; |
| 49 | +import com.oracle.graal.python.test.PythonTests; |
| 50 | +import org.junit.Assert; |
| 51 | +import org.junit.Before; |
| 52 | +import org.junit.Test; |
| 53 | + |
| 54 | +import java.math.BigInteger; |
| 55 | + |
| 56 | +import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError; |
| 57 | +import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError; |
| 58 | + |
| 59 | +public class CastToJavaUnsignedLongNodeTests { |
| 60 | + |
| 61 | + @Before |
| 62 | + public void setUp() { |
| 63 | + PythonTests.enterContext(); |
| 64 | + } |
| 65 | + |
| 66 | + private static CastToJavaUnsignedLongNode castNode = CastToJavaUnsignedLongNode.getUncached(); |
| 67 | + private static PythonObjectFactory factory = PythonObjectFactory.getUncached(); |
| 68 | + |
| 69 | + @Test |
| 70 | + public void positiveInt() { |
| 71 | + Assert.assertEquals(0, castNode.execute(0)); |
| 72 | + Assert.assertEquals(16, castNode.execute(16)); |
| 73 | + Assert.assertEquals(Integer.MAX_VALUE, castNode.execute(Integer.MAX_VALUE)); |
| 74 | + Assert.assertEquals(42, castNode.execute(Integer.valueOf(42))); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void negativeInt() { |
| 79 | + expect(OverflowError, () -> castNode.execute(-1)); |
| 80 | + expect(OverflowError, () -> castNode.execute(Integer.MIN_VALUE)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void positiveLong() { |
| 85 | + Assert.assertEquals(0, castNode.execute(0L)); |
| 86 | + Assert.assertEquals(0x80000000L, castNode.execute(0x80000000L)); |
| 87 | + Assert.assertEquals(Long.MAX_VALUE, castNode.execute(Long.MAX_VALUE)); |
| 88 | + Assert.assertEquals(1234567890123L, castNode.execute(Long.valueOf(1234567890123L))); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void negativeLong() { |
| 93 | + expect(OverflowError, () -> castNode.execute(-1L)); |
| 94 | + expect(OverflowError, () -> castNode.execute(Long.MIN_VALUE)); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void positiveBigInt() { |
| 99 | + Assert.assertEquals(0, castNode.execute(makePInt(0))); |
| 100 | + Assert.assertEquals(1234567890123L, castNode.execute(makePInt(1234567890123L))); |
| 101 | + Assert.assertEquals(Long.MAX_VALUE, castNode.execute(makePInt(Long.MAX_VALUE))); |
| 102 | + Assert.assertEquals(0x8000000000000000L, castNode.execute(makePInt("8000000000000000"))); |
| 103 | + Assert.assertEquals(0xFFFFFFFFFFFFFFFFL, castNode.execute(makePInt("FFFFFFFFFFFFFFFF"))); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void negativeBigInt() { |
| 108 | + expect(OverflowError, () -> castNode.execute(makePInt(-1))); |
| 109 | + expect(OverflowError, () -> castNode.execute(makePInt("-10000000000000000"))); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void largeBigInt() { |
| 114 | + expect(OverflowError, () -> castNode.execute(makePInt("10000000000000000"))); |
| 115 | + expect(OverflowError, () -> castNode.execute(makePInt("10000000000000001"))); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void nonInteger() { |
| 120 | + expect(TypeError, () -> castNode.execute("123")); |
| 121 | + expect(TypeError, () -> castNode.execute(2.7)); |
| 122 | + } |
| 123 | + |
| 124 | + private static PInt makePInt(long l) { |
| 125 | + return factory.createInt(BigInteger.valueOf(l)); |
| 126 | + } |
| 127 | + |
| 128 | + private static PInt makePInt(String hexString) { |
| 129 | + return factory.createInt(new BigInteger(hexString, 16)); |
| 130 | + } |
| 131 | + |
| 132 | + private static void expect(PythonBuiltinClassType errorType, Runnable test) { |
| 133 | + try { |
| 134 | + test.run(); |
| 135 | + Assert.fail("Expected " + errorType.getName()); |
| 136 | + } catch (PException e) { |
| 137 | + e.expect(errorType, IsBuiltinClassProfile.getUncached()); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments