Mark Dickinson | 4a1f593 | 2008-11-12 23:23:36 +0000 | [diff] [blame] | 1 | from collections import deque |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test.support import run_unittest |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 3 | import unittest |
| 4 | |
Guido van Rossum | 24512e6 | 2000-03-06 21:00:29 +0000 | [diff] [blame] | 5 | |
| 6 | class base_set: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 7 | def __init__(self, el): |
| 8 | self.el = el |
Guido van Rossum | 24512e6 | 2000-03-06 21:00:29 +0000 | [diff] [blame] | 9 | |
Mark Dickinson | 4a1f593 | 2008-11-12 23:23:36 +0000 | [diff] [blame] | 10 | class myset(base_set): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 11 | def __contains__(self, el): |
| 12 | return self.el == el |
Guido van Rossum | 24512e6 | 2000-03-06 21:00:29 +0000 | [diff] [blame] | 13 | |
| 14 | class seq(base_set): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 15 | def __getitem__(self, n): |
| 16 | return [self.el][n] |
Guido van Rossum | 24512e6 | 2000-03-06 21:00:29 +0000 | [diff] [blame] | 17 | |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 18 | class TestContains(unittest.TestCase): |
| 19 | def test_common_tests(self): |
| 20 | a = base_set(1) |
Mark Dickinson | 4a1f593 | 2008-11-12 23:23:36 +0000 | [diff] [blame] | 21 | b = myset(1) |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 22 | c = seq(1) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 23 | self.assertIn(1, b) |
| 24 | self.assertNotIn(0, b) |
| 25 | self.assertIn(1, c) |
| 26 | self.assertNotIn(0, c) |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 27 | self.assertRaises(TypeError, lambda: 1 in a) |
| 28 | self.assertRaises(TypeError, lambda: 1 not in a) |
Guido van Rossum | 24512e6 | 2000-03-06 21:00:29 +0000 | [diff] [blame] | 29 | |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 30 | # test char in string |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 31 | self.assertIn('c', 'abc') |
| 32 | self.assertNotIn('d', 'abc') |
Guido van Rossum | 24512e6 | 2000-03-06 21:00:29 +0000 | [diff] [blame] | 33 | |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 34 | self.assertIn('', '') |
| 35 | self.assertIn('', 'abc') |
Guido van Rossum | 24512e6 | 2000-03-06 21:00:29 +0000 | [diff] [blame] | 36 | |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 37 | self.assertRaises(TypeError, lambda: None in 'abc') |
Guido van Rossum | 24512e6 | 2000-03-06 21:00:29 +0000 | [diff] [blame] | 38 | |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 39 | 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 Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 43 | self.assertIn(i, a) |
| 44 | self.assertNotIn(16, a) |
| 45 | self.assertNotIn(a, a) |
Guido van Rossum | da2361a | 2000-03-07 15:52:01 +0000 | [diff] [blame] | 46 | |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 47 | a = tuple(a) |
| 48 | for i in a: |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 49 | self.assertIn(i, a) |
| 50 | self.assertNotIn(16, a) |
| 51 | self.assertNotIn(a, a) |
Guido van Rossum | da2361a | 2000-03-07 15:52:01 +0000 | [diff] [blame] | 52 | |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 53 | class Deviant1: |
| 54 | """Behaves strangely when compared |
Guido van Rossum | da2361a | 2000-03-07 15:52:01 +0000 | [diff] [blame] | 55 | |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 56 | This class is designed to make sure that the contains code |
| 57 | works when the list is modified during the check. |
| 58 | """ |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 59 | aList = list(range(15)) |
| 60 | def __eq__(self, other): |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 61 | if other == 12: |
| 62 | self.aList.remove(12) |
| 63 | self.aList.remove(13) |
| 64 | self.aList.remove(14) |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 65 | return 0 |
Guido van Rossum | da2361a | 2000-03-07 15:52:01 +0000 | [diff] [blame] | 66 | |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 67 | self.assertNotIn(Deviant1(), Deviant1.aList) |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 68 | |
Mark Dickinson | 4a1f593 | 2008-11-12 23:23:36 +0000 | [diff] [blame] | 69 | 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 Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 84 | self.assertIn(elem, container) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 85 | self.assertTrue(container == constructor(values)) |
| 86 | self.assertTrue(container == container) |
Mark Dickinson | 4a1f593 | 2008-11-12 23:23:36 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 88 | |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 89 | def test_main(): |
| 90 | run_unittest(TestContains) |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 91 | |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 92 | if __name__ == '__main__': |
| 93 | test_main() |