/DLR_Main/Languages/IronPython/Tests/compat/sbs_builtin.py

https://bitbucket.org/mdavid/dlr · Python · 227 lines · 172 code · 33 blank · 22 comment · 55 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. from common import *
  16. import testdata
  17. import sys
  18. def complex_case_repr(*args):
  19. ret = "complex with "
  20. for x in args:
  21. ret += "'%s (%s)'" % (str(x), type(x))
  22. return ret
  23. class test_builtin(object):
  24. ''' test built-in type, etc '''
  25. def test_slice(self):
  26. ''' currently mainly test
  27. del list[slice]
  28. '''
  29. test_str = testdata.long_string
  30. str_len = len(test_str)
  31. choices = ['', 0]
  32. 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]
  33. numbers = numbers[::3] # Temporary approach to speed things up...
  34. choices.extend(numbers)
  35. choices.extend([-1 * x for x in numbers])
  36. for x in choices:
  37. for y in choices:
  38. for z in choices:
  39. if z == 0: continue
  40. line = "l = list(test_str); del l[%s:%s:%s]" % (str(x), str(y), str(z))
  41. exec line
  42. printwith("case", "del l[%s:%s:%s]" % (str(x), str(y), str(z)))
  43. printwith("same", eval("l"), eval("len(l)"))
  44. def test_xrange(self):
  45. ''' test xrange with corner cases'''
  46. import sys
  47. maxint = sys.maxint
  48. numbers = [1, 2, maxint/2, maxint-1, maxint, maxint+1, maxint+2]
  49. choices = [0]
  50. choices.extend(numbers)
  51. choices.extend([-1 * x for x in numbers])
  52. for x in choices:
  53. for y in choices:
  54. for z in choices:
  55. line = "xrange(%s, %s, %s)" % (str(x), str(y), str(z))
  56. printwith("case", line)
  57. try:
  58. xr = eval(line)
  59. xl = len(xr)
  60. cnt = 0
  61. first = last = first2 = last2 = "n/a"
  62. # testing XRangeIterator
  63. if xl < 10:
  64. for x in xr:
  65. if cnt == 0: first = x
  66. if cnt == xl -1 : last = x
  67. cnt += 1
  68. # testing this[index]
  69. if xl == 0: first2 = xr[0]
  70. if xl > 1 : first2, last2 = xr[0], xr[xl - 1]
  71. printwith("same", xr, xl, first, last, first2, last2)
  72. except:
  73. printwith("same", sys.exc_type)
  74. def test_complex_ctor_str(self):
  75. 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
  76. for s in l:
  77. try:
  78. printwith("case", complex_case_repr(s))
  79. c = complex(s)
  80. printwithtype(c)
  81. except:
  82. printwith("same", sys.exc_type, sys.exc_value)
  83. s += "j"
  84. try:
  85. printwith("case", complex_case_repr(s))
  86. c = complex(s)
  87. printwithtype(c)
  88. except:
  89. printwith("same", sys.exc_type, sys.exc_value)
  90. for s1 in l:
  91. for s2 in l:
  92. try:
  93. if s2.startswith("+") or s2.startswith("-"):
  94. s = "%s%sJ" % (s1, s2)
  95. else:
  96. s = "%s+%sj" % (s1, s2)
  97. printwith("case", complex_case_repr(s))
  98. c = complex(s)
  99. printwithtype(c)
  100. except:
  101. printwith("same", sys.exc_type, sys.exc_value)
  102. def test_complex_ctor(self):
  103. # None is not included due to defaultvalue issue
  104. ln = [-1, 1L, 1.5, 1.5e+5, 1+2j, -1-9.3j ]
  105. ls = ["1", "1L", "-1.5", "1.5e+5", "-34-2j"]
  106. la = []
  107. la.extend(ln)
  108. la.extend(ls)
  109. for s in la:
  110. try:
  111. printwith("case", complex_case_repr(s))
  112. c = complex(s)
  113. printwithtype(c)
  114. except:
  115. printwith("same", sys.exc_type, sys.exc_value)
  116. for s in la:
  117. try:
  118. printwith("case", "real only", complex_case_repr(s))
  119. c = complex(real=s)
  120. printwithtype(c)
  121. except:
  122. printwith("same", sys.exc_type, sys.exc_value)
  123. for s in la:
  124. try:
  125. printwith("case", "imag only", complex_case_repr(s))
  126. c = complex(imag=s)
  127. printwithtype(c)
  128. except:
  129. printwith("same", sys.exc_type, sys.exc_value)
  130. for s1 in la:
  131. for s2 in ln:
  132. try:
  133. printwith("case", complex_case_repr(s1, s2))
  134. c = complex(s1, s2)
  135. printwithtype(c)
  136. except:
  137. printwith("same", sys.exc_type, sys.exc_value)
  138. def test_bigint(self):
  139. s = '1234567890'
  140. for x in range(10): s += str(x) * x
  141. s = s * 10
  142. l = [7, 1001, 5.89, True]
  143. for start in range(1, 50, 7):
  144. startx = start
  145. for length in [1, 20, 50, 60, 100]:
  146. startx += 1
  147. l.append(long(s[startx:startx + length]))
  148. for x in l:
  149. for y in l:
  150. print x, y
  151. printwith('case', '%s, %s' % (x, y))
  152. printwith('same', x+y)
  153. printwith('same', x-y)
  154. printwith('same', x*y)
  155. if y:
  156. printwith('same', x/y)
  157. t = divmod(x, y)
  158. printwithtype(t[0])
  159. printwithtype(t[1])
  160. l.remove(5.89)
  161. l.remove(True) #
  162. for a in range(1, 100, 7):
  163. for x in l:
  164. for y in l:
  165. if x and y:
  166. printwith('case', a, x, y)
  167. printwith('same', pow(a, x, y))
  168. def test_file_mode(self):
  169. disabled_modes = ['Ut+', 'rUt+', 'Urt+']
  170. disabled_modes += ['Ut', 'U+t', 'rUt', 'rU+t', 'Urt', 'Ur+t'] #http://ironpython.codeplex.com/workitem/28386
  171. arw = ['', 'a', 'r', 'w', 'U', 'rU', 'Ur', 'wU', 'Uw', 'Ua', 'aU']
  172. bt = ['', 'b', 't']
  173. plus = ['', '+']
  174. modes = []
  175. for x in arw:
  176. for y in bt:
  177. for z in plus:
  178. modes.append(x + y + z)
  179. for y in plus:
  180. for z in bt:
  181. modes.append(x + y + z)
  182. modes = [x for x in modes if x not in disabled_modes]
  183. filename = 'tempfile.txt'
  184. for m in modes:
  185. printwith('case', m)
  186. try:
  187. f = file(filename, m)
  188. s = str(f)
  189. atPos = s.find('at')
  190. printwith('same', s[:atPos])
  191. f.close()
  192. except:
  193. printwith("same", 'throw')
  194. runtests(test_builtin)