1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ValueObject; |
4
|
|
|
|
5
|
|
|
use Generator; |
6
|
|
|
use KDTree\Exceptions\InvalidDimensionsCount; |
7
|
|
|
use KDTree\Exceptions\InvalidPointProvided; |
8
|
|
|
use KDTree\Exceptions\InvalidPointsCount; |
9
|
|
|
use KDTree\Exceptions\UnknownDimension; |
10
|
|
|
use KDTree\Structure\PointsList; |
11
|
|
|
use KDTree\ValueObject\Partition; |
12
|
|
|
use KDTree\ValueObject\Point; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
final class PartitionTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function testSuccessConstruction(): void |
18
|
|
|
{ |
19
|
|
|
$partition = $this->preparePartition(); |
20
|
|
|
|
21
|
|
|
$this->assertEquals(0, $partition->getDMin(0)); |
22
|
|
|
$this->assertEquals(1, $partition->getDMin(1)); |
23
|
|
|
$this->assertEquals(4, $partition->getDMax(0)); |
24
|
|
|
$this->assertEquals(4, $partition->getDMax(1)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return Partition |
29
|
|
|
* @throws InvalidPointsCount|InvalidDimensionsCount|InvalidPointProvided |
30
|
|
|
*/ |
31
|
|
|
private function preparePartition(): Partition |
32
|
|
|
{ |
33
|
|
|
$pointsList = new PointsList(2); |
34
|
|
|
$pointsList->addPoint(new Point(1, 2)) |
35
|
|
|
->addPoint(new Point(0, 3)) |
36
|
|
|
->addPoint(new Point(4, 1)) |
37
|
|
|
->addPoint(new Point(1, 4)); |
38
|
|
|
|
39
|
|
|
return new Partition($pointsList); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testFailConstruction(): void |
43
|
|
|
{ |
44
|
|
|
$pointsList = new PointsList(2); |
45
|
|
|
$pointsList->addPoint(new Point(1, 2)) |
46
|
|
|
->addPoint(new Point(2, 1)); |
47
|
|
|
$this->expectException(InvalidPointsCount::class); |
48
|
|
|
|
49
|
|
|
new Partition($pointsList); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param callable $fn |
54
|
|
|
* |
55
|
|
|
* @throws InvalidDimensionsCount|InvalidPointProvided|InvalidPointsCount |
56
|
|
|
* @dataProvider unknownDimensionProvider |
57
|
|
|
*/ |
58
|
|
|
public function testUnknownDimension(callable $fn): void |
59
|
|
|
{ |
60
|
|
|
$partition = $this->preparePartition(); |
61
|
|
|
|
62
|
|
|
$this->expectException(UnknownDimension::class); |
63
|
|
|
$fn($partition); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testIntersects(): void |
67
|
|
|
{ |
68
|
|
|
$partition = $this->preparePartition(); |
69
|
|
|
$pointsList = new PointsList(2); |
70
|
|
|
$pointsList->addPoint(new Point(0, 0)) |
71
|
|
|
->addPoint(new Point(0, 1)) |
72
|
|
|
->addPoint(new Point(1, 0)) |
73
|
|
|
->addPoint(new Point(1, 1)); |
74
|
|
|
$partition2 = new Partition($pointsList); |
75
|
|
|
|
76
|
|
|
$this->assertTrue($partition->intersects($partition2)); |
77
|
|
|
|
78
|
|
|
$pointsList = new PointsList(2); |
79
|
|
|
$pointsList->addPoint(new Point(66, 77)) |
80
|
|
|
->addPoint(new Point(77, 66)) |
81
|
|
|
->addPoint(new Point(55, 66)) |
82
|
|
|
->addPoint(new Point(44, 88)); |
83
|
|
|
$partition3 = new Partition($pointsList); |
84
|
|
|
|
85
|
|
|
$this->assertFalse($partition->intersects($partition3)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testContains(): void |
89
|
|
|
{ |
90
|
|
|
$partition = $this->preparePartition(); |
91
|
|
|
|
92
|
|
|
$this->assertTrue($partition->contains(new Point(1, 1))); |
93
|
|
|
$this->assertFalse($partition->contains(new Point(666, 666))); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testDistanceToPoint(): void |
97
|
|
|
{ |
98
|
|
|
$partition = $this->preparePartition(); |
99
|
|
|
|
100
|
|
|
$this->assertEquals(87.681240867132, $partition->distanceToPoint(new Point(66, 66))); |
101
|
|
|
$this->assertEquals(140.71602609511, $partition->distanceToPoint(new Point(-99, -99))); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testEquals(): void |
105
|
|
|
{ |
106
|
|
|
$partition = $this->preparePartition(); |
107
|
|
|
$pointsList = new PointsList(1); |
108
|
|
|
$pointsList->addPoint(new Point(1)) |
109
|
|
|
->addPoint(new Point(0)); |
110
|
|
|
$oneDPartition = new Partition($pointsList); |
111
|
|
|
|
112
|
|
|
$this->assertFalse($partition->equals($oneDPartition)); |
113
|
|
|
|
114
|
|
|
$pointsList = new PointsList(2); |
115
|
|
|
$pointsList->addPoint(new Point(1, 1)) |
116
|
|
|
->addPoint(new Point(0, 0)) |
117
|
|
|
->addPoint(new Point(0, 1)) |
118
|
|
|
->addPoint(new Point(1, 0)); |
119
|
|
|
$partition2 = new Partition($pointsList); |
120
|
|
|
|
121
|
|
|
$this->assertFalse($partition->equals($partition2)); |
122
|
|
|
$this->assertTrue($partition->equals($partition)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return Generator |
127
|
|
|
*/ |
128
|
|
|
public function unknownDimensionProvider(): Generator |
129
|
|
|
{ |
130
|
|
|
yield 'min' => [ |
131
|
|
|
'fn' => static function (Partition $partition) { |
132
|
|
|
$partition->getDMin(5); |
133
|
|
|
} |
134
|
|
|
]; |
135
|
|
|
|
136
|
|
|
yield 'max' => [ |
137
|
|
|
'fn' => static function (Partition $partition) { |
138
|
|
|
$partition->getDMax(5); |
139
|
|
|
} |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|