Guido van Rossum | 24512e6 | 2000-03-06 21:00:29 +0000 | [diff] [blame] | 1 | from test_support import TestFailed |
| 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") |
Guido van Rossum | 24512e6 | 2000-03-06 21:00:29 +0000 | [diff] [blame] | 34 | except AttributeError: |
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") |
Guido van Rossum | 24512e6 | 2000-03-06 21:00:29 +0000 | [diff] [blame] | 40 | except AttributeError: |
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 | |
| 66 | # Test char in Unicode |
| 67 | |
| 68 | check('c' in u'abc', "'c' not in u'abc'") |
| 69 | check('d' not in u'abc', "'d' in u'abc'") |
| 70 | |
| 71 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 72 | '' in u'abc' |
| 73 | check(0, "'' in u'abc' did not raise error") |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 74 | except TypeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 75 | pass |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 76 | |
| 77 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 78 | 'ab' in u'abc' |
| 79 | check(0, "'ab' in u'abc' did not raise error") |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 80 | except TypeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 81 | pass |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 82 | |
| 83 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 84 | None in u'abc' |
| 85 | check(0, "None in u'abc' did not raise error") |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 86 | except TypeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 87 | pass |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 88 | |
| 89 | # Test Unicode char in Unicode |
| 90 | |
| 91 | check(u'c' in u'abc', "u'c' not in u'abc'") |
| 92 | check(u'd' not in u'abc', "u'd' in u'abc'") |
| 93 | |
| 94 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 95 | u'' in u'abc' |
| 96 | check(0, "u'' in u'abc' did not raise error") |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 97 | except TypeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 98 | pass |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 99 | |
| 100 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 101 | u'ab' in u'abc' |
| 102 | check(0, "u'ab' in u'abc' did not raise error") |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 103 | except TypeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 104 | pass |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 105 | |
| 106 | # Test Unicode char in string |
| 107 | |
| 108 | check(u'c' in 'abc', "u'c' not in 'abc'") |
| 109 | check(u'd' not in 'abc', "u'd' in 'abc'") |
| 110 | |
| 111 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 112 | u'' in 'abc' |
| 113 | check(0, "u'' in 'abc' did not raise error") |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 114 | except TypeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 115 | pass |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 116 | |
| 117 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 118 | u'ab' in 'abc' |
| 119 | check(0, "u'ab' in 'abc' did not raise error") |
Guido van Rossum | 45ad3c4 | 2000-04-10 13:52:13 +0000 | [diff] [blame] | 120 | except TypeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 121 | pass |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 122 | |
| 123 | # A collection of tests on builtin sequence types |
| 124 | a = range(10) |
| 125 | for i in a: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 126 | check(i in a, "%s not in %s" % (`i`, `a`)) |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 127 | check(16 not in a, "16 not in %s" % `a`) |
| 128 | check(a not in a, "%s not in %s" % (`a`, `a`)) |
| 129 | |
| 130 | a = tuple(a) |
| 131 | for i in a: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 132 | check(i in a, "%s not in %s" % (`i`, `a`)) |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 133 | check(16 not in a, "16 not in %s" % `a`) |
| 134 | check(a not in a, "%s not in %s" % (`a`, `a`)) |
| 135 | |
| 136 | class Deviant1: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 137 | """Behaves strangely when compared |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 138 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 139 | This class is designed to make sure that the contains code |
| 140 | works when the list is modified during the check. |
| 141 | """ |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 142 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 143 | aList = range(15) |
| 144 | |
| 145 | def __cmp__(self, other): |
| 146 | if other == 12: |
| 147 | self.aList.remove(12) |
| 148 | self.aList.remove(13) |
| 149 | self.aList.remove(14) |
| 150 | return 1 |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 151 | |
| 152 | check(Deviant1() not in Deviant1.aList, "Deviant1 failed") |
| 153 | |
| 154 | class Deviant2: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 155 | """Behaves strangely when compared |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 156 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 157 | This class raises an exception during comparison. That in |
| 158 | turn causes the comparison to fail with a TypeError. |
| 159 | """ |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 160 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 161 | def __cmp__(self, other): |
| 162 | if other == 4: |
| 163 | raise RuntimeError, "gotcha" |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 164 | |
| 165 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 166 | check(Deviant2() not in a, "oops") |
Jeremy Hylton | 035a07e | 2000-04-27 21:40:08 +0000 | [diff] [blame] | 167 | except TypeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 168 | pass |