GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — develop ( 7af216...ebf010 )
by Baptiste
13:17
created

DoubleIndexTest::testRemove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 26
nc 1
nop 0
dl 0
loc 37
rs 9.504
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Tests\Innmind\Immutable\Map;
5
6
use Innmind\Immutable\{
7
    Map\DoubleIndex,
0 ignored issues
show
Bug introduced by
The type Innmind\Immutable\Map\DoubleIndex was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
    Map\Implementation,
9
    Map,
10
    Pair,
11
    Str,
12
    Set,
13
    Sequence,
14
    Exception\LogicException,
15
    Exception\ElementNotFound,
16
    Exception\CannotGroupEmptyStructure,
17
};
18
use function Innmind\Immutable\unwrap;
19
use PHPUnit\Framework\TestCase;
20
21
class DoubleIndexTest extends TestCase
22
{
23
    public function testInterface()
24
    {
25
        $m = new DoubleIndex('int', 'float');
26
27
        $this->assertInstanceOf(Map\Implementation::class, $m);
28
        $this->assertInstanceOf(\Countable::class, $m);
29
        $this->assertSame('int', $m->keyType());
30
        $this->assertSame('float', $m->valueType());
31
    }
32
33
    public function testPut()
34
    {
35
        $m = new DoubleIndex('int', 'int');
36
37
        $this->assertSame(0, $m->size());
38
        $m2 = ($m)(42, 42);
39
        $this->assertNotSame($m, $m2);
40
        $this->assertSame(0, $m->size());
41
        $this->assertSame(1, $m2->size());
42
43
        $m = new DoubleIndex('int', 'int');
44
        $m = $m
45
            (23, 24)
46
            (41, 42)
47
            (65, 66)
48
            (89, 90)
49
            (65, 1);
50
51
        $this->assertSame(24, $m->get(23));
52
        $this->assertSame(42, $m->get(41));
53
        $this->assertSame(1, $m->get(65));
54
        $this->assertSame(90, $m->get(89));
55
        $this->assertSame(4, $m->size());
56
    }
57
58
    public function testThrowWhenKeyDoesntMatchType()
59
    {
60
        $this->expectException(\TypeError::class);
61
        $this->expectExceptionMessage('Argument 1 must be of type int, string given');
62
63
        $m = new DoubleIndex('int', 'int');
64
        ($m)('24', 42);
65
    }
66
67
    public function testThrowWhenValueDoesntMatchType()
68
    {
69
        $this->expectException(\TypeError::class);
70
        $this->expectExceptionMessage('Argument 2 must be of type int, float given');
71
72
        $m = new DoubleIndex('int', 'int');
73
        ($m)(24, 42.0);
74
    }
75
76
    public function testGet()
77
    {
78
        $m = new DoubleIndex('int', 'int');
79
        $m = ($m)(23, 24);
80
81
        $this->assertSame(24, $m->get(23));
82
    }
83
84
    public function testThrowWhenGettingUnknownKey()
85
    {
86
        $this->expectException(ElementNotFound::class);
87
88
        (new DoubleIndex('int', 'int'))->get(24);
89
    }
90
91
    public function testContains()
92
    {
93
        $m = new DoubleIndex('int', 'int');
94
        $m = ($m)(23, 24);
95
96
        $this->assertFalse($m->contains(24));
97
        $this->assertTrue($m->contains(23));
98
    }
99
100
    public function testClear()
101
    {
102
        $m = new DoubleIndex('int', 'float');
103
        $m = ($m)(24, 42.0);
104
105
        $m2 = $m->clear();
106
        $this->assertNotSame($m, $m2);
107
        $this->assertInstanceOf(DoubleIndex::class, $m2);
108
        $this->assertSame(1, $m->size());
109
        $this->assertSame(0, $m2->size());
110
        $this->assertSame('int', $m2->keyType());
111
        $this->assertSame('float', $m2->valueType());
112
    }
113
114
    public function testEquals()
115
    {
116
        $m = (new DoubleIndex('int', 'int'))(24, 42);
117
        $m2 = (new DoubleIndex('int', 'int'))(24, 42);
118
119
        $this->assertTrue($m->equals($m2));
120
        $this->assertFalse($m->equals(($m2)(65, 66)));
121
        $this->assertFalse($m->equals(($m2)(24, 24)));
122
        $this->assertFalse(
123
            (new DoubleIndex('string', 'string'))
124
                ('foo_res', 'res')
125
                ('foo_bar_res', 'res')
126
                ->equals(
127
                    (new DoubleIndex('string', 'string'))
128
                        ('foo_res', 'res')
129
                        ('bar_res', 'res')
130
                )
131
        );
132
133
        $m = (new DoubleIndex('int', 'int'))
134
            (24, 42)
135
            (42, 24);
136
        $m2 = (new DoubleIndex('int', 'int'))
137
            (42, 24)
138
            (24, 42);
139
140
        $this->assertTrue($m->equals($m2));
141
142
        $this->assertTrue((new DoubleIndex('int', 'int'))->equals(new DoubleIndex('int', 'int')));
143
    }
144
145
    public function testFilter()
146
    {
147
        $m = (new DoubleIndex('int', 'int'))
148
            (0, 1)
149
            (1, 2)
150
            (2, 3)
151
            (4, 5);
152
153
        $m2 = $m->filter(function(int $key, int $value) {
154
            return ($key + $value) % 3 === 0;
155
        });
156
157
        $this->assertNotSame($m, $m2);
158
        $this->assertInstanceOf(DoubleIndex::class, $m2);
159
        $this->assertSame($m->keyType(), $m2->keyType());
160
        $this->assertSame($m->valueType(), $m2->valueType());
161
        $this->assertSame(4, $m->size());
162
        $this->assertSame(2, $m2->size());
163
        $this->assertTrue($m2->contains(1));
164
        $this->assertTrue($m2->contains(4));
165
        $this->assertFalse($m2->contains(0));
166
        $this->assertFalse($m2->contains(2));
167
    }
168
169
    public function testForeach()
170
    {
171
        $m = (new DoubleIndex('int', 'int'))
172
            (0, 1)
173
            (1, 2)
174
            (2, 3)
175
            (3, 4);
176
        $count = 0;
177
178
        $m->foreach(function(int $key, int $value) use (&$count) {
179
            $this->assertSame($count, $key);
180
            $this->assertSame($value, $key + 1);
181
            ++$count;
182
        });
183
        $this->assertSame(4, $count);
184
    }
185
186
    public function testThrowWhenGroupingAnEmptyMap()
187
    {
188
        $this->expectException(CannotGroupEmptyStructure::class);
189
190
        (new DoubleIndex('int', 'int'))->groupBy(function() {});
191
    }
192
193
    public function testGroupBy()
194
    {
195
        $m = (new DoubleIndex('int', 'int'))
196
            (0, 1)
197
            (1, 2)
198
            (2, 3)
199
            (4, 5);
200
201
        $m2 = $m->groupBy(function(int $key, int $value) {
202
            return ($key + $value) % 3;
203
        });
204
        $this->assertNotSame($m, $m2);
205
        $this->assertInstanceOf(Map::class, $m2);
206
        $this->assertSame('int', $m2->keyType());
207
        $this->assertSame(Map::class, $m2->valueType());
208
        $this->assertTrue($m2->contains(0));
209
        $this->assertTrue($m2->contains(1));
210
        $this->assertTrue($m2->contains(2));
211
        $this->assertSame(2, $m2->get(0)->size());
212
        $this->assertSame(1, $m2->get(1)->size());
213
        $this->assertSame(1, $m2->get(2)->size());
214
        $this->assertSame('int', $m2->get(0)->keyType());
215
        $this->assertSame('int', $m2->get(0)->valueType());
216
        $this->assertSame('int', $m2->get(1)->keyType());
217
        $this->assertSame('int', $m2->get(1)->valueType());
218
        $this->assertSame('int', $m2->get(2)->keyType());
219
        $this->assertSame('int', $m2->get(2)->valueType());
220
        $this->assertSame(1, $m2->get(1)->get(0));
221
        $this->assertSame(2, $m2->get(0)->get(1));
222
        $this->assertSame(3, $m2->get(2)->get(2));
223
        $this->assertSame(5, $m2->get(0)->get(4));
224
    }
225
    public function testKeys()
226
    {
227
        $m = (new DoubleIndex('int', 'int'))
228
            (0, 1)
229
            (1, 2)
230
            (2, 3)
231
            (4, 5);
232
233
        $k = $m->keys();
234
        $this->assertInstanceOf(Set::class, $k);
235
        $this->assertSame('int', $k->type());
236
        $this->assertSame([0, 1, 2, 4], unwrap($k));
237
        $this->assertTrue($k->equals($m->keys()));
238
    }
239
240
    public function testValues()
241
    {
242
        $m = (new DoubleIndex('int', 'int'))
243
            (0, 1)
244
            (1, 2)
245
            (2, 3)
246
            (4, 5)
247
            (5, 5);
248
249
        $v = $m->values();
250
        $this->assertInstanceOf(Sequence::class, $v);
251
        $this->assertSame('int', $v->type());
252
        $this->assertSame([1, 2, 3, 5, 5], unwrap($v));
253
        $this->assertTrue($v->equals($m->values()));
254
    }
255
256
    public function testMap()
257
    {
258
        $m = (new DoubleIndex('int', 'int'))
259
            (0, 1)
260
            (1, 2)
261
            (2, 3)
262
            (4, 5);
263
264
        $m2 = $m->map(function(int $key, int $value) {
265
            if ($key % 2 === 0) {
266
                return new Pair($key + 10, $value);
267
            }
268
269
            return $value**2;
270
        });
271
        $this->assertNotSame($m, $m2);
272
        $this->assertInstanceOf(DoubleIndex::class, $m2);
273
        $this->assertSame($m->keyType(), $m2->keyType());
274
        $this->assertSame($m->valueType(), $m2->valueType());
275
        $this->assertSame([0, 1, 2, 4], unwrap($m->keys()));
276
        $this->assertSame([1, 2, 3, 5], unwrap($m->values()));
277
        $this->assertSame([10, 1, 12, 14], unwrap($m2->keys()));
278
        $this->assertSame([1, 4, 3, 5], unwrap($m2->values()));
279
    }
280
281
    public function testThrowWhenTryingToModifyValueTypeInTheMap()
282
    {
283
        $this->expectException(\TypeError::class);
284
        $this->expectExceptionMessage('Argument 2 must be of type int, string given');
285
286
        (new DoubleIndex('int', 'int'))
287
            (1, 2)
288
            ->map(function(int $key, int $value) {
289
                return (string) $value;
290
            });
291
    }
292
293
    public function testThrowWhenTryingToModifyKeyTypeInTheMap()
294
    {
295
        $this->expectException(\TypeError::class);
296
        $this->expectExceptionMessage('Argument 1 must be of type int, string given');
297
298
        (new DoubleIndex('int', 'int'))
299
            (1, 2)
300
            ->map(function(int $key, int $value) {
301
                return new Pair((string) $key, $value);
302
            });
303
    }
304
305
    public function testRemove()
306
    {
307
        $m = (new DoubleIndex('int', 'int'))
308
            (0, 1)
309
            (1, 2)
310
            (2, 3)
311
            (3, 4)
312
            (4, 5);
313
314
        $m2 = $m->remove(12);
315
        $this->assertSame($m, $m2);
316
        $this->assertSame([0, 1, 2, 3, 4], unwrap($m->keys()));
317
        $this->assertSame([1, 2, 3, 4, 5], unwrap($m->values()));
318
319
        $m2 = $m->remove(3);
320
        $this->assertNotSame($m, $m2);
321
        $this->assertInstanceOf(DoubleIndex::class, $m2);
322
        $this->assertSame([0, 1, 2, 3, 4], unwrap($m->keys()));
323
        $this->assertSame([1, 2, 3, 4, 5], unwrap($m->values()));
324
        $this->assertSame([0, 1, 2, 4], unwrap($m2->keys()));
325
        $this->assertSame([1, 2, 3, 5], unwrap($m2->values()));
326
327
        $m2 = $m->remove(4);
328
        $this->assertNotSame($m, $m2);
329
        $this->assertInstanceOf(DoubleIndex::class, $m2);
330
        $this->assertSame([0, 1, 2, 3, 4], unwrap($m->keys()));
331
        $this->assertSame([1, 2, 3, 4, 5], unwrap($m->values()));
332
        $this->assertSame([0, 1, 2, 3], unwrap($m2->keys()));
333
        $this->assertSame([1, 2, 3, 4], unwrap($m2->values()));
334
335
        $m2 = $m->remove(0);
336
        $this->assertNotSame($m, $m2);
337
        $this->assertInstanceOf(DoubleIndex::class, $m2);
338
        $this->assertSame([0, 1, 2, 3, 4], unwrap($m->keys()));
339
        $this->assertSame([1, 2, 3, 4, 5], unwrap($m->values()));
340
        $this->assertSame([1, 2, 3, 4], unwrap($m2->keys()));
341
        $this->assertSame([2, 3, 4, 5], unwrap($m2->values()));
342
    }
343
344
    public function testMerge()
345
    {
346
        $m = (new DoubleIndex(\stdClass::class, 'int'))
347
            ($s = new \stdClass, 24)
348
            ($s2 = new \stdClass, 42);
349
        $m2 = (new DoubleIndex(\stdClass::class, 'int'))
350
            ($s3 = new \stdClass, 24)
351
            ($s2, 66)
352
            ($s4 = new \stdClass, 42);
353
354
        $m3 = $m->merge($m2);
355
        $this->assertNotSame($m, $m3);
356
        $this->assertNotSame($m2, $m3);
357
        $this->assertInstanceOf(DoubleIndex::class, $m3);
358
        $this->assertSame($m->keyType(), $m3->keyType());
359
        $this->assertSame($m->valueType(), $m3->valueType());
360
        $this->assertSame(4, $m3->size());
361
        $this->assertSame([$s, $s2], unwrap($m->keys()));
362
        $this->assertSame([24, 42], unwrap($m->values()));
363
        $this->assertSame([$s3, $s2, $s4], unwrap($m2->keys()));
364
        $this->assertSame([24, 66, 42], unwrap($m2->values()));
365
        $this->assertSame([$s, $s2, $s3, $s4], unwrap($m3->keys()));
366
        $this->assertSame([24, 66, 24, 42], unwrap($m3->values()));
367
        $this->assertFalse($m3->equals($m2->merge($m)));
368
    }
369
370
    public function testPartition()
371
    {
372
        $m = (new DoubleIndex('int', 'int'))
373
            (0, 1)
374
            (1, 2)
375
            (2, 3)
376
            (3, 4)
377
            (4, 5);
378
379
        $p = $m->partition(function(int $i, int $v) {
380
            return ($i + $v) % 3 === 0;
381
        });
382
383
        $this->assertInstanceOf(Map::class, $p);
384
        $this->assertNotSame($p, $m);
385
        $this->assertSame('bool', $p->keyType());
386
        $this->assertSame(Map::class, $p->valueType());
387
        $this->assertSame(
388
            [true, false],
389
            unwrap($p->keys()),
390
        );
391
        $this->assertSame('int', $p->get(true)->keyType());
392
        $this->assertSame('int', $p->get(true)->valueType());
393
        $this->assertSame('int', $p->get(false)->keyType());
394
        $this->assertSame('int', $p->get(false)->valueType());
395
        $this->assertSame(
396
            [1, 4],
397
            unwrap($p->get(true)->keys()),
398
        );
399
        $this->assertSame(
400
            [2, 5],
401
            unwrap($p->get(true)->values()),
402
        );
403
        $this->assertSame(
404
            [0, 2, 3],
405
            unwrap($p->get(false)->keys()),
406
        );
407
        $this->assertSame(
408
            [1, 3, 4],
409
            unwrap($p->get(false)->values()),
410
        );
411
    }
412
413
    public function testReduce()
414
    {
415
        $m = (new DoubleIndex('int', 'int'))
416
            (4, 4);
417
418
        $v = $m->reduce(
419
            42,
420
            function (float $carry, int $key, int $value): float {
421
                return $carry / ($key * $value);
422
            }
423
        );
424
425
        $this->assertSame(2.625, $v);
426
        $this->assertSame([4], unwrap($m->keys()));
427
        $this->assertSame([4], unwrap($m->values()));
428
    }
429
430
    public function testEmpty()
431
    {
432
        $this->assertTrue((new DoubleIndex('int', 'int'))->empty());
433
        $this->assertFalse((new DoubleIndex('int', 'int'))(1, 2)->empty());
434
    }
435
436
    public function testToSequenceOf()
437
    {
438
        $map = (new DoubleIndex('int', 'int'))
439
            (1, 2)
440
            (3, 4);
441
        $sequence = $map->toSequenceOf('int', function($k, $v) {
442
            yield $k;
443
            yield $v;
444
        });
445
446
        $this->assertInstanceOf(Sequence::class, $sequence);
447
        $this->assertSame(
448
            [1, 2, 3, 4],
449
            unwrap($sequence),
450
        );
451
    }
452
453
    public function testToSetOf()
454
    {
455
        $map = (new DoubleIndex('int', 'int'))
456
            (1, 2)
457
            (3, 4);
458
        $set = $map->toSetOf('int', function($k, $v) {
459
            yield $k;
460
            yield $v;
461
        });
462
463
        $this->assertInstanceOf(Set::class, $set);
464
        $this->assertSame(
465
            [1, 2, 3, 4],
466
            unwrap($set),
467
        );
468
    }
469
470
    public function testToMapOf()
471
    {
472
        $map = (new DoubleIndex('int', 'int'))
473
            (1, 2)
474
            (3, 4);
475
        $map = $map->toMapOf('string', 'int', fn($i, $j) => yield (string) $j => $i);
476
477
        $this->assertInstanceOf(Map::class, $map);
478
        $this->assertCount(2, $map);
479
        $this->assertSame(1, $map->get('2'));
480
        $this->assertSame(3, $map->get('4'));
481
    }
482
}
483