Completed
Push — master ( 5bcf33...3feb07 )
by Dan
27:10
created

QuadTreeTest::testSplit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 0
1
<?php
2
3
namespace SixtyNine\Cloud\Tests\Model;
4
5
use SixtyNine\Cloud\Model\Box;
6
use SixtyNine\Cloud\Model\QuadTree;
7
8
class QuadTreeTest extends \PHPUnit_Framework_TestCase
9
{
10
    public function testConstructor()
11
    {
12
        $bounds = new Box(0, 0, 800, 600);
13
        $tree = new QuadTree($bounds);
14
        $this->assertInstanceOf(QuadTree::class, $tree);
15
        $this->assertAttributeEquals($bounds, 'bounds', $tree);
16
        $this->assertAttributeEquals(0, 'level', $tree);
17
        $this->assertAttributeEquals(null, 'nodes', $tree);
18
        $this->assertEquals(0, $tree->count());
19
    }
20
21
    public function testSplit()
22
    {
23
        $bounds = new Box(0, 0, 800, 600);
24
        $tree = new QuadTree($bounds);
25
        $tree->split();
26
27
        $nodes = $this->readAttribute($tree, 'nodes');
28
        $this->assertInternalType('array', $nodes);
29
        $this->assertCount(4, $nodes);
30
31
        foreach ($nodes as $node) {
32
            $this->assertInstanceOf(QuadTree::class, $node);
33
            $this->assertAttributeEquals(null, 'nodes', $node);
34
            $nodeBounds = $this->readAttribute($node, 'bounds');
35
            $this->assertContains($nodeBounds->getX(), array(0, 400));
36
            $this->assertContains($nodeBounds->getY(), array(0, 300));
37
            $this->assertEquals(400, $nodeBounds->getWidth());
38
            $this->assertEquals(300, $nodeBounds->getHeight());
39
        }
40
    }
41
42
    /**
43
     * @dataProvider getIndexProvider
44
     */
45
    public function testGetIndex($expectedIndex, $box)
46
    {
47
        $bounds = new Box(0, 0, 800, 600);
48
        $tree = new QuadTree($bounds);
49
50
        $this->assertEquals($expectedIndex, $tree->getIndex($box));
51
    }
52
53
    public function getIndexProvider()
54
    {
55
        return array(
56
            array(-1, new Box(0, 0, 800, 600)),
57
            array(-1, (new Box(0, 0, 400, 300))->resize(1)),
58
            array(-1, new Box(300, 200, 200, 200)),
59
            array(0, new Box(0, 0, 400, 300)),
60
            array(0, (new Box(0, 0, 400, 300))->resize(-1)),
61
            array(1, new Box(400, 0, 400, 300)),
62
            array(1, new Box(750, 100, 50, 50)),
63
            array(2, new Box(0, 300, 400, 300)),
64
            array(2, new Box(250, 310, 50, 50)),
65
            array(3, new Box(400, 300, 400, 300)),
66
            array(3, new Box(700, 500, 50, 50)),
67
        );
68
    }
69
70
    public function testInsert()
71
    {
72
        $bounds = new Box(0, 0, 800, 600);
73
        $tree = new QuadTree($bounds);
74
        $tree->insert($bounds);
75
76
        $objects = $this->getObjectAttribute($tree, 'objects');
77
        $this->assertEquals(1, $tree->count());
78
        $this->assertCount(1, $objects);
79
        $this->assertEquals($bounds, $objects[0]);
80
81
        $box = new Box(10, 10, 10, 10);
82
        $tree->insert($box);
83
        $this->assertEquals(2, $tree->count());
84
        $objects = $this->getObjectAttribute($tree, 'objects');
85
        $this->assertCount(1, $objects);
86
        $this->assertAttributeNotEquals(null, 'nodes', $tree);
87
        $node = $this->getObjectAttribute($tree, 'nodes')[0];
88
        $this->assertEquals(1, $node->count());
89
    }
90
91
    public function testRetrieve()
92
    {
93
        $bounds = new Box(0, 0, 80, 80);
94
        $tree = new QuadTree($bounds);
95
96
        for ($i = 0; $i < 80; $i += 10) {
97
            for ($j = 0; $j < 80; $j += 10) {
98
                $tree->insert(new Box($i, $j, 10, 10));
99
            }
100
        }
101
102
        // Every box should be distributed on the 3rd level of the three
103
        foreach ($this->getObjectAttribute($tree, 'nodes') as $node1) {
104
            $this->assertEquals(16, $node1->count());
105
            foreach ($this->getObjectAttribute($node1, 'nodes') as $node2) {
106
                $this->assertEquals(4, $node2->count());
107
                foreach ($this->getObjectAttribute($node2, 'nodes') as $node3) {
108
                    $this->assertEquals(1, $node3->count());
109
                }
110
            }
111
        }
112
113
        $nodes = $tree->retrieve(new Box(5, 5, 10, 10));
114
        $this->assertCount(4, $nodes);
115
        $this->assertEquals(new Box(0, 0, 10, 10), $nodes[0]);
116
        $this->assertEquals(new Box(10, 0, 10, 10), $nodes[1]);
117
        $this->assertEquals(new Box(0, 10, 10, 10), $nodes[2]);
118
        $this->assertEquals(new Box(10, 10, 10, 10), $nodes[3]);
119
    }
120
121
    public function testCollides()
122
    {
123
        $bounds = new Box(0, 0, 80, 80);
124
        $tree = new QuadTree($bounds);
125
126
        for ($i = 0; $i < 80; $i += 10) {
127
            for ($j = 0; $j < 80; $j += 10) {
128
                $tree->insert(new Box($i, $j, 10, 10));
129
            }
130
        }
131
132
        $this->assertTrue($tree->collides(new Box(0, 0, 10, 10)));
133
        $this->assertTrue($tree->collides(new Box(5, 5, 10, 10)));
134
        $this->assertFalse($tree->collides(new Box(1000, 1000, 10, 10)));
135
    }
136
137
//    public function testRandom()
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
138
//    {
139
////        $this->markTestSkipped('Makes no sense in "real" tests');
140
//
141
//        $bounds = new Box(0, 0, 10000, 10000);
142
//        $tree = new QuadTree($bounds);
143
//
144
//        for ($i = 0; $i < 1000; $i++) {
145
//            $w = rand(0, 10);
146
//            $h = rand(0, 10);
147
//            $box = new Box(rand(0, 800 - $w), rand(0, 600 - $h), $w, $h);
148
//            $tree->insert($box);
149
//        }
150
//
151
//        $box = new Box(0, 0, 50, 50);
152
//
153
//        $list = $tree->retrieve($box);
154
//        echo (string)$box . PHP_EOL;
155
//
156
//        $noCollision = 0;
157
//        foreach ($list as $object) {
158
//            if ($box->intersects($object)) {
159
//                echo '  Collision: ' . $object . PHP_EOL;
160
//            } else {
161
//                $noCollision++;
162
//            }
163
//        }
164
//        echo '  Boxes count: ' . $tree->count() . PHP_EOL;
165
//        echo '  Total results: ' . count($list) . PHP_EOL;
166
//        echo '  Tests with no collision: ' . $noCollision . PHP_EOL;
167
//
168
////        echo (string)$tree;
169
//    }
170
}
171