Cleanup and improvements (#194)

- Lots of changes for migrating C code to C++
- Improved `Lockable` in several ways like adding `withLock()` (+ tests)
- Improved `Semaphore` a bit for improved readability, and also added some tests
- Upgrade Linux machine in GitHub Actions so that we can compile with a newer GCC
- Simplification of WiFi connection
- Updated funding options
- (and more)
This commit is contained in:
Ken Van Hoeylandt
2025-01-28 17:39:58 +01:00
committed by GitHub
parent 1bb1260ea0
commit 6c67845645
54 changed files with 518 additions and 531 deletions
+39
View File
@@ -0,0 +1,39 @@
#include "Lockable.h"
#include "Semaphore.h"
#include "doctest.h"
#include <Mutex.h>
using namespace tt;
TEST_CASE("withLock() locks correctly on Semaphore") {
auto semaphore = std::make_shared<Semaphore>(2U);
semaphore->withLock([semaphore](){
CHECK_EQ(semaphore->getAvailable(), 1);
});
}
TEST_CASE("withLock() unlocks correctly on Semaphore") {
auto semaphore = std::make_shared<Semaphore>(2U);
semaphore->withLock([=](){
// NO-OP
});
CHECK_EQ(semaphore->getAvailable(), 2);
}
TEST_CASE("withLock() locks correctly on Mutex") {
auto mutex = std::make_shared<Mutex>();
mutex->withLock([mutex](){
CHECK_EQ(mutex->lock(1), false);
});
}
TEST_CASE("withLock() unlocks correctly on Mutex") {
auto mutex = std::make_shared<Mutex>();
mutex->withLock([=](){
// NO-OP
});
CHECK_EQ(mutex->lock(1), true);
CHECK_EQ(mutex->unlock(), true);
}
+19
View File
@@ -32,3 +32,22 @@ TEST_CASE("a mutex can block a thread") {
thread.join();
}
TEST_CASE("a Mutex can be locked exactly once") {
auto mutex = Mutex(Mutex::Type::Normal);
CHECK_EQ(mutex.lock(0), true);
CHECK_EQ(mutex.lock(0), false);
CHECK_EQ(mutex.unlock(), true);
}
TEST_CASE("unlocking a Mutex without locking returns false") {
auto mutex = Mutex(Mutex::Type::Normal);
CHECK_EQ(mutex.unlock(), false);
}
TEST_CASE("unlocking a Mutex twice returns false on the second attempt") {
auto mutex = Mutex(Mutex::Type::Normal);
CHECK_EQ(mutex.lock(0), true);
CHECK_EQ(mutex.unlock(), true);
CHECK_EQ(mutex.unlock(), false);
}
+35
View File
@@ -0,0 +1,35 @@
#include "doctest.h"
#include "Semaphore.h"
using namespace tt;
// We want a distinct test for 1 item, because it creates the Semaphore differently
TEST_CASE("a Semaphore with max count of 1 can be acquired exactly once") {
auto semaphore = Semaphore(1);
CHECK_EQ(semaphore.acquire(0), true);
CHECK_EQ(semaphore.getAvailable(), 0);
CHECK_EQ(semaphore.acquire(0), false);
CHECK_EQ(semaphore.release(), true);
CHECK_EQ(semaphore.getAvailable(), 1);
}
TEST_CASE("a Semaphore with max count of 2 can be acquired exactly twice") {
auto semaphore = Semaphore(2);
CHECK_EQ(semaphore.acquire(0), true);
CHECK_EQ(semaphore.getAvailable(), 1);
CHECK_EQ(semaphore.acquire(0), true);
CHECK_EQ(semaphore.getAvailable(), 0);
CHECK_EQ(semaphore.acquire(0), false);
CHECK_EQ(semaphore.release(), true);
CHECK_EQ(semaphore.getAvailable(), 1);
CHECK_EQ(semaphore.release(), true);
CHECK_EQ(semaphore.getAvailable(), 2);
}
TEST_CASE("the semaphore count should be correct initially") {
auto semaphore_a = Semaphore(2);
CHECK_EQ(semaphore_a.getAvailable(), 2);
auto semaphore_b = Semaphore(2, 0);
CHECK_EQ(semaphore_b.getAvailable(), 0);
}