1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cubiche package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Cubiche |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace Cubiche\Core\Collections\Tests\Units\ArrayCollection; |
11
|
|
|
|
12
|
|
|
use Cubiche\Core\Collections\ArrayCollection\SortedArraySet; |
13
|
|
|
use Cubiche\Core\Comparable\Comparator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* SortedArraySetTests class. |
17
|
|
|
* |
18
|
|
|
* @method protected ArraySetInterface randomCollection($size = null) |
19
|
|
|
* |
20
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class SortedArraySetTests extends ArraySetTests |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
protected function emptyCollection() |
28
|
|
|
{ |
29
|
|
|
return new SortedArraySet([], $this->comparator()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/* |
33
|
|
|
* Test create. |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
public function testCreate() |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$this |
38
|
|
|
->given($comparator = new Comparator()) |
39
|
|
|
->and($collection = new SortedArraySet($this->randomValues(10))) |
40
|
|
|
->and($reverseComparator = $comparator->reverse()) |
41
|
|
|
->then() |
42
|
|
|
->set($collection) |
43
|
|
|
->isSortedUsing($comparator) |
44
|
|
|
->isNotSortedUsing($reverseComparator) |
45
|
|
|
; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Test remove. |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function testRemove() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$this |
54
|
|
|
->given( |
55
|
|
|
$unique = $this->uniqueValue(), |
56
|
|
|
$emptyCollection = $this->emptyCollection() |
57
|
|
|
) |
58
|
|
|
->and($comparator = $this->comparator()) |
59
|
|
|
->when($emptyCollection->add($unique)) |
60
|
|
|
->then() |
61
|
|
|
->set($emptyCollection) |
62
|
|
|
->contains($unique) |
63
|
|
|
->isSortedUsing($comparator) |
64
|
|
|
->and() |
65
|
|
|
->when($result = $emptyCollection->remove($unique)) |
66
|
|
|
->then() |
67
|
|
|
->set($emptyCollection) |
68
|
|
|
->notContains($unique) |
69
|
|
|
->isSortedUsing($comparator) |
70
|
|
|
->boolean($result) |
71
|
|
|
->isTrue() |
72
|
|
|
; |
73
|
|
|
|
74
|
|
|
$this |
75
|
|
|
->given( |
76
|
|
|
$unique = $this->uniqueValue(), |
77
|
|
|
$randomCollection = $this->randomCollection() |
78
|
|
|
) |
79
|
|
|
->and($comparator = $this->comparator()) |
80
|
|
|
->when($randomCollection->add($unique)) |
81
|
|
|
->then() |
82
|
|
|
->set($randomCollection) |
83
|
|
|
->contains($unique) |
84
|
|
|
->isSortedUsing($comparator) |
85
|
|
|
->and() |
86
|
|
|
->when($randomCollection->remove($unique)) |
87
|
|
|
->then() |
88
|
|
|
->set($randomCollection) |
89
|
|
|
->notContains($unique) |
90
|
|
|
->isSortedUsing($comparator) |
91
|
|
|
->and() |
92
|
|
|
->when($result = $randomCollection->remove('foo')) |
93
|
|
|
->then() |
94
|
|
|
->boolean($result) |
95
|
|
|
->isFalse() |
96
|
|
|
; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/* |
100
|
|
|
* Test removeAll. |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function testRemoveAll() |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$this |
105
|
|
|
->given( |
106
|
|
|
$unique = $this->uniqueValue(), |
107
|
|
|
$random = $this->randomValue(), |
108
|
|
|
$collection = $this->emptyCollection() |
109
|
|
|
) |
110
|
|
|
->and($comparator = $this->comparator()) |
111
|
|
|
->and($collection->addAll([$unique, $random])) |
112
|
|
|
->then() |
113
|
|
|
->boolean($collection->contains($unique)) |
114
|
|
|
->isTrue() |
115
|
|
|
->boolean($collection->contains($random)) |
116
|
|
|
->isTrue() |
117
|
|
|
->set($collection) |
118
|
|
|
->isSortedUsing($comparator) |
119
|
|
|
->and() |
120
|
|
|
->when($collection->removeAll([$unique, $random])) |
121
|
|
|
->then() |
122
|
|
|
->boolean($collection->isEmpty()) |
123
|
|
|
->isTrue() |
124
|
|
|
; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/* |
128
|
|
|
* Test sort. |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
public function testSort() |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
$this |
133
|
|
|
->given( |
134
|
|
|
$comparator = $this->comparator(), |
135
|
|
|
$reverseComparator = $comparator->reverse(), |
136
|
|
|
$collection = $this->randomCollection() |
137
|
|
|
) |
138
|
|
|
->when($collection->sort($reverseComparator)) |
139
|
|
|
->then() |
140
|
|
|
->set($collection) |
141
|
|
|
->isSortedUsing($reverseComparator) |
142
|
|
|
->and |
143
|
|
|
->when($collection->sort()) |
144
|
|
|
->then() |
145
|
|
|
->set($collection) |
146
|
|
|
->isSortedUsing($reverseComparator) |
147
|
|
|
->set($collection) |
148
|
|
|
->isNotSortedUsing($comparator) |
149
|
|
|
; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.