示例#1
0
 def test_memo_independence(self):
     from operator import add, sub
     madd = memo(add)
     msub = memo(sub)
     for a in range(10):
         for b in range(10):
             self.assertEqual(madd(a,b), add(a,b))
             self.assertEqual(msub(a,b), sub(a,b))
示例#2
0
    def test_memo_on_fib(self):
        Qs = (30, 28, 27, 26, 25)
        mfib = memo(fib)
        for Q in Qs:
            t1, A1 = time( fib, Q)
            t2, A2 = time(mfib, Q)
            # mfib gives the correct answer
            self.assertEqual(A1, A2)
            # mfib takes as long as fib the first time around
            self.assertTrue(0.9 < t1/t2 < 1.1)

            t3, A3 = time(mfib, Q)
            # mfib gives the correct answer the second time around
            self.assertEqual(A1, A3)
            # mfib is much faster the second time around
            self.assertTrue(t3 == 0 or t1/t3 > 10000)