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
88 # This block is just to verify that the operations don't blow up.
95 print 'array of %s converted to a list: ' % a
.typecode
, a
.tolist()
97 print 'array of %s converted to a string: ' \
98 % a
.typecode
, `a
.tostring()`
101 a
= array
.array(type, "abcde")
103 if a
!= array
.array(type, "abcdee"):
104 raise TestFailed
, "array(%s) self-slice-assign (head)" % `
type`
105 a
= array
.array(type, "abcde")
107 if a
!= array
.array(type, "aabcde"):
108 raise TestFailed
, "array(%s) self-slice-assign (tail)" % `
type`
109 a
= array
.array(type, "abcde")
111 if a
!= array
.array(type, "aabcdee"):
112 raise TestFailed
, "array(%s) self-slice-assign (cntr)" % `
type`
113 if a
.index("e") != 5:
114 raise TestFailed
, "array(%s) index-test" % `
type`
115 if a
.count("a") != 2:
116 raise TestFailed
, "array(%s) count-test" % `
type`
118 if a
!= array
.array(type, "aabcde"):
119 raise TestFailed
, "array(%s) remove-test" % `
type`
121 raise TestFailed
, "array(%s) pop-test" % `
type`
123 raise TestFailed
, "array(%s) pop-test" % `
type`
124 a
.extend(array
.array(type, "xyz"))
125 if a
!= array
.array(type, "acdexyz"):
126 raise TestFailed
, "array(%s) extend-test" % `
type`
132 raise TestFailed
, "array(%s) pop-test" % `
type`
133 if a
!= array
.array(type, "acd"):
134 raise TestFailed
, "array(%s) pop-test" % `
type`
136 if a
!= array
.array(type, "dca"):
137 raise TestFailed
, "array(%s) reverse-test" % `
type`
139 a
= array
.array(type, [1, 2, 3, 4, 5])
141 if a
!= array
.array(type, [1, 2, 3, 4, 5, 5]):
142 raise TestFailed
, "array(%s) self-slice-assign (head)" % `
type`
143 a
= array
.array(type, [1, 2, 3, 4, 5])
145 if a
!= array
.array(type, [1, 1, 2, 3, 4, 5]):
146 raise TestFailed
, "array(%s) self-slice-assign (tail)" % `
type`
147 a
= array
.array(type, [1, 2, 3, 4, 5])
149 if a
!= array
.array(type, [1, 1, 2, 3, 4, 5, 5]):
150 raise TestFailed
, "array(%s) self-slice-assign (cntr)" % `
type`
152 raise TestFailed
, "array(%s) index-test" % `
type`
154 raise TestFailed
, "array(%s) count-test" % `
type`
156 if a
!= array
.array(type, [1, 1, 2, 3, 4, 5]):
157 raise TestFailed
, "array(%s) remove-test" % `
type`
159 raise TestFailed
, "array(%s) pop-test" % `
type`
161 raise TestFailed
, "array(%s) pop-test" % `
type`
162 a
.extend(array
.array(type, [7, 8, 9]))
163 if a
!= array
.array(type, [1, 3, 4, 5, 7, 8, 9]):
164 raise TestFailed
, "array(%s) extend-test" % `
type`
170 raise TestFailed
, "array(%s) pop-test" % `
type`
171 if a
!= array
.array(type, [1, 3, 4]):
172 raise TestFailed
, "array(%s) pop-test" % `
type`
174 if a
!= array
.array(type, [4, 3, 1]):
175 raise TestFailed
, "array(%s) reverse-test" % `
type`
177 # test that overflow exceptions are raised as expected for assignment
178 # to array of specific integral types
180 if type in ('b', 'h', 'i', 'l'):
181 # check signed and unsigned versions
182 a
= array
.array(type)
183 signedLowerLimit
= -1 * long(pow(2, a
.itemsize
* 8 - 1))
184 signedUpperLimit
= long(pow(2, a
.itemsize
* 8 - 1)) - 1L
185 unsignedLowerLimit
= 0
186 unsignedUpperLimit
= long(pow(2, a
.itemsize
* 8)) - 1L
187 testoverflow(type, signedLowerLimit
, signedUpperLimit
)
188 testoverflow(type.upper(), unsignedLowerLimit
, unsignedUpperLimit
)