blob: 1a9a9650762084a223134715f2b03a6e61382a42 [file] [log] [blame]
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001from test_support import TestFailed, have_unicode
Guido van Rossum24512e62000-03-06 21:00:29 +00002
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")
Tim Petersde9725f2001-05-05 10:06:17 +000034except TypeError:
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")
Tim Petersde9725f2001-05-05 10:06:17 +000040except TypeError:
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
Guido van Rossum45ad3c42000-04-10 13:52:13 +000066
Martin v. Löwis339d0f72001-08-17 18:39:25 +000067if have_unicode:
Guido van Rossum45ad3c42000-04-10 13:52:13 +000068
Martin v. Löwis339d0f72001-08-17 18:39:25 +000069 # Test char in Unicode
Guido van Rossum45ad3c42000-04-10 13:52:13 +000070
Martin v. Löwis339d0f72001-08-17 18:39:25 +000071 check('c' in unicode('abc'), "'c' not in u'abc'")
72 check('d' not in unicode('abc'), "'d' in u'abc'")
Guido van Rossum45ad3c42000-04-10 13:52:13 +000073
Martin v. Löwis339d0f72001-08-17 18:39:25 +000074 try:
75 '' in unicode('abc')
76 check(0, "'' in u'abc' did not raise error")
77 except TypeError:
78 pass
Guido van Rossum45ad3c42000-04-10 13:52:13 +000079
Martin v. Löwis339d0f72001-08-17 18:39:25 +000080 try:
81 'ab' in unicode('abc')
82 check(0, "'ab' in u'abc' did not raise error")
83 except TypeError:
84 pass
Guido van Rossum45ad3c42000-04-10 13:52:13 +000085
Martin v. Löwis339d0f72001-08-17 18:39:25 +000086 try:
87 None in unicode('abc')
88 check(0, "None in u'abc' did not raise error")
89 except TypeError:
90 pass
Guido van Rossum45ad3c42000-04-10 13:52:13 +000091
Martin v. Löwis339d0f72001-08-17 18:39:25 +000092 # Test Unicode char in Unicode
Guido van Rossum45ad3c42000-04-10 13:52:13 +000093
Martin v. Löwis339d0f72001-08-17 18:39:25 +000094 check(unicode('c') in unicode('abc'), "u'c' not in u'abc'")
95 check(unicode('d') not in unicode('abc'), "u'd' in u'abc'")
Guido van Rossum45ad3c42000-04-10 13:52:13 +000096
Martin v. Löwis339d0f72001-08-17 18:39:25 +000097 try:
98 unicode('') in unicode('abc')
99 check(0, "u'' in u'abc' did not raise error")
100 except TypeError:
101 pass
Guido van Rossum45ad3c42000-04-10 13:52:13 +0000102
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000103 try:
104 unicode('ab') in unicode('abc')
105 check(0, "u'ab' in u'abc' did not raise error")
106 except TypeError:
107 pass
Guido van Rossum45ad3c42000-04-10 13:52:13 +0000108
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000109 # Test Unicode char in string
Guido van Rossum45ad3c42000-04-10 13:52:13 +0000110
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000111 check(unicode('c') in 'abc', "u'c' not in 'abc'")
112 check(unicode('d') not in 'abc', "u'd' in 'abc'")
113
114 try:
115 unicode('') in 'abc'
116 check(0, "u'' in 'abc' did not raise error")
117 except TypeError:
118 pass
119
120 try:
121 unicode('ab') in 'abc'
122 check(0, "u'ab' in 'abc' did not raise error")
123 except TypeError:
124 pass
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000125
126# A collection of tests on builtin sequence types
127a = range(10)
128for i in a:
Fred Drake004d5e62000-10-23 17:22:08 +0000129 check(i in a, "%s not in %s" % (`i`, `a`))
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000130check(16 not in a, "16 not in %s" % `a`)
131check(a not in a, "%s not in %s" % (`a`, `a`))
132
133a = tuple(a)
134for i in a:
Fred Drake004d5e62000-10-23 17:22:08 +0000135 check(i in a, "%s not in %s" % (`i`, `a`))
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000136check(16 not in a, "16 not in %s" % `a`)
137check(a not in a, "%s not in %s" % (`a`, `a`))
138
139class Deviant1:
Fred Drake004d5e62000-10-23 17:22:08 +0000140 """Behaves strangely when compared
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000141
Fred Drake004d5e62000-10-23 17:22:08 +0000142 This class is designed to make sure that the contains code
143 works when the list is modified during the check.
144 """
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000145
Fred Drake004d5e62000-10-23 17:22:08 +0000146 aList = range(15)
147
148 def __cmp__(self, other):
149 if other == 12:
150 self.aList.remove(12)
151 self.aList.remove(13)
152 self.aList.remove(14)
153 return 1
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000154
155check(Deviant1() not in Deviant1.aList, "Deviant1 failed")
156
157class Deviant2:
Fred Drake004d5e62000-10-23 17:22:08 +0000158 """Behaves strangely when compared
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000159
Fred Drake004d5e62000-10-23 17:22:08 +0000160 This class raises an exception during comparison. That in
161 turn causes the comparison to fail with a TypeError.
162 """
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000163
Fred Drake004d5e62000-10-23 17:22:08 +0000164 def __cmp__(self, other):
165 if other == 4:
166 raise RuntimeError, "gotcha"
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000167
168try:
Fred Drake004d5e62000-10-23 17:22:08 +0000169 check(Deviant2() not in a, "oops")
Jeremy Hylton035a07e2000-04-27 21:40:08 +0000170except TypeError:
Fred Drake004d5e62000-10-23 17:22:08 +0000171 pass