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

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

Issue 2548603003: Adding memoized functions (Closed)
Patch Set: forgot if I pushed the latest before CQ 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
« no previous file with comments | « appengine/findit/libs/math/functions.py ('k') | appengine/findit/libs/math/vectors.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..73775ac8c77d41ee52b40d675515cde1553d783d
--- /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
+_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))
« no previous file with comments | « appengine/findit/libs/math/functions.py ('k') | appengine/findit/libs/math/vectors.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698