~ed.so/duplicity/reuse-passphrase-for-signing-fix

« back to all changes in this revision

Viewing changes to testing/lazytest.py

  • Committer: bescoto
  • Date: 2002-10-29 01:49:46 UTC
  • Revision ID: vcs-imports@canonical.com-20021029014946-3m4rmm5plom7pl6q
Initial checkin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import generators
 
2
import unittest, pickle, sys
 
3
sys.path.insert(0, "../src")
 
4
from lazy import *
 
5
 
 
6
class Iterators(unittest.TestCase):
 
7
        one_to_100 = lambda s: iter(range(1, 101))
 
8
        evens = lambda s: iter(range(2, 101, 2))
 
9
        odds = lambda s: iter(range(1, 100, 2))
 
10
        empty = lambda s: iter([])
 
11
 
 
12
        def __init__(self, *args):
 
13
                apply (unittest.TestCase.__init__, (self,) + args)
 
14
                self.falseerror = self.falseerror_maker()
 
15
                self.trueerror = self.trueerror_maker()
 
16
                self.emptygen = self.emptygen_maker()
 
17
                self.typeerror = self.typeerror_maker()
 
18
                self.nameerror = self.nameerror_maker()
 
19
 
 
20
        def falseerror_maker(self):
 
21
                yield None
 
22
                yield 0
 
23
                yield []
 
24
                raise Exception
 
25
 
 
26
        def trueerror_maker(self):
 
27
                yield 1
 
28
                yield "hello"
 
29
                yield (2, 3)
 
30
                raise Exception
 
31
 
 
32
        def nameerror_maker(self):
 
33
                if 0: yield 1
 
34
                raise NameError
 
35
 
 
36
        def typeerror_maker(self):
 
37
                yield 1
 
38
                yield 2
 
39
                raise TypeError
 
40
 
 
41
        def alwayserror(self, x):
 
42
                raise Exception
 
43
 
 
44
        def emptygen_maker(self):
 
45
                if 0: yield 1
 
46
 
 
47
 
 
48
class IterEqualTestCase(Iterators):
 
49
        """Tests for iter_equal function"""
 
50
        def testEmpty(self):
 
51
                """Empty iterators should be equal"""
 
52
                assert Iter.equal(self.empty(), iter([]))
 
53
 
 
54
        def testNormal(self):
 
55
                """See if normal iterators are equal"""
 
56
                assert Iter.equal(iter((1,2,3)), iter((1,2,3)))
 
57
                assert Iter.equal(self.odds(), iter(range(1, 100, 2)))
 
58
                assert Iter.equal(iter((1,2,3)), iter(range(1, 4)))
 
59
 
 
60
        def testNormalInequality(self):
 
61
                """See if normal unequals work"""
 
62
                assert not Iter.equal(iter((1,2,3)), iter((1,2,4)))
 
63
                assert not Iter.equal(self.odds(), iter(["hello", "there"]))
 
64
 
 
65
        def testGenerators(self):
 
66
                """equals works for generators"""
 
67
                def f():
 
68
                        yield 1
 
69
                        yield "hello"
 
70
                def g():
 
71
                        yield 1
 
72
                        yield "hello"
 
73
                assert Iter.equal(f(), g())
 
74
 
 
75
        def testLength(self):
 
76
                """Differently sized iterators"""
 
77
                assert not Iter.equal(iter((1,2,3)), iter((1,2)))
 
78
                assert not Iter.equal(iter((1,2)), iter((1,2,3)))
 
79
 
 
80
 
 
81
class FilterTestCase(Iterators):
 
82
        """Tests for lazy_filter function"""
 
83
        def testEmpty(self):
 
84
                """empty iterators -> empty iterators"""
 
85
                assert Iter.empty(Iter.filter(self.alwayserror,
 
86
                                                                          self.empty())), \
 
87
                           "Filtering an empty iterator should result in empty iterator"
 
88
 
 
89
        def testNum1(self):
 
90
                """Test numbers 1 - 100 #1"""
 
91
                assert Iter.equal(Iter.filter(lambda x: x % 2 == 0,
 
92
                                                                          self.one_to_100()),
 
93
                                                  self.evens())
 
94
                assert Iter.equal(Iter.filter(lambda x: x % 2,
 
95
                                                                          self.one_to_100()),
 
96
                                                  self.odds())
 
97
 
 
98
        def testError(self):
 
99
                """Should raise appropriate error"""
 
100
                i = Iter.filter(lambda x: x, self.falseerror_maker())
 
101
                self.assertRaises(Exception, i.next)
 
102
 
 
103
 
 
104
class MapTestCase(Iterators):
 
105
        """Test mapping of iterators"""
 
106
        def testNumbers(self):
 
