1
+ <?php
2
+
3
+ namespace Repeat ;
4
+
5
+ class RepeatFunctionTest extends \PHPUnit_Framework_TestCase
6
+ {
7
+ public function testAnonymousFunction ()
8
+ {
9
+ $ count = 0 ;
10
+ Repeat::_function (7 , function () use (&$ count ) {
11
+ $ count ++;
12
+ });
13
+
14
+ $ this ->assertSame (7 , $ count );
15
+ }
16
+
17
+ public function testAnonymousFunctionUntil ()
18
+ {
19
+ $ count = 0 ;
20
+ Repeat::_function (12 ,
21
+ function () use (&$ count ) {
22
+ $ count ++;
23
+ },
24
+ function () use (&$ count ) {
25
+ return $ count == 5 ? true : false ;
26
+ }
27
+ );
28
+
29
+ $ this ->assertSame (5 , $ count );
30
+ }
31
+
32
+ public function testAnonymousFunctionWithReturnValue ()
33
+ {
34
+ $ count = 0 ;
35
+ $ results = Repeat::_function (23 , function () use (&$ count ) {
36
+ $ count ++;
37
+ return 'foo ' ;
38
+ });
39
+
40
+ $ this ->assertSame (23 , $ count );
41
+ $ this ->assertCount (23 , $ results );
42
+ $ this ->assertSame ('foo ' , $ results [0 ]);
43
+ $ this ->assertSame ('foo ' , $ results [22 ]);
44
+ }
45
+
46
+ public function testNamedFunctionWithReturnValue ()
47
+ {
48
+ $ a = 3 ;
49
+ $ b = 6 ;
50
+
51
+ $ multiplication = function () use (&$ a , &$ b ) {
52
+ return $ a ++ * $ b ++;
53
+ };
54
+
55
+ $ results = Repeat::_function (4 , $ multiplication );
56
+
57
+ $ this ->assertCount (4 , $ results );
58
+ $ this ->assertSame (18 , $ results [0 ]);
59
+ $ this ->assertSame (28 , $ results [1 ]);
60
+ $ this ->assertSame (40 , $ results [2 ]);
61
+ $ this ->assertSame (54 , $ results [3 ]);
62
+ }
63
+
64
+ public function testNamedFunctionAnonymousUntilWithReturnValue ()
65
+ {
66
+ $ a = 84 ;
67
+ $ b = 7 ;
68
+
69
+ $ subtraction = function () use (&$ a , &$ b ) {
70
+ return --$ a - ++$ b ;
71
+ };
72
+
73
+ $ results = Repeat::_function (6 , $ subtraction , function () use (&$ a ) {
74
+ return $ a <= 81 ? true : false ;
75
+ });
76
+
77
+ $ this ->assertCount (3 , $ results );
78
+ $ this ->assertSame (75 , $ results [0 ]);
79
+ $ this ->assertSame (73 , $ results [1 ]);
80
+ $ this ->assertSame (71 , $ results [2 ]);
81
+ }
82
+
83
+ public function testNamedFunctionNamedUntilWithResultReferenceAndReturnValue ()
84
+ {
85
+ $ createRandomNumbers = function () {
86
+ return mt_rand (0 , 1000 );
87
+ };
88
+
89
+ $ until = function ($ result ) {
90
+ return count ($ result ) == 3 ? true : false ;
91
+ };
92
+
93
+ $ results = Repeat::_function (9 , $ createRandomNumbers , $ until );
94
+
95
+ $ this ->assertCount (3 , $ results );
96
+ $ this ->assertTrue ($ results [0 ] >= 0 && $ results [0 ] <= 1000 );
97
+ $ this ->assertTrue ($ results [1 ] >= 0 && $ results [1 ] <= 1000 );
98
+ $ this ->assertTrue ($ results [2 ] >= 0 && $ results [2 ] <= 1000 );
99
+ }
100
+
101
+ public function testNamedFunctionWithResultReferenceAndReturnValue ()
102
+ {
103
+ $ start = 4 ;
104
+ $ math = function ($ result ) use ($ start ) {
105
+ $ value = count ($ result ) == 0 ? $ start : $ result [max (array_keys ($ result ))];
106
+ return $ value * $ value ;
107
+ };
108
+
109
+ $ results = Repeat::_function (4 , $ math );
110
+
111
+ $ this ->assertCount (4 , $ results );
112
+ $ this ->assertSame (16 , $ results [0 ]);
113
+ $ this ->assertSame (256 , $ results [1 ]);
114
+ $ this ->assertSame (65536 , $ results [2 ]);
115
+ $ this ->assertSame (4294967296 , $ results [3 ]);
116
+ }
117
+ }
0 commit comments