1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Structure; |
4
|
|
|
|
5
|
|
|
use KDTree\Exceptions\InvalidDimensionsCount; |
6
|
|
|
use KDTree\Exceptions\PointAlreadyExists; |
7
|
|
|
use KDTree\Exceptions\PointNotFound; |
8
|
|
|
use KDTree\Structure\KDTree; |
9
|
|
|
use KDTree\ValueObject\Point; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
final class KDTreeTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function testEmptyConstruction(): void |
15
|
|
|
{ |
16
|
|
|
$dimensions = 2; |
17
|
|
|
$kdTree = new KDTree($dimensions); |
18
|
|
|
|
19
|
|
|
$this->assertEquals(0, $kdTree->size()); |
20
|
|
|
$this->assertTrue($kdTree->isEmpty()); |
21
|
|
|
$this->assertEquals($dimensions, $kdTree->getDimensions()); |
22
|
|
|
$this->assertNull($kdTree->getRoot()); |
23
|
|
|
$this->assertCount(0, $kdTree->points()); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testInvalidDimensionsCount(): void |
27
|
|
|
{ |
28
|
|
|
$this->expectException(InvalidDimensionsCount::class); |
29
|
|
|
new KDTree(0); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testPut(): void |
33
|
|
|
{ |
34
|
|
|
$point = new Point(1, 1); |
35
|
|
|
$kdTree = new KDTree(2); |
36
|
|
|
$kdTree->put($point); |
37
|
|
|
|
38
|
|
|
$this->assertTrue($kdTree->contains($point)); |
39
|
|
|
$this->assertEquals(1, $kdTree->size()); |
40
|
|
|
$this->assertEquals($point, $kdTree->points()->current()); |
41
|
|
|
$this->assertEquals($point, $kdTree->getRoot()->getPoint()); |
42
|
|
|
|
43
|
|
|
$this->expectException(PointAlreadyExists::class); |
44
|
|
|
$kdTree->put($point); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testDelete(): void |
48
|
|
|
{ |
49
|
|
|
$point = new Point(1, 1); |
50
|
|
|
$kdTree = new KDTree(2); |
51
|
|
|
$kdTree->put($point); |
52
|
|
|
$kdTree->delete($point); |
53
|
|
|
|
54
|
|
|
$this->assertFalse($kdTree->contains($point)); |
55
|
|
|
for ($i = 0; $i < 11; ++$i) { |
56
|
|
|
for ($j = 11; $j > 0; --$j) { |
57
|
|
|
$kdTree->put(new Point($i, $j)); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->assertEquals(121, $kdTree->points()->count()); |
62
|
|
|
|
63
|
|
|
for ($i = 0; $i < 11; ++$i) { |
64
|
|
|
for ($j = 11; $j > 0; --$j) { |
65
|
|
|
$kdTree->delete(new Point($i, $j)); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->assertEquals(1, $kdTree->points()->count()); |
70
|
|
|
$kdTree->put(new Point(1, 1)) |
71
|
|
|
->put(new Point(2, 2)) |
72
|
|
|
->put(new Point(3, 3)); |
73
|
|
|
|
74
|
|
|
$kdTree->delete(new Point(2, 2)); |
75
|
|
|
|
76
|
|
|
$this->assertFalse($kdTree->contains(new Point(2, 2))); |
77
|
|
|
|
78
|
|
|
$this->expectException(PointNotFound::class); |
79
|
|
|
$kdTree->delete(new Point(999, 999)); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|