Barry Warsaw | 408b6d3 | 2002-07-30 23:27:12 +0000 | [diff] [blame] | 1 | from test.test_support import TestFailed, have_unicode |
Guido van Rossum | 24512e6 | 2000-03-06 21:00:29 +0000 | [diff] [blame] | 2 | |
| 3 | class base_set: |
| 4 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 5 | def __init__(self, el): |
| 6 | self.el = el |
Guido van Rossum | 24512e6 | 2000-03-06 21:00:29 +0000 | [diff] [blame] | 7 | |
| 8 | class set(base_set): |
| 9 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 10 | def __contains__(self, el): |
| 11 | return self.el == el |
Guido van Rossum | 24512e6 | 2000-03-06 21:00:29 +0000 | [diff] [blame] | 12 | |
| 13 | class seq(base_set): |
| 14 | |
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 | |
| 18 | def check(ok, *args): |
| 19 | if not ok: |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 20 | raise TestFailed, " ".join(map(str, args)) |
Guido van Rossum | 24512e6 | 2000-03-06 21:00:29 +0000 | [diff] [blame] | 21 | |
| 22 | a = base_set(1) |
| 23 | b = set(1) |
| 24 | c = seq(1) |
| 25 | |
| 26 | check(1 in b, "1 not in set(1)") |
| 27 | check(0 not in b, "0 in set(1)") |
| 28 | check(1 in c, "1 not in seq(1)") |
| 29 | check(0 not in c, "0 in seq(1)") |
| 30 | |
| 31 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 32 | 1 in a |
| 33 | check(0, "in base_set did not raise error") |
Tim Peters | de9725f | 2001-05-05 10:06:17 +0000 | [diff] [blame] | 34 | except TypeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 35 | pass |
Guido van Rossum | 24512e6 | 2000-03-06 21:00:29 +0000 | [diff] [blame] | 36 | |
| 37 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 38 | 1 not in a |
| 39 | check(0, "not in base_set did not raise error") |
Tim Peters | de9725f | 2001-05-05 10:06:17 +0000 | [diff] [blame] | 40 | except TypeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 41 | pass |
Guido van Rossum | da2361a | 2000-03-07 15:52:01 +0000 | [diff] [blame] | 42 | |
| 43 | # Test char in string |
| 44 | |
| 45 | check('c' in 'abc', "'c' not in 'abc'") |
| 46 | check('d' not in 'abc', "'d' in 'abc'") |
| 47 | |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 48 | check('' in '', "'' not in ''") |
| 49 | check('' in 'abc', "'' not in 'abc'") |
Guido van Rossum | da2361a | 2000-03-07 15:52:01 +0000 | [diff] [blame] | 50 | |
| 51 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 52 | None in 'abc' |
| 53 | check(0, "None in 'abc' did not raise error") |
Guido van Rossum | da2361a | 2000-03-07 15:52:01 +0000 | [diff] [blame] | 54 | except TypeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 55 | pass |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 57 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 58 | if have_unicode: |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 59 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 60 | # Test char in Unicode |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 61 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 62 | check('c' in unicode('abc'), "'c' not in u'abc'") |
| 63 | check('d' not in unicode('abc'), "'d' in u'abc'") |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 64 | |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 65 | check('' in unicode(''), "'' not in u''") |
| 66 | check(unicode('') in '', "u'' not in ''") |
| 67 | check(unicode('') in unicode(''), "u'' not in u''") |
| 68 | check('' in unicode('abc'), "'' not in u'abc'") |
| 69 | check(unicode('') in 'abc', "u'' not in 'abc'") |
| 70 | check(unicode('') in unicode('abc'), "u'' not in u'abc'") |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 71 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 72 | try: |
| 73 | None in unicode('abc') |
| 74 | check(0, "None in u'abc' did not raise error") |
| 75 | except TypeError: |
| 76 | pass |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 77 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 78 | # Test Unicode char in Unicode |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 79 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 80 | check(unicode('c') in unicode('abc'), "u'c' not in u'abc'") |
| 81 | check(unicode('d') not in unicode('abc'), "u'd' in u'abc'") |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 82 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 83 | # Test Unicode char in string |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 84 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 85 | check(unicode('c') in 'abc', "u'c' not in 'abc'") |
| 86 | check(unicode('d') not in 'abc', "u'd' in 'abc'") |
| 87 | |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 88 | # A collection of tests on builtin sequence types |
| 89 | a = range(10) |
| 90 | for i in a: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 91 | check(i in a, "%r not in %r" % (i, a)) |
| 92 | check(16 not in a, "16 not in %r" % (a,)) |
| 93 | check(a not in a, "%s not in %r" % (a, a)) |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 94 | |
| 95 | a = tuple(a) |
| 96 | for i in a: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 97 | check(i in a, "%r not in %r" % (i, a)) |
| 98 | check(16 not in a, "16 not in %r" % (a,)) |
| 99 | check(a not in a, "%r not in %r" % (a, a)) |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 100 | |
| 101 | class Deviant1: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 102 | """Behaves strangely when compared |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 103 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 104 | This class is designed to make sure that the contains code |
| 105 | works when the list is modified during the check. |
| 106 | """ |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 107 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 108 | aList = range(15) |
| 109 | |
| 110 | def __cmp__(self, other): |
| 111 | if other == 12: |
| 112 | self.aList.remove(12) |
| 113 | self.aList.remove(13) |
| 114 | self.aList.remove(14) |
| 115 | return 1 |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 116 | |
| 117 | check(Deviant1() not in Deviant1.aList, "Deviant1 failed") |
| 118 | |
| 119 | class Deviant2: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 120 | """Behaves strangely when compared |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 121 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 122 | This class raises an exception during comparison. That in |
| 123 | turn causes the comparison to fail with a TypeError. |
| 124 | """ |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 125 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 126 | def __cmp__(self, other): |
| 127 | if other == 4: |
| 128 | raise RuntimeError, "gotcha" |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 129 | |
| 130 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 131 | check(Deviant2() not in a, "oops") |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 132 | except TypeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 133 | pass |