38398c104938cff12f9540e72e1cef1a6f115138
[cython.git] / tests / run / setcomp.pyx
1 __doc__ = u"""
2 >>> type(smoketest_set()) is not list
3 True
4 >>> type(smoketest_set()) is _set
5 True
6 >>> type(smoketest_list()) is _set
7 True
8
9 >>> sorted(smoketest_set())
10 [0, 4, 8]
11 >>> sorted(smoketest_list())
12 [0, 4, 8]
13
14 >>> list(typed())
15 [A, A, A]
16 >>> sorted(iterdict())
17 [1, 2, 3]
18 """
19
20 cimport cython
21
22 # Py2.3 doesn't have the set type, but Cython does :)
23 _set = set
24
25 def smoketest_set():
26     return { x*2
27              for x in range(5)
28              if x % 2 == 0 }
29
30 @cython.test_fail_if_path_exists("//SimpleCallNode//ComprehensionNode")
31 @cython.test_assert_path_exists("//ComprehensionNode",
32                                 "//ComprehensionNode//ComprehensionAppendNode")
33 def smoketest_list():
34     return set([ x*2
35                  for x in range(5)
36                  if x % 2 == 0 ])
37
38 cdef class A:
39     def __repr__(self): return u"A"
40     def __richcmp__(one, other, op): return one is other
41     def __hash__(self): return id(self) % 65536
42
43 def typed():
44     cdef A obj
45     return {obj for obj in {A(), A(), A()}}
46
47 def iterdict():
48     cdef dict d = dict(a=1,b=2,c=3)
49     return {d[key] for key in d}
50
51 def sorted(it):
52     l = list(it)
53     l.sort()
54     return l