try use boost.stackstrace print exception call stack.

This commit is contained in:
amass 2024-05-05 17:21:45 +08:00
parent 88bc6c11a1
commit 264ac80d2b
7 changed files with 9 additions and 258 deletions

View File

@ -4,7 +4,7 @@
"name": "Linux", "name": "Linux",
"includePath": [ "includePath": [
"${workspaceFolder}/**", "${workspaceFolder}/**",
"/opt/Libraries/boost_1_84_0/include" "/opt/Libraries/boost_1_85_0/include"
], ],
"defines": [], "defines": [],
"compilerPath": "/usr/bin/gcc", "compilerPath": "/usr/bin/gcc",

View File

@ -3,7 +3,6 @@ add_library(DataStructure
ArrayList.h ArrayList.h
BinaryTree.h BinaryTree.h
BinarySearchTree.h BinarySearchTree.h
CircularDoublyLinkedList.h
CircularLinkedList.h CircularLinkedList.h
DoublyLinkedList.h DoublyLinkedList.h
DynamicArray.h DynamicArray.h

View File

@ -1,141 +0,0 @@
#ifndef DUALCIRCULARLIST_H
#define DUALCIRCULARLIST_H
#include "DoublyLinkedList.h"
namespace Kylin {
template <typename T>
class CircularDoublyLinkedList : public DoublyLinkedList<T> {
using Node = typename DoublyLinkedList<T>::Node;
using Iterator = typename DoublyLinkedList<T>::Iterator;
public:
static constexpr size_t npos = static_cast<size_t>(-1);
CircularDoublyLinkedList() {
this->m_header.next = reinterpret_cast<Node *>(&(this->m_header));
this->m_header.prev = reinterpret_cast<Node *>(&(this->m_header));
this->m_last = reinterpret_cast<Node *>(&this->m_header);
}
CircularDoublyLinkedList(std::initializer_list<T> init) {
this->m_header.next = reinterpret_cast<Node *>(&(this->m_header));
this->m_header.prev = reinterpret_cast<Node *>(&(this->m_header));
this->m_last = reinterpret_cast<Node *>(&this->m_header);
for (auto &value : init) append(value);
}
CircularDoublyLinkedList(const CircularDoublyLinkedList &other) {
this->m_header.next = reinterpret_cast<Node *>(&(this->m_header));
this->m_header.prev = reinterpret_cast<Node *>(&(this->m_header));
this->m_last = reinterpret_cast<Node *>(&this->m_header);
auto sourceNode = other.m_header.next;
auto targetNode = reinterpret_cast<Node *>(&this->m_header);
for (size_t i = 0; i < other.m_size; i++) {
auto newNode = this->create();
if (newNode == nullptr) THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create node ...");
newNode->value = sourceNode->value;
newNode->prev = targetNode;
newNode->next = targetNode->next;
targetNode->next = newNode;
targetNode = newNode;
sourceNode = sourceNode->next;
this->m_size++;
this->m_last = targetNode;
}
}
CircularDoublyLinkedList(CircularDoublyLinkedList &&other) {
this->m_size = other.m_size;
this->m_header = other.m_header;
this->m_last = other.m_last;
other.m_size = 0;
other.m_header.next = nullptr;
other.m_last = reinterpret_cast<Node *>(&other.m_header);
}
void insert(size_t index, const T &value) override {
index = index % (this->m_size + 1);
if (index == this->m_size) return append(value);
auto node = this->create();
if (node == nullptr) THROW_EXCEPTION(NoEnoughMemoryException, "No momery to insert new element...");
node->value = value;
auto prev = position(index);
auto next = prev->next;
node->next = next;
node->prev = prev;
prev->next = node;
next->prev = node;
this->m_size++;
}
inline void append(const T &value) {
auto node = this->create();
if (node == nullptr) THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create node ...");
node->value = value;
node->prev = this->m_last;
node->next = this->m_last->next;
this->m_last->next->prev = node;
this->m_last->next = node;
this->m_last = node;
this->m_size++;
}
void removeAt(size_t index) override {
index = mod(index);
auto prev = position(index);
auto toDel = prev->next;
auto next = toDel->next;
prev->next = next;
next->prev = prev;
this->destroy(toDel);
this->m_size--;
}
T &last() override {
if (this->m_size <= 0) THROW_EXCEPTION(InvalidOperationException, "There is no element in the container.");
return this->m_header.prev->value;
}
void clear() override {
while (this->m_size > 0) {
removeAt(0);
}
}
Iterator end() override { return Iterator(reinterpret_cast<Node *>(&this->m_header)); }
Iterator end() const override { return Iterator(reinterpret_cast<Node *>(&this->m_header)); }
~CircularDoublyLinkedList() { clear(); }
T &operator[](size_t index) override {
index = mod(index);
return position(index)->next->value;
}
protected:
size_t mod(size_t index) const { return (this->m_size == 0) ? 0 : (index % this->m_size); }
Node *position(size_t index) const override {
auto node = reinterpret_cast<Node *>(&(this->m_header));
for (size_t i = 0; i < index; i++) {
node = node->next;
}
return node;
}
};
} // namespace Kylin
#endif // DUALCIRCULARLIST_H

View File

@ -7,7 +7,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(UnitTest main.cpp add_executable(UnitTest main.cpp
DataStructure/BinarySearchTreeTest.cpp DataStructure/BinarySearchTreeTest.cpp
DataStructure/BinaryTreeTest.cpp DataStructure/BinaryTreeTest.cpp
DataStructure/CircularDoublyLinkedListTest.cpp
DataStructure/CircularLinkedListTest.cpp DataStructure/CircularLinkedListTest.cpp
DataStructure/DoublyLinkedListTest.cpp DataStructure/DoublyLinkedListTest.cpp
DataStructure/DynamicArrayListTest.cpp DataStructure/DynamicArrayListTest.cpp

View File

@ -1,108 +0,0 @@
#include "CircularDoublyLinkedList.h"
#include <boost/test/unit_test.hpp>
#include <iostream>
using namespace Kylin;
class CircularDoublyLinkedListTest {
public:
CircularDoublyLinkedList<size_t> list{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
};
BOOST_AUTO_TEST_SUITE(CircularDoublyLinkedListTestCase)
BOOST_FIXTURE_TEST_CASE(At, CircularDoublyLinkedListTest) { BOOST_CHECK_EQUAL(list.at(5), 5); }
BOOST_FIXTURE_TEST_CASE(Size, CircularDoublyLinkedListTest) { BOOST_CHECK_EQUAL(list.size(), 10); }
BOOST_FIXTURE_TEST_CASE(Last, CircularDoublyLinkedListTest) { BOOST_CHECK_EQUAL(list.last(), 9); }
BOOST_FIXTURE_TEST_CASE(EmptyListCallLastCauseException, CircularDoublyLinkedListTest) {
CircularDoublyLinkedList<size_t> emptyList;
BOOST_CHECK_THROW(emptyList.last(), InvalidOperationException);
}
BOOST_FIXTURE_TEST_CASE(CallLastAfterRemoveLastElement, CircularDoublyLinkedListTest) {
list.removeAt(list.size() - 1);
BOOST_CHECK_EQUAL(list.last(), 8);
}
BOOST_FIXTURE_TEST_CASE(RemoveIndexLessThenSize, CircularDoublyLinkedListTest) {
list.removeAt(3);
BOOST_CHECK_EQUAL(list[3], 4);
BOOST_CHECK_EQUAL(list.length(), 9);
}
BOOST_FIXTURE_TEST_CASE(RemoveIndexBigThenSize, CircularDoublyLinkedListTest) {
list.removeAt(15);
BOOST_CHECK_EQUAL(list[5], 6);
BOOST_CHECK_EQUAL(list.length(), 9);
}
BOOST_FIXTURE_TEST_CASE(Clear, CircularDoublyLinkedListTest) {
list.clear();
BOOST_CHECK_EQUAL(list.length(), 0);
}
BOOST_FIXTURE_TEST_CASE(IndexOf, CircularDoublyLinkedListTest) { BOOST_CHECK_EQUAL(list.indexOf(5), 5); }
BOOST_FIXTURE_TEST_CASE(Append, CircularDoublyLinkedListTest) {
list.append(198);
BOOST_CHECK_EQUAL(list.length(), 11);
BOOST_CHECK_EQUAL(list.at(list.size() - 1), 198);
}
BOOST_FIXTURE_TEST_CASE(Swap, CircularDoublyLinkedListTest) {
CircularDoublyLinkedList<size_t> list2;
for (size_t i = 0; i < 5; i++) {
list2.insert(list2.length(), i + 10);
}
list2.swap(list);
BOOST_CHECK_EQUAL(list.size(), 5);
BOOST_CHECK_EQUAL(list2.size(), 10);
for (size_t i = 0; i < 5; i++) {
BOOST_CHECK_EQUAL(list.at(i), i + 10);
}
for (size_t i = 0; i < 10; i++) {
BOOST_CHECK_EQUAL(list2.at(i), i);
}
}
BOOST_FIXTURE_TEST_CASE(InsertToIndexLessThenSize, CircularDoublyLinkedListTest) {
list.insert(4, 456);
BOOST_CHECK_EQUAL(list.length(), 11);
BOOST_CHECK_EQUAL(list.at(4), 456);
BOOST_CHECK_EQUAL(list.at(5), 4);
}
BOOST_FIXTURE_TEST_CASE(InsertToIndexEuqalSize, CircularDoublyLinkedListTest) {
list.insert(list.size(), 456);
BOOST_CHECK_EQUAL(list.length(), 11);
BOOST_CHECK_EQUAL(list.at(list.size() - 1), 456);
}
BOOST_FIXTURE_TEST_CASE(CopyConstructor, CircularDoublyLinkedListTest) {
auto list2(list);
BOOST_CHECK_EQUAL(list2.size(), 10);
list[9] = 2; //测试是否为浅拷贝
BOOST_CHECK_EQUAL(list2[9], 9);
}
BOOST_FIXTURE_TEST_CASE(MoveConstructor, CircularDoublyLinkedListTest) {
auto list2(std::move(list));
BOOST_CHECK_EQUAL(list.empty(), true);
BOOST_CHECK_EQUAL(list2.size(), 10);
BOOST_CHECK_EQUAL(list2[9], 9);
}
BOOST_FIXTURE_TEST_CASE(Iterator, CircularDoublyLinkedListTest) {
size_t i = 0;
for (const auto &value : list) {
BOOST_CHECK_EQUAL(value, i++);
}
BOOST_CHECK_EQUAL(list.size(), i);
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -1,6 +1,7 @@
#include "IoContext.h" #include "IoContext.h"
#include <boost/asio/executor_work_guard.hpp> #include <boost/asio/executor_work_guard.hpp>
#include <boost/scope_exit.hpp> #include <boost/scope_exit.hpp>
#include <boost/stacktrace.hpp>
IoContext::~IoContext() { IoContext::~IoContext() {
m_ioContext->stop(); m_ioContext->stop();
@ -23,6 +24,7 @@ void IoContext::runIoContext() {
} catch (const boost::exception &e) { } catch (const boost::exception &e) {
LOG(error) << boost::diagnostic_information(e); LOG(error) << boost::diagnostic_information(e);
} catch (const std::exception &e) { } catch (const std::exception &e) {
LOG(error) << e.what(); boost::stacktrace::stacktrace trace = boost::stacktrace::stacktrace::from_current_exception();
LOG(error) << e.what() << ", trace:\n" << trace;
} }
} }

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
base_path=$(pwd) base_path=$(pwd)
qt_prefix_path="/opt/Qt/6.6.1/gcc_64" qt_prefix_path="/opt/Qt/6.6.2/gcc_64"
libraries_root="/opt/Libraries" libraries_root="/opt/Libraries"
if [ $base_path == /home/* ]; then if [ $base_path == /home/* ]; then
build_path=${base_path}/build build_path=${base_path}/build
@ -35,7 +35,7 @@ elif [ -d "/opt/Qt/5.15.2/gcc_64" ]; then
-DQt5Svg_DIR=${qt_prefix_path}/lib/cmake/Qt5Svg " -DQt5Svg_DIR=${qt_prefix_path}/lib/cmake/Qt5Svg "
else else
cmake_qt_parameters="" cmake_qt_parameters=""
echo "please install qt6.6.1 or qt5.15.2 ..." echo "please install qt6.6.2 or qt5.15.2 ..."
fi fi
function cmake_scan() { function cmake_scan() {
@ -48,7 +48,7 @@ function cmake_scan() {
-B ${build_path} \ -B ${build_path} \
-DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_BUILD_TYPE=Debug \
-DUNIT_TEST=ON \ -DUNIT_TEST=ON \
-DBOOST_ROOT=${libraries_root}/boost_1_84_0 \ -DBOOST_ROOT=${libraries_root}/boost_1_85_0 \
-DZeroMQ_ROOT=${libraries_root}/zeromq-4.3.4_debug \ -DZeroMQ_ROOT=${libraries_root}/zeromq-4.3.4_debug \
${cmake_qt_parameters} ${cmake_qt_parameters}
} }
@ -66,7 +66,7 @@ function build() {
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
exit 1 exit 1
fi fi
build/UnitTest/UnitTest $build_path/UnitTest/UnitTest
} }
function main() { function main() {