blob: a667a16d8c79168c1807cc849f05005e5af2de17 [file] [log] [blame]
Mark Dickinson4a1f5932008-11-12 23:23:36 +00001from collections import deque
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test.support import run_unittest
Christian Heimes77c02eb2008-02-09 02:18:51 +00003import unittest
4
Guido van Rossum24512e62000-03-06 21:00:29 +00005
6class base_set:
Fred Drake004d5e62000-10-23 17:22:08 +00007 def __init__(self, el):
8 self.el = el
Guido van Rossum24512e62000-03-06 21:00:29 +00009
Mark Dickinson4a1f5932008-11-12 23:23:36 +000010class myset(base_set):
Fred Drake004d5e62000-10-23 17:22:08 +000011 def __contains__(self, el):
12 return self.el == el
Guido van Rossum24512e62000-03-06 21:00:29 +000013
14class seq(base_set):
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
Christian Heimes77c02eb2008-02-09 02:18:51 +000018class TestContains(unittest.TestCase):
19 def test_common_tests(self):
20 a = base_set(1)
Mark Dickinson4a1f5932008-11-12 23:23:36 +000021 b = myset(1)
Christian Heimes77c02eb2008-02-09 02:18:51 +000022 c = seq(1)
Benjamin Peterson577473f2010-01-19 00:09:57 +000023 self.assertIn(1, b)
24 self.assertNotIn(0, b)
25 self.assertIn(1, c)
26 self.assertNotIn(0, c)
Christian Heimes77c02eb2008-02-09 02:18:51 +000027 self.assertRaises(TypeError, lambda: 1 in a)
28 self.assertRaises(TypeError, lambda: 1 not in a)
Guido van Rossum24512e62000-03-06 21:00:29 +000029
Christian Heimes77c02eb2008-02-09 02:18:51 +000030 # test char in string
Benjamin Peterson577473f2010-01-19 00:09:57 +000031 self.assertIn('c', 'abc')
32 self.assertNotIn('d', 'abc')
Guido van Rossum24512e62000-03-06 21:00:29 +000033
Benjamin Peterson577473f2010-01-19 00:09:57 +000034 self.assertIn('', '')
35 self.assertIn('', 'abc')
Guido van Rossum24512e62000-03-06 21:00:29 +000036
Christian Heimes77c02eb2008-02-09 02:18:51 +000037 self.assertRaises(TypeError, lambda: None in 'abc')
Guido van Rossum24512e62000-03-06 21:00:29 +000038
Christian Heimes77c02eb2008-02-09 02:18:51 +000039 def test_builtin_sequence_types(self):
40 # a collection of tests on builtin sequence types
41 a = range(10)
42 for i in a:
Benjamin Peterson577473f2010-01-19 00:09:57 +000043 self.assertIn(i, a)
44 self.assertNotIn(16, a)
45 self.assertNotIn(a, a)
Guido van Rossumda2361a2000-03-07 15:52:01 +000046
Christian Heimes77c02eb2008-02-09 02:18:51 +000047 a = tuple(a)
48 for i in a:
Benjamin Peterson577473f2010-01-19 00:09:57 +000049 self.assertIn(i, a)
50 self.assertNotIn(16, a)
51 self.assertNotIn(a, a)
Guido van Rossumda2361a2000-03-07 15:52:01 +000052
Christian Heimes77c02eb2008-02-09 02:18:51 +000053 class Deviant1:
54 """Behaves strangely when compared
Guido van Rossumda2361a2000-03-07 15:52:01 +000055
Christian Heimes77c02eb2008-02-09 02:18:51 +000056 This class is designed to make sure that the contains code
57 works when the list is modified during the check.
58 """
Mark Dickinsona56c4672009-01-27 18:17:45 +000059 aList = list(range(15))
60 def __eq__(self, other):
Christian Heimes77c02eb2008-02-09 02:18:51 +000061 if other == 12:
62 self.aList.remove(12)
63 self.aList.remove(13)
64 self.aList.remove(14)
Mark Dickinsona56c4672009-01-27 18:17:45 +000065 return 0
Guido van Rossumda2361a2000-03-07 15:52:01 +000066
Benjamin Peterson577473f2010-01-19 00:09:57 +000067 self.assertNotIn(Deviant1(), Deviant1.aList)
Christian Heimes77c02eb2008-02-09 02:18:51 +000068
Mark Dickinson4a1f5932008-11-12 23:23:36 +000069 def test_nonreflexive(self):
70 # containment and equality tests involving elements that are
71 # not necessarily equal to themselves
72
73 class MyNonReflexive(object):
74 def __eq__(self, other):
75 return False
76 def __hash__(self):
77 return 28
78
79 values = float('nan'), 1, None, 'abc', MyNonReflexive()
80 constructors = list, tuple, dict.fromkeys, set, frozenset, deque
81 for constructor in constructors:
82 container = constructor(values)
83 for elem in container:
Benjamin Peterson577473f2010-01-19 00:09:57 +000084 self.assertIn(elem, container)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000085 self.assertTrue(container == constructor(values))
86 self.assertTrue(container == container)
Mark Dickinson4a1f5932008-11-12 23:23:36 +000087
Guido van Rossum45ad3c42000-04-10 13:52:13 +000088
Christian Heimes77c02eb2008-02-09 02:18:51 +000089def test_main():
90 run_unittest(TestContains)
Jeremy Hylton035a07e2000-04-27 21:40:08 +000091
Christian Heimes77c02eb2008-02-09 02:18:51 +000092if __name__ == '__main__':
93 test_main()