mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
7c090481da
* fix: remove memory leak in queue
* updating DIRECTORY.md
* clang-format and clang-tidy fixes for effd74c9
* style: simplify logic while using reserve
* style: use value_type as return type in front
* style: use proper error message
* style: use pre-increment and pre-decrement
* docs: use doxygen syntax
* docs: improve wording
Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
94 lines
2.2 KiB
C++
94 lines
2.2 KiB
C++
#include <cassert> /// for assert
|
|
#include <iostream> /// for std::cout
|
|
|
|
#include "./queue.hpp"
|
|
|
|
template <typename T>
|
|
void testConstructedQueueIsEmpty() {
|
|
const queue<T> curQueue;
|
|
assert(curQueue.isEmptyQueue());
|
|
}
|
|
|
|
void testEnQueue() {
|
|
queue<int> curQueue;
|
|
curQueue.enQueue(10);
|
|
assert(curQueue.toVector() == std::vector<int>({10}));
|
|
curQueue.enQueue(20);
|
|
assert(curQueue.toVector() == std::vector<int>({10, 20}));
|
|
curQueue.enQueue(30);
|
|
curQueue.enQueue(40);
|
|
assert(curQueue.toVector() == std::vector<int>({10, 20, 30, 40}));
|
|
}
|
|
|
|
void testDeQueue() {
|
|
queue<int> curQueue;
|
|
curQueue.enQueue(10);
|
|
curQueue.enQueue(20);
|
|
curQueue.enQueue(30);
|
|
|
|
curQueue.deQueue();
|
|
assert(curQueue.toVector() == std::vector<int>({20, 30}));
|
|
|
|
curQueue.deQueue();
|
|
assert(curQueue.toVector() == std::vector<int>({30}));
|
|
|
|
curQueue.deQueue();
|
|
assert(curQueue.isEmptyQueue());
|
|
}
|
|
|
|
void testFront() {
|
|
queue<int> curQueue;
|
|
curQueue.enQueue(10);
|
|
assert(curQueue.front() == 10);
|
|
curQueue.enQueue(20);
|
|
assert(curQueue.front() == 10);
|
|
}
|
|
|
|
void testQueueAfterClearIsEmpty() {
|
|
queue<int> curQueue;
|
|
curQueue.enQueue(10);
|
|
curQueue.enQueue(20);
|
|
curQueue.enQueue(30);
|
|
curQueue.clear();
|
|
assert(curQueue.isEmptyQueue());
|
|
}
|
|
|
|
void testFrontThrowsAnInvalidArgumentWhenQueueEmpty() {
|
|
const queue<int> curQueue;
|
|
bool wasException = false;
|
|
try {
|
|
curQueue.front();
|
|
} catch (const std::invalid_argument&) {
|
|
wasException = true;
|
|
}
|
|
assert(wasException);
|
|
}
|
|
|
|
void testDeQueueThrowsAnInvalidArgumentWhenQueueEmpty() {
|
|
queue<int> curQueue;
|
|
bool wasException = false;
|
|
try {
|
|
curQueue.deQueue();
|
|
} catch (const std::invalid_argument&) {
|
|
wasException = true;
|
|
}
|
|
assert(wasException);
|
|
}
|
|
|
|
int main() {
|
|
testConstructedQueueIsEmpty<int>();
|
|
testConstructedQueueIsEmpty<double>();
|
|
testConstructedQueueIsEmpty<std::vector<long double>>();
|
|
|
|
testEnQueue();
|
|
testDeQueue();
|
|
|
|
testQueueAfterClearIsEmpty();
|
|
|
|
testFrontThrowsAnInvalidArgumentWhenQueueEmpty();
|
|
testDeQueueThrowsAnInvalidArgumentWhenQueueEmpty();
|
|
|
|
std::cout << "All tests pass!\n";
|
|
return 0;
|
|
}
|