File: test_rules.py

package info (click to toggle)
python-peak.rules 0.5a1%2Br2713-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 632 kB
  • ctags: 658
  • sloc: python: 3,625; makefile: 29
file content (836 lines) | stat: -rwxr-xr-x 24,826 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
import unittest, sys
from peak.rules.core import *

x2 = lambda a: a*2
x3 = lambda next_method, a: next_method(a)*3

class TypeEngineTests(unittest.TestCase):

    def testIntraSignatureCombinationAndRemoval(self):
        abstract()
        def f(a):
            """blah"""

        rx2 = Rule(x2,(int,), Method)
        rx3 = Rule(x3,(int,), Around)

        rules_for(f).add(rx2)
        self.assertEqual(f(1), 2)

        rules_for(f).add(rx3)
        self.assertEqual(f(1), 6)

        rules_for(f).remove(rx3)
        self.assertEqual(f(1), 2)

    def testAroundDecoratorAndRetroactiveCombining(self):
        def f(a):
            return a

        self.assertEqual(f(1), 1)
        self.assertEqual(f('x'), 'x')

        when(f, (int,))(x2)
        self.assertEqual(f(1), 2)
        self.assertEqual(f('x'), 'x')

        around(f, (int,))(lambda a:42)
        self.assertEqual(f(1), 42)
        self.assertEqual(f('x'), 'x')


