Skip to content

Commit cf1bb17

Browse files
committed
fix tests, lru cache exception
1 parent 94ae374 commit cf1bb17

29 files changed

+515
-89
lines changed

.editorconfig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
root = true
2+
3+
[*]
4+
ij_php_block_brace_style = end_of_line
5+
ij_php_class_brace_style = end_of_line
6+
ij_php_for_brace_force = always
7+
ij_php_if_brace_force = if_multiline
8+
ij_php_method_brace_style = end_of_line
9+
ij_php_space_after_type_cast = true
10+
ij_php_space_after_colon_in_return_type = true
11+
ij_php_blank_lines_after_imports = 1
12+
ij_php_blank_lines_before_imports = 1
13+
ij_php_align_class_constants = true
14+
ij_php_align_assignments = true
15+
ij_php_align_inline_comments = true
16+
ij_php_align_key_value_pairs = true
17+
ij_php_align_group_field_declarations = true
18+
ij_php_align_multiline_array_initializer_expression = true
19+
ij_php_align_multiline_for = true
20+
ij_php_align_phpdoc_comments = true
21+
ij_php_align_phpdoc_param_names = true
22+
ij_php_import_sorting = alphabetic
23+
ij_php_blank_lines_after_class_header = 1
24+
ij_php_blank_lines_before_class_end = 1
25+
ij_php_else_if_style = separate
26+
ij_any_else_on_new_line = false

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
],
1818
"autoload": {
1919
"psr-4": {
20-
"doganoo\\PHPAlgorithms\\": "src/"
20+
"doganoo\\PHPAlgorithms\\": "src/",
21+
"doganoo\\PHPAlgorithmsTest\\": "tests/"
2122
}
2223
},
2324
"require-dev": {

src/Common/Abstracts/AbstractLinkedList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
* TODO sentinels?
4040
*
4141
* @package doganoo\PHPAlgorithms\LinkedLists
42+
*
43+
* from: https://mobile.twitter.com/hillelogram/status/962424365819277312
44+
*
45+
* Linked lists are a necessary data structure, since they give you dynamic memory allocation with less danger of buffer overruns. Which means you had to write linked lists by hand. Which means you had to manipulate the pointers in linked lists by hand.
46+
*
4247
*/
4348
abstract class AbstractLinkedList implements IComparable, JsonSerializable {
4449
/** @var Node */

src/Datastructure/Cache/LRUCache.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828

2929
use doganoo\PHPAlgorithms\Common\Interfaces\ICache;
3030
use doganoo\PHPAlgorithms\Common\Util\Comparator;
31-
use doganoo\PHPAlgorithms\Datastructure\Maps\HashMap;
31+
use doganoo\PHPAlgorithms\Datastructure\Table\HashTable;
3232

3333
/**
3434
* Class LRUCache
3535
*
3636
* @package doganoo\PHPAlgorithms\Datastructure\Cache
3737
*/
3838
class LRUCache implements ICache {
39-
/** @var HashMap|null $hashMap */
39+
/** @var HashTable|null $hashMap */
4040
private $hashMap = null;
4141
/** @var Node $head */
4242
private $head = null;
@@ -49,7 +49,7 @@ class LRUCache implements ICache {
4949
* @param int $capacity
5050
*/
5151
public function __construct($capacity = 128) {
52-
$this->hashMap = new HashMap();
52+
$this->hashMap = new HashTable();
5353
$this->capacity = $capacity;
5454
}
5555

@@ -162,9 +162,10 @@ public function get($key) {
162162
/**
163163
* returns the last accessed, non-deleted value
164164
*
165-
* @return mixed
165+
* @return mixed|null
166166
*/
167167
public function last() {
168+
if (null === $this->head) return null;
168169
return $this->head->getKey();
169170
}
170171

tests/Cache/LRUCacheTest.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
11
<?php
2+
declare(strict_types=1);
3+
/**
4+
* MIT License
5+
*
6+
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
226

27+
namespace doganoo\PHPAlgorithmsTest\Cache;
328

29+
use doganoo\PHPAlgorithms\common\Exception\InvalidKeyTypeException;
30+
use doganoo\PHPAlgorithms\common\Exception\UnsupportedKeyTypeException;
431
use doganoo\PHPAlgorithms\Datastructure\Cache\LRUCache;
32+
use PHPUnit\Framework\TestCase;
533

6-
class LRUCacheTest extends \PHPUnit\Framework\TestCase {
34+
class LRUCacheTest extends TestCase {
735

36+
/**
37+
* @throws InvalidKeyTypeException
38+
* @throws UnsupportedKeyTypeException
39+
*/
840
public function testCache() {
941
$cache = new LRUCache();
1042
$cache->put("a", 1);
@@ -19,6 +51,10 @@ public function testCache() {
1951
$this->assertTrue($last === "b");
2052
}
2153

54+
/**
55+
* @throws InvalidKeyTypeException
56+
* @throws UnsupportedKeyTypeException
57+
*/
2258
public function testCacheWithSize() {
2359
$cache = new LRUCache(2);
2460
$cache->put("a", 1);

tests/Comparator/ComparatorTest.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,62 @@
11
<?php
2+
declare(strict_types=1);
3+
/**
4+
* MIT License
5+
*
6+
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
226

27+
namespace doganoo\PHPAlgorithmsTest\Comparator;
328

429
use doganoo\PHPAlgorithms\Common\Util\Comparator;
530
use doganoo\PHPAlgorithms\Datastructure\Graph\Graph\Node;
631
use doganoo\PHPAlgorithms\Datastructure\Lists\ArrayLists\ArrayList;
32+
use PHPUnit\Framework\TestCase;
733

8-
class ComparatorTest extends \PHPUnit\Framework\TestCase {
34+
class ComparatorTest extends TestCase {
935

1036
public function testComparator() {
11-
$node = new Node(1);
37+
$node = new Node(1);
1238
$node2 = new Node(2);
1339
$this->assertTrue(Comparator::equals($node, $node2) === false);
1440

1541

16-
$node = new Node(1);
42+
$node = new Node(1);
1743
$node2 = new Node(1);
1844
$this->assertTrue(Comparator::equals($node, $node2) === true);
1945

2046

21-
$node = new Node(1);
47+
$node = new Node(1);
2248
$value = "test";
2349
$this->assertTrue(Comparator::equals($node, $value) === false);
2450

25-
$node = "test";
51+
$node = "test";
2652
$value = "test";
2753
$this->assertTrue(Comparator::equals($node, $value) === true);
2854

29-
$node = "1";
55+
$node = "1";
3056
$value = 1;
3157
$this->assertTrue(Comparator::equals($node, $value) === true);
3258

33-
$node = new Node(1);
59+
$node = new Node(1);
3460
$value = new ArrayList();
3561
$this->assertTrue(Comparator::equals($node, $value) === false);
3662
}

tests/Graph/graph/DirectedGraphTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,52 @@
11
<?php
2+
declare(strict_types=1);
3+
/**
4+
* MIT License
5+
*
6+
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
27+
namespace doganoo\PHPAlgorithmsTest\Graph\graph;
228

329

430
use doganoo\PHPAlgorithms\Datastructure\Graph\Graph\DirectedGraph;
531
use doganoo\PHPAlgorithms\Datastructure\Graph\Graph\Node;
32+
use PHPUnit\Framework\TestCase;
633

7-
class DirectedGraphTest extends \PHPUnit\Framework\TestCase {
34+
class DirectedGraphTest extends TestCase {
835

936
public function testAdd() {
1037
$graph = new DirectedGraph();
1138
$this->assertTrue($graph->addNode(new Node(1)) === true);
1239
}
1340

1441
public function testSubGraphSize() {
15-
$one = new Node(1);
42+
$one = new Node(1);
1643
$nine = new Node(9);
1744
$five = new Node(5);
1845

1946
$one->addAdjacent($nine);
2047
$one->addAdjacent($five);
2148

22-
$two = new Node(2);
49+
$two = new Node(2);
2350
$four = new Node(4);
2451

2552
$two->addAdjacent($four);

tests/Graph/trees/AVLTree.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
<?php
2-
3-
use doganoo\PHPAlgorithms\Algorithm\Traversal\PreOrder;
4-
use doganoo\PHPAlgorithms\Datastructure\Graph\Tree\AVLTree as AVLTreee;
5-
use PHPUnit\Framework\TestCase;
6-
2+
declare(strict_types=1);
73
/**
84
* MIT License
95
*
10-
* Copyright (c) 2018 Dogan Ucar
6+
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
117
*
128
* Permission is hereby granted, free of charge, to any person obtaining a copy
139
* of this software and associated documentation files (the "Software"), to deal
@@ -28,6 +24,10 @@
2824
* SOFTWARE.
2925
*/
3026

27+
namespace doganoo\PHPAlgorithmsTest\Graph\trees;
28+
29+
30+
use PHPUnit\Framework\TestCase;
3131

3232
class AVLTree extends TestCase {
3333

tests/Graph/trees/BinarySearchTreeTest.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* MIT License
45
*
5-
* Copyright (c) 2018 Dogan Ucar
6+
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
67
*
78
* Permission is hereby granted, free of charge, to any person obtaining a copy
89
* of this software and associated documentation files (the "Software"), to deal
@@ -23,23 +24,27 @@
2324
* SOFTWARE.
2425
*/
2526

27+
namespace doganoo\PHPAlgorithmsTest\Graph\trees;
28+
2629
use doganoo\PHPAlgorithms\Algorithm\Traversal\InOrder;
2730
use doganoo\PHPAlgorithms\Algorithm\Traversal\PostOrder;
2831
use doganoo\PHPAlgorithms\Algorithm\Traversal\PreOrder;
2932
use doganoo\PHPAlgorithms\Common\Interfaces\IComparable;
3033
use doganoo\PHPAlgorithms\Datastructure\Graph\Tree\BinarySearchTree;
34+
use doganoo\PHPAlgorithmsTest\Util\TreeUtil;
35+
use PHPUnit\Framework\TestCase;
3136

3237
/**
3338
* Class BinaryTreeTest
3439
*/
35-
class BinarySearchTreeTest extends \PHPUnit\Framework\TestCase {
40+
class BinarySearchTreeTest extends TestCase {
3641

3742
/**
3843
* tests addition and height
3944
*/
4045
public function testAdd() {
4146
/** @var BinarySearchTree $bst */
42-
$bst = \TreeUtil::getBinarySearchTree();
47+
$bst = TreeUtil::getBinarySearchTree();
4348
$node = $bst->search(1);
4449
$this->assertTrue($node !== null);
4550
$this->assertTrue($bst->height() === 3);
@@ -61,8 +66,8 @@ public function testMinimumHeight() {
6166
* tests in order Traversal
6267
*/
6368
public function testInOrder() {
64-
$bst = \TreeUtil::getBinarySearchTree();
65-
$array = [];
69+
$bst = TreeUtil::getBinarySearchTree();
70+
$array = [];
6671
$traversal = new InOrder($bst);
6772
$traversal->setCallable(function ($value) use (&$array) {
6873
$array[] = $value;
@@ -75,8 +80,8 @@ public function testInOrder() {
7580
* tests pre order Traversal
7681
*/
7782
public function testPreOrder() {
78-
$bst = \TreeUtil::getBinarySearchTree();
79-
$array = [];
83+
$bst = TreeUtil::getBinarySearchTree();
84+
$array = [];
8085
$traversal = new PreOrder($bst);
8186
$traversal->setCallable(function ($value) use (&$array) {
8287
$array[] = $value;
@@ -89,8 +94,8 @@ public function testPreOrder() {
8994
* tests post order Traversal
9095
*/
9196
public function testPostOrder() {
92-
$bst = \TreeUtil::getBinarySearchTree();
93-
$array = [];
97+
$bst = TreeUtil::getBinarySearchTree();
98+
$array = [];
9499
$traversal = new PostOrder($bst);
95100
$traversal->setCallable(function ($value) use (&$array) {
96101
$array[] = $value;
@@ -100,7 +105,7 @@ public function testPostOrder() {
100105
}
101106

102107
public function testWithObjects() {
103-
$tree = new BinarySearchTree();
108+
$tree = new BinarySearchTree();
104109
$upper = 10;
105110
for ($i = 0; $i < $upper; $i++) {
106111
$x = new TestNode($i);
@@ -113,9 +118,11 @@ public function testWithObjects() {
113118
$this->assertTrue($node === null);
114119

115120
}
121+
116122
}
117123

118124
class TestNode implements IComparable {
125+
119126
private $id = 0;
120127

121128
public function __construct($id) {
@@ -138,6 +145,7 @@ public function compareTo($object): int {
138145
public function getId(): int {
139146
return $this->id;
140147
}
148+
141149
}
142150

143151
;

0 commit comments

Comments
 (0)