From 91726809f9e97d8d4fb7b6c0642234ff96535bdd Mon Sep 17 00:00:00 2001 From: sanine Date: Sun, 28 Aug 2022 12:41:53 -0500 Subject: add channel_init() and channel_reset() --- CMakeLists.txt | 1 + src/channel.c | 24 ++++++++++++++++++-- src/channel.h | 18 +++++++++++++++ src/channel.test.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ src/test/mossrose-test.c | 2 ++ 5 files changed, 101 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c631e11..76ef10d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,6 +32,7 @@ endif() if (MOSSROSE_BUILD_TESTS) add_executable(test) + target_link_libraries(test plibsys) endif() diff --git a/src/channel.c b/src/channel.c index fd6741b..e6e6c3f 100644 --- a/src/channel.c +++ b/src/channel.c @@ -4,5 +4,25 @@ #include "channel.h" -struct channel_t { -}; +void channel_init(struct channel_t *chan) +{ + chan->active = false; + chan->paused = false; + chan->volume = 255; + chan->pan = 0; + + chan->sound_mutex = p_mutex_new(); + chan->sound.left = NULL; + chan->sound.right = NULL; + chan->pos = 0; +} + + +void channel_reset(struct channel_t *chan) +{ + p_atomic_int_set(&(chan->paused), false); + p_atomic_int_set(&(chan->volume), 255); + p_atomic_int_set(&(chan->pan), 0); + chan->pos = 0; + p_atomic_int_set(&(chan->active), false); +} diff --git a/src/channel.h b/src/channel.h index 6266a10..4d0800b 100644 --- a/src/channel.h +++ b/src/channel.h @@ -2,7 +2,25 @@ #define MOSSROSE_CHANNEL_H #include +#include #include +#include + + +struct channel_t { + volatile pint active; /* boolean */ + volatile pint paused; /* boolean */ + volatile pint volume; /* 0-255 */ + volatile pint pan; /* -128-127 */ + + PMutex *sound_mutex; + struct mossrose_sound_t sound; + size_t pos; +}; + + +void channel_init(struct channel_t *chan); +void channel_reset(struct channel_t *chan); #endif diff --git a/src/channel.test.c b/src/channel.test.c index 96b7364..e7f7e06 100644 --- a/src/channel.test.c +++ b/src/channel.test.c @@ -1,6 +1,64 @@ #include "test/mossrose-test.h" +#include + +PMutex * mock_p_mutex_new_(); + +#define p_mutex_new mock_p_mutex_new_ +#include "channel.c" +#undef p_mutex_new + + +lily_mock_t *mock_p_mutex_new = NULL; +PMutex * mock_p_mutex_new_() +{ + mock_p_mutex_new->n_calls += 1; + return p_mutex_new(); +} + + +void test_channel_init() +{ + lily_mock_use(&mock_p_mutex_new); + + struct channel_t chan; + channel_init(&chan); + + lily_assert_int_equal(mock_p_mutex_new->n_calls, 1); + + lily_assert_int_equal(chan.active, false); + lily_assert_int_equal(chan.paused, false); + lily_assert_int_equal(chan.volume, 255); + lily_assert_int_equal(chan.pan, 0); + + lily_assert_null(chan.sound.left); + lily_assert_null(chan.sound.right); + lily_assert_int_equal(chan.pos, 0); +} + + +void test_channel_reset() +{ + struct channel_t chan; + chan.pos = 255; + chan.active = true; + chan.paused = true; + chan.volume = 5; + chan.pan = 30; + + channel_reset(&chan); + + lily_assert_int_equal(chan.pos, 0); + lily_assert_int_equal(chan.active, false); + lily_assert_int_equal(chan.paused, false); + lily_assert_int_equal(chan.volume, 255); + lily_assert_int_equal(chan.pan, 0); +} void suite_channel() { + lily_run_test(test_channel_init); + lily_run_test(test_channel_reset); + + lily_mock_destroy(mock_p_mutex_new); } diff --git a/src/test/mossrose-test.c b/src/test/mossrose-test.c index 88ce5ff..e71fc2a 100644 --- a/src/test/mossrose-test.c +++ b/src/test/mossrose-test.c @@ -1,8 +1,10 @@ +#include #include "mossrose-test.h" int main() { + p_libsys_init(); RUN_TESTS() return 0; } -- cgit v1.2.1