class MiscTests(unittest.TestCase):

    def testPointers(self):
        from peak.rules.indexing import IsObject
        from sys import maxint
        anOb = object()
        ptr = IsObject(anOb)
        self.assertEqual(id(anOb)&maxint,ptr)
        self.assertEqual(hash(id(anOb)&maxint),hash(ptr))

        self.assertEqual(ptr.match, True)
        self.assertEqual(IsObject(anOb, False).match, False)
        self.assertNotEqual(IsObject(anOb, False), ptr)

        class X: pass
        anOb = X()
        ptr = IsObject(anOb)
        oid = id(anOb)&maxint
        self.assertEqual(oid,ptr)
        self.assertEqual(hash(oid),hash(ptr))
        del anOb
        self.assertNotEqual(ptr,"foo")
        self.assertEqual(ptr,ptr)
        self.assertEqual(hash(oid),hash(ptr))

    def testRuleSetReentrance(self):
        from peak.rules.core import Rule, RuleSet
        rs = RuleSet()
        log = []
        class MyListener:
            def actions_changed(self, added, removed):
                log.append(1)
                if self is ml1:
                    rs.unsubscribe(ml2)
        ml1, ml2 = MyListener(), MyListener()
        rs.subscribe(ml1)
        rs.subscribe(ml2)
        self.assertEqual(log, [])
        rs.add(Rule(lambda:None))
        self.assertEqual(log, [1, 1])

    def testAbstract(self):
        def f1(x,y=None):
            raise AssertionError("Should never get here")
        d = Dispatching(f1)
        log = []
        d.rules.default_action = lambda *args: log.append(args)
        f1 = abstract(f1)
        f1(27,42)
        self.assertEqual(log, [(27,42)])
        when(f1, ())(lambda *args: 99)
        self.assertRaises(AssertionError, abstract, f1)

    def testAbstractRegeneration(self):
        def f1(x,y=None):
            raise AssertionError("Should never get here")
        d = Dispatching(f1)
        log = []
        d.rules.default_action = lambda *args: log.append(args)
        d.request_regeneration()
        f1 = abstract(f1)
        self.assertNotEqual(d.backup, f1.func_code)
        self.assertEqual(f1.func_code, d._regen)
        f1.func_code = d.backup
        f1(27,42)
        self.assertEqual(log, [(27,42)])
        
    def testCreateEngine(self):
        def f1(x,y=None):
            raise AssertionError("Should never get here")
        d = Dispatching(f1)
        old_engine = d.engine
        self.assertEqual(d.rules.listeners, [old_engine])
        from peak.rules.core import TypeEngine
        class MyEngine(TypeEngine): pass
        d.create_engine(MyEngine)
        new_engine = d.engine
        self.assertNotEqual(new_engine, old_engine)
        self.failUnless(isinstance(new_engine, MyEngine))
        self.assertEqual(d.rules.listeners, [new_engine])


    def testIndexClassicMRO(self):
        class MyEngine: pass
        eng = MyEngine()
        from peak.rules.indexing import TypeIndex
        from peak.rules.criteria import Class
        from types import InstanceType
        ind = TypeIndex(eng, 'classes')
        ind.add_case(0, Class(MyEngine))
        ind.add_case(1, Class(object))
        ind.reseed(Class(InstanceType))
        self.assertEqual(
            dict(ind.expanded_sets()),
            {MyEngine: [[0,1],[]], InstanceType: [[1],[]], object: [[1],[]]}
        )

    def testEngineArgnames(self):
        argnames = lambda func: Dispatching(func).engine.argnames
        self.assertEqual(
            argnames(lambda a,b,c=None,*d,**e: None), list('abcde')
        )
        self.assertEqual(
            argnames(lambda a,b,c=None,*d: None), list('abcd')
        )
        self.assertEqual(
            argnames(lambda a,b,c=None,**e: None), list('abce')
        )
        self.assertEqual(
            argnames(lambda a,b,c=None: None), list('abc')
        )
        self.assertEqual(
            argnames(lambda a,(b,(c,d)), e: None), list('abcde')
        )

    def test_istype_implies(self):
        self.failUnless(implies(istype(object), object))
        self.failUnless(implies(istype(int), object))
        self.failIf(implies(istype(object, False), object))
        self.failIf(implies(istype(int, False), object))



    def testIndexedEngine(self):
        from peak.rules.predicates import IndexedEngine, Comparison
        from peak.rules.criteria import Range, Value, Test, Signature
        from peak.util.assembler import Local
        from peak.util.extremes import Min, Max
        abstract()
        def classify(age): pass
        Dispatching(classify).create_engine(IndexedEngine)
        def setup(r, f):
            when(classify, Signature([Test(Comparison(Local('age')), r)]))(f)
        setup(Range(hi=( 2,-1)), lambda age:"infant")
        setup(Range(hi=(13,-1)), lambda age:"preteen")
        setup(Range(hi=( 5,-1)), lambda age:"preschooler")
        setup(Range(hi=(20,-1)), lambda age:"teenager")
        setup(Range(lo=(20,-1)), lambda age:"adult")
        setup(Range(lo=(55,-1)), lambda age:"senior")
        setup(Value(16), lambda age:"sweet sixteen")

        self.assertEqual(classify(0),"infant")
        self.assertEqual(classify(25),"adult")
        self.assertEqual(classify(17),"teenager")
        self.assertEqual(classify(13),"teenager")
        self.assertEqual(classify(12.99),"preteen")
        self.assertEqual(classify(4),"preschooler")
        self.assertEqual(classify(55),"senior")
        self.assertEqual(classify(54.9),"adult")
        self.assertEqual(classify(14.5),"teenager")
        self.assertEqual(classify(16),"sweet sixteen")
        self.assertEqual(classify(16.5),"teenager")
        self.assertEqual(classify(99),"senior")
        self.assertEqual(classify(Min),"infant")
        self.assertEqual(classify(Max),"senior")

    def testSignatureOfTruthTests(self):
        from peak.rules.predicates import Truth        
        from peak.rules.criteria import Test, Signature
        # this line used to fail with a recursion error:
        Signature([Test(Truth(99), True), Test(Truth(88), False)])



    def testClassBodyRules(self):
        from peak.rules.core import Local, Rule
        from peak.rules.criteria import Signature, Test, Class, Value
        from peak.rules.predicates import IsInstance, Truth

        abstract()
        def f1(a): pass

        abstract()
        def f2(b): pass

        # This is to verify that the rules have sequence numbers by definition
        # order, not reverse definition order, inside a class.
        num = Rule(None).sequence
        
        class T:
            f1=sys._getframe(1).f_locals['f1']  # ugh
            when(f1)
            def f1_(a): pass

            f2=sys._getframe(1).f_locals['f2']
            when(f2, 'b')
            def f2_(b): pass

        self.assertEqual(
            list(rules_for(f1)), [Rule(T.f1_.im_func, (T,), Method, num+1)]
        )
        self.assertEqual(
            list(rules_for(f2)), [Rule(
                T.f2_.im_func, Signature([
                    Test(IsInstance(Local('b')), Class(T)),
                    Test(Truth(Local('b')), Value(True))
                ]), Method, num+2)
            ]
        )






    def testParseInequalities(self):
        from peak.rules.predicates import CriteriaBuilder, Comparison, Truth
        from peak.util.assembler import Compare, Local
        from peak.rules.criteria import Inequality, Test, Value
        from peak.rules.ast_builder import parse_expr
        builder = CriteriaBuilder(
            dict(x=Local('x'), y=Local('y')), locals(), globals(), __builtins__
        )
        pe = builder.parse

        x_cmp_y = lambda op, t=True: Test(
            Truth(Compare(Local('x'), ((op, Local('y')),))), Value(True, t)
        )      
        x,y = Comparison(Local('x')), Comparison(Local('y'))

        for op, mirror_op, not_op, stdop, not_stdop in [
            ('>', '<', '<=','>','<='),
            ('<', '>', '>=','<','>='),
            ('==','==','!=','==','!='),
            ('<>','<>','==','!=','=='),
        ]:
            fwd_sig = Test(x, Inequality(op, 1))
            self.assertEqual(pe('x %s 1' % op), fwd_sig)
            self.assertEqual(pe('1 %s x' % mirror_op), fwd_sig)

            rev_sig = Test(x, Inequality(mirror_op, 1))
            self.assertEqual(pe('x %s 1' % mirror_op), rev_sig)
            self.assertEqual(pe('1 %s x' % op), rev_sig)

            not_sig = Test(x, Inequality(not_op, 1))
            self.assertEqual(pe('not x %s 1' % op), not_sig)
            self.assertEqual(pe('not x %s 1' % not_op), fwd_sig)

            self.assertEqual(pe('x %s y' % op), x_cmp_y(stdop))
            self.assertEqual(pe('x %s y' % not_op), x_cmp_y(not_stdop))

            self.assertEqual(pe('not x %s y' % op),x_cmp_y(stdop,False))
            self.assertEqual(pe('not x %s y' % not_op),x_cmp_y(not_stdop,False))



    def testInheritance(self):
        class X: pass
        class Y(X): pass
        x = Y()
        f = lambda x: "f"
        when(f, "isinstance(x, X)")(lambda x: "g")
        when(f, "type(x) is X")(lambda x: "h")
        self.assertEqual(f(x), 'g')

    def testNotInherited(self):
        f = abstract(lambda x: "f")
        when(f, "not isinstance(x, int)")(lambda x: "g")
        when(f, "type(x) is object")(lambda x: "h")
        self.assertEqual(f(None), 'g')

    def testTypeImplicationAndIsSubclassOrdering(self):
        from inspect import isclass
        class A(object): pass
        class B(A): pass
        whats_this = abstract(lambda obj: obj)
        when(whats_this, "isclass(obj) and issubclass(obj, A)")(lambda o:"A")
        when(whats_this, "isclass(obj) and issubclass(obj, B)")(lambda o:"B")
        when(whats_this, (B,))(lambda o:"B()")
        when(whats_this, (A,))(lambda o:"A()")
        self.failUnlessEqual(whats_this(B), "B")
        self.failUnlessEqual(whats_this(A), "A")
        self.failUnlessEqual(whats_this(B()), "B()")
        self.failUnlessEqual(whats_this(A()), "A()")

    def testRuleSetClear(self):
        from peak.rules.core import Rule, RuleSet
        rs = RuleSet(); r = Rule(lambda:None,actiontype=Method)
        rs.add(r)
        self.assertEqual(list(rs), [r])
        rs.clear()
        self.assertEqual(list(rs), [])





    def testTypeEngineKeywords(self):
        def func(x, **k): pass
        def fstr(x,**k): return x,k
        abstract(func)
        when(func,(str,))(fstr)
        self.assertEqual(func('x',s='7'), ('x',{'s':'7'}))

    def testFlatPriorities(self):
        from peak.rules import value, AmbiguousMethods
        from peak.rules.predicates import priority
        
        f = lambda n, m: 0
        f1, f2, f3 = value(1), value(2), value(3)
        
        when(f, "n==5 and priority(1)")(f1)
        when(f, "m==5 and priority(1)")(f2)
        when(f, "n==5 and m==5 and priority(1)")(f3)
        self.assertEqual(f(5,5), 3)

    def testIsNot(self):
        def func(x): pass
        p,q,r = object(),object(),object()
        when(func,"x is not p")(value('~p'))
        when(func,"x is not p and x is not q and x is not r")(value('nada'))
        self.assertEqual(func(23),'nada')
        self.assertEqual(func(q),'~p')
        
    def testTypeVsIsTypePrecedence(self):
        def func(x): pass
        when(func, (int,        ))(value(1))
        when(func, (istype(int),))(value(2))
        self.assertEqual(func(42), 2)

    def testNamedGFExtension(self):
        p,q,r = object(),object(),object()
        when("%s:%s.named_func" % (__name__, self.__class__.__name__), "x is not p")(value('~p'))
        self.assertEqual(self.named_func(q),'~p')

    def named_func(x): pass
    named_func = staticmethod(named_func)

