summaryrefslogtreecommitdiff
path: root/src/channel.test.c
blob: 532d153db1e0771863a7ae60bfc17896bda16dfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "test/mossrose-test.h"
#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_()
{
	mock_p_mutex_new->n_calls += 1;
	return 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);

	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 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);
}