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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
|
cimport cython
def cython_set():
"""
>>> cython_set() is set
True
"""
assert set is cython.set
return cython.set
def cython_frozenset():
"""
>>> cython_frozenset() is frozenset
True
"""
assert frozenset is cython.frozenset
return cython.frozenset
def cython_set_override():
"""
>>> cython_set_override() is set
True
"""
set = 1
return cython.set
def cython_frozenset_override():
"""
>>> cython_frozenset_override() is frozenset
True
"""
frozenset = 1
return cython.frozenset
def test_set_literal():
"""
>>> type(test_set_literal()) is set
True
>>> sorted(test_set_literal())
['a', 'b', 1]
"""
cdef set s1 = {1,'a',1,'b','a'}
return s1
def test_set_add():
"""
>>> type(test_set_add()) is set
True
>>> sorted(test_set_add())
['a', 1, (1, 2)]
"""
cdef set s1
s1 = set([1, (1, 2)])
s1.add(1)
s1.add('a')
s1.add(1)
s1.add((1,2))
return s1
def test_set_contains(v):
"""
>>> test_set_contains(1)
True
>>> test_set_contains(2)
False
>>> test_set_contains(frozenset([1, 2, 3]))
True
>>> test_set_contains(frozenset([1, 2]))
False
>>> test_set_contains(set([1, 2, 3]))
True
>>> test_set_contains(set([1, 2]))
False
>>> try: test_set_contains([1, 2])
... except TypeError: pass
... else: print("NOT RAISED!")
"""
cdef set s1
s1 = set()
s1.add(1)
s1.add('a')
s1.add(frozenset([1, 2, 3]))
return v in s1
def test_set_update(v=None):
"""
>>> type(test_set_update()) is set
True
>>> sorted(test_set_update())
['a', 'b', 'c', 1, 2, (1, 2)]
>>> sorted(test_set_update([]))
['a', 'b', 'c', 1, 2, (1, 2)]
>>> try: test_set_update(object())
... except TypeError: pass
... else: print("NOT RAISED!")
"""
cdef set s1
s1 = set([1, (1, 2)])
s1.update((1,))
s1.update('abc')
s1.update(set([1]))
s1.update(frozenset((1,2)))
if v is not None:
s1.update(v)
return s1
def test_set_multi_update():
"""
>>> type(test_set_multi_update()) is set
True
>>> sorted(test_set_multi_update())
['a', 'b', 'c', 1, 2, 3]
"""
cdef set s1 = set()
s1.update('abc', set([1, 3]), frozenset([1, 2]))
return s1
def test_object_update(v=None):
"""
>>> type(test_object_update()) is set
True
>>> sorted(test_object_update())
['a', 'b', 'c', 1, 2, (1, 2)]
>>> sorted(test_object_update([]))
['a', 'b', 'c', 1, 2, (1, 2)]
>>> try: test_object_update(object())
... except TypeError: pass
... else: print("NOT RAISED!")
"""
cdef object s1
s1 = set([1, (1, 2)])
s1.update((1,))
s1.update('abc')
s1.update(set([1]))
s1.update(frozenset((1,2)))
if v is not None:
s1.update(v)
return s1
def test_set_clear():
"""
>>> type(test_set_clear()) is set
True
>>> list(test_set_clear())
[]
"""
cdef set s1
s1 = set([1])
s1.clear()
return s1
def test_set_clear_None():
"""
>>> test_set_clear_None()
Traceback (most recent call last):
AttributeError: 'NoneType' object has no attribute 'clear'
"""
cdef set s1 = None
s1.clear()
def test_set_list_comp():
"""
>>> type(test_set_list_comp()) is set
True
>>> sorted(test_set_list_comp())
[0, 1, 2]
"""
cdef set s1
s1 = set([i%3 for i in range(5)])
return s1
def test_frozenset_list_comp():
"""
>>> type(test_frozenset_list_comp()) is frozenset
True
>>> sorted(test_frozenset_list_comp())
[0, 1, 2]
"""
cdef frozenset s1
s1 = frozenset([i%3 for i in range(5)])
return s1
def test_set_pop():
"""
>>> type(test_set_pop()) is set
True
>>> list(test_set_pop())
[]
"""
cdef set s1
s1 = set()
s1.add('2')
two = s1.pop()
return s1
@cython.test_fail_if_path_exists("//SimpleCallNode//NameNode")
def test_object_pop(s):
"""
>>> s = set([2])
>>> test_object_pop(s)
2
>>> list(s)
[]
"""
return s.pop()
def test_noop_pop():
"""
>>> test_noop_pop()
"""
set([0]).pop()
def test_noop_pop_exception():
"""
>>> try: test_noop_pop_exception()
... except KeyError: pass
... else: print("KeyError expected but not raised!")
"""
set([]).pop()
def test_set_discard():
"""
>>> type(test_set_discard()) is set
True
>>> sorted(test_set_discard())
['12', 233]
"""
cdef set s1
s1 = set()
s1.add('12')
s1.add(3)
s1.add(233)
s1.discard('3')
s1.discard(3)
return s1
def test_set_sideeffect_unhashable_failure():
"""
>>> test_set_sideeffect_unhashable_failure()
[2, 4, 5]
"""
L = []
def sideeffect(x):
L.append(x)
return x
def unhashable_value(x):
L.append(x)
return set()
try:
s = set([1,sideeffect(2),3,unhashable_value(4),sideeffect(5)])
except TypeError: pass
else: assert False, "expected exception not raised"
return L
def test_set_sideeffect_unhashable_failure_literal():
"""
>>> test_set_sideeffect_unhashable_failure_literal()
[2, 4, 5]
"""
L = []
def sideeffect(x):
L.append(x)
return x
def unhashable_value(x):
L.append(x)
return set()
try:
s = {1,sideeffect(2),3,unhashable_value(4),sideeffect(5)}
except TypeError: pass
else: assert False, "expected exception not raised"
return L
def test_frozenset_sideeffect_unhashable_failure():
"""
>>> test_frozenset_sideeffect_unhashable_failure()
[2, 4, 5]
"""
L = []
def sideeffect(x):
L.append(x)
return x
def unhashable_value(x):
L.append(x)
return set()
try:
s = frozenset([1,sideeffect(2),3,unhashable_value(4),sideeffect(5)])
except TypeError: pass
else: assert False, "expected exception not raised"
return L
@cython.test_assert_path_exists("//SetNode")
@cython.test_fail_if_path_exists(
"//SimpleCallNode",
"//PythonCapiCallNode"
)
def test_set_of_list():
"""
>>> s = test_set_of_list()
>>> isinstance(s, set)
True
>>> sorted(s)
[1, 2, 3]
"""
return set([1, 2, 3])
@cython.test_assert_path_exists("//PythonCapiCallNode")
@cython.test_fail_if_path_exists("//SetNode")
def test_frozenset_of_list():
"""
>>> s = test_frozenset_of_list()
>>> isinstance(s, frozenset)
True
>>> sorted(s)
[1, 2, 3]
"""
return frozenset([1, 2, 3])
@cython.test_assert_path_exists("//SetNode")
@cython.test_fail_if_path_exists("//SimpleCallNode")
def test_set_of_tuple():
"""
>>> s = test_set_of_tuple()
>>> isinstance(s, set)
True
>>> sorted(s)
[1, 2, 3]
"""
return set((1, 2, 3))
@cython.test_assert_path_exists("//PythonCapiCallNode")
@cython.test_fail_if_path_exists("//SetNode")
def test_frozenset_of_tuple():
"""
>>> s = test_frozenset_of_tuple()
>>> isinstance(s, frozenset)
True
>>> sorted(s)
[1, 2, 3]
"""
return frozenset((1, 2, 3))
@cython.test_assert_path_exists("//PythonCapiCallNode")
@cython.test_fail_if_path_exists(
"//SimpleCallNode",
"//SetNode"
)
def test_set_of_iterable(x):
"""
>>> s = test_set_of_iterable([1, 2, 3])
>>> isinstance(s, set)
True
>>> sorted(s)
[1, 2, 3]
"""
return set(x)
@cython.test_assert_path_exists("//PythonCapiCallNode")
@cython.test_fail_if_path_exists(
"//SimpleCallNode",
"//SetNode"
)
def test_frozenset_of_iterable(x):
"""
>>> s = test_frozenset_of_iterable([1, 2, 3])
>>> isinstance(s, frozenset)
True
>>> sorted(s)
[1, 2, 3]
>>> s = test_frozenset_of_iterable(frozenset([1, 2, 3]))
>>> isinstance(s, frozenset)
True
>>> sorted(s)
[1, 2, 3]
"""
return frozenset(x)
@cython.test_assert_path_exists("//PythonCapiCallNode")
@cython.test_fail_if_path_exists(
"//SimpleCallNode",
"//SetNode"
)
def test_empty_frozenset():
"""
>>> s = test_empty_frozenset()
>>> isinstance(s, frozenset)
True
>>> len(s)
0
>>> import sys
>>> sys.version_info >= (3, 10) or s is frozenset() # singleton (in Python < 3.10)!
True
"""
return frozenset()
@cython.test_fail_if_path_exists(
'//ListNode//ListNode',
'//ListNode//PythonCapiCallNode//PythonCapiCallNode',
'//ListNode//SimpleCallNode//SimpleCallNode',
)
def test_singleton_empty_frozenset():
"""
>>> import sys
>>> test_singleton_empty_frozenset() if sys.version_info < (3, 10) else 1 # from CPython's test_set.py
1
"""
f = frozenset()
efs = [frozenset(), frozenset([]), frozenset(()), frozenset(''),
frozenset(), frozenset([]), frozenset(()), frozenset(''),
frozenset(range(0)), frozenset(frozenset()),
frozenset(f), f]
return len(set(map(id, efs))) # note, only a singleton in Python <3.10
def sorted(it):
# Py3 can't compare different types
chars = []
nums = []
tuples = []
for item in it:
if type(item) is int:
nums.append(item)
elif type(item) is tuple:
tuples.append(item)
else:
chars.append(item)
nums.sort()
chars.sort()
tuples.sort()
return chars+nums+tuples
|