public function testMemoize() { // from js $fib = function ($n) use(&$fib) { return $n < 2 ? $n : $fib($n - 1) + $fib($n - 2); }; $fastFib = __u::memoize($fib); $this->assertEquals(55, $fib(10), 'a memoized version of fibonacci produces identical results'); $this->assertEquals(55, $fastFib(10), 'a memoized version of fibonacci produces identical results'); $o = function ($str) { return $str; }; $fastO = __u::memoize($o); $this->assertEquals('toString', $o('toString'), 'checks hasOwnProperty'); $this->assertEquals('toString', $fastO('toString'), 'checks hasOwnProperty'); // extra $name = function () { return 'moe'; }; $fastName = __u::memoize($name); $this->assertEquals('moe', $name(), 'works with no parameters'); $this->assertEquals('moe', $fastName(), 'works with no parameters'); $names = function ($one, $two, $three) { return join(', ', array($one, $two, $three)); }; $fastNames = __u::memoize($names); $this->assertEquals('moe, larry, curly', $names('moe', 'larry', 'curly'), 'works with multiple parameters'); $this->assertEquals('moe, larry, curly', $fastNames('moe', 'larry', 'curly'), 'works with multiple parameters'); $foo = function () { return 'foo'; }; $fastFoo = __u($foo)->memoize(); $this->assertEquals('foo', $foo(), 'can handle OO-style calls'); $this->assertEquals('foo', $fastFoo(), 'can handle OO-style calls'); $bar = function () { return 'bar'; }; $fastBar = __u::memoize($bar, function ($function, $args) { return sha1(join('x', array(var_export($function, 1), var_export($args, 1)))); }); $this->assertEquals('bar', $bar(), 'can custom hash function'); $this->assertEquals('bar', $fastBar(), 'can use custom hash function'); // docs $fibonacci = function ($n) use(&$fibonacci) { return $n < 2 ? $n : $fibonacci($n - 1) + $fibonacci($n - 2); }; $fastFibonacci = __u::memoize($fibonacci); $this->assertEquals($fibonacci(2), $fastFibonacci(2)); }