mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-02-20 07:25:06 +00:00
Fixes and tests
This commit is contained in:
parent
3e0dd608da
commit
1a7d603a64
@ -8,7 +8,7 @@
|
|||||||
#define TAG LOG_TAG(esp32_i2c)
|
#define TAG LOG_TAG(esp32_i2c)
|
||||||
|
|
||||||
struct InternalData {
|
struct InternalData {
|
||||||
Mutex mutex {};
|
Mutex mutex { 0 };
|
||||||
|
|
||||||
InternalData() {
|
InternalData() {
|
||||||
mutex_construct(&mutex);
|
mutex_construct(&mutex);
|
||||||
|
|||||||
@ -154,8 +154,21 @@ static inline void device_unlock(struct Device* device) {
|
|||||||
static inline const struct DeviceType* device_get_type(struct Device* device) {
|
static inline const struct DeviceType* device_get_type(struct Device* device) {
|
||||||
return device->internal.driver->device_type;
|
return device->internal.driver->device_type;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Iterate through all the known devices
|
||||||
|
* @param callback_context the parameter to pass to the callback. NULL is valid.
|
||||||
|
* @param on_device the function to call for each filtered device. return true to continue iterating or false to stop.
|
||||||
|
*/
|
||||||
|
void for_each_device(void* callback_context, bool(*on_device)(struct Device* device, void* context));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Iterate through all the child devices of the specified device
|
||||||
|
* @param callback_context the parameter to pass to the callback. NULL is valid.
|
||||||
|
* @param on_device the function to call for each filtered device. return true to continue iterating or false to stop.
|
||||||
|
*/
|
||||||
|
void for_each_device_child(struct Device* device, void* callback_context, bool(*on_device)(struct Device* device, void* context));
|
||||||
|
|
||||||
|
/**
|
||||||
* Iterate through all the known devices of a specific type
|
* Iterate through all the known devices of a specific type
|
||||||
* @param type the type to filter
|
* @param type the type to filter
|
||||||
* @param callback_context the parameter to pass to the callback. NULL is valid.
|
* @param callback_context the parameter to pass to the callback. NULL is valid.
|
||||||
|
|||||||
@ -17,7 +17,7 @@ struct DeviceData {
|
|||||||
|
|
||||||
struct DeviceLedger {
|
struct DeviceLedger {
|
||||||
std::vector<Device*> devices;
|
std::vector<Device*> devices;
|
||||||
Mutex mutex {};
|
Mutex mutex { 0 };
|
||||||
|
|
||||||
DeviceLedger() {
|
DeviceLedger() {
|
||||||
mutex_construct(&mutex);
|
mutex_construct(&mutex);
|
||||||
@ -54,7 +54,8 @@ int device_destruct(Device* device) {
|
|||||||
/** Add a child to the list of children */
|
/** Add a child to the list of children */
|
||||||
static void device_add_child(struct Device* device, struct Device* child) {
|
static void device_add_child(struct Device* device, struct Device* child) {
|
||||||
device_lock(device);
|
device_lock(device);
|
||||||
get_device_data(device)->children.push_back(device);
|
assert(device->internal.state.added);
|
||||||
|
get_device_data(device)->children.push_back(child);
|
||||||
device_unlock(device);
|
device_unlock(device);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,6 +176,25 @@ void device_set_parent(Device* device, Device* parent) {
|
|||||||
device->parent = parent;
|
device->parent = parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void for_each_device(void* callback_context, bool(*on_device)(Device* device, void* context)) {
|
||||||
|
ledger_lock();
|
||||||
|
for (auto* device : ledger.devices) {
|
||||||
|
if (!on_device(device, callback_context)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ledger_unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
void for_each_device_child(Device* device, void* callback_context, bool(*on_device)(struct Device* device, void* context)) {
|
||||||
|
auto* data = get_device_data(device);
|
||||||
|
for (auto* child_device : data->children) {
|
||||||
|
if (!on_device(child_device, callback_context)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void for_each_device_of_type(const DeviceType* type, void* callback_context, bool(*on_device)(Device* device, void* context)) {
|
void for_each_device_of_type(const DeviceType* type, void* callback_context, bool(*on_device)(Device* device, void* context)) {
|
||||||
ledger_lock();
|
ledger_lock();
|
||||||
for (auto* device : ledger.devices) {
|
for (auto* device : ledger.devices) {
|
||||||
|
|||||||
@ -11,7 +11,7 @@
|
|||||||
#define TAG LOG_TAG(driver)
|
#define TAG LOG_TAG(driver)
|
||||||
|
|
||||||
struct DriverInternalData {
|
struct DriverInternalData {
|
||||||
Mutex mutex {};
|
Mutex mutex { 0 };
|
||||||
int use_count = 0;
|
int use_count = 0;
|
||||||
|
|
||||||
DriverInternalData() {
|
DriverInternalData() {
|
||||||
@ -25,7 +25,7 @@ struct DriverInternalData {
|
|||||||
|
|
||||||
struct DriverLedger {
|
struct DriverLedger {
|
||||||
std::vector<Driver*> drivers = {};
|
std::vector<Driver*> drivers = {};
|
||||||
Mutex mutex {};
|
Mutex mutex { 0 };
|
||||||
|
|
||||||
DriverLedger() {
|
DriverLedger() {
|
||||||
mutex_construct(&mutex);
|
mutex_construct(&mutex);
|
||||||
|
|||||||
@ -5,9 +5,11 @@ set(DOCTESTINC ${PROJECT_SOURCE_DIR}/Include)
|
|||||||
enable_testing()
|
enable_testing()
|
||||||
add_subdirectory(TactilityCore)
|
add_subdirectory(TactilityCore)
|
||||||
add_subdirectory(TactilityFreeRtos)
|
add_subdirectory(TactilityFreeRtos)
|
||||||
|
add_subdirectory(TactilityKernel)
|
||||||
add_subdirectory(Tactility)
|
add_subdirectory(Tactility)
|
||||||
|
|
||||||
add_custom_target(build-tests)
|
add_custom_target(build-tests)
|
||||||
add_dependencies(build-tests TactilityCoreTests)
|
add_dependencies(build-tests TactilityCoreTests)
|
||||||
add_dependencies(build-tests TactilityFreeRtosTests)
|
add_dependencies(build-tests TactilityFreeRtosTests)
|
||||||
add_dependencies(build-tests TactilityTests)
|
add_dependencies(build-tests TactilityTests)
|
||||||
|
add_dependencies(build-tests TactilityKernelTests)
|
||||||
|
|||||||
14
Tests/TactilityKernel/CMakeLists.txt
Normal file
14
Tests/TactilityKernel/CMakeLists.txt
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
project(TactilityCoreTests)
|
||||||
|
|
||||||
|
enable_language(C CXX ASM)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_COMPILER g++)
|
||||||
|
|
||||||
|
file(GLOB_RECURSE TEST_SOURCES ${PROJECT_SOURCE_DIR}/*.cpp)
|
||||||
|
add_executable(TactilityKernelTests EXCLUDE_FROM_ALL ${TEST_SOURCES})
|
||||||
|
|
||||||
|
target_include_directories(TactilityKernelTests PRIVATE ${DOCTESTINC})
|
||||||
|
|
||||||
|
add_test(NAME TactilityKernelTests COMMAND TactilityKernelTests)
|
||||||
|
|
||||||
|
target_link_libraries(TactilityKernelTests PUBLIC TactilityKernel)
|
||||||
92
Tests/TactilityKernel/DeviceTest.cpp
Normal file
92
Tests/TactilityKernel/DeviceTest.cpp
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
#include "doctest.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <Tactility/Device.h>
|
||||||
|
|
||||||
|
TEST_CASE("device_construct and device_destruct should set and unset the correct fields") {
|
||||||
|
Device device = { 0 };
|
||||||
|
|
||||||
|
device_construct(&device);
|
||||||
|
|
||||||
|
CHECK_NE(device.internal.data, nullptr);
|
||||||
|
CHECK_NE(device.internal.mutex.handle, nullptr);
|
||||||
|
|
||||||
|
device_destruct(&device);
|
||||||
|
|
||||||
|
CHECK_EQ(device.internal.data, nullptr);
|
||||||
|
CHECK_EQ(device.internal.mutex.handle, nullptr);
|
||||||
|
|
||||||
|
Device comparison_device = { 0 };
|
||||||
|
comparison_device.internal.data = device.internal.data;
|
||||||
|
comparison_device.internal.mutex.handle = device.internal.mutex.handle;
|
||||||
|
|
||||||
|
// Check that no other data was set
|
||||||
|
CHECK_EQ(memcmp(&device, &comparison_device, sizeof(struct Device)), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("device_add should make the device discoverable") {
|
||||||
|
Device device = { 0 };
|
||||||
|
device_construct(&device);
|
||||||
|
device_add(&device);
|
||||||
|
|
||||||
|
// Gather all devices
|
||||||
|
std::vector<Device*> devices;
|
||||||
|
for_each_device(&devices, [](auto* device, auto* context) {
|
||||||
|
auto* devices_ptr = (std::vector<Device*>*)context;
|
||||||
|
devices_ptr->push_back(device);
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
CHECK_EQ(devices.size(), 1);
|
||||||
|
CHECK_EQ(devices[0], &device);
|
||||||
|
|
||||||
|
device_remove(&device);
|
||||||
|
device_destruct(&device);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("device_add should add the device to its parent") {
|
||||||
|
Device parent = { 0 };
|
||||||
|
|
||||||
|
Device child = {
|
||||||
|
.name = nullptr,
|
||||||
|
.config = nullptr,
|
||||||
|
.parent = &parent
|
||||||
|
};
|
||||||
|
|
||||||
|
device_construct(&parent);
|
||||||
|
device_add(&parent);
|
||||||
|
|
||||||
|
device_construct(&child);
|
||||||
|
device_add(&child);
|
||||||
|
|
||||||
|
// Gather all child devices
|
||||||
|
std::vector<Device*> children;
|
||||||
|
for_each_device_child(&parent, &children, [](auto* child_device, auto* context) {
|
||||||
|
auto* children_ptr = (std::vector<Device*>*)context;
|
||||||
|
children_ptr->push_back(child_device);
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
CHECK_EQ(children.size(), 1);
|
||||||
|
CHECK_EQ(children[0], &child);
|
||||||
|
|
||||||
|
device_remove(&child);
|
||||||
|
device_destruct(&child);
|
||||||
|
|
||||||
|
device_remove(&parent);
|
||||||
|
device_destruct(&parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("device_add should set the state to 'added'") {
|
||||||
|
Device device = { 0 };
|
||||||
|
device_construct(&device);
|
||||||
|
|
||||||
|
CHECK_EQ(device.internal.state.added, false);
|
||||||
|
device_add(&device);
|
||||||
|
CHECK_EQ(device.internal.state.added, true);
|
||||||
|
|
||||||
|
device_remove(&device);
|
||||||
|
device_destruct(&device);
|
||||||
|
}
|
||||||
6
Tests/TactilityKernel/DriverTest.cpp
Normal file
6
Tests/TactilityKernel/DriverTest.cpp
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#include "doctest.h"
|
||||||
|
#include <Tactility/Driver.h>
|
||||||
|
|
||||||
|
TEST_CASE("placeholder driver test") {
|
||||||
|
// TODO: Implement
|
||||||
|
}
|
||||||
59
Tests/TactilityKernel/Main.cpp
Normal file
59
Tests/TactilityKernel/Main.cpp
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
#define DOCTEST_CONFIG_IMPLEMENT
|
||||||
|
#include "doctest.h"
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
#include <Tactility/FreeRTOS/task.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int argc;
|
||||||
|
char** argv;
|
||||||
|
int result;
|
||||||
|
} TestTaskData;
|
||||||
|
|
||||||
|
void test_task(void* parameter) {
|
||||||
|
auto* data = (TestTaskData*)parameter;
|
||||||
|
|
||||||
|
doctest::Context context;
|
||||||
|
|
||||||
|
context.applyCommandLine(data->argc, data->argv);
|
||||||
|
|
||||||
|
// overrides
|
||||||
|
context.setOption("no-breaks", true); // don't break in the debugger when assertions fail
|
||||||
|
|
||||||
|
data->result = context.run();
|
||||||
|
|
||||||
|
if (context.shouldExit()) { // important - query flags (and --exit) rely on the user doing this
|
||||||
|
vTaskEndScheduler();
|
||||||
|
}
|
||||||
|
|
||||||
|
vTaskDelete(nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
TestTaskData data = {
|
||||||
|
.argc = argc,
|
||||||
|
.argv = argv,
|
||||||
|
.result = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
BaseType_t task_result = xTaskCreate(
|
||||||
|
test_task,
|
||||||
|
"test_task",
|
||||||
|
8192,
|
||||||
|
&data,
|
||||||
|
1,
|
||||||
|
nullptr
|
||||||
|
);
|
||||||
|
assert(task_result == pdPASS);
|
||||||
|
|
||||||
|
vTaskStartScheduler();
|
||||||
|
|
||||||
|
return data.result;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
// Required for FreeRTOS
|
||||||
|
void vAssertCalled(unsigned long line, const char* const file) {
|
||||||
|
__assert_fail("assert failed", file, line, "");
|
||||||
|
}
|
||||||
|
}
|
||||||
42
Tests/TactilityKernel/TestFile.h
Normal file
42
Tests/TactilityKernel/TestFile.h
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "doctest.h"
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <Tactility/file/File.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A class for creating test files that can automatically clean themselves up.
|
||||||
|
*/
|
||||||
|
class TestFile {
|
||||||
|
const char* path;
|
||||||
|
bool autoClean;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
TestFile(const char* path, bool autoClean = true) : path(path), autoClean(autoClean) {
|
||||||
|
if (autoClean && exists()) {
|
||||||
|
remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~TestFile() {
|
||||||
|
if (autoClean && exists()) {
|
||||||
|
remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* getPath() const { return path; }
|
||||||
|
|
||||||
|
void writeData(const char* data) const {
|
||||||
|
CHECK_EQ(tt::file::writeString(path, data), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool exists() const {
|
||||||
|
return access(path, F_OK) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove() const {
|
||||||
|
::remove(path);
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user