|
| 1 | +# Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 2 | +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 3 | +# |
| 4 | +# The Universal Permissive License (UPL), Version 1.0 |
| 5 | +# |
| 6 | +# Subject to the condition set forth below, permission is hereby granted to any |
| 7 | +# person obtaining a copy of this software, associated documentation and/or |
| 8 | +# data (collectively the "Software"), free of charge and under any and all |
| 9 | +# copyright rights in the Software, and any and all patent rights owned or |
| 10 | +# freely licensable by each licensor hereunder covering either (i) the |
| 11 | +# unmodified Software as contributed to or provided by such licensor, or (ii) |
| 12 | +# the Larger Works (as defined below), to deal in both |
| 13 | +# |
| 14 | +# (a) the Software, and |
| 15 | +# |
| 16 | +# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 17 | +# one is included with the Software each a "Larger Work" to which the Software |
| 18 | +# is contributed by such licensors), |
| 19 | +# |
| 20 | +# without restriction, including without limitation the rights to copy, create |
| 21 | +# derivative works of, display, perform, and distribute the Software and make, |
| 22 | +# use, sell, offer for sale, import, export, have made, and have sold the |
| 23 | +# Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 24 | +# either these or other terms. |
| 25 | +# |
| 26 | +# This license is subject to the following condition: |
| 27 | +# |
| 28 | +# The above copyright notice and either this complete permission notice or at a |
| 29 | +# minimum a reference to the UPL must be included in all copies or substantial |
| 30 | +# portions of the Software. |
| 31 | +# |
| 32 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 33 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 34 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 35 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 36 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 37 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 38 | +# SOFTWARE. |
| 39 | + |
| 40 | +import sys |
| 41 | + |
| 42 | + |
| 43 | +def test_pow(): |
| 44 | + if sys.implementation.name == "graalpython": |
| 45 | + try: |
| 46 | + 2 ** (2**128) |
| 47 | + except ArithmeticError: |
| 48 | + assert True |
| 49 | + else: |
| 50 | + assert False |
| 51 | + |
| 52 | + assert 2 ** -(2**128) == 0.0 |
| 53 | + |
| 54 | + class X(float): |
| 55 | + def __rpow__(self, other): |
| 56 | + return 42 |
| 57 | + |
| 58 | + assert 2 ** X() == 42 |
| 59 | + |
| 60 | + try: |
| 61 | + 2.0 .__pow__(2, 2) |
| 62 | + except TypeError as e: |
| 63 | + assert True |
| 64 | + else: |
| 65 | + assert False |
| 66 | + |
| 67 | + try: |
| 68 | + 2.0 .__pow__(2.0, 2.0) |
| 69 | + except TypeError as e: |
| 70 | + assert True |
| 71 | + else: |
| 72 | + assert False |
| 73 | + |
| 74 | + assert 2 ** 2.0 == 4.0 |
| 75 | + |
| 76 | + assert 2 .__pow__("a") == NotImplemented |
| 77 | + assert 2 .__pow__("a", 2) == NotImplemented |
| 78 | + assert 2 .__pow__(2**30, 2**128) == 0 |
| 79 | + |
| 80 | + # crafted to try specializations |
| 81 | + def mypow(a, b, c): |
| 82 | + return a.__pow__(b, c) |
| 83 | + |
| 84 | + values = [ |
| 85 | + [1, 2, None, 1], # long |
| 86 | + [1, 128, None, 1], # BigInteger |
| 87 | + [1, -2, None, 1.0], # double result |
| 88 | + [1, 0xffffffffffffffffffffffffffffffff & 0x80, None, 1], # narrow to long |
| 89 | + [2, 0xffffffffffffffffffffffffffffffff & 0x80, None, 340282366920938463463374607431768211456], # cannot narrow |
| 90 | + [2, -(0xffffffffffffffffffffffffffffffff & 0x80), None, 2.938735877055719e-39], # double result |
| 91 | + [2**128, 0, None, 1], # narrow to long |
| 92 | + [2**128, 1, None, 340282366920938463463374607431768211456], # cannot narrow |
| 93 | + [2**128, -2, None, 8.636168555094445e-78], # double |
| 94 | + [2**128, 0xffffffffffffffffffffffffffffffff & 0x2, None, 115792089237316195423570985008687907853269984665640564039457584007913129639936], # large |
| 95 | + [2**128, -(0xffffffffffffffffffffffffffffffff & 0x8), None, 5.562684646268003e-309], # double result |
| 96 | + [1, 2, 3, 1], # fast path |
| 97 | + [2, 2**30, 2**128, 0], # generic |
| 98 | + ] + [] |
| 99 | + |
| 100 | + if sys.version_info.minor >= 8: |
| 101 | + values += [ |
| 102 | + [1, -2, 3, 1], # fast path double |
| 103 | + [1, 2, -3, -2], # negative mod |
| 104 | + [1, -2, -3, -2], # negative mod and negative right |
| 105 | + [1, -2**128, 3, 1], # mod and large negative right |
| 106 | + [1, -2**128, -3, -2], # negative mod and large negative right |
| 107 | + [1, -2**128, -2**64, -18446744073709551615], # large negative mod and large negative right |
| 108 | + ] |
| 109 | + |
| 110 | + for args in values: |
| 111 | + assert mypow(*args[:-1]) == args[-1], "%r -> %r == %r" % (args, mypow(*args[:-1]), args[-1]) |
| 112 | + |
| 113 | + def mypow_rev(a, b, c): |
| 114 | + return a.__pow__(b, c) |
| 115 | + |
| 116 | + for args in reversed(values): |
| 117 | + assert mypow_rev(*args[:-1]) == args[-1], "%r -> %r == %r" % (args, mypow(*args[:-1]), args[-1]) |
| 118 | + |
| 119 | + assert 2**1.0 == 2.0 |
0 commit comments