Skip to content

Commit 6f90ebd

Browse files
committed
added cpp workflow
1 parent 9e7a65a commit 6f90ebd

File tree

4 files changed

+151
-1
lines changed

4 files changed

+151
-1
lines changed

.github/workflows/cpp-tests.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: C++ Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v2
18+
19+
- name: Install Dependencies
20+
run: sudo apt-get install -y g++
21+
22+
- name: Change Directory
23+
run: cd ./cpp/LinkedList/singly
24+
25+
- name: Compile Tests
26+
run: g++ -std=c++14 list_test.cc -o list_test
27+
28+
- name: Run Tests
29+
run: ./list_test

cpp/LinkedList/singly/list.cc renamed to cpp/LinkedList/singly/list.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# ifndef LIST_H
2+
# define LIST_H
3+
14
#include <iostream>
25
#include <memory>
36

@@ -169,4 +172,6 @@ class LinkedList {
169172

170173
private:
171174
std::shared_ptr<Node<T>> head;
172-
};
175+
};
176+
177+
#endif

cpp/LinkedList/singly/list_test.cc

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include <iostream>
2+
#include <assert.h>
3+
#include "list.h" // Adjust the include path as needed
4+
5+
void testPrepend() {
6+
LinkedList<int> list;
7+
list.prepend(1);
8+
assert(list.getHead()->data == 1);
9+
list.prepend(0);
10+
assert(list.getHead()->data == 0);
11+
assert(list.getHead()->next->data == 1);
12+
std::cout << "testPrepend passed!" << std::endl;
13+
}
14+
15+
void testAppend() {
16+
LinkedList<int> list;
17+
list.append(1);
18+
assert(list.getTail()->data == 1);
19+
list.append(2);
20+
assert(list.getTail()->data == 2);
21+
std::cout << "testAppend passed!" << std::endl;
22+
}
23+
24+
void testInsertBetween() {
25+
LinkedList<int> list;
26+
list.append(1);
27+
list.append(3);
28+
list.insertBetween(2, 3, 1);
29+
assert(list.getHead()->next->data == 2);
30+
assert(list.getHead()->next->next->data == 3);
31+
std::cout << "testInsertBetween passed!" << std::endl;
32+
}
33+
34+
void testDeleteHead() {
35+
LinkedList<int> list;
36+
list.append(1);
37+
list.append(2);
38+
list.deleteHead();
39+
assert(list.getHead()->data == 2);
40+
std::cout << "testDeleteHead passed!" << std::endl;
41+
}
42+
43+
void testDeleteTail() {
44+
LinkedList<int> list;
45+
list.append(1);
46+
list.append(2);
47+
list.deleteTail();
48+
assert(list.getTail()->data == 1);
49+
std::cout << "testDeleteTail passed!" << std::endl;
50+
}
51+
52+
void testDeleteAny() {
53+
LinkedList<int> list;
54+
list.append(1);
55+
list.append(2);
56+
list.append(3);
57+
list.deleteAny(2);
58+
assert(list.getHead()->next->data == 3);
59+
std::cout << "testDeleteAny passed!" << std::endl;
60+
}
61+
62+
void testSize() {
63+
LinkedList<int> list;
64+
assert(list.size() == 0);
65+
list.append(1);
66+
list.append(2);
67+
list.append(3);
68+
assert(list.size() == 3);
69+
std::cout << "testSize passed!" << std::endl;
70+
}
71+
72+
void testReverseList() {
73+
LinkedList<int> list;
74+
list.append(1);
75+
list.append(2);
76+
list.append(3);
77+
list.reverseList();
78+
assert(list.getHead()->data == 3);
79+
assert(list.getTail()->data == 1);
80+
std::cout << "testReverseList passed!" << std::endl;
81+
}
82+
83+
void testFind() {
84+
LinkedList<int> list;
85+
list.append(1);
86+
list.append(2);
87+
assert(list.find(2)->data == 2);
88+
assert(list.find(3) == nullptr);
89+
std::cout << "testFind passed!" << std::endl;
90+
}
91+
92+
void testClear() {
93+
LinkedList<int> list;
94+
list.append(1);
95+
list.append(2);
96+
list.clear();
97+
assert(list.getHead() == nullptr);
98+
assert(list.size() == 0);
99+
std::cout << "testClear passed!" << std::endl;
100+
}
101+
102+
int main() {
103+
testPrepend();
104+
testAppend();
105+
testInsertBetween();
106+
testDeleteHead();
107+
testDeleteTail();
108+
testDeleteAny();
109+
testSize();
110+
testReverseList();
111+
testFind();
112+
testClear();
113+
114+
std::cout << "All tests passed!" << std::endl;
115+
return 0;
116+
}

cpp/LinkedList/singly/test

40.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)