summaryrefslogtreecommitdiff
path: root/src/channel.test.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/channel.test.c')
-rw-r--r--src/channel.test.c137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/channel.test.c b/src/channel.test.c
index e7f7e06..532d153 100644
--- a/src/channel.test.c
+++ b/src/channel.test.c
@@ -2,12 +2,18 @@
#include <plibsys.h>
PMutex * mock_p_mutex_new_();
+pboolean mock_p_mutex_trylock_();
+pboolean mock_p_mutex_unlock_();
#define p_mutex_new mock_p_mutex_new_
+#define p_mutex_trylock mock_p_mutex_trylock_
+#define p_mutex_unlock mock_p_mutex_unlock_
#include "channel.c"
#undef p_mutex_new
+/* ~~~~~~~~ mocks ~~~~~~~~ */
+
lily_mock_t *mock_p_mutex_new = NULL;
PMutex * mock_p_mutex_new_()
{
@@ -16,6 +22,26 @@ PMutex * mock_p_mutex_new_()
}
+lily_mock_t *mock_p_mutex_trylock = NULL;
+pboolean mock_p_mutex_trylock_(PMutex *m)
+{
+ mock_p_mutex_trylock->n_calls += 1;
+ pboolean result;
+ lily_get_value(mock_p_mutex_trylock, pboolean, &result);
+ return result;
+}
+
+
+lily_mock_t *mock_p_mutex_unlock = NULL;
+pboolean mock_p_mutex_unlock_(PMutex *m)
+{
+ mock_p_mutex_unlock->n_calls += 1;
+ return TRUE;
+}
+
+
+/* ~~~~~~~~ tests ~~~~~~~~ */
+
void test_channel_init()
{
lily_mock_use(&mock_p_mutex_new);
@@ -55,10 +81,121 @@ void test_channel_reset()
}
+void test_channel_get_next_sample_inactive()
+{
+ lily_mock_use(&mock_p_mutex_trylock);
+ lily_mock_use(&mock_p_mutex_unlock);
+
+ struct channel_t chan;
+ chan.active = false;
+ chan.pos = 0;
+ float l = 22; float r = 22;
+ channel_get_next_sample(&l, &r, &chan);
+
+ lily_assert_int_equal(mock_p_mutex_trylock->n_calls, 0);
+ lily_assert_int_equal(mock_p_mutex_unlock->n_calls, 0);
+ lily_assert_int_equal(chan.pos, 0);
+ lily_assert_float_equal(l, 0.0f, 0.1f);
+ lily_assert_float_equal(r, 0.0f, 0.1f);
+}
+
+
+void test_channel_get_next_sample_paused()
+{
+ lily_mock_use(&mock_p_mutex_trylock);
+ lily_mock_use(&mock_p_mutex_unlock);
+
+ struct channel_t chan;
+ chan.active = true;
+ chan.paused = true;
+ chan.pos = 0;
+ float l = 22; float r = 22;
+ channel_get_next_sample(&l, &r, &chan);
+
+ lily_assert_int_equal(mock_p_mutex_trylock->n_calls, 0);
+ lily_assert_int_equal(mock_p_mutex_unlock->n_calls, 0);
+ lily_assert_int_equal(chan.pos, 0);
+ lily_assert_float_equal(l, 0.0f, 0.1f);
+ lily_assert_float_equal(r, 0.0f, 0.1f);
+}
+
+
+void test_channel_get_next_sample_nolock()
+{
+ lily_mock_use(&mock_p_mutex_trylock);
+ lily_mock_use(&mock_p_mutex_unlock);
+
+ struct channel_t chan;
+ chan.active = true;
+ chan.paused = false;
+ chan.pos = 0;
+ float l = 22; float r = 22;
+
+ lily_store_value(mock_p_mutex_trylock, pboolean, FALSE);
+ channel_get_next_sample(&l, &r, &chan);
+ lily_assert_int_equal(mock_p_mutex_trylock->n_calls, 1);
+ lily_assert_int_equal(mock_p_mutex_unlock->n_calls, 0);
+
+ lily_assert_int_equal(chan.pos, 0);
+ lily_assert_float_equal(l, 0.0f, 0.1f);
+ lily_assert_float_equal(r, 0.0f, 0.1f);
+}
+
+
+void test_channel_get_next_sample_normal()
+{
+ lily_mock_use(&mock_p_mutex_trylock);
+ lily_mock_use(&mock_p_mutex_unlock);
+
+ struct channel_t chan;
+ chan.active = true;
+ chan.paused = false;
+ chan.volume = 255;
+ chan.pan = 0;
+ float audio_left[] = { 0.0, 0.5, 1.0 };
+ float audio_right[] = { 1.0, 0.5, 0.0 };
+ chan.sound.left = audio_left;
+ chan.sound.right = audio_right;
+ chan.sound.len = 3;
+ chan.pos = 0;
+
+ float l, r;
+
+ lily_store_value(mock_p_mutex_trylock, pboolean, TRUE);
+ channel_get_next_sample(&l, &r, &chan);
+ lily_assert_int_equal(mock_p_mutex_trylock->n_calls, 1);
+ lily_assert_int_equal(mock_p_mutex_unlock->n_calls, 1);
+ lily_assert_int_equal(chan.pos, 1);
+ lily_assert_float_equal(l, 0.0f, 0.1f);
+ lily_assert_float_equal(r, 1.0f, 0.1f);
+
+ lily_store_value(mock_p_mutex_trylock, pboolean, TRUE);
+ channel_get_next_sample(&l, &r, &chan);
+ lily_assert_int_equal(mock_p_mutex_trylock->n_calls, 2);
+ lily_assert_int_equal(mock_p_mutex_unlock->n_calls, 2);
+ lily_assert_int_equal(chan.pos, 2);
+ lily_assert_float_equal(l, 0.5f, 0.1f);
+ lily_assert_float_equal(r, 0.5f, 0.1f);
+
+ lily_store_value(mock_p_mutex_trylock, pboolean, TRUE);
+ channel_get_next_sample(&l, &r, &chan);
+ lily_assert_int_equal(mock_p_mutex_trylock->n_calls, 3);
+ lily_assert_int_equal(mock_p_mutex_unlock->n_calls, 3);
+ lily_assert_int_equal(chan.pos, 0);
+ lily_assert_int_equal(chan.active, false);
+ lily_assert_float_equal(l, 1.0f, 0.1f);
+ lily_assert_float_equal(r, 0.0f, 0.1f);
+}
+
+
void suite_channel()
{
lily_run_test(test_channel_init);
lily_run_test(test_channel_reset);
+ lily_run_test(test_channel_get_next_sample_inactive);
+ lily_run_test(test_channel_get_next_sample_paused);
+ lily_run_test(test_channel_get_next_sample_normal);
+ lily_run_test(test_channel_get_next_sample_nolock);
lily_mock_destroy(mock_p_mutex_new);
}