PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_2_0/Src/Tests/test_tuple.py

#
Python | 157 lines | 101 code | 37 blank | 19 comment | 5 complexity | efed797fa0d3a5d3fa39793db41eab00 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. All rights reserved.
  4. #
  5. # This source code is subject to terms and conditions of the Microsoft Public License. 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 Microsoft Public License, 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 Microsoft Public License.
  10. #
  11. # You must not remove this notice, or any other, from this software.
  12. #
  13. #
  14. #####################################################################################
  15. from iptest.assert_util import *
  16. # Disallow assignment to empty tuple
  17. def test_assign_to_empty():
  18. y = ()
  19. AssertError(SyntaxError, compile, "() = y", "Error", "exec")
  20. AssertError(SyntaxError, compile, "(), t = y, 0", "Error", "exec")
  21. AssertError(SyntaxError, compile, "((()))=((y))", "Error", "exec")
  22. del y
  23. # Disallow unequal unpacking assignment
  24. def test_unpack():
  25. tupleOfSize2 = (1, 2)
  26. def f1(): (a, b, c) = tupleOfSize2
  27. def f2(): del a
  28. AssertError(ValueError, f1)
  29. AssertError(NameError, f2)
  30. (a) = tupleOfSize2
  31. AreEqual(a, tupleOfSize2)
  32. del a
  33. (a, (b, c)) = (tupleOfSize2, tupleOfSize2)
  34. AreEqual(a, tupleOfSize2)
  35. AreEqual(b, 1)
  36. AreEqual(c, 2)
  37. del a, b, c
  38. ((a, b), c) = (tupleOfSize2, tupleOfSize2)
  39. AreEqual(a, 1)
  40. AreEqual(b, 2)
  41. AreEqual(c, tupleOfSize2)
  42. del a, b, c
  43. def test_add_mul():
  44. AreEqual((1,2,3) + (4,5,6), (1,2,3,4,5,6))
  45. AreEqual((1,2,3) * 2, (1,2,3,1,2,3))
  46. AreEqual(2 * (1,2,3), (1,2,3,1,2,3))
  47. class mylong(long): pass
  48. AreEqual((1,2) * mylong(2L), (1, 2, 1, 2))
  49. AreEqual((3, 4).__mul__(mylong(2L)), (3, 4, 3, 4))
  50. AreEqual((5, 6).__rmul__(mylong(2L)), (5, 6, 5, 6))
  51. AreEqual(mylong(2L) * (7,8) , (7, 8, 7, 8))
  52. class mylong2(long):
  53. def __rmul__(self, other):
  54. return 42
  55. AreEqual((1,2) * mylong2(2l), 42) # this one uses __rmul__
  56. #TODO AreEqual((3, 4).__mul__(mylong2(2L)), (3, 4, 3, 4))
  57. AreEqual((5, 6).__rmul__(mylong2(2L)), (5, 6, 5, 6))
  58. AreEqual(mylong2(2L) * (7,8) , (7, 8, 7, 8))
  59. def test_tuple_hash():
  60. class myhashable(object):
  61. def __init__(self):
  62. self.hashcalls = 0
  63. def __hash__(self):
  64. self.hashcalls += 1
  65. return 42
  66. def __eq__(self, other):
  67. return type(self) == type(other)
  68. test = (myhashable(), myhashable(), myhashable())
  69. hash(test)
  70. AreEqual(test[0].hashcalls, 1)
  71. AreEqual(test[1].hashcalls, 1)
  72. AreEqual(test[2].hashcalls, 1)
  73. hashes = set()
  74. for i in range(1000):
  75. for j in range(1000):
  76. hashes.add(hash((i, j)))
  77. AreEqual(len(hashes), 1000000)
  78. @skip('win32')
  79. def test_tuple_cli_interactions():
  80. # verify you can call ToString on a tuple after importing clr
  81. import clr
  82. a = (0,)
  83. AreEqual(str(a), a.ToString())
  84. def test_sequence_assign():
  85. try:
  86. a, b = None
  87. AssertUnreachable()
  88. except TypeError, e:
  89. AreEqual(e.message, "'NoneType' object is not iterable")
  90. def test_sort():
  91. # very simple test for sorting lists of tuples
  92. s=[(3,0),(1,2),(1,1)]
  93. t=s[:]
  94. t.sort()
  95. AreEqual(sorted(s),t)
  96. AreEqual(t,[(1,1),(1,2),(3,0)])
  97. def test_indexing():
  98. t = (2,3,4)
  99. AssertError(TypeError, lambda : t[2.0])
  100. class mytuple(tuple):
  101. def __getitem__(self, index):
  102. return tuple.__getitem__(self, int(index))
  103. t = mytuple(t)
  104. AreEqual(t[2.0], 4)
  105. def test_tuple_slicing():
  106. l = [0, 1, 2, 3, 4]
  107. u = tuple(l)
  108. AreEqual(u[-100:100:-1], ())
  109. def test_tuple_iteration():
  110. class T(tuple):
  111. def __getitem__(self):
  112. return None
  113. for x in T((1,)):
  114. AreEqual(x, 1)
  115. def test_mul_subclass():
  116. class subclass(tuple):
  117. pass
  118. u = subclass([0,1])
  119. Assert(u is not u*1)
  120. run_test(__name__)