Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: appengine/findit/libs/math/test/functions_test.py

Issue 2548603003: Adding memoized functions (Closed)
Patch Set: rebase Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2016 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import unittest
6
7 from libs.math.functions import Function
8 from libs.math.functions import MemoizedFunction
9
10 # Some arbitrary functions:
11 F = lambda x: x + 1
12 G = lambda x: x * x
13
Sharu Jiang 2016/12/02 23:47:18 nit: 2 lines
wrengr 2016/12/05 18:50:45 Done.
14 class FunctionsTest(unittest.TestCase):
Martin Barbella 2016/12/02 23:50:21 Same nits for whitespace and docstrings.
wrengr 2016/12/05 18:50:45 Done.
15
16 def testFunctionCall(self):
17 self.assertEqual(F(5), Function(F)(5))
18 self.assertEqual(G(5), Function(G)(5))
19
20 def testFunctionMap(self):
21 self.assertEqual(G(F(5)), Function(F).map(G)(5))
22 self.assertEqual(F(G(5)), Function(G).map(F)(5))
23
24 def testMemoizedFunctionCall(self):
25 self.assertEqual(F(5), MemoizedFunction(F)(5))
26 self.assertEqual(G(5), MemoizedFunction(G)(5))
27
28 def testMemoizedFunctionMap(self):
29 self.assertEqual(G(F(5)), MemoizedFunction(F).map(G)(5))
30 self.assertEqual(F(G(5)), MemoizedFunction(G).map(F)(5))
31
32 def testMemoization(self):
33 f = MemoizedFunction(F)
34 f(5) # Call it once, to make a memo
35 del f._f # Discard the function (to be sure the next call comes form memos)
inferno 2016/12/05 00:02:10 Put comment in previous line ending with period.,
wrengr 2016/12/05 18:50:45 Done.
36 self.assertEqual(F(5), f(5))
37
38 def testClearMemos(self):
39 f = MemoizedFunction(F)
40 f(5) # Call it once, to make a memo
inferno 2016/12/05 00:02:10 Put comment in previous line ending with period.,
wrengr 2016/12/05 18:50:45 Done.
41 f._f = G # Change the function, so it's different than the memos.
Sharu Jiang 2016/12/02 23:47:18 Is changing _f only for testing or it can be actua
wrengr 2016/12/05 18:50:45 Changing _f is only for testing. Ideally there sho
42 f._ClearMemos()
43 self.assertEqual(G(5), f(5))
OLDNEW
« appengine/findit/libs/math/functions.py ('K') | « appengine/findit/libs/math/functions.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698