-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.cpp
145 lines (119 loc) · 3.82 KB
/
main_test.cpp
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
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "problem_a.cpp"
using namespace strukdat::stack;
TEST_CASE("dapat membuat stack baru", "[case_1]") {
Top<int> = createStack(Top<int>);
REQUIRE(Top<int> == nullptr);
}
TEST_CASE("dapat membuat elemen baru dengan pointer", "[case_2]") {
pointer<int> elementInt;
pointer<float> elementFloat;
createElement(elementInt, 66);
createElement(elementFloat, 48.4f);
REQUIRE(elementFloat->data == 48.4f);
REQUIRE(elementInt->data == 66);
REQUIRE(elementInt->next == nullptr);
}
TEST_CASE("dapat mengambil puncak dari stack", "[case_3]") {
pointer<int> newElement;
int data[5] = {87, 90, 41, 85, 25};
Top<int> = createStack(Top<int>);
for (int i = 0; i < 5; i++) {
createElement(newElement, data[i]);
Top<int> = push(Top<int>, newElement);
}
REQUIRE(peek(Top<int>) == 25);
}
TEST_CASE("dapat mengecek stack kosong", "[case_4]") {
pointer<int> newElement;
int data[5] = {87, 90, 41, 85, 25};
Top<int> = createStack(Top<int>);
REQUIRE(isEmpty(Top<int>) == true);
for (int i = 0; i < 5; i++) {
createElement(newElement, data[i]);
Top<int> = push(Top<int>, newElement);
REQUIRE(isEmpty(Top<int>) == false);
}
}
TEST_CASE("dapat menambah elemen pada stack (push)", "[case_5]") {
pointer<int> newElement;
int data[5] = {87, 90, 41, 85, 25};
Top<int> = createStack(Top<int>);
for (int i = 0; i < 5; i++) {
createElement(newElement, data[i]);
Top<int> = push(Top<int>, newElement);
REQUIRE(Top<int>->data == data[i]);
}
}
TEST_CASE("dapat menghapus elemen pada stack (pop)", "[case_6]") {
pointer<int> newElement, pDel;
int data[5] = {87, 90, 41, 85, 25};
Top<int> = createStack(Top<int>);
pDel = pop(Top<int>, pDel);
REQUIRE(pDel == nullptr);
createElement(newElement, 80);
Top<int> = push(Top<int>, newElement);
pDel = pop(Top<int>, pDel);
REQUIRE(Top<int> == nullptr);
REQUIRE(pDel->data == 80);
for (int i = 0; i < 5; i++) {
createElement(newElement, data[i]);
Top<int> = push(Top<int>, newElement);
}
pDel = pop(Top<int>, pDel);
pDel = pop(Top<int>, pDel);
pDel = pop(Top<int>, pDel);
REQUIRE(pDel->data == 41);
}
TEST_CASE("dapat mencari elemen terdalam pada stack", "[case_7]") {
pointer<int> newElement, pLast;
int data[5] = {87, 90, 41, 85, 25};
Top<int> = createStack(Top<int>);
pLast = lastNode(Top<int>);
REQUIRE(pLast == nullptr);
for (int i = 0; i < 5; i++) {
createElement(newElement, data[i]);
Top<int> = push(Top<int>, newElement);
}
pLast = lastNode(Top<int>);
REQUIRE(pLast->data == 87);
}
TEST_CASE("dapat mengambil elemen terbesar pada stack", "[case_8]") {
pointer<int> newElement;
int data[5] = {87, 90, 41, 85, 25};
Top<int> = createStack(Top<int>);
for (int i = 0; i < 5; i++) {
createElement(newElement, data[i]);
Top<int> = push(Top<int>, newElement);
}
REQUIRE(getMax(Top<int>) == 90);
}
TEST_CASE("dapat menentukan jumlah maksimal yang setara dari 3 stack", "[case_9]") {
pointer<int> newElement;
int data1[5] = { 3, 2, 1, 1, 1 };
int data2[3] = { 4, 3, 2 };
int data3[4] = { 1, 1, 4, 1 };
Stack<int> Top1, Top2, Top3;
Top1 = createStack(Top1);
Top2 = createStack(Top2);
Top3 = createStack(Top3);
for (int i = 0; i < 5; i++) {
createElement(newElement, data1[i]);
Top1 = push(Top1, newElement);
}
for (int i = 0; i < 3; i++) {
createElement(newElement, data2[i]);
Top2 = push(Top2, newElement);
}
for (int i = 0; i < 4; i++) {
createElement(newElement, data3[i]);
Top3 = push(Top3, newElement);
}
REQUIRE(getEqualStack(Top1, Top2, Top3) == 7);
}
TEST_CASE("dapat menentukan apakah kurung yang diberikan seimbang atau tidak", "[case_10]") {
REQUIRE(isBracketBalanced("{[()]}") == true);
REQUIRE(isBracketBalanced("{[(])}") == false);
REQUIRE(isBracketBalanced("{{[[(())]]}}") == true);
}