| 1 |
import unittest |
| 2 |
from test.support import bigmemtest, _2G, impl_detail |
| 3 |
import sys |
| 4 |
from ctypes import * |
| 5 |
|
| 6 |
from ctypes.test import need_symbol |
| 7 |
|
| 8 |
formats = "bBhHiIlLqQfd" |
| 9 |
|
| 10 |
|
| 11 |
formats = c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, \ |
| 12 |
c_long, c_ulonglong, c_float, c_double |
| 13 |
|
| 14 |
class ArrayTestCase(unittest.TestCase): |
| 15 |
|
| 16 |
@impl_detail('long double not supported by PyPy', pypy=False) |
| 17 |
def test_longdouble(self): |
| 18 |
""" |
| 19 |
This test is empty. It's just here to remind that we commented out |
| 20 |
c_longdouble in "formats". If pypy will ever supports c_longdouble, we |
| 21 |
should kill this test and uncomment c_longdouble inside formats. |
| 22 |
""" |
| 23 |
|
| 24 |
def test_simple(self): |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
init = list(range(15, 25)) |
| 29 |
|
| 30 |
for fmt in formats: |
| 31 |
alen = len(init) |
| 32 |
int_array = ARRAY(fmt, alen) |
| 33 |
|
| 34 |
ia = int_array(*init) |
| 35 |
|
| 36 |
self.assertEqual(len(ia), alen) |
| 37 |
|
| 38 |
|
| 39 |
values = [ia[i] for i in range(alen)] |
| 40 |
self.assertEqual(values, init) |
| 41 |
|
| 42 |
|
| 43 |
with self.assertRaises(IndexError): ia[alen] |
| 44 |
with self.assertRaises(IndexError): ia[-alen-1] |
| 45 |
|
| 46 |
|
| 47 |
from operator import setitem |
| 48 |
new_values = list(range(42, 42+alen)) |
| 49 |
[setitem(ia, n, new_values[n]) for n in range(alen)] |
| 50 |
values = [ia[i] for i in range(alen)] |
| 51 |
self.assertEqual(values, new_values) |
| 52 |
|
| 53 |
|
| 54 |
ia = int_array() |
| 55 |
values = [ia[i] for i in range(alen)] |
| 56 |
self.assertEqual(values, [0] * alen) |
| 57 |
|
| 58 |
|
| 59 |
self.assertRaises(IndexError, int_array, *range(alen*2)) |
| 60 |
|
| 61 |
CharArray = ARRAY(c_char, 3) |
| 62 |
|
| 63 |
ca = CharArray(b"a", b"b", b"c") |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
self.assertRaises(TypeError, CharArray, "abc") |
| 68 |
|
| 69 |
self.assertEqual(ca[0], b"a") |
| 70 |
self.assertEqual(ca[1], b"b") |
| 71 |
self.assertEqual(ca[2], b"c") |
| 72 |
self.assertEqual(ca[-3], b"a") |
| 73 |
self.assertEqual(ca[-2], b"b") |
| 74 |
self.assertEqual(ca[-1], b"c") |
| 75 |
|
| 76 |
self.assertEqual(len(ca), 3) |
| 77 |
|
| 78 |
|
| 79 |
from operator import delitem |
| 80 |
self.assertRaises(TypeError, delitem, ca, 0) |
| 81 |
|
| 82 |
def test_step_overflow(self): |
| 83 |
a = (c_int * 5)() |
| 84 |
a[3::sys.maxsize] = (1,) |
| 85 |
self.assertListEqual(a[3::sys.maxsize], [1]) |
| 86 |
a = (c_char * 5)() |
| 87 |
a[3::sys.maxsize] = b"A" |
| 88 |
self.assertEqual(a[3::sys.maxsize], b"A") |
| 89 |
a = (c_wchar * 5)() |
| 90 |
a[3::sys.maxsize] = u"X" |
| 91 |
self.assertEqual(a[3::sys.maxsize], u"X") |
| 92 |
|
| 93 |
def test_numeric_arrays(self): |
| 94 |
|
| 95 |
alen = 5 |
| 96 |
|
| 97 |
numarray = ARRAY(c_int, alen) |
| 98 |
|
| 99 |
na = numarray() |
| 100 |
values = [na[i] for i in range(alen)] |
| 101 |
self.assertEqual(values, [0] * alen) |
| 102 |
|
| 103 |
na = numarray(*[c_int()] * alen) |
| 104 |
values = [na[i] for i in range(alen)] |
| 105 |
self.assertEqual(values, [0]*alen) |
| 106 |
|
| 107 |
na = numarray(1, 2, 3, 4, 5) |
| 108 |
values = [i for i in na] |
| 109 |
self.assertEqual(values, [1, 2, 3, 4, 5]) |
| 110 |
|
| 111 |
na = numarray(*map(c_int, (1, 2, 3, 4, 5))) |
| 112 |
values = [i for i in na] |
| 113 |
self.assertEqual(values, [1, 2, 3, 4, 5]) |
| 114 |
|
| 115 |
def test_classcache(self): |
| 116 |
self.assertIsNot(ARRAY(c_int, 3), ARRAY(c_int, 4)) |
| 117 |
self.assertIs(ARRAY(c_int, 3), ARRAY(c_int, 3)) |
| 118 |
|
| 119 |
def test_from_address(self): |
| 120 |
|
| 121 |
p = create_string_buffer(b"foo") |
| 122 |
sz = (c_char * 3).from_address(addressof(p)) |
| 123 |
self.assertEqual(sz[:], b"foo") |
| 124 |
self.assertEqual(sz[::], b"foo") |
| 125 |
self.assertEqual(sz[::-1], b"oof") |
| 126 |
self.assertEqual(sz[::3], b"f") |
| 127 |
self.assertEqual(sz[1:4:2], b"o") |
| 128 |
self.assertEqual(sz.value, b"foo") |
| 129 |
|
| 130 |
@need_symbol('create_unicode_buffer') |
| 131 |
def test_from_addressW(self): |
| 132 |
p = create_unicode_buffer("foo") |
| 133 |
sz = (c_wchar * 3).from_address(addressof(p)) |
| 134 |
self.assertEqual(sz[:], "foo") |
| 135 |
self.assertEqual(sz[::], "foo") |
| 136 |
self.assertEqual(sz[::-1], "oof") |
| 137 |
self.assertEqual(sz[::3], "f") |
| 138 |
self.assertEqual(sz[1:4:2], "o") |
| 139 |
self.assertEqual(sz.value, "foo") |
| 140 |
|
| 141 |
def test_cache(self): |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
class my_int(c_int): |
| 149 |
pass |
| 150 |
|
| 151 |
t1 = my_int * 1 |
| 152 |
t2 = my_int * 1 |
| 153 |
self.assertIs(t1, t2) |
| 154 |
|
| 155 |
def test_subclass(self): |
| 156 |
class T(Array): |
| 157 |
_type_ = c_int |
| 158 |
_length_ = 13 |
| 159 |
class U(T): |
| 160 |
pass |
| 161 |
class V(U): |
| 162 |
pass |
| 163 |
class W(V): |
| 164 |
pass |
| 165 |
class X(T): |
| 166 |
_type_ = c_short |
| 167 |
class Y(T): |
| 168 |
_length_ = 187 |
| 169 |
|
| 170 |
for c in [T, U, V, W]: |
| 171 |
self.assertEqual(c._type_, c_int) |
| 172 |
self.assertEqual(c._length_, 13) |
| 173 |
self.assertEqual(c()._type_, c_int) |
| 174 |
self.assertEqual(c()._length_, 13) |
| 175 |
|
| 176 |
self.assertEqual(X._type_, c_short) |
| 177 |
self.assertEqual(X._length_, 13) |
| 178 |
self.assertEqual(X()._type_, c_short) |
| 179 |
self.assertEqual(X()._length_, 13) |
| 180 |
|
| 181 |
self.assertEqual(Y._type_, c_int) |
| 182 |
self.assertEqual(Y._length_, 187) |
| 183 |
self.assertEqual(Y()._type_, c_int) |
| 184 |
self.assertEqual(Y()._length_, 187) |
| 185 |
|
| 186 |
def test_bad_subclass(self): |
| 187 |
with self.assertRaises(AttributeError): |
| 188 |
class T(Array): |
| 189 |
pass |
| 190 |
with self.assertRaises(AttributeError): |
| 191 |
class T2(Array): |
| 192 |
_type_ = c_int |
| 193 |
with self.assertRaises(AttributeError): |
| 194 |
class T3(Array): |
| 195 |
_length_ = 13 |
| 196 |
|
| 197 |
def test_bad_length(self): |
| 198 |
with self.assertRaises(ValueError): |
| 199 |
class T(Array): |
| 200 |
_type_ = c_int |
| 201 |
_length_ = - sys.maxsize * 2 |
| 202 |
with self.assertRaises(ValueError): |
| 203 |
class T2(Array): |
| 204 |
_type_ = c_int |
| 205 |
_length_ = -1 |
| 206 |
with self.assertRaises(TypeError): |
| 207 |
class T3(Array): |
| 208 |
_type_ = c_int |
| 209 |
_length_ = 1.87 |
| 210 |
with self.assertRaises(OverflowError): |
| 211 |
class T4(Array): |
| 212 |
_type_ = c_int |
| 213 |
_length_ = sys.maxsize * 2 |
| 214 |
|
| 215 |
def test_zero_length(self): |
| 216 |
|
| 217 |
class T(Array): |
| 218 |
_type_ = c_int |
| 219 |
_length_ = 0 |
| 220 |
|
| 221 |
def test_empty_element_struct(self): |
| 222 |
class EmptyStruct(Structure): |
| 223 |
_fields_ = [] |
| 224 |
|
| 225 |
obj = (EmptyStruct * 2)() |
| 226 |
self.assertEqual(sizeof(obj), 0) |
| 227 |
|
| 228 |
def test_empty_element_array(self): |
| 229 |
class EmptyArray(Array): |
| 230 |
_type_ = c_int |
| 231 |
_length_ = 0 |
| 232 |
|
| 233 |
obj = (EmptyArray * 2)() |
| 234 |
self.assertEqual(sizeof(obj), 0) |
| 235 |
|
| 236 |
def test_bpo36504_signed_int_overflow(self): |
| 237 |
|
| 238 |
|
| 239 |
with self.assertRaises(OverflowError): |
| 240 |
c_char * sys.maxsize * 2 |
| 241 |
|
| 242 |
@unittest.skipUnless(sys.maxsize > 2**32, 'requires 64bit platform') |
| 243 |
@bigmemtest(size=_2G, memuse=1, dry_run=False) |
| 244 |
def test_large_array(self, size): |
| 245 |
c_char * size |
| 246 |
|
| 247 |
if __name__ == '__main__': |
| 248 |
unittest.main() |