107
                """1 to 100 * 2 = 2 to 200"""
 
108
                assert Iter.equal(Iter.map(lambda x: 2*x, self.one_to_100()),
 
109
                                                  iter(range(2, 201, 2)))
 
110
 
 
111
        def testShortcut(self):
 
112
                """Map should go in order"""
 
113
                def f(x):
 
114
                        if x == "hello":
 
115
                                raise NameError
 
116
                i = Iter.map(f, self.trueerror_maker())
 
117
                i.next()
 
118
                self.assertRaises(NameError, i.next)
 
119
 
 
120
        def testEmpty(self):
 
121
                """Map of an empty iterator is empty"""
 
122
                assert Iter.empty(Iter.map(lambda x: x, iter([])))
 
123
 
 
124
 
 
125
class CatTestCase(Iterators):
 
126
        """Test concatenation of iterators"""
 
127
        def testEmpty(self):
 
128
                """Empty + empty = empty"""
 
129
                assert Iter.empty(Iter.cat(iter([]), iter([])))
 
130
 
 
131
        def testNumbers(self):
 
132
                """1 to 50 + 51 to 100 = 1 to 100"""
 
133
                assert Iter.equal(Iter.cat(iter(range(1, 51)), iter(range(51, 101))),
 
134
                                                  self.one_to_100())
 
135
 
 
136
        def testShortcut(self):
 
137
                """Process iterators in order"""
 
138
                i = Iter.cat(self.typeerror_maker(), self.nameerror_maker())
 
139
                i.next()
 
140
                i.next()
 
141
                self.assertRaises(TypeError, i.next)
 
142
 
 
143
 
 
144
class AndOrTestCase(Iterators):
 
145
        """Test And and Or"""
 
146
        def testEmpty(self):
 
147
                """And() -> true, Or() -> false"""
 
148
                assert Iter.And(self.empty())
 
149
                assert not Iter.Or(self.empty())
 
150
 
 
151
        def testAndShortcut(self):
 
152
                """And should return if any false"""
 
153
                assert Iter.And(self.falseerror_maker()) is None
 
154
 
 
155
        def testOrShortcut(self):
 
156
                """Or should return if any true"""
 
157
                assert Iter.Or(self.trueerror_maker()) == 1
 
158
 
 
159
        def testNormalAnd(self):
 
160
                """And should go through true iterators, picking last"""
 
161
                assert Iter.And(iter([1,2,3,4])) == 4
 
162
                self.assertRaises(Exception, Iter.And, self.trueerror_maker())
 
163
 
 
164
        def testNormalOr(self):
 
165
                """Or goes through false iterators, picking last"""
 
166
                assert Iter.Or(iter([0, None, []])) == []
 
167
                self.assertRaises(Exception, Iter.Or, self.falseerror_maker())
 
168
 
 
169
 
 
170
class FoldingTest(Iterators):
 
171
        """Test folding operations"""
 
172
        def f(self, x, y): return x + y
 
173
 
 
174
        def testEmpty(self):
 
175
                """Folds of empty iterators should produce defaults"""
 
176
                assert Iter.foldl(self.f, 23, self.empty()) == 23
 
177
                assert Iter.foldr(self.f, 32, self.empty()) == 32
 
178
 
 
179
        def testAddition(self):
 
180
                """Use folds to sum lists"""
 
181
                assert Iter.foldl(self.f, 0, self.one_to_100()) == 5050
 
182
                assert Iter.foldr(self.f, 0, self.one_to_100()) == 5050
 
183
 
 
184
        def testLargeAddition(self):
 
185
                """Folds on 10000 element iterators"""
 
186
                assert Iter.foldl(self.f, 0, iter(range(1, 10001))) == 50005000
 
187
                self.assertRaises(RuntimeError,
 
188
                                                  Iter.foldr, self.f, 0, iter(range(1, 10001)))
 
189
 
 
190
        def testLen(self):
 
191
                """Use folds to calculate length of lists"""
 
192
                assert Iter.foldl(lambda x, y: x+1, 0, self.evens()) == 50
 
193
                assert Iter.foldr(lambda x, y: y+1, 0, self.odds()) == 50
 
194
 
 
195
class MultiplexTest(Iterators):
 
196
        def testSingle(self):
 
197
                """Test multiplex single stream"""
 
198
                i_orig = self.one_to_100()
 
199
                i2_orig = self.one_to_100()
 
200
                i = Iter.multiplex(i_orig, 1)[0]
 
201
                assert Iter.equal(i, i2_orig)
 
202
 
 
203
        def testTrible(self):
 
204
                """Test splitting iterator into three"""
 
205
                counter = [0]
 
206
                def ff(x): counter[0] += 1
 
207
                i_orig = self.one_to_100()
 
