Files
TheAlgorithms-PHP/tests/DataStructures/TrieTest.php
Ramy eba642dbbe Implemented Segment Tree Data Structure (#166)
* Added Disjoint Sets Data structure

* Moved DisjointSetTest.php to  tests/DataStructures

* Update DataStructures/DisjointSets/DisjointSet.php

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>

* Update DataStructures/DisjointSets/DisjointSetNode.php

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>

* Update DataStructures/DisjointSets/DisjointSetNode.php

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>

* Update tests/DataStructures/DisjointSetTest.php

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>

* Update tests/DataStructures/DisjointSetTest.php

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>

* Update tests/DataStructures/DisjointSetTest.php

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>

* Considered PHPCS remarks. Unit Testing is now working.

* Remove data type mixed. Considered annotations for php7.4.

* Remove data type mixed. Considered annotations for php7.4.

* updating DIRECTORY.md

* Implemented Trie DataStructure

* Added Trie to DIRECTORY.md

* updating DIRECTORY.md

* Implemented AVLTree DataStructure

* updating DIRECTORY.md

* Implemented AVLTree DataStructure

* Implemented SegmentTreeNode.php

* Implementing SegmentTree

* Implementing SegmentTree with updateTree

* Implementing SegmentTree with rangeUpdateTree

* Implementing SegmentTree with query and queryTree

* Added serializing and deserializing of the SegmentTree

* Adding unit tests SegmentTree implementation

* Added unit tests for SegmentTree updates and range updates

* considering PHPCS for Added unit tests for SegmentTree updates and range updates

* Added unit tests for SegmentTree serialization/deserialization and array updates reflections

* Added unit tests for SegmentTree  Edge Cases

* Added unit tests for SegmentTree Exceptions (OutOfBoundsException, InvalidArgumentException)

* Added SegmentTree to DIRECTORY.md

* Implemented Segment Tree Data Structure

* Added some comments to my files in: #160, #162, #163, #166. Implemented Segment Tree Data Structure.

* Added some comments to my files in: #160, #162, #163, #166. Implemented Segment Tree Data Structure.

* Added comments time complexity for query(), update() and buildTree()

---------

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>
Co-authored-by: Ramy-Badr-Ahmed <Ramy-Badr-Ahmed@users.noreply.github.com>
2024-09-30 23:28:03 -06:00

146 lines
5.4 KiB
PHP

<?php
/*
* Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) in Pull Request: #162
* https://github.com/TheAlgorithms/PHP/pull/162
*
* Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request addressing bugs/corrections to this file.
* Thank you!
*/
namespace DataStructures;
require_once __DIR__ . '/../../DataStructures/Trie/Trie.php';
require_once __DIR__ . '/../../DataStructures/Trie/TrieNode.php';
use DataStructures\Trie\Trie;
use PHPUnit\Framework\TestCase;
class TrieTest extends TestCase
{
private Trie $trie;
protected function setUp(): void
{
$this->trie = new Trie();
}
public function testInsertAndSearch()
{
$this->trie->insert('the');
$this->trie->insert('universe');
$this->trie->insert('is');
$this->trie->insert('vast');
$this->assertTrue($this->trie->search('the'), 'Expected "the" to be found in the Trie.');
$this->assertTrue($this->trie->search('universe'), 'Expected "universe" to be found in the Trie.');
$this->assertTrue($this->trie->search('is'), 'Expected "is" to be found in the Trie.');
$this->assertTrue($this->trie->search('vast'), 'Expected "vast" to be found in the Trie.');
$this->assertFalse(
$this->trie->search('the universe'),
'Expected "the universe" not to be found in the Trie.'
);
}
public function testStartsWith()
{
$this->trie->insert('hello');
$this->assertEquals(['hello'], $this->trie->startsWith('he'), 'Expected words starting with "he" to be found.');
$this->assertEquals(
['hello'],
$this->trie->startsWith('hello'),
'Expected words starting with "hello" to be found.'
);
$this->assertEquals(
[],
$this->trie->startsWith('world'),
'Expected no words starting with "world" to be found.'
);
}
public function testDelete()
{
// Insert words into the Trie
$this->trie->insert('the');
$this->trie->insert('universe');
$this->trie->insert('is');
$this->trie->insert('vast');
$this->trie->insert('big');
$this->trie->insert('rather');
// Test deleting an existing word
$this->trie->delete('the');
$this->assertFalse($this->trie->search('the'), 'Expected "the" not to be found after deletion.');
// Test that other words are still present
$this->assertTrue($this->trie->search('universe'), 'Expected "universe" to be found.');
$this->assertTrue($this->trie->search('is'), 'Expected "is" to be found.');
$this->assertTrue($this->trie->search('vast'), 'Expected "vast" to be found.');
$this->assertTrue($this->trie->search('big'), 'Expected "big" to be found.');
$this->assertTrue($this->trie->search('rather'), 'Expected "rather" to be found.');
}
public function testDeleteNonExistentWord()
{
$this->trie->delete('nonexistent');
$this->assertFalse($this->trie->search('nonexistent'), 'Expected "nonexistent" to not be found.');
}
public function testTraverseTrieNode()
{
$this->trie->insert('hello');
$this->trie->insert('helium');
$this->trie->insert('helicopter');
$words = $this->trie->getWords();
$this->assertContains('hello', $words, 'Expected "hello" to be found in the Trie.');
$this->assertContains('helium', $words, 'Expected "helium" to be found in the Trie.');
$this->assertContains('helicopter', $words, 'Expected "helicopter" to be found in the Trie.');
$this->assertCount(3, $words, 'Expected 3 words in the Trie.');
}
public function testEmptyTrie()
{
$this->assertEquals([], $this->trie->getWords(), 'Expected an empty Trie to return an empty array.');
}
public function testGetWords()
{
$this->trie->insert('apple');
$this->trie->insert('app');
$this->trie->insert('applet');
$words = $this->trie->getWords();
$this->assertContains('apple', $words, 'Expected "apple" to be found in the Trie.');
$this->assertContains('app', $words, 'Expected "app" to be found in the Trie.');
$this->assertContains('applet', $words, 'Expected "applet" to be found in the Trie.');
$this->assertCount(3, $words, 'Expected 3 words in the Trie.');
}
public function testInsertEmptyString()
{
$this->trie->insert('');
$this->assertTrue($this->trie->search(''), 'Expected empty string to be found in the Trie.');
}
public function testDeleteEmptyString()
{
$this->trie->insert('');
$this->trie->delete('');
$this->assertFalse($this->trie->search(''), 'Expected empty string not to be found after deletion.');
}
public function testStartsWithWithCommonPrefix()
{
$this->trie->insert('trie');
$this->trie->insert('tried');
$this->trie->insert('trier');
$words = $this->trie->startsWith('tri');
$this->assertContains('trie', $words, 'Expected "trie" to be found with prefix "tri".');
$this->assertContains('tried', $words, 'Expected "tried" to be found with prefix "tri".');
$this->assertContains('trier', $words, 'Expected "trier" to be found with prefix "tri".');
$this->assertCount(3, $words, 'Expected 3 words with prefix "tri".');
}
}