class RuleDispatchTests(unittest.TestCase):

    def testSimplePreds(self):
        from peak.rules import dispatch

        [dispatch.generic()]
        def classify(age):
            """Stereotype for age"""

        def defmethod(gf,s,func):
            gf.when(s)(func)

        defmethod(classify,'not not age<2', lambda age:"infant")
        defmethod(classify,'age<13', lambda age:"preteen")
        defmethod(classify,'age<5',  lambda age:"preschooler")
        defmethod(classify,'20>age', lambda age:"teenager")
        defmethod(classify,'not age<20',lambda age:"adult")
        defmethod(classify,'age>=55',lambda age:"senior")
        defmethod(classify,'age==16',lambda age:"sweet sixteen")
        self.assertEqual(classify(25),"adult")
        self.assertEqual(classify(17),"teenager")
        self.assertEqual(classify(13),"teenager")
        self.assertEqual(classify(12.99),"preteen")
        self.assertEqual(classify(0),"infant")
        self.assertEqual(classify(4),"preschooler")
        self.assertEqual(classify(55),"senior")
        self.assertEqual(classify(54.9),"adult")
        self.assertEqual(classify(14.5),"teenager")
        self.assertEqual(classify(16),"sweet sixteen")
        self.assertEqual(classify(16.5),"teenager")
        self.assertEqual(classify(99),"senior")
        self.assertEqual(classify(dispatch.strategy.Min),"infant")
        self.assertEqual(classify(dispatch.strategy.Max),"senior")








    def testKwArgHandling(self):
        from peak.rules import dispatch
        [dispatch.generic()]
        def f(**fiz): """Test of kw handling"""

        [f.when("'x' in fiz")]
        def f(**fiz): return "x"

        [f.when("'y' in fiz")]
        def f(**fiz): return "y"

        self.assertEqual(f(x=1),"x")
        self.assertEqual(f(y=1),"y")
        self.assertRaises(dispatch.AmbiguousMethod, f, x=1, y=1)

    def testVarArgHandling(self):
        from peak.rules import dispatch
        [dispatch.generic()]
        def f(*fiz): """Test of vararg handling"""

        [f.when("'x' in fiz")]
        def f(*fiz): return "x"

        [f.when("'y' in fiz")]
        def f(*fiz): return "y"

        self.assertEqual(f("foo","x"),"x")
        self.assertEqual(f("bar","q","y"),"y")
        self.assertEqual(f("bar","q","y"),"y")
        self.assertEqual(f("y","q",),"y")
        self.assertRaises(dispatch.AmbiguousMethod, f, "x","y")

    def test_NoApplicableMethods_is_raised(self):
        from peak.rules import dispatch
        [dispatch.generic()]
        def demo_func(number):
            pass
        demo_func.when("number < 10")(lambda x: 0)
        self.assertEqual(demo_func(3),0)
        self.assertRaises(dispatch.NoApplicableMethods, demo_func, 33)

    def testSingles(self):
        from peak.rules import dispatch        

        [dispatch.on('t')]
        def gm (t) : pass

        [gm.when(object)]
        def gm (t) : return 'default'

        [gm.when(int)]
        def gm2 (t) : return 'int'

        self.assertEqual(gm(42),"int")
        self.assertEqual(gm("x"),"default")
        self.assertEqual(gm(42.0),"default")


    def testSubclassDispatch(self):
        from peak.rules import dispatch        

        [dispatch.generic()]
        def gm (t) : pass

        [gm.when(dispatch.strategy.default)]
        def gm (t) : return 'default'

        [gm.when('issubclass(t,int)')]
        def gm2 (t) : return 'int'

        self.assertEqual(gm(int),"int")
        self.assertEqual(gm(object),"default")
        self.assertEqual(gm(float),"default")









    def testTrivialities(self):
        from peak.rules import dispatch        

        [dispatch.on('x')]
        def f1(x,*y,**z): "foo bar"

        [dispatch.on('x')]
        def f2(x,*y,**z): "baz spam"

        for f,doc in (f1,"foo bar"),(f2,"baz spam"):
            self.assertEqual(f.__doc__, doc)

            # Empty generic should raise NoApplicableMethods
            self.assertRaises(dispatch.NoApplicableMethods, f, 1, 2, 3)
            self.assertRaises(dispatch.NoApplicableMethods, f, "x", y="z")

            # Must have at least one argument to do dispatching
            self.assertRaises(TypeError, f)
            self.assertRaises(TypeError, f, foo="bar")






















