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

Unified Diff: appengine/findit/libs/math/test/functions_test.py

Issue 2548603003: Adding memoized functions (Closed)
Patch Set: Addressing nits 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..e79dbf2e9ddfdcdde854c1d54c2a6fe1b8c847d5
--- /dev/null
+++ b/appengine/findit/libs/math/test/functions_test.py
@@ -0,0 +1,62 @@
+# 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
stgao 2016/12/05 19:30:56 style nit: for top-level definition, if it is not
+G = lambda x: x * x
+
+
+class FunctionsTest(unittest.TestCase):
+
+ def testFunctionCall(self):
+ """``Function.__call__`` returns same value as the underlying callable."""
+ self.assertEqual(F(5), Function(F)(5))
+ self.assertEqual(G(5), Function(G)(5))
+
+ def testFunctionMap(self):
+ """``Function.map`` composes functions as described in the docstring."""
+ self.assertEqual(G(F(5)), Function(F).map(G)(5))
+ self.assertEqual(F(G(5)), Function(G).map(F)(5))
+
+ def testMemoizedFunctionCall(self):
+ """``MemoizedFunction.__call__`` returns same value as its callable."""
+ self.assertEqual(F(5), MemoizedFunction(F)(5))
+ self.assertEqual(G(5), MemoizedFunction(G)(5))
+
+ def testMemoizedFunctionMap(self):
+ """``MemoizedFunction.map`` composes functions as described."""
+ self.assertEqual(G(F(5)), MemoizedFunction(F).map(G)(5))
+ self.assertEqual(F(G(5)), MemoizedFunction(G).map(F)(5))
+
+ def testMemoization(self):
+ """``MemoizedFunction.__call__`` actually does memoize.
+
+ That is, we call the underlying function once (to set the memo), then
+ we discard the underlying function (to be sure the next ``__call__``
+ is handled from the memos, and finally call the function to check.
+ """
+ f = MemoizedFunction(F)
+ f(5)
+ del f._f
+ self.assertEqual(F(5), f(5))
+
+ def testClearMemos(self):
+ """``MemoizedFunction._ClearMemos`` does actually clear the memos.
+
+ That is, we call the underlying function once (to set the memo),
+ then swap put the underlying function with a different one (to be
+ sure we know whether the next ``__call__`` goes to the memos or to
+ the function), and finally clear the memos and check.
+ """
+ f = MemoizedFunction(F)
+ f(5)
+ f._f = G
+ 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