mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-04-24 03:29:57 -07:00
Merge remote-tracking branch 'ofw/dev' into mntm-dev
This commit is contained in:
@@ -236,3 +236,11 @@ App(
|
||||
entry_point="get_api",
|
||||
requires=["unit_tests"],
|
||||
)
|
||||
|
||||
App(
|
||||
appid="test_pipe",
|
||||
sources=["tests/common/*.c", "tests/pipe/*.c"],
|
||||
apptype=FlipperAppType.PLUGIN,
|
||||
entry_point="get_api",
|
||||
requires=["unit_tests"],
|
||||
)
|
||||
|
||||
@@ -265,6 +265,7 @@ static bool test_write(const char* file_name) {
|
||||
if(!flipper_format_file_open_always(file, file_name)) break;
|
||||
if(!flipper_format_write_header_cstr(file, test_filetype, test_version)) break;
|
||||
if(!flipper_format_write_comment_cstr(file, "This is comment")) break;
|
||||
if(!flipper_format_write_empty_line(file)) break;
|
||||
if(!flipper_format_write_string_cstr(file, test_string_key, test_string_data)) break;
|
||||
if(!flipper_format_write_int32(file, test_int_key, test_int_data, COUNT_OF(test_int_data)))
|
||||
break;
|
||||
|
||||
108
applications/debug/unit_tests/tests/furi/furi_stdio_test.c
Normal file
108
applications/debug/unit_tests/tests/furi/furi_stdio_test.c
Normal file
@@ -0,0 +1,108 @@
|
||||
#include <furi.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include "../test.h" // IWYU pragma: keep
|
||||
|
||||
#define TAG "StdioTest"
|
||||
|
||||
#define CONTEXT_MAGIC ((void*)0xDEADBEEF)
|
||||
|
||||
// stdin
|
||||
|
||||
static char mock_in[256];
|
||||
static size_t mock_in_len, mock_in_pos;
|
||||
|
||||
static void set_mock_in(const char* str) {
|
||||
size_t len = strlen(str);
|
||||
strcpy(mock_in, str);
|
||||
mock_in_len = len;
|
||||
mock_in_pos = 0;
|
||||
}
|
||||
|
||||
static size_t mock_in_cb(char* buffer, size_t size, FuriWait wait, void* context) {
|
||||
UNUSED(wait);
|
||||
furi_check(context == CONTEXT_MAGIC);
|
||||
size_t remaining = mock_in_len - mock_in_pos;
|
||||
size = MIN(remaining, size);
|
||||
memcpy(buffer, mock_in + mock_in_pos, size);
|
||||
mock_in_pos += size;
|
||||
return size;
|
||||
}
|
||||
|
||||
void test_stdin(void) {
|
||||
FuriThreadStdinReadCallback in_cb = furi_thread_get_stdin_callback();
|
||||
furi_thread_set_stdin_callback(mock_in_cb, CONTEXT_MAGIC);
|
||||
char buf[256];
|
||||
|
||||
// plain in
|
||||
set_mock_in("Hello, World!\n");
|
||||
fgets(buf, sizeof(buf), stdin);
|
||||
mu_assert_string_eq("Hello, World!\n", buf);
|
||||
mu_assert_int_eq(EOF, getchar());
|
||||
|
||||
// ungetc
|
||||
ungetc('i', stdin);
|
||||
ungetc('H', stdin);
|
||||
fgets(buf, sizeof(buf), stdin);
|
||||
mu_assert_string_eq("Hi", buf);
|
||||
mu_assert_int_eq(EOF, getchar());
|
||||
|
||||
// ungetc + plain in
|
||||
set_mock_in(" World");
|
||||
ungetc('i', stdin);
|
||||
ungetc('H', stdin);
|
||||
fgets(buf, sizeof(buf), stdin);
|
||||
mu_assert_string_eq("Hi World", buf);
|
||||
mu_assert_int_eq(EOF, getchar());
|
||||
|
||||
// partial plain in
|
||||
set_mock_in("Hello, World!\n");
|
||||
fgets(buf, strlen("Hello") + 1, stdin);
|
||||
mu_assert_string_eq("Hello", buf);
|
||||
mu_assert_int_eq(',', getchar());
|
||||
fgets(buf, sizeof(buf), stdin);
|
||||
mu_assert_string_eq(" World!\n", buf);
|
||||
|
||||
furi_thread_set_stdin_callback(in_cb, CONTEXT_MAGIC);
|
||||
}
|
||||
|
||||
// stdout
|
||||
|
||||
static FuriString* mock_out;
|
||||
FuriThreadStdoutWriteCallback original_out_cb;
|
||||
|
||||
static void mock_out_cb(const char* data, size_t size, void* context) {
|
||||
furi_check(context == CONTEXT_MAGIC);
|
||||
// there's no furi_string_cat_strn :(
|
||||
for(size_t i = 0; i < size; i++) {
|
||||
furi_string_push_back(mock_out, data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void assert_and_clear_mock_out(const char* expected) {
|
||||
// return the original stdout callback for the duration of the check
|
||||
// if the check fails, we don't want the error to end up in our buffer,
|
||||
// we want to be able to see it!
|
||||
furi_thread_set_stdout_callback(original_out_cb, CONTEXT_MAGIC);
|
||||
mu_assert_string_eq(expected, furi_string_get_cstr(mock_out));
|
||||
furi_thread_set_stdout_callback(mock_out_cb, CONTEXT_MAGIC);
|
||||
|
||||
furi_string_reset(mock_out);
|
||||
}
|
||||
|
||||
void test_stdout(void) {
|
||||
original_out_cb = furi_thread_get_stdout_callback();
|
||||
furi_thread_set_stdout_callback(mock_out_cb, CONTEXT_MAGIC);
|
||||
mock_out = furi_string_alloc();
|
||||
|
||||
puts("Hello, World!");
|
||||
assert_and_clear_mock_out("Hello, World!\n");
|
||||
|
||||
printf("He");
|
||||
printf("llo!");
|
||||
fflush(stdout);
|
||||
assert_and_clear_mock_out("Hello!");
|
||||
|
||||
furi_string_free(mock_out);
|
||||
furi_thread_set_stdout_callback(original_out_cb, CONTEXT_MAGIC);
|
||||
}
|
||||
@@ -10,6 +10,8 @@ void test_furi_memmgr(void);
|
||||
void test_furi_event_loop(void);
|
||||
void test_errno_saving(void);
|
||||
void test_furi_primitives(void);
|
||||
void test_stdin(void);
|
||||
void test_stdout(void);
|
||||
|
||||
static int foo = 0;
|
||||
|
||||
@@ -52,6 +54,11 @@ MU_TEST(mu_test_furi_primitives) {
|
||||
test_furi_primitives();
|
||||
}
|
||||
|
||||
MU_TEST(mu_test_stdio) {
|
||||
test_stdin();
|
||||
test_stdout();
|
||||
}
|
||||
|
||||
MU_TEST_SUITE(test_suite) {
|
||||
MU_SUITE_CONFIGURE(&test_setup, &test_teardown);
|
||||
MU_RUN_TEST(test_check);
|
||||
@@ -61,6 +68,7 @@ MU_TEST_SUITE(test_suite) {
|
||||
MU_RUN_TEST(mu_test_furi_pubsub);
|
||||
MU_RUN_TEST(mu_test_furi_memmgr);
|
||||
MU_RUN_TEST(mu_test_furi_event_loop);
|
||||
MU_RUN_TEST(mu_test_stdio);
|
||||
MU_RUN_TEST(mu_test_errno_saving);
|
||||
MU_RUN_TEST(mu_test_furi_primitives);
|
||||
}
|
||||
|
||||
153
applications/debug/unit_tests/tests/pipe/pipe_test.c
Normal file
153
applications/debug/unit_tests/tests/pipe/pipe_test.c
Normal file
@@ -0,0 +1,153 @@
|
||||
#include "../test.h" // IWYU pragma: keep
|
||||
|
||||
#include <furi.h>
|
||||
#include <lib/toolbox/pipe.h>
|
||||
|
||||
#define PIPE_SIZE 128U
|
||||
#define PIPE_TRG_LEVEL 1U
|
||||
|
||||
MU_TEST(pipe_test_trivial) {
|
||||
PipeSideBundle bundle = pipe_alloc(PIPE_SIZE, PIPE_TRG_LEVEL);
|
||||
PipeSide* alice = bundle.alices_side;
|
||||
PipeSide* bob = bundle.bobs_side;
|
||||
|
||||
mu_assert_int_eq(PipeRoleAlice, pipe_role(alice));
|
||||
mu_assert_int_eq(PipeRoleBob, pipe_role(bob));
|
||||
mu_assert_int_eq(PipeStateOpen, pipe_state(alice));
|
||||
mu_assert_int_eq(PipeStateOpen, pipe_state(bob));
|
||||
|
||||
mu_assert_int_eq(PIPE_SIZE, pipe_spaces_available(alice));
|
||||
mu_assert_int_eq(PIPE_SIZE, pipe_spaces_available(bob));
|
||||
mu_assert_int_eq(0, pipe_bytes_available(alice));
|
||||
mu_assert_int_eq(0, pipe_bytes_available(bob));
|
||||
|
||||
for(uint8_t i = 0;; ++i) {
|
||||
mu_assert_int_eq(PIPE_SIZE - i, pipe_spaces_available(alice));
|
||||
mu_assert_int_eq(i, pipe_bytes_available(bob));
|
||||
|
||||
if(pipe_send(alice, &i, sizeof(uint8_t), 0) != sizeof(uint8_t)) {
|
||||
break;
|
||||
}
|
||||
|
||||
mu_assert_int_eq(PIPE_SIZE - i, pipe_spaces_available(bob));
|
||||
mu_assert_int_eq(i, pipe_bytes_available(alice));
|
||||
|
||||
if(pipe_send(bob, &i, sizeof(uint8_t), 0) != sizeof(uint8_t)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pipe_free(alice);
|
||||
mu_assert_int_eq(PipeStateBroken, pipe_state(bob));
|
||||
|
||||
for(uint8_t i = 0;; ++i) {
|
||||
mu_assert_int_eq(PIPE_SIZE - i, pipe_bytes_available(bob));
|
||||
|
||||
uint8_t value;
|
||||
if(pipe_receive(bob, &value, sizeof(uint8_t), 0) != sizeof(uint8_t)) {
|
||||
break;
|
||||
}
|
||||
|
||||
mu_assert_int_eq(i, value);
|
||||
}
|
||||
|
||||
pipe_free(bob);
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
TestFlagDataArrived = 1 << 0,
|
||||
TestFlagSpaceFreed = 1 << 1,
|
||||
TestFlagBecameBroken = 1 << 2,
|
||||
} TestFlag;
|
||||
|
||||
typedef struct {
|
||||
TestFlag flag;
|
||||
FuriEventLoop* event_loop;
|
||||
} AncillaryThreadContext;
|
||||
|
||||
static void on_data_arrived(PipeSide* pipe, void* context) {
|
||||
AncillaryThreadContext* ctx = context;
|
||||
ctx->flag |= TestFlagDataArrived;
|
||||
uint8_t buffer[PIPE_SIZE];
|
||||
size_t size = pipe_receive(pipe, buffer, sizeof(buffer), 0);
|
||||
pipe_send(pipe, buffer, size, 0);
|
||||
}
|
||||
|
||||
static void on_space_freed(PipeSide* pipe, void* context) {
|
||||
AncillaryThreadContext* ctx = context;
|
||||
ctx->flag |= TestFlagSpaceFreed;
|
||||
const char* message = "Hi!";
|
||||
pipe_send(pipe, message, strlen(message), 0);
|
||||
}
|
||||
|
||||
static void on_became_broken(PipeSide* pipe, void* context) {
|
||||
UNUSED(pipe);
|
||||
AncillaryThreadContext* ctx = context;
|
||||
ctx->flag |= TestFlagBecameBroken;
|
||||
furi_event_loop_stop(ctx->event_loop);
|
||||
}
|
||||
|
||||
static int32_t ancillary_thread(void* context) {
|
||||
PipeSide* pipe = context;
|
||||
AncillaryThreadContext thread_ctx = {
|
||||
.flag = 0,
|
||||
.event_loop = furi_event_loop_alloc(),
|
||||
};
|
||||
|
||||
pipe_attach_to_event_loop(pipe, thread_ctx.event_loop);
|
||||
pipe_set_callback_context(pipe, &thread_ctx);
|
||||
pipe_set_data_arrived_callback(pipe, on_data_arrived, 0);
|
||||
pipe_set_space_freed_callback(pipe, on_space_freed, FuriEventLoopEventFlagEdge);
|
||||
pipe_set_broken_callback(pipe, on_became_broken, 0);
|
||||
|
||||
furi_event_loop_run(thread_ctx.event_loop);
|
||||
|
||||
pipe_detach_from_event_loop(pipe);
|
||||
pipe_free(pipe);
|
||||
furi_event_loop_free(thread_ctx.event_loop);
|
||||
return thread_ctx.flag;
|
||||
}
|
||||
|
||||
MU_TEST(pipe_test_event_loop) {
|
||||
PipeSideBundle bundle = pipe_alloc(PIPE_SIZE, PIPE_TRG_LEVEL);
|
||||
PipeSide* alice = bundle.alices_side;
|
||||
PipeSide* bob = bundle.bobs_side;
|
||||
|
||||
FuriThread* thread = furi_thread_alloc_ex("PipeTestAnc", 2048, ancillary_thread, bob);
|
||||
furi_thread_start(thread);
|
||||
|
||||
const char* message = "Hello!";
|
||||
pipe_send(alice, message, strlen(message), FuriWaitForever);
|
||||
|
||||
char buffer_1[16];
|
||||
size_t size = pipe_receive(alice, buffer_1, sizeof(buffer_1), FuriWaitForever);
|
||||
buffer_1[size] = 0;
|
||||
|
||||
char buffer_2[16];
|
||||
const char* expected_reply = "Hi!";
|
||||
size = pipe_receive(alice, buffer_2, sizeof(buffer_2), FuriWaitForever);
|
||||
buffer_2[size] = 0;
|
||||
|
||||
pipe_free(alice);
|
||||
furi_thread_join(thread);
|
||||
|
||||
mu_assert_string_eq(message, buffer_1);
|
||||
mu_assert_string_eq(expected_reply, buffer_2);
|
||||
mu_assert_int_eq(
|
||||
TestFlagDataArrived | TestFlagSpaceFreed | TestFlagBecameBroken,
|
||||
furi_thread_get_return_code(thread));
|
||||
|
||||
furi_thread_free(thread);
|
||||
}
|
||||
|
||||
MU_TEST_SUITE(test_pipe) {
|
||||
MU_RUN_TEST(pipe_test_trivial);
|
||||
MU_RUN_TEST(pipe_test_event_loop);
|
||||
}
|
||||
|
||||
int run_minunit_test_pipe(void) {
|
||||
MU_RUN_SUITE(test_pipe);
|
||||
return MU_EXIT_CODE;
|
||||
}
|
||||
|
||||
TEST_API_DEFINE(run_minunit_test_pipe)
|
||||
Reference in New Issue
Block a user