Skip to content

Commit

Permalink
fix tests, lru cache exception
Browse files Browse the repository at this point in the history
  • Loading branch information
doganoo committed Sep 20, 2019
1 parent 94ae374 commit cf1bb17
Show file tree
Hide file tree
Showing 29 changed files with 515 additions and 89 deletions.
26 changes: 26 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
root = true

[*]
ij_php_block_brace_style = end_of_line
ij_php_class_brace_style = end_of_line
ij_php_for_brace_force = always
ij_php_if_brace_force = if_multiline
ij_php_method_brace_style = end_of_line
ij_php_space_after_type_cast = true
ij_php_space_after_colon_in_return_type = true
ij_php_blank_lines_after_imports = 1
ij_php_blank_lines_before_imports = 1
ij_php_align_class_constants = true
ij_php_align_assignments = true
ij_php_align_inline_comments = true
ij_php_align_key_value_pairs = true
ij_php_align_group_field_declarations = true
ij_php_align_multiline_array_initializer_expression = true
ij_php_align_multiline_for = true
ij_php_align_phpdoc_comments = true
ij_php_align_phpdoc_param_names = true
ij_php_import_sorting = alphabetic
ij_php_blank_lines_after_class_header = 1
ij_php_blank_lines_before_class_end = 1
ij_php_else_if_style = separate
ij_any_else_on_new_line = false
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
],
"autoload": {
"psr-4": {
"doganoo\\PHPAlgorithms\\": "src/"
"doganoo\\PHPAlgorithms\\": "src/",
"doganoo\\PHPAlgorithmsTest\\": "tests/"
}
},
"require-dev": {
Expand Down
5 changes: 5 additions & 0 deletions src/Common/Abstracts/AbstractLinkedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
* TODO sentinels?
*
* @package doganoo\PHPAlgorithms\LinkedLists
*
* from: https://mobile.twitter.com/hillelogram/status/962424365819277312
*
* 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.
*
*/
abstract class AbstractLinkedList implements IComparable, JsonSerializable {
/** @var Node */
Expand Down
9 changes: 5 additions & 4 deletions src/Datastructure/Cache/LRUCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@

use doganoo\PHPAlgorithms\Common\Interfaces\ICache;
use doganoo\PHPAlgorithms\Common\Util\Comparator;
use doganoo\PHPAlgorithms\Datastructure\Maps\HashMap;
use doganoo\PHPAlgorithms\Datastructure\Table\HashTable;

/**
* Class LRUCache
*
* @package doganoo\PHPAlgorithms\Datastructure\Cache
*/
class LRUCache implements ICache {
/** @var HashMap|null $hashMap */
/** @var HashTable|null $hashMap */
private $hashMap = null;
/** @var Node $head */
private $head = null;
Expand All @@ -49,7 +49,7 @@ class LRUCache implements ICache {
* @param int $capacity
*/
public function __construct($capacity = 128) {
$this->hashMap = new HashMap();
$this->hashMap = new HashTable();
$this->capacity = $capacity;
}

Expand Down Expand Up @@ -162,9 +162,10 @@ public function get($key) {
/**
* returns the last accessed, non-deleted value
*
* @return mixed
* @return mixed|null
*/
public function last() {
if (null === $this->head) return null;
return $this->head->getKey();
}

Expand Down
38 changes: 37 additions & 1 deletion tests/Cache/LRUCacheTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
<?php
declare(strict_types=1);
/**
* MIT License
*
* Copyright (c) 2018 Dogan Ucar, <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace doganoo\PHPAlgorithmsTest\Cache;

use doganoo\PHPAlgorithms\common\Exception\InvalidKeyTypeException;
use doganoo\PHPAlgorithms\common\Exception\UnsupportedKeyTypeException;
use doganoo\PHPAlgorithms\Datastructure\Cache\LRUCache;
use PHPUnit\Framework\TestCase;

class LRUCacheTest extends \PHPUnit\Framework\TestCase {
class LRUCacheTest extends TestCase {

/**
* @throws InvalidKeyTypeException
* @throws UnsupportedKeyTypeException
*/
public function testCache() {
$cache = new LRUCache();
$cache->put("a", 1);
Expand All @@ -19,6 +51,10 @@ public function testCache() {
$this->assertTrue($last === "b");
}

/**
* @throws InvalidKeyTypeException
* @throws UnsupportedKeyTypeException
*/
public function testCacheWithSize() {
$cache = new LRUCache(2);
$cache->put("a", 1);
Expand Down
40 changes: 33 additions & 7 deletions tests/Comparator/ComparatorTest.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,62 @@
<?php
declare(strict_types=1);
/**
* MIT License
*
* Copyright (c) 2018 Dogan Ucar, <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace doganoo\PHPAlgorithmsTest\Comparator;

use doganoo\PHPAlgorithms\Common\Util\Comparator;
use doganoo\PHPAlgorithms\Datastructure\Graph\Graph\Node;
use doganoo\PHPAlgorithms\Datastructure\Lists\ArrayLists\ArrayList;
use PHPUnit\Framework\TestCase;

class ComparatorTest extends \PHPUnit\Framework\TestCase {
class ComparatorTest extends TestCase {

public function testComparator() {
$node = new Node(1);
$node = new Node(1);
$node2 = new Node(2);
$this->assertTrue(Comparator::equals($node, $node2) === false);


$node = new Node(1);
$node = new Node(1);
$node2 = new Node(1);
$this->assertTrue(Comparator::equals($node, $node2) === true);


$node = new Node(1);
$node = new Node(1);
$value = "test";
$this->assertTrue(Comparator::equals($node, $value) === false);

$node = "test";
$node = "test";
$value = "test";
$this->assertTrue(Comparator::equals($node, $value) === true);

$node = "1";
$node = "1";
$value = 1;
$this->assertTrue(Comparator::equals($node, $value) === true);

$node = new Node(1);
$node = new Node(1);
$value = new ArrayList();
$this->assertTrue(Comparator::equals($node, $value) === false);
}
Expand Down
33 changes: 30 additions & 3 deletions tests/Graph/graph/DirectedGraphTest.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,52 @@
<?php
declare(strict_types=1);
/**
* MIT License
*
* Copyright (c) 2018 Dogan Ucar, <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace doganoo\PHPAlgorithmsTest\Graph\graph;


use doganoo\PHPAlgorithms\Datastructure\Graph\Graph\DirectedGraph;
use doganoo\PHPAlgorithms\Datastructure\Graph\Graph\Node;
use PHPUnit\Framework\TestCase;

class DirectedGraphTest extends \PHPUnit\Framework\TestCase {
class DirectedGraphTest extends TestCase {

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

public function testSubGraphSize() {
$one = new Node(1);
$one = new Node(1);
$nine = new Node(9);
$five = new Node(5);

$one->addAdjacent($nine);
$one->addAdjacent($five);

$two = new Node(2);
$two = new Node(2);
$four = new Node(4);

$two->addAdjacent($four);
Expand Down
12 changes: 6 additions & 6 deletions tests/Graph/trees/AVLTree.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<?php

use doganoo\PHPAlgorithms\Algorithm\Traversal\PreOrder;
use doganoo\PHPAlgorithms\Datastructure\Graph\Tree\AVLTree as AVLTreee;
use PHPUnit\Framework\TestCase;

declare(strict_types=1);
/**
* MIT License
*
* Copyright (c) 2018 Dogan Ucar
* Copyright (c) 2018 Dogan Ucar, <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -28,6 +24,10 @@
* SOFTWARE.
*/

namespace doganoo\PHPAlgorithmsTest\Graph\trees;


use PHPUnit\Framework\TestCase;

class AVLTree extends TestCase {

Expand Down
28 changes: 18 additions & 10 deletions tests/Graph/trees/BinarySearchTreeTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php
declare(strict_types=1);
/**
* MIT License
*
* Copyright (c) 2018 Dogan Ucar
* Copyright (c) 2018 Dogan Ucar, <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,23 +24,27 @@
* SOFTWARE.
*/

namespace doganoo\PHPAlgorithmsTest\Graph\trees;

use doganoo\PHPAlgorithms\Algorithm\Traversal\InOrder;
use doganoo\PHPAlgorithms\Algorithm\Traversal\PostOrder;
use doganoo\PHPAlgorithms\Algorithm\Traversal\PreOrder;
use doganoo\PHPAlgorithms\Common\Interfaces\IComparable;
use doganoo\PHPAlgorithms\Datastructure\Graph\Tree\BinarySearchTree;
use doganoo\PHPAlgorithmsTest\Util\TreeUtil;
use PHPUnit\Framework\TestCase;

/**
* Class BinaryTreeTest
*/
class BinarySearchTreeTest extends \PHPUnit\Framework\TestCase {
class BinarySearchTreeTest extends TestCase {

/**
* tests addition and height
*/
public function testAdd() {
/** @var BinarySearchTree $bst */
$bst = \TreeUtil::getBinarySearchTree();
$bst = TreeUtil::getBinarySearchTree();
$node = $bst->search(1);
$this->assertTrue($node !== null);
$this->assertTrue($bst->height() === 3);
Expand All @@ -61,8 +66,8 @@ public function testMinimumHeight() {
* tests in order Traversal
*/
public function testInOrder() {
$bst = \TreeUtil::getBinarySearchTree();
$array = [];
$bst = TreeUtil::getBinarySearchTree();
$array = [];
$traversal = new InOrder($bst);
$traversal->setCallable(function ($value) use (&$array) {
$array[] = $value;
Expand All @@ -75,8 +80,8 @@ public function testInOrder() {
* tests pre order Traversal
*/
public function testPreOrder() {
$bst = \TreeUtil::getBinarySearchTree();
$array = [];
$bst = TreeUtil::getBinarySearchTree();
$array = [];
$traversal = new PreOrder($bst);
$traversal->setCallable(function ($value) use (&$array) {
$array[] = $value;
Expand All @@ -89,8 +94,8 @@ public function testPreOrder() {
* tests post order Traversal
*/
public function testPostOrder() {
$bst = \TreeUtil::getBinarySearchTree();
$array = [];
$bst = TreeUtil::getBinarySearchTree();
$array = [];
$traversal = new PostOrder($bst);
$traversal->setCallable(function ($value) use (&$array) {
$array[] = $value;
Expand All @@ -100,7 +105,7 @@ public function testPostOrder() {
}

public function testWithObjects() {
$tree = new BinarySearchTree();
$tree = new BinarySearchTree();
$upper = 10;
for ($i = 0; $i < $upper; $i++) {
$x = new TestNode($i);
Expand All @@ -113,9 +118,11 @@ public function testWithObjects() {
$this->assertTrue($node === null);

}

}

class TestNode implements IComparable {

private $id = 0;

public function __construct($id) {
Expand All @@ -138,6 +145,7 @@ public function compareTo($object): int {
public function getId(): int {
return $this->id;
}

}

;
Loading

0 comments on commit cf1bb17

Please sign in to comment.