PageRenderTime 22ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_1_1/Src/Tests/compat/sbs_simple_compare.py

#
Python | 186 lines | 140 code | 26 blank | 20 comment | 21 complexity | e92a0d793d5c834f52fae5740d1cea7a MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. #####################################################################################
  2. #
  3. # Copyright (c) Microsoft Corporation.
  4. #
  5. # This source code is subject to terms and conditions of the Microsoft Public
  6. # License. A copy of the license can be found in the License.html file at the
  7. # root of this distribution. If you cannot locate the Microsoft Public
  8. # License, please send an email to dlr@microsoft.com. By using this source
  9. # code in any fashion, you are agreeing to be bound by the terms of the
  10. # Microsoft Public License.
  11. #
  12. # You must not remove this notice, or any other, from this software.
  13. #
  14. #####################################################################################
  15. from common import *
  16. import testdata
  17. # comparison among python numbers
  18. class oldstyle:
  19. def __init__(self, value):
  20. self.value = value
  21. def __cmp__(self, other):
  22. return cmp(self.value, other)
  23. def __repr__(self):
  24. return "oldstyle(%s)" % str(self.value)
  25. class newstyle(object):
  26. def __init__(self, value):
  27. self.value = value
  28. def __cmp__(self, other):
  29. return cmp(self.value, other)
  30. def __repr__(self):
  31. return "newstyle(%s)" % str(self.value)
  32. import sys
  33. collection = testdata.merge_lists(
  34. [None],
  35. testdata.list_int,
  36. testdata.list_float,
  37. testdata.list_long,
  38. testdata.list_bool,
  39. testdata.list_myint,
  40. testdata.list_myfloat,
  41. testdata.list_mylong,
  42. testdata.get_clrnumbers(),
  43. )
  44. collection_oldstyle = [oldstyle(x) for x in collection]
  45. collection_newstyle = [newstyle(x) for x in collection]
  46. class common(object):
  47. def true_compare(self, leftc, rightc, oplist = ["<", ">", ">=", "<=", "==", "<>" ]):
  48. for a in leftc:
  49. for b in rightc:
  50. for op in oplist:
  51. try:
  52. printwith("case", a, op, b, type(a), type(b))
  53. printwith("same", eval("a %s b" % op))
  54. except:
  55. printwith("except", sys.exc_type)
  56. try:
  57. printwith("case", "cmp(", a, ",", b, ")", type(a), type(b))
  58. printwith("same", cmp(a, b))
  59. except:
  60. printwith("except", sys.exc_type)
  61. def compare_asbool(self, leftc, rightc, oplist = ["<", ">", ">=", "<=", "==", "<>" ]):
  62. for a in leftc:
  63. for b in rightc:
  64. for op in oplist:
  65. line = "if a %s b: print 'same##', True\nelse: print 'same##', False" % op
  66. try:
  67. printwith("case", "RetBool", a, op, b, type(a), type(b))
  68. exec line
  69. except:
  70. printwith("except", sys.exc_type)
  71. class test_simple(common):
  72. def __init__(self):
  73. self.collection = collection
  74. self.collection_oldstyle = collection_oldstyle
  75. self.collection_newstyle = collection_newstyle
  76. def test_true_compare(self): super(test_simple, self).true_compare(self.collection, self.collection)
  77. def test_compare_asbool(self): super(test_simple, self).compare_asbool(self.collection, self.collection)
  78. def test_true_compare_oldc_left(self): super(test_simple, self).true_compare(self.collection_oldstyle, self.collection)
  79. def test_compare_asbool_oldc_left(self): super(test_simple, self).compare_asbool(self.collection_oldstyle, self.collection)
  80. def test_true_compare_oldc_right(self): super(test_simple, self).true_compare(self.collection, self.collection_oldstyle)
  81. def test_compare_asbool_oldc_right(self): super(test_simple, self).compare_asbool(self.collection, self.collection_oldstyle)
  82. def test_true_compare_newc_left(self): super(test_simple, self).true_compare(self.collection_newstyle, self.collection)
  83. def test_compare_asbool_newc_left(self): super(test_simple, self).compare_asbool(self.collection_newstyle, self.collection)
  84. def test_true_compare_newc_right(self): super(test_simple, self).true_compare(self.collection, self.collection_newstyle)
  85. def test_compare_asbool_newc_right(self): super(test_simple, self).compare_asbool(self.collection, self.collection_newstyle)
  86. class test_enum(test_simple):
  87. def __init__(self):
  88. self.collection = testdata.merge_lists(testdata.get_enums(), testdata.list_bool, testdata.list_int)
  89. self.collection_oldstyle = [oldstyle(x) for x in self.collection]
  90. self.collection_newstyle = [newstyle(x) for x in self.collection]
  91. class test_onetype(common):
  92. def test_true_compare_pos(self):
  93. super(test_onetype, self).true_compare(self.data, self.data)
  94. super(test_onetype, self).true_compare(self.data, [newstyle(x) for x in self.data])
  95. super(test_onetype, self).true_compare([newstyle(x) for x in self.data], self.data)
  96. super(test_onetype, self).true_compare(self.data, [oldstyle(x) for x in self.data])
  97. super(test_onetype, self).true_compare([oldstyle(x) for x in self.data], self.data)
  98. def test_compare_asbool_pos(self):
  99. super(test_onetype, self).compare_asbool(self.data, self.data)
  100. super(test_onetype, self).compare_asbool(self.data, [newstyle(x) for x in self.data])
  101. super(test_onetype, self).compare_asbool([newstyle(x) for x in self.data], self.data)
  102. super(test_onetype, self).compare_asbool(self.data, [oldstyle(x) for x in self.data])
  103. super(test_onetype, self).compare_asbool([oldstyle(x) for x in self.data], self.data)
  104. def xtest_true_compare_neg(self):
  105. # comparing to None is still valid
  106. super(test_onetype, self).true_compare(self.data, collection)
  107. super(test_onetype, self).true_compare(collection, self.data)
  108. super(test_onetype, self).true_compare(self.data, collection_newstyle)
  109. super(test_onetype, self).true_compare(self.data, collection_oldstyle)
  110. super(test_onetype, self).true_compare(collection_newstyle, self.data)
  111. super(test_onetype, self).true_compare(collection_oldstyle, self.data)
  112. def xtest_compare_asbool_neg(self):
  113. super(test_onetype, self).compare_asbool(self.data, collection)
  114. super(test_onetype, self).compare_asbool(collection, self.data)
  115. super(test_onetype, self).compare_asbool(self.data, collection_newstyle)
  116. super(test_onetype, self).compare_asbool(self.data, collection_oldstyle)
  117. super(test_onetype, self).compare_asbool(collection_newstyle, self.data)
  118. super(test_onetype, self).compare_asbool(collection_oldstyle, self.data)
  119. class test_string(test_onetype):
  120. def __init__(self):
  121. self.data = testdata.merge_lists(testdata.list_str, testdata.list_mystr)
  122. class test_complex(test_onetype):
  123. def __init__(self):
  124. self.data = testdata.merge_lists(testdata.list_complex, testdata.list_mycomplex)
  125. class test_dict(test_onetype):
  126. def __init__(self):
  127. self.data = testdata.merge_lists(testdata.list_dict)
  128. runtests(test_simple)
  129. #guess this is not going to happen
  130. #runtests(test_enum)
  131. runtests(test_string)
  132. runtests(test_complex)
  133. runtests(test_dict)
  134. # redefine oldstyle/newstyle, since cmp does not work on set by spec
  135. class oldstyle:
  136. def __init__(self, value): self.value = value
  137. def __lt__(self, other): return self.value < other
  138. def __gt__(self, other): return self.value > other
  139. def __le__(self, other): return self.value <= other
  140. def __ge__(self, other): return self.value >= other
  141. def __eq__(self, other): return self.value == other
  142. def __ne__(self, other): return self.value <> other
  143. def __repr__(self): return "oldstyle(%s)" % str(self.value)
  144. class newstyle(object):
  145. def __init__(self, value): self.value = value
  146. def __lt__(self, other): return self.value < other
  147. def __gt__(self, other): return self.value > other
  148. def __le__(self, other): return self.value <= other
  149. def __ge__(self, other): return self.value >= other
  150. def __eq__(self, other): return self.value == other
  151. def __ne__(self, other): return self.value <> other
  152. def __repr__(self): return "newstyle(%s)" % str(self.value)
  153. class test_set(test_onetype):
  154. def __init__(self):
  155. self.data = testdata.merge_lists(
  156. [None],
  157. testdata.list_set,
  158. testdata.list_frozenset,
  159. )
  160. #runtests(test_set)