Skip to content

Commit 4821a8a

Browse files
committed
update listdata and dictionary
1 parent 1f3ad86 commit 4821a8a

File tree

6 files changed

+396
-21
lines changed

6 files changed

+396
-21
lines changed

Tests/DictionaryTest.php

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
use Nejcc\PhpDatatypes\Composite\Dictionary;
4+
use PHPUnit\Framework\TestCase;
5+
6+
class DictionaryTest extends TestCase
7+
{
8+
public function testCanInitializeWithElements()
9+
{
10+
$dictionary = new Dictionary([
11+
'name' => 'John Doe',
12+
'age' => 30,
13+
]);
14+
15+
$this->assertEquals('John Doe', $dictionary->get('name'));
16+
$this->assertEquals(30, $dictionary->get('age'));
17+
}
18+
19+
public function testAddAndGetElements()
20+
{
21+
$dictionary = new Dictionary();
22+
$dictionary->add('name', 'Alice');
23+
$dictionary->add('age', 25);
24+
25+
$this->assertEquals('Alice', $dictionary->get('name'));
26+
$this->assertEquals(25, $dictionary->get('age'));
27+
}
28+
29+
public function testContainsKey()
30+
{
31+
$dictionary = new Dictionary([
32+
'name' => 'Bob',
33+
]);
34+
35+
$this->assertTrue($dictionary->containsKey('name'));
36+
$this->assertFalse($dictionary->containsKey('age'));
37+
}
38+
39+
public function testRemoveElement()
40+
{
41+
$dictionary = new Dictionary([
42+
'name' => 'Charlie',
43+
'age' => 40,
44+
]);
45+
46+
$dictionary->remove('age');
47+
$this->assertFalse($dictionary->containsKey('age'));
48+
$this->assertEquals('Charlie', $dictionary->get('name'));
49+
50+
$this->expectException(OutOfBoundsException::class);
51+
$dictionary->get('age'); // This should throw an exception as 'age' was removed
52+
}
53+
54+
public function testGetKeysAndValues()
55+
{
56+
$dictionary = new Dictionary([
57+
'name' => 'David',
58+
'age' => 35,
59+
'country' => 'UK',
60+
]);
61+
62+
$this->assertEquals(['name', 'age', 'country'], $dictionary->getKeys());
63+
$this->assertEquals(['David', 35, 'UK'], $dictionary->getValues());
64+
}
65+
66+
public function testDictionarySize()
67+
{
68+
$dictionary = new Dictionary([
69+
'name' => 'Eve',
70+
'city' => 'Berlin',
71+
]);
72+
73+
$this->assertEquals(2, $dictionary->size());
74+
75+
$dictionary->add('age', 28);
76+
$this->assertEquals(3, $dictionary->size());
77+
}
78+
79+
public function testClearDictionary()
80+
{
81+
$dictionary = new Dictionary([
82+
'name' => 'Frank',
83+
'job' => 'Developer',
84+
]);
85+
86+
$dictionary->clear();
87+
88+
$this->assertEquals(0, $dictionary->size());
89+
$this->assertFalse($dictionary->containsKey('name'));
90+
}
91+
92+
public function testInvalidKeyThrowsException()
93+
{
94+
$this->expectException(InvalidArgumentException::class);
95+
$this->expectExceptionMessage("Dictionary keys must be non-integer (string) keys.");
96+
97+
// Attempt to initialize with an invalid key (integer key)
98+
new Dictionary([1 => 'Invalid']);
99+
}
100+
101+
public function testGetNonExistentKeyThrowsException()
102+
{
103+
$dictionary = new Dictionary(['name' => 'Grace']);
104+
$this->expectException(OutOfBoundsException::class);
105+
$this->expectExceptionMessage("Key 'age' does not exist in the dictionary.");
106+
107+
$dictionary->get('age'); // This should throw an exception
108+
}
109+
110+
public function testRemoveNonExistentKeyThrowsException()
111+
{
112+
$dictionary = new Dictionary(['name' => 'Hank']);
113+
$this->expectException(OutOfBoundsException::class);
114+
$this->expectExceptionMessage("Key 'age' does not exist in the dictionary.");
115+
116+
$dictionary->remove('age'); // This should throw an exception
117+
}
118+
}

