blob: 499d587a0ac81ce726f854e93593facf14dae349 [file] [log] [blame]
Guido van Rossum24512e62000-03-06 21:00:29 +00001from test_support import TestFailed
2
3class base_set:
4
Fred Drake004d5e62000-10-23 17:22:08 +00005 def __init__(self, el):
6 self.el = el
Guido van Rossum24512e62000-03-06 21:00:29 +00007
8class set(base_set):
9
Fred Drake004d5e62000-10-23 17:22:08 +000010 def __contains__(self, el):
11 return self.el == el
Guido van Rossum24512e62000-03-06 21:00:29 +000012
13class seq(base_set):
14
Fred Drake004d5e62000-10-23 17:22:08 +000015 def __getitem__(self, n):
16 return [self.el][n]
Guido van Rossum24512e62000-03-06 21:00:29 +000017
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:
Fred Drake004d5e62000-10-23 17:22:08 +000032 1 in a
33 check(0, "in base_set did not raise error")
Guido van Rossum24512e62000-03-06 21:00:29 +000034except AttributeError:
Fred Drake004d5e62000-10-23 17:22:08 +000035 pass
Guido van Rossum24512e62000-03-06 21:00:29 +000036
37try:
Fred Drake004d5e62000-10-23 17:22:08 +000038 1 not in a
39 check(0, "not in base_set did not raise error")
Guido van Rossum24512e62000-03-06 21:00:29 +000040except AttributeError:
Fred Drake004d5e62000-10-23 17:22:08 +000041 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:
Fred Drake004d5e62000-10-23 17:22:08 +000049 '' in 'abc'
50 check(0, "'' in 'abc' did not raise error")
Guido van Rossumda2361a2000-03-07 15:52:01 +000051except TypeError:
Fred Drake004d5e62000-10-23 17:22:08 +000052 pass
Guido van Rossumda2361a2000-03-07 15:52:01 +000053
54try:
Fred Drake004d5e62000-10-23 17:22:08 +000055 'ab' in 'abc'
56 check(0, "'ab' in 'abc' did not raise error")
Guido van Rossumda2361a2000-03-07 15:52:01 +000057except TypeError:
Fred Drake004d5e62000-10-23 17:22:08 +000058 pass
Guido van Rossumda2361a2000-03-07 15:52:01 +000059
60try:
Fred Drake004d5e62000-10-23 17:22:08 +000061 None in 'abc'
62 check(0, "None in 'abc' did not raise error")
Guido van Rossumda2361a2000-03-07 15:52:01 +000063except TypeError:
Fred Drake004d5e62000-10-23 17:22:08 +000064 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:
Fred Drake004d5e62000-10-23 17:22:08 +000072 '' in u'abc'
73 check(0, "'' in u'abc' did not raise error")
Guido van Rossum45ad3c42000-04-10 13:52:13 +000074except TypeError:
Fred Drake004d5e62000-10-23 17:22:08 +000075 pass
Guido van Rossum45ad3c42000-04-10 13:52:13 +000076
77try:
Fred Drake004d5e62000-10-23 17:22:08 +000078 'ab' in u'abc'
79 check(0, "'ab' in u'abc' did not raise error")
Guido van Rossum45ad3c42000-04-10 13:52:13 +000080except TypeError:
Fred Drake004d5e62000-10-23 17:22:08 +000081 pass
Guido van Rossum45ad3c42000-04-10 13:52:13 +000082
83try:
Fred Drake004d5e62000-10-23 17:22:08 +000084 None in u'abc'
85 check(0, "None in u'abc' did not raise error")
Guido van Rossum45ad3c42000-04-10 13:52:13 +000086except TypeError:
Fred Drake004d5e62000-10-23 17:22:08 +000087 pass
Guido van Rossum45ad3c42000-04-10 13:52:13 +000088
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:
Fred Drake004d5e62000-10-23 17:22:08 +000095 u'' in u'abc'
96 check(0, "u'' in u'abc' did not raise error")
Guido van Rossum45ad3c42000-04-10 13:52:13 +000097except TypeError:
Fred Drake004d5e62000-10-23 17:22:08 +000098 pass
Guido van Rossum45ad3c42000-04-10 13:52:13 +000099
100try:
Fred Drake004d5e62000-10-23 17:22:08 +0000101 u'ab' in u'abc'
102 check(0, "u'ab' in u'abc' did not raise error")
Guido van Rossum45ad3c42000-04-10 13:52:13 +0000103except TypeError:
Fred Drake004d5e62000-10-23 17:22:08 +0000104 pass
Guido van Rossum45ad3c42000-04-10 13:52:13 +0000105
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:
Fred Drake004d5e62000-10-23 17:22:08 +0000112 u'' in 'abc'
113 check(0, "u'' in 'abc' did not raise error")
Guido van Rossum45ad3c42000-04-10 13:52:13 +0000114except TypeError:
Fred Drake004d5e62000-10-23 17:22:08 +0000115 pass
Guido van Rossum45ad3c42000-04-10 13:52:13 +0000116
117try:
Fred Drake004d5e62000-10-23 17:22:08 +0000118 u'ab' in 'abc'
119 check(0, "u'ab' in 'abc' did not raise error")
Guido van Rossum45ad3c42000-04-10 13:52:13 +0000120except TypeError:
Fred Drake004d5e62000-10-23 17:22:08 +0000121 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:
Fred Drake004d5e62000-10-23 17:22:08 +0000126 check(i in a, "%s not in %s" % (`i`, `a`))
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000127check(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:
Fred Drake004d5e62000-10-23 17:22:08 +0000132 check(i in a, "%s not in %s" % (`i`, `a`))
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000133check(16 not in a, "16 not in %s" % `a`)
134check(a not in a, "%s not in %s" % (`a`, `a`))
135
136class Deviant1:
Fred Drake004d5e62000-10-23 17:22:08 +0000137 """Behaves strangely when compared
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000138
Fred Drake004d5e62000-10-23 17:22:08 +0000139 This class is designed to make sure that the contains code
140 works when the list is modified during the check.
141 """
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000142
Fred Drake004d5e62000-10-23 17:22:08 +0000143 aList = range(15)
144
145 def __cmp__(self, other):
146 if other == 12:
147 self.aList.remove(12)
148 self.aList.remove(13)
149 self.aList.remove(14)
150 return 1
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000151
152check(Deviant1() not in Deviant1.aList, "Deviant1 failed")
153
154class Deviant2:
Fred Drake004d5e62000-10-23 17:22:08 +0000155 """Behaves strangely when compared
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000156
Fred Drake004d5e62000-10-23 17:22:08 +0000157 This class raises an exception during comparison. That in
158 turn causes the comparison to fail with a TypeError.
159 """
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000160
Fred Drake004d5e62000-10-23 17:22:08 +0000161 def __cmp__(self, other):
162 if other == 4:
163 raise RuntimeError, "gotcha"
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000164
165try:
Fred Drake004d5e62000-10-23 17:22:08 +0000166 check(Deviant2() not in a, "oops")
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000167except TypeError:
Fred Drake004d5e62000-10-23 17:22:08 +0000168 pass