1<?php
2
3/**
4 * Hoa
5 *
6 *
7 * @license
8 *
9 * New BSD License
10 *
11 * Copyright © 2007-2017, Hoa community. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 *     * Redistributions of source code must retain the above copyright
16 *       notice, this list of conditions and the following disclaimer.
17 *     * Redistributions in binary form must reproduce the above copyright
18 *       notice, this list of conditions and the following disclaimer in the
19 *       documentation and/or other materials provided with the distribution.
20 *     * Neither the name of the Hoa nor the names of its contributors may be
21 *       used to endorse or promote products derived from this software without
22 *       specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37namespace Hoa\Iterator\Test\Unit;
38
39use Hoa\Iterator as LUT;
40use Hoa\Iterator\Buffer as SUT;
41use Hoa\Test;
42
43/**
44 * Class \Hoa\Iterator\Test\Unit\Buffer.
45 *
46 * Test suite of the buffer iterator.
47 *
48 * @copyright  Copyright © 2007-2017 Hoa community
49 * @license    New BSD License
50 */
51class Buffer extends Test\Unit\Suite
52{
53    public function case_constructor()
54    {
55        $this
56            ->given(
57                $innerIterator = $this->getInnerIterator(),
58                $bufferSize    = 3
59            )
60            ->when($result = new SUT($innerIterator, $bufferSize))
61            ->then
62                ->object($result->getInnerIterator())
63                    ->isIdenticalTo($innerIterator)
64                ->integer($result->getBufferSize())
65                    ->isEqualTo($bufferSize)
66                ->let($buffer = $this->invoke($result)->getBuffer())
67                ->object($buffer)
68                    ->isInstanceOf(\SplDoublyLinkedList::class)
69                ->boolean($buffer->isEmpty())
70                    ->isTrue();
71    }
72
73    public function case_negative_buffer_size()
74    {
75        $this
76            ->given(
77                $innerIterator = $this->getInnerIterator(),
78                $bufferSize    = -42
79            )
80            ->when($result = new SUT($innerIterator, $bufferSize))
81            ->then
82                ->integer($result->getBufferSize())
83                    ->isEqualTo(1);
84    }
85
86    public function case_null_buffer_size()
87    {
88        $this
89            ->given(
90                $innerIterator = $this->getInnerIterator(),
91                $bufferSize    = 0
92            )
93            ->when($result = new SUT($innerIterator, $bufferSize))
94            ->then
95                ->integer($result->getBufferSize())
96                    ->isEqualTo(1);
97    }
98
99    public function case_fast_forward()
100    {
101        $this
102            ->given($iterator = new SUT($this->getInnerIterator(), 3))
103            ->when($result = iterator_to_array($iterator))
104            ->then
105                ->array($result)
106                    ->isEqualTo(['a', 'b', 'c', 'd', 'e'])
107                ->array(iterator_to_array($this->invoke($iterator)->getBuffer()))
108                    ->isEqualTo([
109                        0 => [
110                            $iterator::BUFFER_KEY   => 3,
111                            $iterator::BUFFER_VALUE => 'd'
112                        ],
113                        1 => [
114                            $iterator::BUFFER_KEY   => 4,
115                            $iterator::BUFFER_VALUE => 'e'
116                        ],
117                        2 => [
118                            $iterator::BUFFER_KEY   => null,
119                            $iterator::BUFFER_VALUE => null
120                        ]
121                    ]);
122    }
123
124    public function case_fast_forward_with_too_big_buffer()
125    {
126        $this
127            ->given($iterator = new SUT($this->getInnerIterator(), 10))
128            ->when($result = iterator_to_array($iterator))
129            ->then
130                ->array($result)
131                    ->isEqualTo(['a', 'b', 'c', 'd', 'e'])
132                ->array(iterator_to_array($this->invoke($iterator)->getBuffer()))
133                    ->isEqualTo([
134                        0 => [
135                            $iterator::BUFFER_KEY   => 0,
136                            $iterator::BUFFER_VALUE => 'a'
137                        ],
138                        1 => [
139                            $iterator::BUFFER_KEY   => 1,
140                            $iterator::BUFFER_VALUE => 'b'
141                        ],
142                        2 => [
143                            $iterator::BUFFER_KEY   => 2,
144                            $iterator::BUFFER_VALUE => 'c'
145                        ],
146                        3 => [
147                            $iterator::BUFFER_KEY   => 3,
148                            $iterator::BUFFER_VALUE => 'd'
149                        ],
150                        4 => [
151                            $iterator::BUFFER_KEY   => 4,
152                            $iterator::BUFFER_VALUE => 'e'
153                        ],
154                        5 => [
155                            $iterator::BUFFER_KEY   => null,
156                            $iterator::BUFFER_VALUE => null
157                        ]
158                    ]);
159    }
160
161    public function case_fast_forward_with_smallest_buffer()
162    {
163        $this
164            ->given($iterator = new SUT($this->getInnerIterator(), 1))
165            ->when($result = iterator_to_array($iterator))
166            ->then
167                ->array($result)
168                    ->isEqualTo(['a', 'b', 'c', 'd', 'e'])
169                ->array(iterator_to_array($this->invoke($iterator)->getBuffer()))
170                    ->isEqualTo([
171                        0 => [
172                            $iterator::BUFFER_KEY   => null,
173                            $iterator::BUFFER_VALUE => null
174                        ]
175                    ]);
176    }
177
178    public function case_forward_forward_forward()
179    {
180        $this
181            ->when($result = new SUT(new LUT\Map(['a', 'b', 'c']), 2))
182            ->then
183                ->variable($result->rewind())
184                    ->isNull()
185                ->boolean($result->valid())
186                    ->isTrue()
187                ->integer($result->key())
188                    ->isEqualTo(0)
189                ->string($result->current())
190                    ->isEqualTo('a')
191                ->variable($result->next())
192                    ->isNull()
193
194                ->boolean($result->valid())
195                    ->isTrue()
196                ->integer($result->key())
197                    ->isEqualTo(1)
198                ->string($result->current())
199                    ->isEqualTo('b')
200                ->variable($result->next())
201                    ->isNull()
202
203                ->boolean($result->valid())
204                    ->isTrue()
205                ->integer($result->key())
206                    ->isEqualTo(2)
207                ->string($result->current())
208                    ->isEqualTo('c')
209                ->variable($result->next())
210                    ->isNull()
211
212                ->boolean($result->valid())
213                    ->isFalse()
214                ->variable($result->key())
215                    ->isNull()
216                ->variable($result->current())
217                    ->isNull();
218    }
219
220    public function case_forward_forward_backward_backward_forward_forward_forward_step_by_step()
221    {
222        $this
223            ->when($result = new SUT(new LUT\Map(['a', 'b', 'c']), 3))
224            ->then
225                ->variable($result->rewind())
226                    ->isNull()
227                ->array(iterator_to_array($this->invoke($result)->getBuffer()))
228                    ->isEqualTo([
229                        0 => [
230                            $result::BUFFER_KEY   => 0,
231                            $result::BUFFER_VALUE => 'a'
232                        ]
233                    ])
234
235                ->boolean($result->valid())
236                    ->isTrue()
237                ->integer($result->key())
238                    ->isEqualTo(0)
239                ->string($result->current())
240                    ->isEqualTo('a')
241                ->variable($result->next())
242                    ->isNull()
243                ->array(iterator_to_array($this->invoke($result)->getBuffer()))
244                    ->isEqualTo([
245                        0 => [
246                            $result::BUFFER_KEY   => 0,
247                            $result::BUFFER_VALUE => 'a'
248                        ],
249                        1 => [
250                            $result::BUFFER_KEY   => 1,
251                            $result::BUFFER_VALUE => 'b'
252                        ]
253                    ])
254
255                ->boolean($result->valid())
256                    ->isTrue()
257                ->integer($result->key())
258                    ->isEqualTo(1)
259                ->string($result->current())
260                    ->isEqualTo('b')
261                ->variable($result->next())
262                    ->isNull()
263                ->array(iterator_to_array($this->invoke($result)->getBuffer()))
264                    ->isEqualTo([
265                        0 => [
266                            $result::BUFFER_KEY   => 0,
267                            $result::BUFFER_VALUE => 'a'
268                        ],
269                        1 => [
270                            $result::BUFFER_KEY   => 1,
271                            $result::BUFFER_VALUE => 'b'
272                        ],
273                        2 => [
274                            $result::BUFFER_KEY   => 2,
275                            $result::BUFFER_VALUE => 'c'
276                        ]
277                    ])
278
279                ->boolean($result->valid())
280                    ->isTrue()
281                ->integer($result->key())
282                    ->isEqualTo(2)
283                ->string($result->current())
284                    ->isEqualTo('c')
285                ->variable($result->previous())
286                    ->isNull()
287                ->array(iterator_to_array($this->invoke($result)->getBuffer()))
288                    ->isEqualTo([
289                        0 => [
290                            $result::BUFFER_KEY   => 0,
291                            $result::BUFFER_VALUE => 'a'
292                        ],
293                        1 => [
294                            $result::BUFFER_KEY   => 1,
295                            $result::BUFFER_VALUE => 'b'
296                        ],
297                        2 => [
298                            $result::BUFFER_KEY   => 2,
299                            $result::BUFFER_VALUE => 'c'
300                        ]
301                    ])
302
303                ->boolean($result->valid())
304                    ->isTrue()
305                ->integer($result->key())
306                    ->isEqualTo(1)
307                ->string($result->current())
308                    ->isEqualTo('b')
309                ->variable($result->previous())
310                    ->isNull()
311                ->array(iterator_to_array($this->invoke($result)->getBuffer()))
312                    ->isEqualTo([
313                        0 => [
314                            $result::BUFFER_KEY   => 0,
315                            $result::BUFFER_VALUE => 'a'
316                        ],
317                        1 => [
318                            $result::BUFFER_KEY   => 1,
319                            $result::BUFFER_VALUE => 'b'
320                        ],
321                        2 => [
322                            $result::BUFFER_KEY   => 2,
323                            $result::BUFFER_VALUE => 'c'
324                        ]
325                    ])
326
327                ->boolean($result->valid())
328                    ->isTrue()
329                ->integer($result->key())
330                    ->isEqualTo(0)
331                ->string($result->current())
332                    ->isEqualTo('a')
333                ->variable($result->next())
334                    ->isNull()
335                ->array(iterator_to_array($this->invoke($result)->getBuffer()))
336                    ->isEqualTo([
337                        0 => [
338                            $result::BUFFER_KEY   => 0,
339                            $result::BUFFER_VALUE => 'a'
340                        ],
341                        1 => [
342                            $result::BUFFER_KEY   => 1,
343                            $result::BUFFER_VALUE => 'b'
344                        ],
345                        2 => [
346                            $result::BUFFER_KEY   => 2,
347                            $result::BUFFER_VALUE => 'c'
348                        ]
349                    ])
350
351                ->boolean($result->valid())
352                    ->isTrue()
353                ->integer($result->key())
354                    ->isEqualTo(1)
355                ->string($result->current())
356                    ->isEqualTo('b')
357                ->variable($result->next())
358                    ->isNull()
359                ->array(iterator_to_array($this->invoke($result)->getBuffer()))
360                    ->isEqualTo([
361                        0 => [
362                            $result::BUFFER_KEY   => 0,
363                            $result::BUFFER_VALUE => 'a'
364                        ],
365                        1 => [
366                            $result::BUFFER_KEY   => 1,
367                            $result::BUFFER_VALUE => 'b'
368                        ],
369                        2 => [
370                            $result::BUFFER_KEY   => 2,
371                            $result::BUFFER_VALUE => 'c'
372                        ]
373                    ])
374
375                ->boolean($result->valid())
376                    ->isTrue()
377                ->integer($result->key())
378                    ->isEqualTo(2)
379                ->string($result->current())
380                    ->isEqualTo('c')
381                ->variable($result->next())
382                    ->isNull()
383                ->array(iterator_to_array($this->invoke($result)->getBuffer()))
384                    ->isEqualTo([
385                        0 => [
386                            $result::BUFFER_KEY   => 1,
387                            $result::BUFFER_VALUE => 'b'
388                        ],
389                        1 => [
390                            $result::BUFFER_KEY   => 2,
391                            $result::BUFFER_VALUE => 'c'
392                        ],
393                        2 => [
394                            $result::BUFFER_KEY   => null,
395                            $result::BUFFER_VALUE => null
396                        ]
397                    ])
398
399                ->boolean($result->valid())
400                    ->isFalse()
401                ->variable($result->key())
402                    ->isNull()
403                ->variable($result->current())
404                    ->isNull();
405    }
406
407    public function case_backward_out_of_buffer()
408    {
409        $this
410            ->when($result = new SUT(new LUT\Map(['a', 'b', 'c']), 1))
411            ->then
412                ->variable($result->rewind())
413                    ->isNull()
414                ->boolean($result->valid())
415                    ->isTrue()
416                ->integer($result->key())
417                    ->isEqualTo(0)
418                ->string($result->current())
419                    ->isEqualTo('a')
420                ->variable($result->next())
421                    ->isNull()
422
423                ->boolean($result->valid())
424                    ->isTrue()
425                ->integer($result->key())
426                    ->isEqualTo(1)
427                ->string($result->current())
428                    ->isEqualTo('b')
429                ->variable($result->previous())
430                    ->isNull()
431
432                ->boolean($result->valid())
433                    ->isFalse();
434    }
435
436    public function case_rewind_rewind()
437    {
438        $this
439            ->when($result = new SUT(new LUT\Map(['a', 'b']), 3))
440            ->then
441                ->variable($result->rewind())
442                    ->isNull()
443                ->boolean($result->valid())
444                    ->isTrue()
445                ->integer($result->key())
446                    ->isEqualTo(0)
447                ->string($result->current())
448                    ->isEqualTo('a')
449                ->variable($result->next())
450                    ->isNull()
451
452                ->variable($result->rewind())
453                    ->isNull()
454                ->boolean($result->valid())
455                    ->isTrue()
456                ->integer($result->key())
457                    ->isEqualTo(0)
458                ->string($result->current())
459                    ->isEqualTo('a')
460                ->variable($result->next())
461                    ->isNull()
462
463                ->array(iterator_to_array($this->invoke($result)->getBuffer()))
464                    ->isEqualTo([
465                        0 => [
466                            $result::BUFFER_KEY   => 0,
467                            $result::BUFFER_VALUE => 'a'
468                        ],
469                        1 => [
470                            $result::BUFFER_KEY   => 1,
471                            $result::BUFFER_VALUE => 'b'
472                        ]
473                    ]);
474    }
475
476    protected function getInnerIterator()
477    {
478        return new LUT\Map(['a', 'b', 'c', 'd', 'e']);
479    }
480}
481