TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
test_queue.cpp
1#include <cassert>
2#include <iostream>
3
4#include "./queue.hpp"
5
6template <typename T>
7void testConstructedQueueIsEmpty() {
8 const queue<T> curQueue;
9 assert(curQueue.isEmptyQueue());
10}
11
12void testEnQueue() {
13 queue<int> curQueue;
14 curQueue.enQueue(10);
15 assert(curQueue.toVector() == std::vector<int>({10}));
16 curQueue.enQueue(20);
17 assert(curQueue.toVector() == std::vector<int>({10, 20}));
18 curQueue.enQueue(30);
19 curQueue.enQueue(40);
20 assert(curQueue.toVector() == std::vector<int>({10, 20, 30, 40}));
21}
22
23void testDeQueue() {
24 queue<int> curQueue;
25 curQueue.enQueue(10);
26 curQueue.enQueue(20);
27 curQueue.enQueue(30);
28
29 curQueue.deQueue();
30 assert(curQueue.toVector() == std::vector<int>({20, 30}));
31
32 curQueue.deQueue();
33 assert(curQueue.toVector() == std::vector<int>({30}));
34
35 curQueue.deQueue();
36 assert(curQueue.isEmptyQueue());
37}
38
39void testFront() {
40 queue<int> curQueue;
41 curQueue.enQueue(10);
42 assert(curQueue.front() == 10);
43 curQueue.enQueue(20);
44 assert(curQueue.front() == 10);
45}
46
47void testQueueAfterClearIsEmpty() {
48 queue<int> curQueue;
49 curQueue.enQueue(10);
50 curQueue.enQueue(20);
51 curQueue.enQueue(30);
52 curQueue.clear();
53 assert(curQueue.isEmptyQueue());
54}
55
56void testFrontThrowsAnInvalidArgumentWhenQueueEmpty() {
57 const queue<int> curQueue;
58 bool wasException = false;
59 try {
60 curQueue.front();
61 } catch (const std::invalid_argument&) {
62 wasException = true;
63 }
64 assert(wasException);
65}
66
67void testDeQueueThrowsAnInvalidArgumentWhenQueueEmpty() {
68 queue<int> curQueue;
69 bool wasException = false;
70 try {
71 curQueue.deQueue();
72 } catch (const std::invalid_argument&) {
73 wasException = true;
74 }
75 assert(wasException);
76}
77
78int main() {
79 testConstructedQueueIsEmpty<int>();
80 testConstructedQueueIsEmpty<double>();
81 testConstructedQueueIsEmpty<std::vector<long double>>();
82
83 testEnQueue();
84 testDeQueue();
85
86 testQueueAfterClearIsEmpty();
87
88 testFrontThrowsAnInvalidArgumentWhenQueueEmpty();
89 testDeQueueThrowsAnInvalidArgumentWhenQueueEmpty();
90
91 std::cout << "All tests pass!\n";
92 return 0;
93}
Definition queue.hpp:9
std::vector< value_type > toVector() const
converts the queue into the std::vector
Definition queue.hpp:29
bool isEmptyQueue() const
checks if the queue has no elements
Definition queue.hpp:49
void clear()
removes all elements from the queue
Definition queue.hpp:90
value_type front() const
Definition queue.hpp:72
void enQueue(const value_type &item)
inserts a new item into the queue
Definition queue.hpp:54
void deQueue()
removes the first element from the queue
Definition queue.hpp:81
int main()
Main function.