class MockBuilder:
    def __init__(self, test, expr, cases, remaining, seeds, index=None):
        self.test = test
        self.args = expr, cases, remaining, {}
        self.seeds = seeds
        self.index = index

    def test_func(self, func):
        return func(self, *self.args)

    def build(self, cases, remaining_exprs, memo):
        self.test.failUnless(memo is self.args[-1])
        self.test.assertEqual(self.args[-2], remaining_exprs)
        return cases

    def seed_bits(self, expr, cases):
        self.test.assertEqual(self.args[1], cases)
        if self.index is not None:
            return self.index.seed_bits(cases)
        return self.seeds
        
    def reseed(self, expr, criterion):
        self.test.assertEqual(self.args[0], expr)
        self.index.reseed(criterion)

















class NodeBuildingTests(unittest.TestCase):

    def build(self, func, dontcare, seeds, index=None):
        seedbits = dontcare, seeds
        builder = MockBuilder(
            self, 'expr', 'cases', 'remaining', seedbits, index
        )
        return builder.test_func(func)

    def testTruthNode(self):
        from peak.rules.predicates import truth_node
        node = self.build(truth_node, 27,
            {True: (128,0), False: (64,0)})
        self.assertEqual(node, (27|128, 27|64))

    def testIdentityNode(self):
        from peak.rules.predicates import identity_node
        node = self.build(identity_node, 27,
            {9127: (128,0), 6499: (64,0), None: (0,0)})
        self.assertEqual(node, {None:27, 9127:27|128, 6499: 27|64})

    def testRangeNode(self):
        from peak.rules.indexing import RangeIndex, to_bits
        from peak.rules.predicates import range_node
        from peak.rules.criteria import Range, Value, Min, Max
        ind = RangeIndex(self, 'expr')
        ind.add_case(0, Value(19))
        ind.add_case(1, Value(23))
        ind.add_case(2, Value(23, False))
        ind.add_case(3, Range(lo=(57,1)))
        ind.add_case(4, Range(lo=(57,-1)))
        dontcare, seeds = ind.seed_bits(to_bits(range(6)))
        exact, ranges = self.build(range_node, dontcare, seeds)
        self.assertEqual(exact,
            {19:to_bits([0, 2, 5]), 23:to_bits([1,5]), 57:to_bits([2,4,5])})
        self.assertEqual(ranges,
            [((Min,57), to_bits([2,5])),  ((57,Max), to_bits([2,3,4,5]))])




    def testClassNode(self):
        from peak.rules.indexing import TypeIndex, to_bits
        from peak.rules.predicates import class_node
        from peak.rules.criteria import Class, Conjunction
        from types import InstanceType
        ind = TypeIndex(self, 'expr')
        class a: pass
        class b: pass
        class c(a,b): pass
        class x(a,b,object): pass

        ind.add_case(0, Class(InstanceType))
        ind.add_case(1, Conjunction([Class(a), Class(b), Class(c,False)]))
        ind.add_case(2, Class(object))
        ind.add_case(3, Conjunction([Class(a), Class(b)]))
        ind.add_case(4, Class(a))
        ind.selectivity(range(6))
        cases = to_bits(range(6))
        builder = MockBuilder(
            self, 'expr', cases, 'remaining', ind.seed_bits(cases), ind
        )
        cache, lookup = builder.test_func(class_node,)
        self.assertEqual(cache, {})
        data = [
            (object, to_bits([2,5])),
            (InstanceType, to_bits([0,2,5])),
            (a, to_bits([0,2,4,5])),
            (b, to_bits([0,2,5])),
            (c, to_bits([0,2,3,4,5])),
            (x, to_bits([1,2,3,4,5]))
        ]
        for k, v in data:
            self.assertEqual(lookup(k), v)
        self.assertEqual(cache, dict(data))
        
        





