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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: appengine/findit/libs/math/test/functions_test.py
diff --git a/appengine/findit/libs/math/test/functions_test.py b/appengine/findit/libs/math/test/functions_test.py
new file mode 100644
index 0000000000000000000000000000000000000000..6cbce2c03bf7991ef5b41a67f842fab5f521bded
--- /dev/null
+++ b/appengine/findit/libs/math/test/functions_test.py
@@ -0,0 +1,43 @@
+# Copyright 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import unittest
+
+from libs.math.functions import Function
+from libs.math.functions import MemoizedFunction
+
+# Some arbitrary functions:
+F = lambda x: x + 1
+G = lambda x: x * x
+
Sharu Jiang 2016/12/02 23:47:18 nit: 2 lines
wrengr 2016/12/05 18:50:45 Done.
+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.
+
+ def testFunctionCall(self):
+ self.assertEqual(F(5), Function(F)(5))
+ self.assertEqual(G(5), Function(G)(5))
+
+ def testFunctionMap(self):
+ self.assertEqual(G(F(5)), Function(F).map(G)(5))
+ self.assertEqual(F(G(5)), Function(G).map(F)(5))
+
+ def testMemoizedFunctionCall(self):
+ self.assertEqual(F(5), MemoizedFunction(F)(5))
+ self.assertEqual(G(5), MemoizedFunction(G)(5))
+
+ def testMemoizedFunctionMap(self):
+ self.assertEqual(G(F(5)), MemoizedFunction(F).map(G)(5))
+ self.assertEqual(F(G(5)), MemoizedFunction(G).map(F)(5))
+
+ def testMemoization(self):
+ f = MemoizedFunction(F)
+ f(5) # Call it once, to make a memo
+ 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.
+ self.assertEqual(F(5), f(5))
+
+ def testClearMemos(self):
+ f = MemoizedFunction(F)
+ 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.
+ 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
+ f._ClearMemos()
+ self.assertEqual(G(5), f(5))
« 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