1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the pinepain/php-object-maps PHP library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016-2017 Bogdan Padalko <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Licensed under the MIT license: http://opensource.org/licenses/MIT |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code or visit http://opensource.org/licenses/MIT |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
namespace Pinepain\ObjectMaps\Tests; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
use Pinepain\ObjectMaps\ObjectBiMap; |
19
|
|
|
use Pinepain\ObjectMaps\ObjectBiMapInterface; |
20
|
|
|
use Pinepain\ObjectMaps\ObjectMap; |
21
|
|
|
use Pinepain\ObjectMaps\ObjectMapInterface; |
22
|
|
|
use stdClass; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
class ObjectBiMapTest extends AbstractObjectMapInterfaceTest |
26
|
|
|
{ |
27
|
|
|
public function buildMap(int $behavior = ObjectMapInterface::DEFAULT) |
28
|
|
|
{ |
29
|
|
|
return new ObjectBiMap($behavior); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testValuesForRegularMap() |
33
|
|
|
{ |
34
|
|
|
$map = $this->buildMap(); |
35
|
|
|
|
36
|
|
|
$key = new stdClass(); |
37
|
|
|
$value = new stdClass(); |
38
|
|
|
|
39
|
|
|
$map->put($key, $value); |
40
|
|
|
|
41
|
|
|
$this->assertTrue($map->has($key)); |
42
|
|
|
$this->assertFalse($map->has($value)); |
43
|
|
|
|
44
|
|
|
$vmap = $map->values(); |
45
|
|
|
|
46
|
|
|
$this->assertInstanceOf(ObjectBiMapInterface::class, $vmap); |
47
|
|
|
|
48
|
|
|
$this->assertFalse($vmap->has($key)); |
49
|
|
|
$this->assertTrue($vmap->has($value)); |
50
|
|
|
|
51
|
|
|
$map->clear(); |
52
|
|
|
|
53
|
|
|
$this->assertSame(0, $map->count()); |
54
|
|
|
$this->assertSame(0, $vmap->count()); |
55
|
|
|
|
56
|
|
|
$vmap->put($key, $value); |
57
|
|
|
|
58
|
|
|
$this->assertSame(1, $vmap->count()); |
59
|
|
|
$this->assertSame(1, $map->count()); |
60
|
|
|
|
61
|
|
|
$this->assertFalse($map->has($key)); |
62
|
|
|
$this->assertTrue($map->has($value)); |
63
|
|
|
|
64
|
|
|
$key = null; |
65
|
|
|
$this->assertSame(1, $vmap->count()); |
66
|
|
|
$this->assertSame(1, $map->count()); |
67
|
|
|
|
68
|
|
|
$value = null; |
69
|
|
|
$this->assertSame(1, $vmap->count()); |
70
|
|
|
$this->assertSame(1, $map->count()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testValuesForWeakKeyMap() |
74
|
|
|
{ |
75
|
|
|
$map = $this->buildMap(ObjectBiMapInterface::WEAK_KEY); |
76
|
|
|
|
77
|
|
|
$key = new stdClass(); |
78
|
|
|
$value = new stdClass(); |
79
|
|
|
|
80
|
|
|
$map->put($key, $value); |
81
|
|
|
|
82
|
|
|
$this->assertSame(1, $map->count()); |
83
|
|
|
|
84
|
|
|
$vmap = $map->values(); |
85
|
|
|
|
86
|
|
|
$this->assertInstanceOf(ObjectBiMapInterface::class, $vmap); |
87
|
|
|
|
88
|
|
|
$this->assertFalse($vmap->has($key)); |
89
|
|
|
$this->assertTrue($vmap->has($value)); |
90
|
|
|
|
91
|
|
|
$this->assertSame(1, $map->count()); |
92
|
|
|
$this->assertSame(1, $vmap->count()); |
93
|
|
|
|
94
|
|
|
$value = null; |
95
|
|
|
$this->assertSame(1, $map->count()); |
96
|
|
|
$this->assertSame(1, $vmap->count()); |
97
|
|
|
|
98
|
|
|
$key = null; |
99
|
|
|
$this->assertSame(0, $map->count()); |
100
|
|
|
$this->assertSame(0, $vmap->count()); |
101
|
|
|
|
102
|
|
|
$key = new stdClass(); |
103
|
|
|
$value = new stdClass(); |
104
|
|
|
|
105
|
|
|
$vmap->put($key, $value); |
106
|
|
|
|
107
|
|
|
$this->assertSame(1, $map->count()); |
108
|
|
|
$this->assertSame(1, $vmap->count()); |
109
|
|
|
|
110
|
|
|
$key = null; |
111
|
|
|
$this->assertSame(1, $map->count()); |
112
|
|
|
$this->assertSame(1, $vmap->count()); |
113
|
|
|
|
114
|
|
|
$value = null; |
115
|
|
|
$this->assertSame(0, $map->count()); |
116
|
|
|
$this->assertSame(0, $vmap->count()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testValuesForWeakValueMap() |
120
|
|
|
{ |
121
|
|
|
$map = $this->buildMap(ObjectBiMapInterface::WEAK_VALUE); |
122
|
|
|
|
123
|
|
|
$key = new stdClass(); |
124
|
|
|
$value = new stdClass(); |
125
|
|
|
|
126
|
|
|
$map->put($key, $value); |
127
|
|
|
|
128
|
|
|
$this->assertSame(1, $map->count()); |
129
|
|
|
|
130
|
|
|
$vmap = $map->values(); |
131
|
|
|
|
132
|
|
|
$this->assertInstanceOf(ObjectBiMapInterface::class, $vmap); |
133
|
|
|
|
134
|
|
|
$this->assertSame(1, $map->count()); |
135
|
|
|
$this->assertSame(1, $vmap->count()); |
136
|
|
|
|
137
|
|
|
$key = null; |
138
|
|
|
$this->assertSame(1, $map->count()); |
139
|
|
|
$this->assertSame(1, $vmap->count()); |
140
|
|
|
|
141
|
|
|
$value = null; |
142
|
|
|
$this->assertSame(0, $map->count()); |
143
|
|
|
$this->assertSame(0, $vmap->count()); |
144
|
|
|
|
145
|
|
|
$key = new stdClass(); |
146
|
|
|
$value = new stdClass(); |
147
|
|
|
|
148
|
|
|
$vmap->put($key, $value); |
149
|
|
|
|
150
|
|
|
$this->assertSame(1, $map->count()); |
151
|
|
|
$this->assertSame(1, $vmap->count()); |
152
|
|
|
|
153
|
|
|
$value = null; |
154
|
|
|
$this->assertSame(1, $map->count()); |
155
|
|
|
$this->assertSame(1, $vmap->count()); |
156
|
|
|
|
157
|
|
|
$key = null; |
158
|
|
|
$this->assertSame(0, $map->count()); |
159
|
|
|
$this->assertSame(0, $vmap->count()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
public function testValuesForWeakKeyValueMap() |
164
|
|
|
{ |
165
|
|
|
$map = $this->buildMap(ObjectBiMapInterface::WEAK_KEY_VALUE); |
166
|
|
|
|
167
|
|
|
$key_1 = new stdClass(); |
168
|
|
|
$value_1 = new stdClass(); |
169
|
|
|
|
170
|
|
|
$key_2 = new stdClass(); |
171
|
|
|
$value_2 = new stdClass(); |
172
|
|
|
|
173
|
|
|
$map->put($key_1, $value_1); |
174
|
|
|
$map->put($key_2, $value_2); |
175
|
|
|
|
176
|
|
|
$this->assertSame(2, $map->count()); |
177
|
|
|
|
178
|
|
|
$vmap = $map->values(); |
179
|
|
|
|
180
|
|
|
$this->assertInstanceOf(ObjectBiMapInterface::class, $vmap); |
181
|
|
|
|
182
|
|
|
$this->assertSame(2, $map->count()); |
183
|
|
|
$this->assertSame(2, $vmap->count()); |
184
|
|
|
|
185
|
|
|
$key_1 = null; |
186
|
|
|
$this->assertSame(1, $map->count()); |
187
|
|
|
$this->assertSame(1, $vmap->count()); |
188
|
|
|
|
189
|
|
|
$value_2 = null; |
190
|
|
|
$this->assertSame(0, $map->count()); |
191
|
|
|
$this->assertSame(0, $vmap->count()); |
192
|
|
|
|
193
|
|
|
$key_1 = new stdClass(); |
194
|
|
|
$value_1 = new stdClass(); |
195
|
|
|
|
196
|
|
|
$key_2 = new stdClass(); |
197
|
|
|
$value_2 = new stdClass(); |
198
|
|
|
|
199
|
|
|
$vmap->put($key_1, $value_1); |
200
|
|
|
$vmap->put($key_2, $value_2); |
201
|
|
|
|
202
|
|
|
$this->assertSame(2, $map->count()); |
203
|
|
|
$this->assertSame(2, $vmap->count()); |
204
|
|
|
|
205
|
|
|
$key_1 = null; |
206
|
|
|
$this->assertSame(1, $map->count()); |
207
|
|
|
$this->assertSame(1, $vmap->count()); |
208
|
|
|
|
209
|
|
|
$value_2 = null; |
210
|
|
|
$this->assertSame(0, $map->count()); |
211
|
|
|
$this->assertSame(0, $vmap->count()); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @expectedException \Pinepain\ObjectMaps\Exceptions\OverflowException |
216
|
|
|
* @expectedExceptionMessage Key with such value already exists |
217
|
|
|
*/ |
218
|
|
|
public function testKeyValuesOutOfSyncPutFails() |
219
|
|
|
{ |
220
|
|
|
$key_1 = new stdClass(); |
221
|
|
|
$value_1 = new stdClass(); |
222
|
|
|
|
223
|
|
|
$key_2 = new stdClass(); |
224
|
|
|
$value_2 = new stdClass(); |
225
|
|
|
|
226
|
|
|
$keys_map = new ObjectMap(); |
227
|
|
|
$values_map = new ObjectMap(); |
228
|
|
|
|
229
|
|
|
$keys_map->put($key_1, $value_1); |
230
|
|
|
$values_map->put($value_2, $key_2); |
231
|
|
|
|
232
|
|
|
$map = new class($keys_map, $values_map) extends ObjectBiMap |
233
|
|
|
{ |
234
|
|
|
public function __construct(ObjectMapInterface $keys, ObjectMapInterface $values) |
235
|
|
|
{ |
236
|
|
|
$this->keys = $keys; |
237
|
|
|
$this->values = $values; |
238
|
|
|
} |
239
|
|
|
}; |
240
|
|
|
|
241
|
|
|
$map->put($key_2, $value_2); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @expectedException \Pinepain\ObjectMaps\Exceptions\OutOfBoundsException |
246
|
|
|
* @expectedExceptionMessage Key with such value not found |
247
|
|
|
*/ |
248
|
|
|
public function testKeyValuesOutOfSyncRemoveFails() |
249
|
|
|
{ |
250
|
|
|
$key_1 = new stdClass(); |
251
|
|
|
$value_1 = new stdClass(); |
252
|
|
|
|
253
|
|
|
$key_2 = new stdClass(); |
254
|
|
|
$value_2 = new stdClass(); |
255
|
|
|
|
256
|
|
|
$keys_map = new ObjectMap(); |
257
|
|
|
$values_map = new ObjectMap(); |
258
|
|
|
|
259
|
|
|
$keys_map->put($key_1, $value_1); |
260
|
|
|
$values_map->put($value_2, $key_2); |
261
|
|
|
|
262
|
|
|
$map = new class($keys_map, $values_map) extends ObjectBiMap |
263
|
|
|
{ |
264
|
|
|
public function __construct(ObjectMapInterface $keys, ObjectMapInterface $values) |
265
|
|
|
{ |
266
|
|
|
$this->keys = $keys; |
267
|
|
|
$this->values = $values; |
268
|
|
|
} |
269
|
|
|
}; |
270
|
|
|
|
271
|
|
|
$map->remove($key_1); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|