blob: 355135f01d58a8e4b93232cf64263e4d2ee78b64 [file] [log] [blame]
Guido van Rossum24512e62000-03-06 21:00:29 +00001from test_support import TestFailed
2
3class base_set:
4
5 def __init__(self, el):
6 self.el = el
7
8class set(base_set):
9
10 def __contains__(self, el):
11 return self.el == el
12
13class seq(base_set):
14
15 def __getitem__(self, n):
16 return [self.el][n]
17
18def check(ok, *args):
19 if not ok:
Guido van Rossum45ad3c42000-04-10 13:52:13 +000020 raise TestFailed, " ".join(map(str, args))
Guido van Rossum24512e62000-03-06 21:00:29 +000021
22a = base_set(1)
23b = set(1)
24c = seq(1)
25
26check(1 in b, "1 not in set(1)")
27check(0 not in b, "0 in set(1)")
28check(1 in c, "1 not in seq(1)")
29check(0 not in c, "0 in seq(1)")
30
31try:
32 1 in a
33 check(0, "in base_set did not raise error")
34except AttributeError:
35 pass
36
37try:
38 1 not in a
39 check(0, "not in base_set did not raise error")
40except AttributeError:
41 pass
Guido van Rossumda2361a2000-03-07 15:52:01 +000042
43# Test char in string
44
45check('c' in 'abc', "'c' not in 'abc'")
46check('d' not in 'abc', "'d' in 'abc'")
47
48try:
49 '' in 'abc'
50 check(0, "'' in 'abc' did not raise error")
51except TypeError:
52 pass
53
54try:
55 'ab' in 'abc'
56 check(0, "'ab' in 'abc' did not raise error")
57except TypeError:
58 pass
59
60try:
61 None in 'abc'
62 check(0, "None in 'abc' did not raise error")
63except TypeError:
64 pass
Guido van Rossum45ad3c42000-04-10 13:52:13 +000065
66# Test char in Unicode
67
68check('c' in u'abc', "'c' not in u'abc'")
69check('d' not in u'abc', "'d' in u'abc'")
70
71try:
72 '' in u'abc'
73 check(0, "'' in u'abc' did not raise error")
74except TypeError:
75 pass
76
77try:
78 'ab' in u'abc'
79 check(0, "'ab' in u'abc' did not raise error")
80except TypeError:
81 pass
82
83try:
84 None in u'abc'
85 check(0, "None in u'abc' did not raise error")
86except TypeError:
87 pass
88
89# Test Unicode char in Unicode
90
91check(u'c' in u'abc', "u'c' not in u'abc'")
92check(u'd' not in u'abc', "u'd' in u'abc'")
93
94try:
95 u'' in u'abc'
96 check(0, "u'' in u'abc' did not raise error")
97except TypeError:
98 pass
99
100try:
101 u'ab' in u'abc'
102 check(0, "u'ab' in u'abc' did not raise error")
103except TypeError:
104 pass
105
106# Test Unicode char in string
107
108check(u'c' in 'abc', "u'c' not in 'abc'")
109check(u'd' not in 'abc', "u'd' in 'abc'")
110
111try:
112 u'' in 'abc'
113 check(0, "u'' in 'abc' did not raise error")
114except TypeError:
115 pass
116
117try:
118 u'ab' in 'abc'
119 check(0, "u'ab' in 'abc' did not raise error")
120except TypeError:
121 pass
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000122
123# A collection of tests on builtin sequence types
124a = range(10)
125for i in a:
126 check(i in a, "%s not in %s" % (`i`, `a`))
127check(16 not in a, "16 not in %s" % `a`)
128check(a not in a, "%s not in %s" % (`a`, `a`))
129
130a = tuple(a)
131for i in a:
132 check(i in a, "%s not in %s" % (`i`, `a`))
133check(16 not in a, "16 not in %s" % `a`)
134check(a not in a, "%s not in %s" % (`a`, `a`))
135
136class Deviant1:
137 """Behaves strangely when compared
138
139 This class is designed to make sure that the contains code
140 works when the list is modified during the check.
141 """
142
Jeremy Hylton7c4f96f2000-04-27 21:42:48 +0000143 aList = range(15)
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000144
145 def __cmp__(self, other):
146 if other == 12:
Jeremy Hylton7c4f96f2000-04-27 21:42:48 +0000147 self.aList.remove(12)
148 self.aList.remove(13)
149 self.aList.remove(14)
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000150 return 1
151
152check(Deviant1() not in Deviant1.aList, "Deviant1 failed")
153
154class Deviant2:
155 """Behaves strangely when compared
156
157 This class raises an exception during comparison. That in
158 turn causes the comparison to fail with a TypeError.
159 """
160
161 def __cmp__(self, other):
162 if other == 4:
163 raise RuntimeError, "gotcha"
164
165try:
166 check(Deviant2() not in a, "oops")
167except TypeError:
168 pass