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