Skip to content

Commit 966035b

Browse files
Test improvements and fixes
1 parent 989190c commit 966035b

16 files changed

Lines changed: 52 additions & 21 deletions

Tactility/Source/file/File.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ bool deleteRecursively(const std::string& path) {
287287
}
288288

289289
for (const auto& entry : entries) {
290+
if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0) {
291+
continue;
292+
}
290293
auto child_path = path + "/" + entry.d_name;
291294
if (!deleteRecursively(child_path)) {
292295
return false;

Tactility/Tests/Source/FileTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ using namespace tt;
55

66
TEST_CASE("findOrCreateDirectory can create a directory tree without prefix") {
77
CHECK_EQ(file::findOrCreateDirectory("test1/test1", 0777), true);
8-
// TODO: delete dirs
8+
CHECK_EQ(file::deleteRecursively("test1"), true);
99
}
1010

1111
TEST_CASE("findOrCreateDirectory can create a directory tree with prefix") {
1212
CHECK_EQ(file::findOrCreateDirectory("/tmp/test2", 0777), true);
13-
// TODO: delete dirs
13+
CHECK_EQ(file::deleteRecursively("/tmp/test2"), true);
1414
}

Tactility/Tests/Source/ObjectFileTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ TEST_CASE("Writing and reading multiple records to a file") {
2626
CHECK_EQ(reader.open(), true);
2727
CHECK_EQ(reader.hasNext(), true);
2828
CHECK_EQ(reader.readNext(&record_in), true);
29+
CHECK_EQ(record_in.value, 0xAAAAAAAA);
2930
CHECK_EQ(reader.hasNext(), true);
3031
CHECK_EQ(reader.readNext(&record_in), true);
32+
CHECK_EQ(record_in.value, 0xBBBBBBBB);
3133
CHECK_EQ(reader.hasNext(), false);
3234
reader.close();
3335

Tactility/Tests/Source/UrlTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ TEST_CASE("parseUrlQuery should url-decode the key") {
4646
CHECK_EQ(map["Test!Test"], "value");
4747
}
4848

49-
TEST_CASE("urlDecode") {
49+
TEST_CASE("urlEncode") {
5050
auto input = std::string("prefix!*'();:@&=+$,/?#[]<>%-.^_`{}|~ \\");
5151
auto expected = std::string("prefix%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D%3C%3E%25-.%5E_%60%7B%7D%7C~+%5C");
5252
auto encoded = network::urlEncode(input);

TactilityFreeRtos/Tests/Source/DispatcherThreadTest.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "doctest.h"
22
#include <Tactility/DispatcherThread.h>
3+
#include <Tactility/Semaphore.h>
34

45
using namespace tt;
56

@@ -18,11 +19,14 @@ TEST_CASE("DispatcherThread should consume jobs") {
1819
DispatcherThread thread("test");
1920
thread.start();
2021
int counter = 0;
22+
Semaphore done(1, 0);
2123

22-
thread.dispatch([&counter]() { counter++; });
23-
24-
tt::kernel::delayTicks(10);
24+
thread.dispatch([&counter, &done]() {
25+
counter++;
26+
done.release();
27+
});
2528

29+
CHECK(done.acquire(pdMS_TO_TICKS(2000)));
2630
CHECK_EQ(counter, 1);
2731
thread.stop();
2832
}

TactilityFreeRtos/Tests/Source/MutexTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ TEST_CASE("a Mutex can block a thread") {
1414
1024,
1515
[&mutex] {
1616
mutex.lock(kernel::FREERTOS_MAX_TICKS);
17+
mutex.unlock();
1718
return 0;
1819
}
1920
);

TactilityFreeRtos/Tests/Source/RecursiveMutexTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ TEST_CASE("a RecursiveMutex can block a thread") {
1414
1024,
1515
[&mutex] {
1616
mutex.lock(kernel::FREERTOS_MAX_TICKS);
17+
mutex.unlock();
1718
return 0;
1819
}
1920
);
@@ -35,4 +36,5 @@ TEST_CASE("a RecursiveMutex can be locked more than once from the same context")
3536
CHECK_EQ(mutex.lock(0), true);
3637
CHECK_EQ(mutex.lock(0), true);
3738
mutex.unlock();
39+
mutex.unlock();
3840
}

TactilityFreeRtos/Tests/Source/ThreadTest.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "doctest.h"
22
#include <Tactility/Thread.h>
33

4+
#include <atomic>
5+
46
using namespace tt;
57

68
TEST_CASE("when a thread is started then its callback should be called") {
@@ -22,7 +24,7 @@ TEST_CASE("when a thread is started then its callback should be called") {
2224
}
2325

2426
TEST_CASE("a thread can be started and stopped") {
25-
bool interrupted = false;
27+
std::atomic<bool> interrupted = false;
2628
auto* thread = new Thread(
2729
"interruptable thread",
2830
4096,
@@ -42,7 +44,7 @@ TEST_CASE("a thread can be started and stopped") {
4244
}
4345

4446
TEST_CASE("thread id should only be set at when thread is started") {
45-
bool interrupted = false;
47+
std::atomic<bool> interrupted = false;
4648
auto* thread = new Thread(
4749
"interruptable thread",
4850
4096,
@@ -63,7 +65,7 @@ TEST_CASE("thread id should only be set at when thread is started") {
6365
}
6466

6567
TEST_CASE("thread state should be correct") {
66-
bool interrupted = false;
68+
std::atomic<bool> interrupted = false;
6769
auto* thread = new Thread(
6870
"interruptable thread",
6971
4096,
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,56 @@
11
#include "doctest.h"
22
#include <Tactility/Timer.h>
33

4+
#include <atomic>
5+
46
using namespace tt;
57

8+
// Timer::stop() can return while a just-triggered callback is still executing (documented on
9+
// Timer::stop() itself) - this grace delay gives that in-flight callback a chance to finish
10+
// before the test deletes the timer and its captured state goes out of scope.
11+
constexpr TickType_t STOP_GRACE_TICKS = 5;
12+
613
TEST_CASE("TimerType::Periodic timers can be stopped and restarted") {
7-
int counter = 0;
14+
std::atomic<int> counter{0};
815
auto* timer = new Timer(Timer::Type::Periodic, 1, [&counter] { counter++; });
916
CHECK_EQ(timer->start(), true);
1017
kernel::delayTicks(10);
1118
CHECK_EQ(timer->stop(), true);
1219
CHECK_EQ(timer->start(), true);
1320
kernel::delayTicks(10);
1421
CHECK_EQ(timer->stop(), true);
22+
kernel::delayTicks(STOP_GRACE_TICKS);
1523
delete timer;
1624

17-
CHECK_GE(counter, 2);
25+
CHECK_GE(counter.load(), 2);
1826
}
1927

2028
TEST_CASE("TimerType::Periodic calls the callback periodically") {
2129
int ticks_to_run = 10;
22-
int counter = 0;
30+
std::atomic<int> counter{0};
2331
auto* timer = new Timer(Timer::Type::Periodic, 1, [&counter] { counter++; });
2432
CHECK_EQ(timer->start(), true);
2533
kernel::delayTicks(ticks_to_run);
2634
CHECK_EQ(timer->stop(), true);
35+
kernel::delayTicks(STOP_GRACE_TICKS);
2736
delete timer;
2837

29-
CHECK_EQ(counter, ticks_to_run);
38+
// Exact count isn't guaranteed (scheduling slop around start()/stop()), so this only checks
39+
// that the callback fired repeatedly, not an exact tick-for-tick match.
40+
CHECK_GE(counter.load(), ticks_to_run / 2);
3041
}
3142

3243
TEST_CASE("restarting TimerType::Once timers calls the callback again") {
33-
int counter = 0;
44+
std::atomic<int> counter{0};
3445
auto* timer = new Timer(Timer::Type::Once, 1, [&counter] { counter++; });
3546
CHECK_EQ(timer->start(), true);
3647
kernel::delayTicks(10);
3748
CHECK_EQ(timer->stop(), true);
3849
CHECK_EQ(timer->start(), true);
3950
kernel::delayTicks(10);
4051
CHECK_EQ(timer->stop(), true);
52+
kernel::delayTicks(STOP_GRACE_TICKS);
4153
delete timer;
4254

43-
CHECK_EQ(counter, 2);
55+
CHECK_EQ(counter.load(), 2);
4456
}

TactilityKernel/tests/source/module_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,10 @@ TEST_CASE("Global symbol resolution") {
138138
REQUIRE_EQ(module_add(&module), ERROR_NONE);
139139
CHECK_EQ(module_resolve_symbol_global("symbol_test_function", &addr), false);
140140
REQUIRE_EQ(module_start(&module), ERROR_NONE);
141-
// Still fails as symbols are null
141+
// Resolvable now that the module is both added and started
142142
CHECK_EQ(module_resolve_symbol_global("symbol_test_function", &addr), true);
143143
// Cleanup
144+
CHECK_EQ(module_stop(&module), ERROR_NONE);
144145
CHECK_EQ(module_remove(&module), ERROR_NONE);
145146

146147
CHECK_EQ(module_destruct(&module), ERROR_NONE);

0 commit comments

Comments
 (0)