14 use PHPUnit\Framework\TestCase;
24 private $numeratedArrayList;
32 public function setUp()
34 $this->numeratedArrayList =
new ArrayList([
51 $this->assertTrue($this->numeratedArrayList->current()->getAttr2() ===
"aa");
53 $this->assertFalse($arrayList->current());
58 $this->assertTrue($this->numeratedArrayList->next()->getAttr2() ===
"bb");
63 $this->numeratedArrayList->next();
64 $this->assertEquals($this->numeratedArrayList->prev()->getAttr2(),
"aa");
65 $this->assertFalse($this->numeratedArrayList->prev());
70 $i = $this->numeratedArrayList->count();
71 $this->numeratedArrayList->append(
new Element(
"3",
"33"));
72 $j = $this->numeratedArrayList->count();
73 $this->assertEquals($i + 1, $j);
74 $this->assertEquals(
"3", $this->numeratedArrayList->toArray()[$i]->getAttr1());
79 $this->hashMap->set(
"c",
new Element(
"ce"));
80 $this->assertEquals(
"ce", $this->hashMap->toArray()[
'c']->getAttr1());
85 $arr = $this->hashMap->toArray();
90 $this->assertEquals(
"a", $arr[0]->getAttr1());
91 $this->assertEquals(
"c", $arr[1]->getAttr1());
92 $this->assertEquals(
"h", $arr[2]->getAttr1());
97 $this->hashMap->replace($this->numeratedArrayList->toArray());
98 $keys = array_keys($this->hashMap->toArray());
99 foreach ($keys as $key) {
100 $this->assertInternalType(
"int", $key);
101 $this->assertNotEmpty($this->hashMap->get($key));
107 $this->assertTrue($this->hashMap->count() > 0);
108 $this->assertEquals(0, $this->hashMap->clear()->count());
113 $this->hashMap->setArray($this->numeratedArrayList->toArray());
114 $keys = array_keys($this->hashMap->toArray());
115 foreach ($keys as $key) {
116 $this->assertInternalType(
"int", $key);
117 $this->assertNotEmpty($this->hashMap->get($key));
123 $arr = $this->numeratedArrayList->toArray();
127 $this->numeratedArrayList->replace($arr);
128 for ($i = 0; $i < $this->numeratedArrayList->count() - 1; ++$i) {
129 $lte = ($this->numeratedArrayList->get($i)->getAttr1() <= $this->numeratedArrayList->get($i + 1)->getAttr1());
135 $this->assertTrue($lte);
136 $arr1 = $this->numeratedArrayList->toArray();
138 $this->numeratedArrayList->shuffle();
140 $arr2 = $this->numeratedArrayList->toArray();
143 for ($i = 0; $i <
count($arr); ++$i) {
144 $equal = ($arr1[$i]->getAttr1() == $arr2[$i]->getAttr1());
149 $this->assertFalse($equal);
155 $this->assertTrue($this->numeratedArrayList->hasKey(0));
156 $this->assertTrue($this->hashMap->hasKey(
"c"));
167 $this->assertTrue($list->hasValue(
"a"));
172 $it = $this->numeratedArrayList->getIterator();
174 foreach ($it as $key => $e) {
175 $this->assertTrue(is_int($key));
176 $this->assertInstanceOf(
"Seboettg\\Collection\\Test\\Element", $e);
189 $this->assertTrue($list->hasValue(
"d"));
191 $this->assertFalse($list->hasValue(
"a"));
196 $this->assertNotEmpty($this->numeratedArrayList[0]);
197 $this->assertEmpty($this->numeratedArrayList[333]);
202 $pos = $this->numeratedArrayList->count();
203 $this->numeratedArrayList[$pos] =
new Element($pos, $pos . $pos);
204 $arr = $this->numeratedArrayList->toArray();
205 $this->assertNotEmpty($arr[$pos]);
206 $this->assertEquals($pos, $arr[$pos]->getAttr1());
211 $this->assertTrue(isset($this->hashMap[
'a']));
212 $this->assertFalse(isset($this->numeratedArrayList[111]));
217 $list =
new ArrayList([
'a' =>
'aa',
'b' =>
'bb']);
219 $this->assertFalse($list->hasKey(
'a'));
220 $this->assertTrue($list->hasKey(
'b'));
225 $list =
new ArrayList([
'a' =>
'aa',
'b' =>
'bb',
'c' =>
'cc']);
226 $list->add(
'd',
'dd');
227 $this->assertEquals(
'dd', $list->get(
'd'));
228 $list->add(
'd',
'ddd');
230 $dl = $list->get(
'd');
231 $this->assertTrue(is_array($dl));
232 $this->assertEquals(
'dd', $dl[0]);
233 $this->assertEquals(
'ddd', $dl[1]);
239 $arrayList = $this->hashMap->filter(
function (
Element $elem) {
243 $this->assertTrue($arrayList->hasKey(
'c'));
244 $this->assertTrue($arrayList->hasKey(
'h'));
245 $this->assertFalse($arrayList->hasKey(
'a'));
246 $this->assertEquals($arrayList->get(
'c')->getAttr1(),
'c');
247 $this->assertEquals($arrayList->get(
'h')->getAttr1(),
'h');
252 $arrayList = $this->numeratedArrayList->filterByKeys([0, 3]);
253 $this->assertFalse($arrayList->hasKey(1));
254 $this->assertEquals($arrayList->count(), 2);
255 $this->assertEquals($arrayList->current()->getAttr1(),
"a");
256 $this->assertEquals($arrayList->next()->getAttr1(),
"k");
261 $cubic =
function($i) {
265 $cubicList = $list->map($cubic);
266 $this->assertEquals([1, 8, 27, 64, 125], $cubicList->toArray());
269 $toUpper = $list->map(
function($item) {
return ucfirst($item);});
270 $this->assertEquals([
'A',
'B',
'C'], $toUpper->toArray());
275 $list =
new ArrayList([[
'a',
'b'],
'c']);
276 $this->assertEquals([
'a',
'b',
'c'], $list->flatten()->toArray());
277 $list =
new ArrayList([
"term" => [
'a',
'b'],
'c']);
278 $this->assertEquals([
'a',
'b',
'c'], $list->flatten()->toArray());
291 $this->attr1 = $attr1;
292 $this->attr2 = $attr2;
308 $this->attr1 = $attr1;
324 $this->attr2 = $attr2;
338 return strcmp($this->attr1, $b->getAttr1());