mirror of
https://github.com/DarkFlippers/unleashed-firmware.git
synced 2025-12-13 05:06:30 +04:00
cli: Buzzer command (#4006)
* Add args_read_float_and_trim function * Add args_read_duration function * Add notes_frequency_from_name function * Add cli_sleep function and sleep CLI command * Update CLI top command to use cli_sleep * Add buzzer CLI command * toolbox: make args_read_duration less convoluted * notification: make notification_messages_notes_frequency_from_name less convoluted * unit_tests: better float checking * fix formatting and f18 --------- Co-authored-by: Anna Antonenko <portasynthinca3@gmail.com> Co-authored-by: hedger <hedger@nanode.su>
This commit is contained in:
@@ -244,3 +244,20 @@ App(
|
||||
entry_point="get_api",
|
||||
requires=["unit_tests"],
|
||||
)
|
||||
|
||||
App(
|
||||
appid="test_args",
|
||||
sources=["tests/common/*.c", "tests/args/*.c"],
|
||||
apptype=FlipperAppType.PLUGIN,
|
||||
entry_point="get_api",
|
||||
requires=["unit_tests"],
|
||||
)
|
||||
|
||||
|
||||
App(
|
||||
appid="test_notification",
|
||||
sources=["tests/common/*.c", "tests/notification/*.c"],
|
||||
apptype=FlipperAppType.PLUGIN,
|
||||
entry_point="get_api",
|
||||
requires=["unit_tests"],
|
||||
)
|
||||
|
||||
211
applications/debug/unit_tests/tests/args/args_test.c
Normal file
211
applications/debug/unit_tests/tests/args/args_test.c
Normal file
@@ -0,0 +1,211 @@
|
||||
|
||||
#include "../test.h" // IWYU pragma: keep
|
||||
#include <toolbox/args.h>
|
||||
|
||||
const uint32_t one_ms = 1;
|
||||
const uint32_t one_s = 1000 * one_ms;
|
||||
const uint32_t one_m = 60 * one_s;
|
||||
const uint32_t one_h = 60 * one_m;
|
||||
|
||||
MU_TEST(args_read_duration_default_values_test) {
|
||||
FuriString* args_string;
|
||||
uint32_t value = 0;
|
||||
|
||||
// Check default == NULL (ms)
|
||||
args_string = furi_string_alloc_set_str("1");
|
||||
mu_check(args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, one_ms);
|
||||
furi_string_free(args_string);
|
||||
value = 0;
|
||||
|
||||
// Check default == ms
|
||||
args_string = furi_string_alloc_set_str("1");
|
||||
mu_check(args_read_duration(args_string, &value, "ms"));
|
||||
mu_assert_int_eq(value, one_ms);
|
||||
furi_string_free(args_string);
|
||||
value = 0;
|
||||
|
||||
// Check default == s
|
||||
args_string = furi_string_alloc_set_str("1");
|
||||
mu_check(args_read_duration(args_string, &value, "s"));
|
||||
mu_assert_int_eq(value, one_s);
|
||||
furi_string_free(args_string);
|
||||
value = 0;
|
||||
|
||||
// Check default == m
|
||||
args_string = furi_string_alloc_set_str("1");
|
||||
mu_check(args_read_duration(args_string, &value, "m"));
|
||||
mu_assert_int_eq(value, one_m);
|
||||
furi_string_free(args_string);
|
||||
value = 0;
|
||||
|
||||
// Check default == h
|
||||
args_string = furi_string_alloc_set_str("1");
|
||||
mu_check(args_read_duration(args_string, &value, "h"));
|
||||
mu_assert_int_eq(value, one_h);
|
||||
furi_string_free(args_string);
|
||||
value = 0;
|
||||
}
|
||||
|
||||
MU_TEST(args_read_duration_suffix_values_test) {
|
||||
FuriString* args_string;
|
||||
uint32_t value = 0;
|
||||
|
||||
// Check ms
|
||||
args_string = furi_string_alloc_set_str("1ms");
|
||||
mu_check(args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, one_ms);
|
||||
furi_string_free(args_string);
|
||||
value = 0;
|
||||
|
||||
// Check s
|
||||
args_string = furi_string_alloc_set_str("1s");
|
||||
mu_check(args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, one_s);
|
||||
furi_string_free(args_string);
|
||||
value = 0;
|
||||
|
||||
// Check m
|
||||
args_string = furi_string_alloc_set_str("1m");
|
||||
mu_check(args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, one_m);
|
||||
furi_string_free(args_string);
|
||||
value = 0;
|
||||
|
||||
// Check h
|
||||
args_string = furi_string_alloc_set_str("1h");
|
||||
mu_check(args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, one_h);
|
||||
furi_string_free(args_string);
|
||||
value = 0;
|
||||
}
|
||||
|
||||
MU_TEST(args_read_duration_values_test) {
|
||||
FuriString* args_string;
|
||||
uint32_t value = 0;
|
||||
|
||||
// Check for ms
|
||||
args_string = furi_string_alloc_set_str("4294967295ms");
|
||||
mu_check(args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, 4294967295U);
|
||||
furi_string_free(args_string);
|
||||
|
||||
// Check for s
|
||||
args_string = furi_string_alloc_set_str("4294967s");
|
||||
mu_check(args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, 4294967U * one_s);
|
||||
furi_string_free(args_string);
|
||||
|
||||
// Check for m
|
||||
args_string = furi_string_alloc_set_str("71582m");
|
||||
mu_check(args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, 71582U * one_m);
|
||||
furi_string_free(args_string);
|
||||
|
||||
// Check for h
|
||||
args_string = furi_string_alloc_set_str("1193h");
|
||||
mu_check(args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, 1193U * one_h);
|
||||
furi_string_free(args_string);
|
||||
|
||||
// Check for ms in float
|
||||
args_string = furi_string_alloc_set_str("4.2ms");
|
||||
mu_check(args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, 4);
|
||||
furi_string_free(args_string);
|
||||
|
||||
// Check for s in float
|
||||
args_string = furi_string_alloc_set_str("1.5s");
|
||||
mu_check(args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, (uint32_t)(1.5 * one_s));
|
||||
furi_string_free(args_string);
|
||||
|
||||
// Check for m in float
|
||||
args_string = furi_string_alloc_set_str("1.5m");
|
||||
mu_check(args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, (uint32_t)(1.5 * one_m));
|
||||
furi_string_free(args_string);
|
||||
|
||||
// Check for h in float
|
||||
args_string = furi_string_alloc_set_str("1.5h");
|
||||
mu_check(args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, (uint32_t)(1.5 * one_h));
|
||||
furi_string_free(args_string);
|
||||
}
|
||||
|
||||
MU_TEST(args_read_duration_errors_test) {
|
||||
FuriString* args_string;
|
||||
uint32_t value = 0;
|
||||
|
||||
// Check wrong suffix
|
||||
args_string = furi_string_alloc_set_str("1x");
|
||||
mu_check(!args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, 0);
|
||||
furi_string_free(args_string);
|
||||
|
||||
// Check wrong suffix
|
||||
args_string = furi_string_alloc_set_str("1xs");
|
||||
mu_check(!args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, 0);
|
||||
furi_string_free(args_string);
|
||||
|
||||
// Check negative value
|
||||
args_string = furi_string_alloc_set_str("-1s");
|
||||
mu_check(!args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, 0);
|
||||
furi_string_free(args_string);
|
||||
|
||||
// Check wrong values
|
||||
|
||||
// Check only suffix
|
||||
args_string = furi_string_alloc_set_str("s");
|
||||
mu_check(!args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, 0);
|
||||
furi_string_free(args_string);
|
||||
|
||||
// Check doubled point
|
||||
args_string = furi_string_alloc_set_str("0.1.1");
|
||||
mu_check(!args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, 0);
|
||||
furi_string_free(args_string);
|
||||
|
||||
// Check overflow values
|
||||
|
||||
// Check for ms
|
||||
args_string = furi_string_alloc_set_str("4294967296ms");
|
||||
mu_check(!args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, 0);
|
||||
furi_string_free(args_string);
|
||||
|
||||
// Check for s
|
||||
args_string = furi_string_alloc_set_str("4294968s");
|
||||
mu_check(!args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, 0);
|
||||
furi_string_free(args_string);
|
||||
|
||||
// Check for m
|
||||
args_string = furi_string_alloc_set_str("71583m");
|
||||
mu_check(!args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, 0);
|
||||
furi_string_free(args_string);
|
||||
|
||||
// Check for h
|
||||
args_string = furi_string_alloc_set_str("1194h");
|
||||
mu_check(!args_read_duration(args_string, &value, NULL));
|
||||
mu_assert_int_eq(value, 0);
|
||||
furi_string_free(args_string);
|
||||
}
|
||||
|
||||
MU_TEST_SUITE(toolbox_args_read_duration_suite) {
|
||||
MU_RUN_TEST(args_read_duration_default_values_test);
|
||||
MU_RUN_TEST(args_read_duration_suffix_values_test);
|
||||
MU_RUN_TEST(args_read_duration_values_test);
|
||||
MU_RUN_TEST(args_read_duration_errors_test);
|
||||
}
|
||||
|
||||
int run_minunit_test_toolbox_args(void) {
|
||||
MU_RUN_SUITE(toolbox_args_read_duration_suite);
|
||||
return MU_EXIT_CODE;
|
||||
}
|
||||
|
||||
TEST_API_DEFINE(run_minunit_test_toolbox_args)
|
||||
@@ -389,8 +389,8 @@ void minunit_printf_warning(const char* format, ...);
|
||||
__func__, \
|
||||
__FILE__, \
|
||||
__LINE__, \
|
||||
minunit_tmp_e, \
|
||||
minunit_tmp_r, \
|
||||
minunit_tmp_e, \
|
||||
minunit_tmp_m); \
|
||||
minunit_status = 1; \
|
||||
return; \
|
||||
|
||||
165
applications/debug/unit_tests/tests/notification/notes_test.c
Normal file
165
applications/debug/unit_tests/tests/notification/notes_test.c
Normal file
@@ -0,0 +1,165 @@
|
||||
#include "../test.h" // IWYU pragma: keep
|
||||
#include <float_tools.h>
|
||||
#include <notification/notification_messages_notes.h>
|
||||
|
||||
void frequency_assert(const char* note_name, const NotificationMessage* message) {
|
||||
double a = notification_messages_notes_frequency_from_name(note_name);
|
||||
double b = message->data.sound.frequency;
|
||||
const double epsilon = message->data.sound.frequency > 5000 ? 0.02f : 0.01f;
|
||||
mu_assert_double_between(b - epsilon, b + epsilon, a);
|
||||
}
|
||||
|
||||
MU_TEST(notification_messages_notes_frequency_from_name_test) {
|
||||
// Upper case
|
||||
mu_check(float_is_equal(
|
||||
notification_messages_notes_frequency_from_name("C0"),
|
||||
notification_messages_notes_frequency_from_name("c0")));
|
||||
|
||||
// Mixed case
|
||||
mu_check(float_is_equal(
|
||||
notification_messages_notes_frequency_from_name("Cs0"),
|
||||
notification_messages_notes_frequency_from_name("cs0")));
|
||||
|
||||
// Check errors
|
||||
mu_check(
|
||||
float_is_equal(notification_messages_notes_frequency_from_name("0"), 0.0)); // Without note
|
||||
mu_check(float_is_equal(
|
||||
notification_messages_notes_frequency_from_name("C"), 0.0)); // Without octave
|
||||
mu_check(float_is_equal(
|
||||
notification_messages_notes_frequency_from_name("C9"), 0.0)); // Unsupported octave
|
||||
mu_check(float_is_equal(
|
||||
notification_messages_notes_frequency_from_name("C10"), 0.0)); // Unsupported octave
|
||||
mu_check(float_is_equal(
|
||||
notification_messages_notes_frequency_from_name("X0"), 0.0)); // Unknown note
|
||||
mu_check(float_is_equal(
|
||||
notification_messages_notes_frequency_from_name("CCC0"), 0.0)); // Note name overflow
|
||||
|
||||
// Notes and structures
|
||||
frequency_assert("c0", &message_note_c0);
|
||||
frequency_assert("cs0", &message_note_cs0);
|
||||
frequency_assert("d0", &message_note_d0);
|
||||
frequency_assert("ds0", &message_note_ds0);
|
||||
frequency_assert("e0", &message_note_e0);
|
||||
frequency_assert("f0", &message_note_f0);
|
||||
frequency_assert("fs0", &message_note_fs0);
|
||||
frequency_assert("g0", &message_note_g0);
|
||||
frequency_assert("gs0", &message_note_gs0);
|
||||
frequency_assert("a0", &message_note_a0);
|
||||
frequency_assert("as0", &message_note_as0);
|
||||
frequency_assert("b0", &message_note_b0);
|
||||
|
||||
frequency_assert("c1", &message_note_c1);
|
||||
frequency_assert("cs1", &message_note_cs1);
|
||||
frequency_assert("d1", &message_note_d1);
|
||||
frequency_assert("ds1", &message_note_ds1);
|
||||
frequency_assert("e1", &message_note_e1);
|
||||
frequency_assert("f1", &message_note_f1);
|
||||
frequency_assert("fs1", &message_note_fs1);
|
||||
frequency_assert("g1", &message_note_g1);
|
||||
frequency_assert("gs1", &message_note_gs1);
|
||||
frequency_assert("a1", &message_note_a1);
|
||||
frequency_assert("as1", &message_note_as1);
|
||||
frequency_assert("b1", &message_note_b1);
|
||||
|
||||
frequency_assert("c2", &message_note_c2);
|
||||
frequency_assert("cs2", &message_note_cs2);
|
||||
frequency_assert("d2", &message_note_d2);
|
||||
frequency_assert("ds2", &message_note_ds2);
|
||||
frequency_assert("e2", &message_note_e2);
|
||||
frequency_assert("f2", &message_note_f2);
|
||||
frequency_assert("fs2", &message_note_fs2);
|
||||
frequency_assert("g2", &message_note_g2);
|
||||
frequency_assert("gs2", &message_note_gs2);
|
||||
frequency_assert("a2", &message_note_a2);
|
||||
frequency_assert("as2", &message_note_as2);
|
||||
frequency_assert("b2", &message_note_b2);
|
||||
|
||||
frequency_assert("c3", &message_note_c3);
|
||||
frequency_assert("cs3", &message_note_cs3);
|
||||
frequency_assert("d3", &message_note_d3);
|
||||
frequency_assert("ds3", &message_note_ds3);
|
||||
frequency_assert("e3", &message_note_e3);
|
||||
frequency_assert("f3", &message_note_f3);
|
||||
frequency_assert("fs3", &message_note_fs3);
|
||||
frequency_assert("g3", &message_note_g3);
|
||||
frequency_assert("gs3", &message_note_gs3);
|
||||
frequency_assert("a3", &message_note_a3);
|
||||
frequency_assert("as3", &message_note_as3);
|
||||
frequency_assert("b3", &message_note_b3);
|
||||
|
||||
frequency_assert("c4", &message_note_c4);
|
||||
frequency_assert("cs4", &message_note_cs4);
|
||||
frequency_assert("d4", &message_note_d4);
|
||||
frequency_assert("ds4", &message_note_ds4);
|
||||
frequency_assert("e4", &message_note_e4);
|
||||
frequency_assert("f4", &message_note_f4);
|
||||
frequency_assert("fs4", &message_note_fs4);
|
||||
frequency_assert("g4", &message_note_g4);
|
||||
frequency_assert("gs4", &message_note_gs4);
|
||||
frequency_assert("a4", &message_note_a4);
|
||||
frequency_assert("as4", &message_note_as4);
|
||||
frequency_assert("b4", &message_note_b4);
|
||||
|
||||
frequency_assert("c5", &message_note_c5);
|
||||
frequency_assert("cs5", &message_note_cs5);
|
||||
frequency_assert("d5", &message_note_d5);
|
||||
frequency_assert("ds5", &message_note_ds5);
|
||||
frequency_assert("e5", &message_note_e5);
|
||||
frequency_assert("f5", &message_note_f5);
|
||||
frequency_assert("fs5", &message_note_fs5);
|
||||
frequency_assert("g5", &message_note_g5);
|
||||
frequency_assert("gs5", &message_note_gs5);
|
||||
frequency_assert("a5", &message_note_a5);
|
||||
frequency_assert("as5", &message_note_as5);
|
||||
frequency_assert("b5", &message_note_b5);
|
||||
|
||||
frequency_assert("c6", &message_note_c6);
|
||||
frequency_assert("cs6", &message_note_cs6);
|
||||
frequency_assert("d6", &message_note_d6);
|
||||
frequency_assert("ds6", &message_note_ds6);
|
||||
frequency_assert("e6", &message_note_e6);
|
||||
frequency_assert("f6", &message_note_f6);
|
||||
frequency_assert("fs6", &message_note_fs6);
|
||||
frequency_assert("g6", &message_note_g6);
|
||||
frequency_assert("gs6", &message_note_gs6);
|
||||
frequency_assert("a6", &message_note_a6);
|
||||
frequency_assert("as6", &message_note_as6);
|
||||
frequency_assert("b6", &message_note_b6);
|
||||
|
||||
frequency_assert("c7", &message_note_c7);
|
||||
frequency_assert("cs7", &message_note_cs7);
|
||||
frequency_assert("d7", &message_note_d7);
|
||||
frequency_assert("ds7", &message_note_ds7);
|
||||
frequency_assert("e7", &message_note_e7);
|
||||
frequency_assert("f7", &message_note_f7);
|
||||
frequency_assert("fs7", &message_note_fs7);
|
||||
frequency_assert("g7", &message_note_g7);
|
||||
frequency_assert("gs7", &message_note_gs7);
|
||||
frequency_assert("a7", &message_note_a7);
|
||||
frequency_assert("as7", &message_note_as7);
|
||||
frequency_assert("b7", &message_note_b7);
|
||||
|
||||
frequency_assert("c8", &message_note_c8);
|
||||
frequency_assert("cs8", &message_note_cs8);
|
||||
frequency_assert("d8", &message_note_d8);
|
||||
frequency_assert("ds8", &message_note_ds8);
|
||||
frequency_assert("e8", &message_note_e8);
|
||||
frequency_assert("f8", &message_note_f8);
|
||||
frequency_assert("fs8", &message_note_fs8);
|
||||
frequency_assert("g8", &message_note_g8);
|
||||
frequency_assert("gs8", &message_note_gs8);
|
||||
frequency_assert("a8", &message_note_a8);
|
||||
frequency_assert("as8", &message_note_as8);
|
||||
frequency_assert("b8", &message_note_b8);
|
||||
}
|
||||
|
||||
MU_TEST_SUITE(notes_suite) {
|
||||
MU_RUN_TEST(notification_messages_notes_frequency_from_name_test);
|
||||
}
|
||||
|
||||
int run_minunit_test_notes(void) {
|
||||
MU_RUN_SUITE(notes_suite);
|
||||
return MU_EXIT_CODE;
|
||||
}
|
||||
|
||||
TEST_API_DEFINE(run_minunit_test_notes)
|
||||
Reference in New Issue
Block a user