FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ArrayOperationTest.php
1 <?php
2 /*
3 Copyright (C) 2014, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 namespace Fossology\Lib\Util;
20 
21 class ArrayOperationTest extends \PHPUnit\Framework\TestCase
22 {
23 
24  protected function setUp()
25  {
26  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
27  }
28 
29  protected function tearDown()
30  {
31  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
32  }
33 
34  public function testCallChunked()
35  {
36  $values = array(1, 2, 3);
37 
38  assertThat(ArrayOperation::callChunked(function ($values)
39  {
40  return array(count($values));
41  }, $values, 2), is(array(2, 1)));
42  }
43 
44  public function testCallChunkedWithNoValues()
45  {
46  $values = array();
47 
48  assertThat(ArrayOperation::callChunked(function ($values)
49  {
50  return array(count($values));
51  }, $values, 2), is(emptyArray()));
52  }
53 
54  public function testCallChunkedWithValueSizeSmallerThanChunkLimit()
55  {
56  $values = array(1, 2, 3);
57 
58  assertThat(ArrayOperation::callChunked(function ($values)
59  {
60  return array(count($values));
61  }, $values, 5), is(array(3)));
62  }
63 
64  public function testCallChunkedWithExactMultiple()
65  {
66  $values = array(1, 2, 3, 4);
67 
68  assertThat(ArrayOperation::callChunked(function ($values)
69  {
70  return array(count($values));
71  }, $values, 2), is(array(2, 2)));
72  }
73 
78  {
79  ArrayOperation::callChunked(function ($values)
80  {
81  return array(count($values));
82  }, array(), 0);
83  }
84 
85  public function testMultiSearch()
86  {
87  $haystack = array(100, 101, 102, 101);
88  assertThat(ArrayOperation::multiSearch(array(100),$haystack),is(0));
89  assertThat(ArrayOperation::multiSearch(array(101),$haystack),is(1));
90  assertThat(ArrayOperation::multiSearch(array(100,102),$haystack),is(0));
91  assertThat(ArrayOperation::multiSearch(array(200),$haystack),is(false));
92  assertThat(ArrayOperation::multiSearch(array(200,102),$haystack),is(2));
93  }
94 }