|
6 | 6 |
|
7 | 7 | class UnderscoreTest extends \PHPUnit_Framework_TestCase |
8 | 8 | { |
9 | | - public function test_memoize() |
| 9 | + public function test_constant() |
10 | 10 | { |
11 | | - $memoSum = _::_()->memoize(function ($a, $b) { |
12 | | - echo "sum $a + $b"; |
| 11 | + foreach ([1, 'A', [], new \stdClass()] as $value) { |
| 12 | + $fn = _::_()->constant($value); |
13 | 13 |
|
14 | | - return $a + $b; |
15 | | - }); |
16 | | - |
17 | | - // Every time the sum callback is called it echoes something. |
18 | | - // But since it is memorized, it should only echo in the first call. |
19 | | - ob_start(); |
20 | | - |
21 | | - // Call 3 times! |
22 | | - $this->assertSame(1 + 2, $memoSum(1, 2)); |
23 | | - $this->assertSame(1 + 2, $memoSum(1, 2)); |
24 | | - $this->assertSame(1 + 2, $memoSum(1, 2)); |
| 14 | + $this->assertSame($value, $fn()); |
| 15 | + } |
| 16 | + } |
25 | 17 |
|
26 | | - // Call twice for different args! |
27 | | - $this->assertSame(3 + 2, $memoSum(3, 2)); |
28 | | - $this->assertSame(3 + 2, $memoSum(3, 2)); |
| 18 | + public function test_noop() |
| 19 | + { |
| 20 | + $epsilon = 0.0000000001; |
29 | 21 |
|
30 | | - $buffer = ob_get_clean(); |
| 22 | + $t = microtime(1); |
| 23 | + $m = memory_get_usage(); |
| 24 | + $x = _::_()->noop(); |
| 25 | + $t = microtime(1) - $t; |
| 26 | + $m = memory_get_usage() - $m; |
31 | 27 |
|
32 | | - $this->assertSame(1, substr_count($buffer, 'sum 1 + 2'), |
33 | | - 'Should be called only once, subsequent calls uses memo' |
34 | | - ); |
35 | | - $this->assertSame(1, substr_count($buffer, 'sum 3 + 2'), |
36 | | - 'Should be called only once, subsequent calls uses memo' |
37 | | - ); |
| 28 | + $this->assertLessThanOrEqual($t, $epsilon); |
| 29 | + $this->assertLessThanOrEqual($m, $epsilon); |
38 | 30 | } |
39 | 31 |
|
40 | | - public function test_delay() |
| 32 | + public function test_times() |
41 | 33 | { |
42 | | - $callback = function () { |
43 | | - // Do nothing! |
| 34 | + $fn = function ($i) { |
| 35 | + return $i * 2; |
44 | 36 | }; |
45 | 37 |
|
46 | | - // Calibrate time taken by callback! |
47 | | - $cTime = microtime(1); |
48 | | - $callback(); |
49 | | - $cTime = microtime(1) - $cTime; |
50 | | - |
51 | | - // Now delay this callback by 10millis (0.01sec). |
52 | | - $delayCall = _::_()->delay($callback, 10); |
| 38 | + $o = _::_()->times(5, $fn); |
53 | 39 |
|
54 | | - $time = microtime(1); |
55 | | - $delayCall(); |
56 | | - $time = microtime(1) - $time; |
57 | | - |
58 | | - // The overall time must be >= (cTime + 1sec). |
59 | | - $this->assertGreaterThanOrEqual(0.01 + $cTime, $time); |
| 40 | + $this->assertSame([0, 2, 4, 6, 8], $o->toArray()); |
60 | 41 | } |
61 | 42 |
|
62 | | - public function test_throttle() |
| 43 | + public function test_random() |
63 | 44 | { |
64 | | - $callback = function () { |
65 | | - echo 'throttle'; |
66 | | - }; |
| 45 | + $i = 10; |
67 | 46 |
|
68 | | - // Throttle the call for once per 10millis (0.01 sec) |
69 | | - // So that for a period of 300millis it should be actually called at most 3 times. |
70 | | - $throtCall = _::_()->throttle($callback, 10); |
| 47 | + while ($i--) { |
| 48 | + $cases[rand(1, 10)] = rand(11, 20); |
| 49 | + } |
71 | 50 |
|
72 | | - ob_start(); |
| 51 | + foreach ($cases as $l => $r) { |
| 52 | + $rand = _::_()->random($l, $r); |
73 | 53 |
|
74 | | - $start = microtime(1); |
75 | | - while (microtime(1) - $start <= 0.031) { |
76 | | - $throtCall(); |
| 54 | + $this->assertGreaterThanOrEqual($l, $rand); |
| 55 | + $this->assertLessThanOrEqual($r, $rand); |
77 | 56 | } |
| 57 | + } |
78 | 58 |
|
79 | | - $buffer = ob_get_clean(); |
| 59 | + public function test_unique_id() |
| 60 | + { |
| 61 | + $u = _::_()->uniqueId(); |
| 62 | + $u1 = _::_()->uniqueId(); |
| 63 | + $u3 = _::_()->uniqueId('id:'); |
80 | 64 |
|
81 | | - $this->assertLessThanOrEqual(3, substr_count($buffer, 'throttle'), |
82 | | - 'Should be called only once, subsequent calls uses memo' |
83 | | - ); |
| 65 | + $this->assertSame('1', $u); |
| 66 | + $this->assertSame('2', $u1); |
| 67 | + $this->assertSame('id:3', $u3); |
84 | 68 | } |
85 | 69 | } |
0 commit comments