/DLR_Main/Languages/IronPython/Tests/compat/sbs_builtin.py
Python | 227 lines | 203 code | 6 blank | 18 comment | 1 complexity | 662353a36b6e88cc812882edcb255330 MD5 | raw file
1#####################################################################################
2#
3# Copyright (c) Microsoft Corporation. All rights reserved.
4#
5# This source code is subject to terms and conditions of the Apache License, Version 2.0. A
6# copy of the license can be found in the License.html file at the root of this distribution. If
7# you cannot locate the Apache License, Version 2.0, please send an email to
8# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
9# by the terms of the Apache License, Version 2.0.
10#
11# You must not remove this notice, or any other, from this software.
12#
13#
14#####################################################################################
15
16from common import *
17import testdata
18
19import sys
20
21def complex_case_repr(*args):
22 ret = "complex with "
23 for x in args:
24 ret += "'%s (%s)'" % (str(x), type(x))
25 return ret
26
27class test_builtin(object):
28 ''' test built-in type, etc '''
29
30 def test_slice(self):
31 ''' currently mainly test
32 del list[slice]
33 '''
34 test_str = testdata.long_string
35 str_len = len(test_str)
36
37 choices = ['', 0]
38 numbers = [1, 2, 3, str_len/2-1, str_len/2, str_len/2+1, str_len-3, str_len-2, str_len-1, str_len, str_len+1, str_len+2, str_len+3, str_len*2]
39 numbers = numbers[::3] # Temporary approach to speed things up...
40 choices.extend(numbers)
41 choices.extend([-1 * x for x in numbers])
42
43 for x in choices:
44 for y in choices:
45 for z in choices:
46 if z == 0: continue
47
48 line = "l = list(test_str); del l[%s:%s:%s]" % (str(x), str(y), str(z))
49 exec line
50 printwith("case", "del l[%s:%s:%s]" % (str(x), str(y), str(z)))
51 printwith("same", eval("l"), eval("len(l)"))
52
53 def test_xrange(self):
54 ''' test xrange with corner cases'''
55 import sys
56 maxint = sys.maxint
57 numbers = [1, 2, maxint/2, maxint-1, maxint, maxint+1, maxint+2]
58 choices = [0]
59 choices.extend(numbers)
60 choices.extend([-1 * x for x in numbers])
61
62 for x in choices:
63 for y in choices:
64 for z in choices:
65 line = "xrange(%s, %s, %s)" % (str(x), str(y), str(z))
66 printwith("case", line)
67 try:
68 xr = eval(line)
69 xl = len(xr)
70 cnt = 0
71 first = last = first2 = last2 = "n/a"
72 # testing XRangeIterator
73 if xl < 10:
74 for x in xr:
75 if cnt == 0: first = x
76 if cnt == xl -1 : last = x
77 cnt += 1
78 # testing this[index]
79 if xl == 0: first2 = xr[0]
80 if xl > 1 : first2, last2 = xr[0], xr[xl - 1]
81
82 printwith("same", xr, xl, first, last, first2, last2)
83 except:
84 printwith("same", sys.exc_type)
85
86 def test_complex_ctor_str(self):
87 l = [ "-1", "0", "1", "+1", "+1.1", "-1.01", "-.101", ".234", "-1.3e3", "1.09e-3", "33.2e+10"] #, " ", ""] #http://ironpython.codeplex.com/workitem/28385
88
89
90 for s in l:
91 try:
92 printwith("case", complex_case_repr(s))
93 c = complex(s)
94 printwithtype(c)
95 except:
96 printwith("same", sys.exc_type, sys.exc_value)
97
98 s += "j"
99 try:
100 printwith("case", complex_case_repr(s))
101 c = complex(s)
102 printwithtype(c)
103 except:
104 printwith("same", sys.exc_type, sys.exc_value)
105
106 for s1 in l:
107 for s2 in l:
108 try:
109 if s2.startswith("+") or s2.startswith("-"):
110 s = "%s%sJ" % (s1, s2)
111 else:
112 s = "%s+%sj" % (s1, s2)
113
114 printwith("case", complex_case_repr(s))
115 c = complex(s)
116 printwithtype(c)
117 except:
118 printwith("same", sys.exc_type, sys.exc_value)
119
120 def test_complex_ctor(self):
121 # None is not included due to defaultvalue issue
122 ln = [-1, 1L, 1.5, 1.5e+5, 1+2j, -1-9.3j ]
123 ls = ["1", "1L", "-1.5", "1.5e+5", "-34-2j"]
124
125 la = []
126 la.extend(ln)
127 la.extend(ls)
128
129 for s in la:
130 try:
131 printwith("case", complex_case_repr(s))
132 c = complex(s)
133 printwithtype(c)
134 except:
135 printwith("same", sys.exc_type, sys.exc_value)
136
137 for s in la:
138 try:
139 printwith("case", "real only", complex_case_repr(s))
140 c = complex(real=s)
141 printwithtype(c)
142 except:
143 printwith("same", sys.exc_type, sys.exc_value)
144
145 for s in la:
146 try:
147 printwith("case", "imag only", complex_case_repr(s))
148 c = complex(imag=s)
149 printwithtype(c)
150 except:
151 printwith("same", sys.exc_type, sys.exc_value)
152
153 for s1 in la:
154 for s2 in ln:
155 try:
156 printwith("case", complex_case_repr(s1, s2))
157 c = complex(s1, s2)
158 printwithtype(c)
159 except:
160 printwith("same", sys.exc_type, sys.exc_value)
161
162 def test_bigint(self):
163 s = '1234567890'
164 for x in range(10): s += str(x) * x
165 s = s * 10
166
167 l = [7, 1001, 5.89, True]
168 for start in range(1, 50, 7):
169 startx = start
170 for length in [1, 20, 50, 60, 100]:
171 startx += 1
172 l.append(long(s[startx:startx + length]))
173
174 for x in l:
175 for y in l:
176 print x, y
177 printwith('case', '%s, %s' % (x, y))
178 printwith('same', x+y)
179 printwith('same', x-y)
180 printwith('same', x*y)
181 if y:
182 printwith('same', x/y)
183 t = divmod(x, y)
184 printwithtype(t[0])
185 printwithtype(t[1])
186
187 l.remove(5.89)
188 l.remove(True) #
189 for a in range(1, 100, 7):
190 for x in l:
191 for y in l:
192 if x and y:
193 printwith('case', a, x, y)
194 printwith('same', pow(a, x, y))
195
196 def test_file_mode(self):
197 disabled_modes = ['Ut+', 'rUt+', 'Urt+']
198 disabled_modes += ['Ut', 'U+t', 'rUt', 'rU+t', 'Urt', 'Ur+t'] #http://ironpython.codeplex.com/workitem/28386
199
200 arw = ['', 'a', 'r', 'w', 'U', 'rU', 'Ur', 'wU', 'Uw', 'Ua', 'aU']
201 bt = ['', 'b', 't']
202 plus = ['', '+']
203 modes = []
204
205 for x in arw:
206 for y in bt:
207 for z in plus:
208 modes.append(x + y + z)
209 for y in plus:
210 for z in bt:
211 modes.append(x + y + z)
212
213 modes = [x for x in modes if x not in disabled_modes]
214
215 filename = 'tempfile.txt'
216 for m in modes:
217 printwith('case', m)
218 try:
219 f = file(filename, m)
220 s = str(f)
221 atPos = s.find('at')
222 printwith('same', s[:atPos])
223 f.close()
224 except:
225 printwith("same", 'throw')
226
227runtests(test_builtin)