class DecompilationTests(unittest.TestCase):

    def setUp(self):
        from peak.rules.ast_builder import parse_expr
        from peak.rules.codegen import ExprBuilder        
        from peak.util.assembler import Local, Const
        from peak.rules.debug.decompile import decompile
        class Const(Const): pass    # non-folding
        class ExprBuilder(ExprBuilder):
            def Const(self,value): return Const(value)
        argmap = dict([(name,Local(name)) for name in 'abcdefgh'])
        builder = ExprBuilder(argmap, locals(), globals(), __builtins__)
        self.parse = builder.parse
        self.decompile = decompile

    def roundtrip(self, s1, s2=None):
        if s2 is None:
            s2 = s1
        self.check(self.parse(s1), s2)

    def check(self, expr, s):
        self.assertEqual(self.decompile(expr), s)

    def test_basics(self):
        from peak.util.assembler import Pass, Getattr, Local
        self.check(Pass, '')
        self.check(Getattr(Local('a'), 'b'), 'a.b')
        self.roundtrip('not -+~`a`')
        self.roundtrip('a+b-c*d/e%f**g//h')
        self.roundtrip('a and b or not c')
        self.roundtrip('~a|b^c&d<<e>>f')
        self.roundtrip('(a, b)')
        self.roundtrip('[a, b]')
        self.roundtrip('[a]')
        self.roundtrip('[]')
        self.roundtrip('()')
        self.roundtrip('1')
        self.roundtrip('a.b.c')
        # TODO: Compare


    def test_slices(self):
        self.roundtrip('a[:]')
        self.roundtrip('a[1:]')
        self.roundtrip('a[:2]')
        self.roundtrip('a[1:2]')
        self.roundtrip('a[1:2:3]')
        self.roundtrip('a[::]', 'a[:]')
        self.roundtrip('a[b:c:d]')
        self.roundtrip('a[b::d]')
        self.roundtrip('a[-b:c+d:e*f]')
        self.roundtrip('a[::d]')
        
    def test_paren_precedence(self):
        self.roundtrip('(1).__class__')
        self.roundtrip('1.2.__class__')
        self.roundtrip('`(a, b)`')
        self.roundtrip('`a,b`', '`(a, b)`')
        self.roundtrip('(a, [b, c])')
        self.roundtrip('a,b', '(a, b)')
        self.roundtrip('a+b*c')
        self.roundtrip('c*(a+b)')
        self.roundtrip('a*b*c')
        self.roundtrip('(a*b)*c', 'a*b*c')
        self.roundtrip('a*(b*c)')
        self.roundtrip('(a*b).c')
        self.roundtrip('(a, b, c)')
        self.roundtrip('a**b**c')
        self.roundtrip('a**(b**c)', 'a**b**c')
        self.roundtrip('a and b or c and not d')
        self.roundtrip('a and (b or c) and not d')

        if sys.version>='2.5':
            # if-else is right-associative
            self.roundtrip('(a, b, c) if d else e')
            self.roundtrip('(a if b else c) if d else e')
            self.roundtrip('a if (b if c else d) else e')
            self.roundtrip('a if b else (c if d else e)',
                           'a if b else c if d else e')



    def test_powers(self):
        # Exponentiation operator binds less tightly than unary numeric/bitwise
        # on the right:
        self.roundtrip('2**-1')
        self.roundtrip('2**+1')
        self.roundtrip('2**~1')
        self.roundtrip('2**1*2')
        self.roundtrip('2**(1*2)')
        
    def test_dicts(self):
        self.roundtrip('{}')
        self.roundtrip('{a: b}')
        self.roundtrip('{a: b, c: d}')

    def test_calls(self):
        self.roundtrip('1()')
        self.roundtrip('a()')
        self.roundtrip('a(b)')
        self.roundtrip('a(c=d)')
        self.roundtrip('a(*e)')
        self.roundtrip('a(**f)')
        self.roundtrip('a(b, c=d, *e, **f)')
        self.roundtrip('a.b(c)')
        self.roundtrip('a+b(c)')
        self.roundtrip('(a+b)(c)')
        if sys.version>='2.5':
            self.roundtrip('a if b(c) else d')
            self.roundtrip('a(b if c else d, *e if f else g)')





























def additional_tests():
    import doctest
    files = [
        'README.txt', 'DESIGN.txt', 'Indexing.txt', 'AST-Builder.txt',
        'Code-Generation.txt', 'Syntax-Matching.txt', 'Criteria.txt',
        'Predicates.txt', 
    ][sys.version<'2.4':]   # skip README.txt on 2.3 due to @ syntax
    return doctest.DocFileSuite(
        optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE,
        globs=dict(__name__=None),   # workaround PyPy type repr() issue 1292
         *files
    )