Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame^] | 1 | from 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 | |
| 48 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 49 | '' in 'abc' |
| 50 | check(0, "'' in 'abc' did not raise error") |
Guido van Rossum | da2361a | 2000-03-07 15:52:01 +0000 | [diff] [blame] | 51 | except TypeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 52 | pass |
Guido van Rossum | da2361a | 2000-03-07 15:52:01 +0000 | [diff] [blame] | 53 | |
| 54 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 55 | 'ab' in 'abc' |
| 56 | check(0, "'ab' in 'abc' did not raise error") |
Guido van Rossum | da2361a | 2000-03-07 15:52:01 +0000 | [diff] [blame] | 57 | except TypeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 58 | pass |
Guido van Rossum | da2361a | 2000-03-07 15:52:01 +0000 | [diff] [blame] | 59 | |
| 60 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 61 | None in 'abc' |
| 62 | check(0, "None in 'abc' did not raise error") |
Guido van Rossum | da2361a | 2000-03-07 15:52:01 +0000 | [diff] [blame] | 63 | except TypeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 64 | pass |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 66 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame^] | 67 | if have_unicode: |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 68 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame^] | 69 | # Test char in Unicode |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 70 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame^] | 71 | check('c' in unicode('abc'), "'c' not in u'abc'") |
| 72 | check('d' not in unicode('abc'), "'d' in u'abc'") |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 73 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame^] | 74 | try: |
| 75 | '' in unicode('abc') |
| 76 | check(0, "'' in u'abc' did not raise error") |
| 77 | except TypeError: |
| 78 | pass |
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 | try: |
| 81 | 'ab' in unicode('abc') |
| 82 | check(0, "'ab' in u'abc' did not raise error") |
| 83 | except TypeError: |
| 84 | pass |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 85 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame^] | 86 | try: |
| 87 | None in unicode('abc') |
| 88 | check(0, "None in u'abc' did not raise error") |
| 89 | except TypeError: |
| 90 | pass |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 91 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame^] | 92 | # Test Unicode char in Unicode |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 93 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame^] | 94 | 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 Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 96 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame^] | 97 | try: |
| 98 | unicode('') in unicode('abc') |
| 99 | check(0, "u'' in u'abc' did not raise error") |
| 100 | except TypeError: |
| 101 | pass |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 102 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame^] | 103 | 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 Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 108 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame^] | 109 | # Test Unicode char in string |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 110 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame^] | 111 | 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 Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 125 | |
| 126 | # A collection of tests on builtin sequence types |
| 127 | a = range(10) |
| 128 | for i in a: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 129 | check(i in a, "%s not in %s" % (`i`, `a`)) |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 130 | check(16 not in a, "16 not in %s" % `a`) |
| 131 | check(a not in a, "%s not in %s" % (`a`, `a`)) |
| 132 | |
| 133 | a = tuple(a) |
| 134 | for i in a: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 135 | check(i in a, "%s not in %s" % (`i`, `a`)) |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 136 | check(16 not in a, "16 not in %s" % `a`) |
| 137 | check(a not in a, "%s not in %s" % (`a`, `a`)) |
| 138 | |
| 139 | class Deviant1: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 140 | """Behaves strangely when compared |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 141 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 142 | This class is designed to make sure that the contains code |
| 143 | works when the list is modified during the check. |
| 144 | """ |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 145 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 146 | 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 Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 154 | |
| 155 | check(Deviant1() not in Deviant1.aList, "Deviant1 failed") |
| 156 | |
| 157 | class Deviant2: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 158 | """Behaves strangely when compared |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 159 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 160 | This class raises an exception during comparison. That in |
| 161 | turn causes the comparison to fail with a TypeError. |
| 162 | """ |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 163 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 164 | def __cmp__(self, other): |
| 165 | if other == 4: |
| 166 | raise RuntimeError, "gotcha" |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 167 | |
| 168 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 169 | check(Deviant2() not in a, "oops") |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 170 | except TypeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 171 | pass |