Tests/ListDataTest.php

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
4+
use Nejcc\PhpDatatypes\Composite\ListData;
5+
use PHPUnit\Framework\TestCase;
6+
class ListDataTest extends TestCase
7+
{
8+
public function testCanInitializeWithElements()
9+
{
10+
$list = new ListData(['Element 1', 'Element 2']);
11+
12+
$this->assertEquals('Element 1', $list->get(0));
13+
$this->assertEquals('Element 2', $list->get(1));
14+
}
15+
16+
public function testAddElementsToList()
17+
{
18+
$list = new ListData();
19+
$list->add('First element');
20+
$list->add(42);
21+
22+
$this->assertEquals('First element', $list->get(0));
23+
$this->assertEquals(42, $list->get(1));
24+
}
25+
26+
public function testRemoveElementFromList()
27+
{
28+
$list = new ListData(['Element 1', 'Element 2', 'Element 3']);
29+
30+
// Remove 'Element 2'
31+
$list->remove(1);
32+
33+
// After removal, 'Element 3' should move to index 1
34+
$this->assertEquals('Element 3', $list->get(1));
35+
36+
// Ensure 'Element 2' no longer exists and 'Element 3' is now at the correct index
37+
$this->expectException(OutOfBoundsException::class);
38+
$list->get(2); // Trying to access index 2 should now throw an exception because there is no element at that index
39+
}
40+
41+
42+
public function testContainsElement()
43+
{
44+
$list = new ListData(['First', 'Second', 'Third']);
45+
46+
$this->assertTrue($list->contains('First'));
47+
$this->assertFalse($list->contains('Fourth'));
48+
}
49+
50+
public function testGetAllElements()
51+
{
52+
$list = new ListData(['Element 1', 'Element 2', 'Element 3']);
53+
54+
$this->assertEquals(['Element 1', 'Element 2', 'Element 3'], $list->getAll());
55+
}
56+
57+
public function testGetListSize()
58+
{
59+
$list = new ListData(['Element 1', 'Element 2']);
60+
61+
$this->assertEquals(2, $list->size());
62+
63+
$list->add('Element 3');
64+
$this->assertEquals(3, $list->size());
65+
}
66+
67+
public function testClearList()
68+
{
69+
$list = new ListData(['Element 1', 'Element 2', 'Element 3']);
70+
$list->clear();
71+
72+
$this->assertEquals(0, $list->size());
73+
$this->assertEmpty($list->getAll());
74+
}
75+
76+
public function testGetNonExistentElementThrowsException()
77+
{
78+
$list = new ListData(['Element 1']);
79+
80+
$this->expectException(OutOfBoundsException::class);
81+
$list->get(5); // This should throw an exception because index 5 doesn't exist
82+
}
83+
84+
public function testRemoveNonExistentElementThrowsException()
85+
{
86+
$list = new ListData(['Element 1', 'Element 2']);
87+
88+
$this->expectException(OutOfBoundsException::class);
89+
$list->remove(10); // This should throw an exception because index 10 doesn't exist
90+
}
91+
}

examples/dictionary.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
include_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use Nejcc\PhpDatatypes\Composite\Dictionary;
6+
7+
// Create a new dictionary
8+
$dictionary = new Dictionary([
9+
'name' => 'John Doe',
10+
'age' => 30,
11+
]);
12+
13+
// Add a new key-value pair
14+
$dictionary->add('country', 'USA');
15+
16+
// Get a value by key
17+
echo $dictionary->get('name'); // Output: John Doe
18+
19+
// Check if a key exists
20+
if ($dictionary->containsKey('age')) {
21+
echo $dictionary->get('age'); // Output: 30
22+
}
23+
24+
// Remove a key
25+
$dictionary->remove('country');
26+
27+
// Get the size of the dictionary
28+
echo $dictionary->size(); // Output: 2
29+
var_dump($dictionary);
30+
// Clear the dictionary
31+
$dictionary->clear();
32+
33+
var_dump($dictionary);

examples/listdata.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
include_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use Nejcc\PhpDatatypes\Composite\ListData;
6+
7+
$list = new ListData();
8+
9+
// Add elements to the list
10+
$list->add('First element');
11+
$list->add(42);
12+
$list->add(['nested', 'array']);
13+
14+
// Retrieve an element by index
15+
echo $list->get(0); // Output: First element
16+
17+
// Remove an element by index
18+
$list->remove(1);
19+
20+
// Check if the list contains a specific element
21+
if ($list->contains('First element')) {
22+
echo "Element exists!";
23+
}
24+
25+
// Get the entire list
26+
print_r($list->getAll()); // Output: Array containing remaining elements
27+
28+
// Get the size of the list
29+
echo $list->size(); // Output: 2
30+
var_dump($list);
31+
32+
// Clear the list
33+
$list->clear();
34+
echo $list->size(); // Output: 0 (empty list)
35+
36+
var_dump($list);

0 commit comments

Comments
 (0)