|
| 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 | +import unittest |
| 42 | + |
| 43 | +import platform |
| 44 | + |
| 45 | +# The platform.system() == 'Java' is to make it possible to run with Jython |
| 46 | +if platform.system() == 'Java' or sys.implementation.name == "graalpython": |
| 47 | + from java.util import Arrays |
| 48 | + from java.lang import StringBuilder |
| 49 | + import jarray |
| 50 | + |
| 51 | + |
| 52 | + class AbstractJArrayTest: |
| 53 | + type = None |
| 54 | + default_value = None |
| 55 | + instance = None |
| 56 | + bad_instance = object() |
| 57 | + assertValueEqual = unittest.TestCase.assertEqual |
| 58 | + |
| 59 | + def test_zeros(self): |
| 60 | + if self.type is None: |
| 61 | + return |
| 62 | + array = jarray.zeros(2, self.type) |
| 63 | + self.assertValueEqual(len(array), 2) |
| 64 | + self.assertValueEqual(array[0], self.default_value) |
| 65 | + self.assertValueEqual(array[1], self.default_value) |
| 66 | + array[0] = self.instance |
| 67 | + self.assertValueEqual(array[0], self.instance) |
| 68 | + with self.assertRaises(TypeError): |
| 69 | + array[0] = self.bad_instance |
| 70 | + Arrays.fill(array, self.instance) |
| 71 | + self.assertValueEqual(array[0], self.instance) |
| 72 | + self.assertValueEqual(array[1], self.instance) |
| 73 | + |
| 74 | + def test_array(self): |
| 75 | + if self.type is None: |
| 76 | + return |
| 77 | + array = jarray.array([self.instance, self.default_value], self.type) |
| 78 | + self.assertValueEqual(array[0], self.instance) |
| 79 | + self.assertValueEqual(array[1], self.default_value) |
| 80 | + with self.assertRaises(TypeError): |
| 81 | + jarray.array([self.instance, self.bad_instance], self.type) |
| 82 | + |
| 83 | + def gen(): |
| 84 | + yield self.instance |
| 85 | + yield self.default_value |
| 86 | + |
| 87 | + array = jarray.array(gen(), self.type) |
| 88 | + self.assertValueEqual(array[0], self.instance) |
| 89 | + self.assertValueEqual(array[1], self.default_value) |
| 90 | + with self.assertRaises(TypeError): |
| 91 | + jarray.array(5, self.type) |
| 92 | + |
| 93 | + |
| 94 | + class BooleanJArrayTest(AbstractJArrayTest, unittest.TestCase): |
| 95 | + type = 'z' |
| 96 | + default_value = False |
| 97 | + instance = True |
| 98 | + |
| 99 | + |
| 100 | + class ByteJArrayTest(AbstractJArrayTest, unittest.TestCase): |
| 101 | + type = 'b' |
| 102 | + default_value = 0 |
| 103 | + instance = -1 |
| 104 | + |
| 105 | + |
| 106 | + class CharJArrayTest(AbstractJArrayTest, unittest.TestCase): |
| 107 | + type = 'c' |
| 108 | + default_value = '\0' |
| 109 | + instance = 'a' |
| 110 | + |
| 111 | + |
| 112 | + class ShortJArrayTest(AbstractJArrayTest, unittest.TestCase): |
| 113 | + type = 'h' |
| 114 | + default_value = 0 |
| 115 | + instance = 266 |
| 116 | + |
| 117 | + |
| 118 | + class IntJArrayTest(AbstractJArrayTest, unittest.TestCase): |
| 119 | + type = 'i' |
| 120 | + default_value = 0 |
| 121 | + instance = 123456 |
| 122 | + |
| 123 | + |
| 124 | + class LongJArrayTest(AbstractJArrayTest, unittest.TestCase): |
| 125 | + type = 'l' |
| 126 | + default_value = 0 |
| 127 | + instance = 1099511627776 |
| 128 | + |
| 129 | + # TODO interop doesn't support assigning doubles to a float array |
| 130 | + # class FloatJArrayTest(AbstractJArrayTest, unittest.TestCase): |
| 131 | + # type = 'f' |
| 132 | + # default_value = 0.0 |
| 133 | + # instance = 0.14 |
| 134 | + # assertValueEqual = unittest.TestCase.assertAlmostEqual |
| 135 | + |
| 136 | + |
| 137 | + class DoubleJArrayTest(AbstractJArrayTest, unittest.TestCase): |
| 138 | + type = 'd' |
| 139 | + default_value = 0.0 |
| 140 | + instance = 0.14 |
| 141 | + assertValueEqual = unittest.TestCase.assertAlmostEqual |
| 142 | + |
| 143 | + |
| 144 | + class ObjectJArrayTest(AbstractJArrayTest, unittest.TestCase): |
| 145 | + type = StringBuilder |
| 146 | + default_value = None |
| 147 | + instance = StringBuilder("aaa") |
| 148 | + |
| 149 | + |
| 150 | + class ErrorTest(unittest.TestCase): |
| 151 | + def test_wrong_type(self): |
| 152 | + with self.assertRaises(ValueError): |
| 153 | + jarray.array([1, 2], 'x') |
| 154 | + with self.assertRaises(TypeError): |
| 155 | + jarray.array([1, 2]) |
| 156 | + with self.assertRaises(TypeError): |
| 157 | + jarray.array([1, 2], 6) |
| 158 | + with self.assertRaises(TypeError): |
| 159 | + jarray.array([1, 2], StringBuilder()) |
| 160 | + |
| 161 | +if __name__ == '__main__': |
| 162 | + unittest.main() |
0 commit comments