Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / lib / python2.4 / test / test_contains.py
CommitLineData
920dae64
AT
1from test.test_support import TestFailed, have_unicode
2
3class base_set:
4
5 def __init__(self, el):
6 self.el = el
7
8class set(base_set):
9
10 def __contains__(self, el):
11 return self.el == el
12
13class seq(base_set):
14
15 def __getitem__(self, n):
16 return [self.el][n]
17
18def check(ok, *args):
19 if not ok:
20 raise TestFailed, " ".join(map(str, args))
21
22a = base_set(1)
23b = set(1)
24c = seq(1)
25
26check(1 in b, "1 not in set(1)")
27check(0 not in b, "0 in set(1)")
28check(1 in c, "1 not in seq(1)")
29check(0 not in c, "0 in seq(1)")
30
31try:
32 1 in a
33 check(0, "in base_set did not raise error")
34except TypeError:
35 pass
36
37try:
38 1 not in a
39 check(0, "not in base_set did not raise error")
40except TypeError:
41 pass
42
43# Test char in string
44
45check('c' in 'abc', "'c' not in 'abc'")
46check('d' not in 'abc', "'d' in 'abc'")
47
48check('' in '', "'' not in ''")
49check('' in 'abc', "'' not in 'abc'")
50
51try:
52 None in 'abc'
53 check(0, "None in 'abc' did not raise error")
54except TypeError:
55 pass
56
57
58if have_unicode:
59
60 # Test char in Unicode
61
62 check('c' in unicode('abc'), "'c' not in u'abc'")
63 check('d' not in unicode('abc'), "'d' in u'abc'")
64
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'")
71
72 try:
73 None in unicode('abc')
74 check(0, "None in u'abc' did not raise error")
75 except TypeError:
76 pass
77
78 # Test Unicode char in Unicode
79
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'")
82
83 # Test Unicode char in string
84
85 check(unicode('c') in 'abc', "u'c' not in 'abc'")
86 check(unicode('d') not in 'abc', "u'd' in 'abc'")
87
88# A collection of tests on builtin sequence types
89a = range(10)
90for i in a:
91 check(i in a, "%r not in %r" % (i, a))
92check(16 not in a, "16 not in %r" % (a,))
93check(a not in a, "%s not in %r" % (a, a))
94
95a = tuple(a)
96for i in a:
97 check(i in a, "%r not in %r" % (i, a))
98check(16 not in a, "16 not in %r" % (a,))
99check(a not in a, "%r not in %r" % (a, a))
100
101class Deviant1:
102 """Behaves strangely when compared
103
104 This class is designed to make sure that the contains code
105 works when the list is modified during the check.
106 """
107
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
116
117check(Deviant1() not in Deviant1.aList, "Deviant1 failed")
118
119class Deviant2:
120 """Behaves strangely when compared
121
122 This class raises an exception during comparison. That in
123 turn causes the comparison to fail with a TypeError.
124 """
125
126 def __cmp__(self, other):
127 if other == 4:
128 raise RuntimeError, "gotcha"
129
130try:
131 check(Deviant2() not in a, "oops")
132except TypeError:
133 pass