208
                i2_orig = self.one_to_100()
 
209
                i1, i2, i3 = Iter.multiplex(i_orig, 3, ff)
 
210
                assert Iter.equal(i1, i2)
 
211
                assert Iter.equal(i3, i2_orig)
 
212
                assert counter[0] == 100, counter
 
213
 
 
214
        def testDouble(self):
 
215
                """Test splitting into two..."""
 
216
                i1, i2 = Iter.multiplex(self.one_to_100(), 2)
 
217
                assert Iter.equal(i1, self.one_to_100())
 
218
                assert Iter.equal(i2, self.one_to_100())
 
219
 
 
220
 
 
221
class ITRBadder(ITRBranch):
 
222
        def start_process(self, index):
 
223
                self.total = 0
 
224
 
 
225
        def end_process(self):
 
226
                if self.base_index:
 
227
                        summand = self.base_index[-1]
 
228
                        #print "Adding ", summand
 
229
                        self.total += summand
 
230
 
 
231
        def branch_process(self, subinstance):
 
232
                #print "Adding subinstance ", subinstance.total
 
233
                self.total += subinstance.total
 
234
 
 
235
class ITRBadder2(ITRBranch):
 
236
        def start_process(self, index):
 
237
                self.total = 0
 
238
 
 
239
        def end_process(self):
 
240
                #print "Adding ", self.base_index
 
241
                self.total += reduce(lambda x,y: x+y, self.base_index, 0)
 
242
 
 
243
        def can_fast_process(self, index):
 
244
                if len(index) == 3: return 1
 
245
                else: return None
 
246
 
 
247
        def fast_process(self, index):
 
248
                self.total += index[0] + index[1] + index[2]
 
249
 
 
250
        def branch_process(self, subinstance):
 
251
                #print "Adding branch ", subinstance.total
 
252
                self.total += subinstance.total
 
253
 
 
254
class TreeReducerTest(unittest.TestCase):
 
255
        def setUp(self):
 
256
                self.i1 = [(), (1,), (2,), (3,)]
 
257
                self.i2 = [(0,), (0,1), (0,1,0), (0,1,1), (0,2), (0,2,1), (0,3)]
 
258
 
 
259
                self.i1a = [(), (1,)]
 
260
                self.i1b = [(2,), (3,)]
 
261
                self.i2a = [(0,), (0,1), (0,1,0)]
 
262
                self.i2b = [(0,1,1), (0,2)]
 
263
                self.i2c = [(0,2,1), (0,3)]
 
264
 
 
265
        def testTreeReducer(self):
 
266
                """testing IterTreeReducer"""
 
267
                itm = IterTreeReducer(ITRBadder, [])
 
268
                for index in self.i1:
 
269
                        val = itm(index)
 
270
                        assert val, (val, index)
 
271
                itm.Finish()
 
272
                assert itm.root_branch.total == 6, itm.root_branch.total
 
273
 
 
274
                itm2 = IterTreeReducer(ITRBadder2, [])
 
275
                for index in self.i2:
 
276
                        val = itm2(index)
 
277
                        if index == (): assert not val
 
278
                        else: assert val
 
279
                itm2.Finish()
 
280
                assert itm2.root_branch.total == 12, itm2.root_branch.total
 
281
 
 
282
        def testTreeReducerState(self):
 
283
                """Test saving and recreation of an IterTreeReducer"""
 
284
                itm1a = IterTreeReducer(ITRBadder, [])
 
285
                for index in self.i1a:
 
286
                        val = itm1a(index)
 
287
                        assert val, index
 
288
                itm1b = pickle.loads(pickle.dumps(itm1a))
 
289
                for index in self.i1b:
 
290
                        val = itm1b(index)
 
291
                        assert val, index
 
292
                itm1b.Finish()
 
293
                assert itm1b.root_branch.total == 6, itm1b.root_branch.total
 
294
 
 
295
                itm2a = IterTreeReducer(ITRBadder2, [])
 
296
                for index in self.i2a:
 
297
                        val = itm2a(index)
 
298
                        if index == (): assert not val
 
299
                        else: assert val
 
300
                itm2b = pickle.loads(pickle.dumps(itm2a))
 
301
                for index in self.i2b:
 
302
                        val = itm2b(index)
 
303
                        if index == (): assert not val
 
304
                        else: assert val
 
305
                itm2c = pickle.loads(pickle.dumps(itm2b))
 
306
                for index in self.i2c:
 
307
                        val = itm2c(index)
 
308
                        if index == (): assert not val
 
309
                        else: assert val
 
310
                itm2c.Finish()
 
311
                assert itm2c.root_branch.total == 12, itm2c.root_branch.total
 
312
 
 
313
 
 
314
if __name__ == "__main__": unittest.main()