-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathArrayStackTest.php
146 lines (123 loc) · 3.4 KB
/
ArrayStackTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* @author Igor Pomiluyko pomiluyko@worksolutions.ru
* @license MIT
*/
namespace WS\Utils\Collections;
use PHPUnit\Framework\TestCase;
use RuntimeException;
class ArrayStackTest extends TestCase
{
use CollectionInterfaceTestTrait;
private function createInstance(...$args): Stack
{
return new ArrayStack($args);
}
/**
* @test
*/
public function peeking(): void
{
$stack = ArrayStack::of(1, 2, 3);
$this->assertEquals(3, $stack->peek());
$this->assertEquals(3, $stack->size());
}
/**
* @test
*/
public function peekingEmpty(): void
{
$this->expectException(RuntimeException::class);
$stack = new ArrayStack();
$stack->peek();
}
/**
* @test
*/
public function pushing(): void
{
$stack = ArrayStack::of(100, 0, 50);
$this->assertTrue($stack->push(10));
$this->assertTrue($stack->push(10));
$this->assertEquals(5, $stack->size());
$this->assertTrue($stack->push(15));
$this->assertEquals(15, $stack->peek());
$this->assertEquals(6, $stack->size());
}
/**
* @test
*/
public function popping(): void
{
$stack = ArrayStack::of(100, 0, '50', 'asd', 20);
$this->assertEquals(20, $stack->pop());
$this->assertEquals('asd', $stack->pop());
$this->assertEquals('50', $stack->pop());
$this->assertEquals(0, $stack->pop());
$this->assertEquals(100, $stack->pop());
}
/**
* @test
*/
public function emptyPopping(): void
{
$this->expectException(RuntimeException::class);
$stack = new ArrayStack();
$stack->pop();
}
/**
* @test
*/
public function iterating(): void
{
$stack = ArrayStack::of(9, 8, 'a', 'n');
$elements = [];
/** @noinspection PhpUnhandledExceptionInspection */
foreach ($stack->getIterator() as $element) {
$elements[] = $element;
}
$this->assertEquals(['n', 'a', 8, 9], $elements);
$elements = [];
/** @noinspection PhpUnhandledExceptionInspection */
foreach ($stack->getIterator() as $element) {
$elements[] = $element;
}
$this->assertEquals(['n', 'a', 8, 9], $elements);
}
/**
* @test
*/
public function merging(): void
{
$stack1 = ArrayStack::of(1, 7);
$stack2 = ArrayStack::of('c', 'a');
$stack1->merge($stack2);
$this->assertEquals(['c', 'a', 7, 1], $stack1->toArray());
$stack2->merge($stack2);
$this->assertEquals(['c', 'a', 'a', 'c'], $stack2->toArray());
}
/**
* @test
*/
public function equivalency(): void
{
$stack1 = ArrayStack::of(1, 7);
$stack2 = ArrayStack::of(1, 7);
$this->assertTrue($stack1->equals($stack2));
$this->assertTrue($stack2->equals($stack1));
}
/**
* @test
*/
public function absentEquivalency(): void
{
$stack1 = ArrayStack::of(1, 7);
$stack2 = ArrayStack::of(7, 1);
$stack3 = ArrayStack::of(1, 7, 7);
$stack4 = ArrayStack::of('1', '7');
$this->assertFalse($stack1->equals($stack2));
$this->assertFalse($stack1->equals($stack3));
$this->assertFalse($stack1->equals($stack4));
$this->assertFalse($stack2->equals($stack3));
}
}