2 """Test the arraymodule.
6 from test_support
import verbose
, TESTFN
, unlink
, TestFailed
12 for type in (['b', 'h', 'i', 'l', 'f', 'd']):
18 def testoverflow(type, lowerLimit
, upperLimit
):
19 # should not overflow assigning lower limit
21 print "overflow test: array(%s, [%s])" % (`
type`
, `lowerLimit`
)
23 a
= array
.array(type, [lowerLimit
])
25 raise TestFailed
, "array(%s) overflowed assigning %s" %\
26 (`
type`
, `lowerLimit`
)
27 # should overflow assigning less than lower limit
29 print "overflow test: array(%s, [%s])" % (`
type`
, `lowerLimit
-1`
)
31 a
= array
.array(type, [lowerLimit
-1])
32 raise TestFailed
, "array(%s) did not overflow assigning %s" %\
33 (`
type`
, `lowerLimit
-1`
)
36 # should not overflow assigning upper limit
38 print "overflow test: array(%s, [%s])" % (`
type`
, `upperLimit`
)
40 a
= array
.array(type, [upperLimit
])
42 raise TestFailed
, "array(%s) overflowed assigning %s" %\
43 (`
type`
, `upperLimit`
)
44 # should overflow assigning more than upper limit
46 print "overflow test: array(%s, [%s])" % (`
type`
, `upperLimit
+1`
)
48 a
= array
.array(type, [upperLimit
+1])
49 raise TestFailed
, "array(%s) did not overflow assigning %s" %\
50 (`
type`
, `upperLimit
+1`
)
56 def testtype(type, example
):
62 print 'array after append: ', a
65 if a
.typecode
in ('i', 'b', 'h', 'l'):
70 f
.write("The quick brown fox jumps over the lazy dog.\n")
76 print 'char array with 10 bytes of TESTFN appended: ', a
77 a
.fromlist(['a', 'b', 'c'])
79 print 'char array with list appended: ', a
83 print 'array of %s after inserting another:' % a
.typecode
, a
90 print 'array of %s converted to a list: ' % a
.typecode
, a
.tolist()
92 print 'array of %s converted to a string: ' \
93 % a
.typecode
, `a
.tostring()`
96 a
= array
.array(type, "abcde")
98 if a
!= array
.array(type, "abcdee"):
99 raise TestFailed
, "array(%s) self-slice-assign (head)" % `
type`
100 a
= array
.array(type, "abcde")
102 if a
!= array
.array(type, "aabcde"):
103 raise TestFailed
, "array(%s) self-slice-assign (tail)" % `
type`
104 a
= array
.array(type, "abcde")
106 if a
!= array
.array(type, "aabcdee"):
107 raise TestFailed
, "array(%s) self-slice-assign (cntr)" % `
type`
109 a
= array
.array(type, [1, 2, 3, 4, 5])
111 if a
!= array
.array(type, [1, 2, 3, 4, 5, 5]):
112 raise TestFailed
, "array(%s) self-slice-assign (head)" % `
type`
113 a
= array
.array(type, [1, 2, 3, 4, 5])
115 if a
!= array
.array(type, [1, 1, 2, 3, 4, 5]):
116 raise TestFailed
, "array(%s) self-slice-assign (tail)" % `
type`
117 a
= array
.array(type, [1, 2, 3, 4, 5])
119 if a
!= array
.array(type, [1, 1, 2, 3, 4, 5, 5]):
120 raise TestFailed
, "array(%s) self-slice-assign (cntr)" % `
type`
122 # test that overflow exceptions are raised as expected for assignment
123 # to array of specific integral types
125 if type in ('b', 'h', 'i', 'l'):
126 # check signed and unsigned versions
127 a
= array
.array(type)
128 signedLowerLimit
= -1 * long(pow(2, a
.itemsize
* 8 - 1))
129 signedUpperLimit
= long(pow(2, a
.itemsize
* 8 - 1)) - 1L
130 unsignedLowerLimit
= 0
131 unsignedUpperLimit
= long(pow(2, a
.itemsize
* 8)) - 1L
132 testoverflow(type, signedLowerLimit
, signedUpperLimit
)
133 testoverflow(type.upper(), unsignedLowerLimit
, unsignedUpperLimit
)