1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
import unittest
import random
import threading
import time
from test import test_support
if test_support.is_jython:
from java.util import ArrayList
from java.lang import String
class ListTestCase(unittest.TestCase):
def test_recursive_list_slices(self):
x = [1,2,3,4,5]
x[1:] = x
self.assertEquals(x, [1, 1, 2, 3, 4, 5],
"Recursive assignment to list slices failed")
def test_subclass_richcmp(self):
# http://bugs.jython.org/issue1115
class Foo(list):
def __init__(self, dotstring):
list.__init__(self, map(int, dotstring.split(".")))
bar1 = Foo('1.2.3')
bar2 = Foo('1.2.4')
self.assert_(bar1 < bar2)
self.assert_(bar1 <= bar2)
self.assert_(bar2 > bar1)
self.assert_(bar2 >= bar1)
def test_setget_override(self):
if not test_support.is_jython:
return
# http://bugs.jython.org/issue600790
class GoofyListMapThing(ArrayList):
def __init__(self):
self.silly = "Nothing"
def __setitem__(self, key, element):
self.silly = "spam"
def __getitem__(self, key):
self.silly = "eggs"
glmt = GoofyListMapThing()
glmt['my-key'] = String('el1')
self.assertEquals(glmt.silly, "spam")
glmt['my-key']
self.assertEquals(glmt.silly, "eggs")
def test_tuple_equality(self):
self.assertEqual([(1,), [1]].count([1]), 1) # http://bugs.jython.org/issue1317
class ThreadSafetyTestCase(unittest.TestCase):
def run_threads(self, f, num=10):
threads = []
for i in xrange(num):
t = threading.Thread(target=f)
t.start()
threads.append(t)
timeout = 10. # be especially generous
for t in threads:
t.join(timeout)
timeout = 0.
for t in threads:
self.assertFalse(t.isAlive())
def test_append_remove(self):
# derived from Itamar Shtull-Trauring's test for issue 521701
lst = []
def tester():
ct = threading.currentThread()
for i in range(1000):
lst.append(ct)
time.sleep(0.0001)
lst.remove(ct)
self.run_threads(tester)
self.assertEqual(lst, [])
def test_sort(self):
lst = []
def tester():
ct = threading.currentThread()
for i in range(1000):
lst.append(ct)
lst.sort()
lst.remove(ct)
time.sleep(0.0001)
self.run_threads(tester)
self.assertEqual(lst, [])
def test_count_reverse(self):
lst = [0,1,2,3,4,5,6,7,8,9,10,0]
def tester():
ct = threading.currentThread()
for i in range(1000):
self.assertEqual(lst[0], 0)
if random.random() > 0.5:
time.sleep(0.0001)
lst.reverse()
self.assertEqual(lst.count(0), 2)
self.assert_(lst[1] in (1,10))
self.run_threads(tester)
class ExtendedSliceTestCase(unittest.TestCase):
'''Really thrash extended slice operations on list.'''
type2test = list
def test_extended_slice_delete(self):
# Based on list_tests.CommonTest.test_extendedslicing .
# In the cited test case, the stop value is nearly always the default
# (None), meaning the end of the list, and often the start value is too.
# This contributed to the release of http://bugs.jython.org/issue1873 .
# This is a supplementary test focused on correct stopping.
initial = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13]
expected1 = self.type2test([ 0, 1, 2, 3, 4, 5, 10,11,12,13])
expected2 = self.type2test([ 0, 1, 2, 4, 6, 8, 10, 12,13])
expected4 = self.type2test([ 0, 1, 3, 4, 5, 7, 8, 9, 11,12,13])
# Positive step
a = self.type2test(initial)
del a[6:10:1]
self.assertEqual(a, expected1)
a = self.type2test(initial)
del a[3:13:2]
self.assertEqual(a, expected2)
a = self.type2test(initial)
del a[3:12:2]
self.assertEqual(a, expected2)
a = self.type2test(initial)
del a[2:11:4]
self.assertEqual(a, expected4)
a = self.type2test(initial)
del a[2::4]
self.assertEqual(a, expected4)
# Negative step (same results)
a = self.type2test(initial)
del a[9:5:-1]
self.assertEqual(a, expected1)
a = self.type2test(initial)
del a[11:1:-2]
self.assertEqual(a, expected2)
a = self.type2test(initial)
del a[11:2:-2]
self.assertEqual(a, expected2)
a = self.type2test(initial)
del a[10:1:-4]
self.assertEqual(a, expected4)
a = self.type2test(initial)
del a[10::-4]
self.assertEqual(a, expected4)
def test_extended_slice_assign(self):
# Based on list_tests.CommonTest.test_extendedslicing .
# In the cited test case, the stop value is nearly always the default.
# This is a supplementary test focused on correct stopping.
aa, bb, cc = 91, 92, 93
src = self.type2test([aa,bb,cc])
initial = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13]
expected1 = self.type2test([ 0, 1, 2, 3, 4, 5,aa,bb,cc, 9,10,11,12,13])
expected2 = self.type2test([ 0, 1, 2,aa, 4,bb, 6,cc, 8, 9,10,11,12,13])
expected4 = self.type2test([ 0, 1,aa, 3, 4, 5,bb, 7, 8, 9,cc,11,12,13])
# Positive step
a = self.type2test(initial)
a[6:9:1] = src
self.assertEqual(a, expected1)
a = self.type2test(initial)
a[3:9:2] = src
self.assertEqual(a, expected2)
a = self.type2test(initial)
a[3:8:2] = src
self.assertEqual(a, expected2)
a = self.type2test(initial)
a[2:11:4] = src
self.assertEqual(a, expected4)
a = self.type2test(initial)
a[2::4] = src
self.assertEqual(a, expected4)
# Negative step (same results, as src is reversed)
src.reverse()
a = self.type2test(initial)
a[8:5:-1] = src
self.assertEqual(a, expected1)
a = self.type2test(initial)
a[7:2:-2] = src
self.assertEqual(a, expected2)
a = self.type2test(initial)
a[7:1:-2] = src
self.assertEqual(a, expected2)
a = self.type2test(initial)
a[10:1:-4] = src
self.assertEqual(a, expected4)
a = self.type2test(initial)
a[10::-4] = src
self.assertEqual(a, expected4)
def test_main():
test_support.run_unittest(ListTestCase,
ThreadSafetyTestCase,
ExtendedSliceTestCase)
if __name__ == "__main__":
test_main()
|