PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Languages/IronPython/Tests/compat/sbs_true_division.py

http://github.com/IronLanguages/main
Python | 114 lines | 82 code | 16 blank | 16 comment | 12 complexity | 3b7e3a5d2663c2a98772183aaf0fb3f1 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  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 __future__ import division
  16. # not all are testing true division
  17. import sys
  18. from common import *
  19. import testdata
  20. collection = testdata.merge_lists(
  21. [None],
  22. testdata.list_int,
  23. testdata.list_float,
  24. testdata.list_bool,
  25. testdata.list_long,
  26. testdata.list_complex,
  27. testdata.list_myint,
  28. testdata.list_myfloat,
  29. testdata.list_mylong,
  30. testdata.list_mycomplex,
  31. )
  32. class oldstyle:
  33. def __init__(self, value):
  34. self.value = value
  35. def __truediv__(self, other):
  36. return self.value / other
  37. def __rtruediv__(self, other):
  38. return other / self.value
  39. def __itruediv__(self, other):
  40. # left side is no longer an oldstyle instance after /=
  41. return self.value / other
  42. def __repr__(self):
  43. return "oldstyle(%s)" % str(self.value)
  44. class newstyle(object):
  45. def __init__(self, value):
  46. self.value = value
  47. def __truediv__(self, other):
  48. return self.value / other
  49. def __rtruediv__(self, other):
  50. return other / self.value
  51. def __itruediv__(self, other):
  52. return self.value / other
  53. def __repr__(self):
  54. return "newstyle(%s)" % str(self.value)
  55. collection_oldstyle = [oldstyle(x) for x in collection]
  56. collection_newstyle = [newstyle(x) for x in collection]
  57. def clone_list(l):
  58. l2 = []
  59. for x in l:
  60. if x is newstyle:
  61. l2.append(newstyle(x.value))
  62. elif x is oldstyle:
  63. l2.append(oldstyle(x.value))
  64. else :
  65. l2.append(x)
  66. return l2
  67. class common(object):
  68. def division(self, leftc, rightc):
  69. for x in leftc:
  70. for y in rightc:
  71. printwith("case", x, "/", y, type(x), type(y))
  72. try:
  73. ret = x / y
  74. printwithtype(ret)
  75. except:
  76. printwith("same", sys.exc_type)
  77. def inplace(self, leftc, rightc):
  78. rc = clone_list(rightc)
  79. for y in rc:
  80. for x in clone_list(leftc):
  81. printwith("case", x, "/=", y, type(x), type(y))
  82. try:
  83. x /= y
  84. printwithtype(x)
  85. except:
  86. printwith("same", sys.exc_type)
  87. class test_division(common):
  88. def test_simple(self): super(test_division, self).division(collection, collection)
  89. def test_class_ol(self): super(test_division, self).division(collection_oldstyle, collection)
  90. def test_class_or(self): super(test_division, self).division(collection, collection_oldstyle)
  91. def test_class_nl(self): super(test_division, self).division(collection_newstyle, collection)
  92. def test_class_nr(self): super(test_division, self).division(collection, collection_newstyle)
  93. def test_ip_simple(self): super(test_division, self).inplace(collection, collection)
  94. def test_ip_class_ol(self): super(test_division, self).inplace(collection_oldstyle, collection)
  95. def test_ip_class_or(self): super(test_division, self).inplace(collection, collection_oldstyle)
  96. def test_ip_class_nl(self): super(test_division, self).inplace(collection_newstyle, collection)
  97. def test_ip_class_nr(self): super(test_division, self).inplace(collection, collection_newstyle)
  98. runtests(test_division)