aboutsummaryrefslogtreecommitdiffstats
path: root/tests/dtmf/dtmf_test.c
blob: d92011cc960ab558f0d35aef02d08efd0b7b3a93 (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
/*
 * (C) 2012-2013 by Holger Hans Peter Freyther <zecke@selfish.org>
 * (C) 2012-2013 by On-Waves
 * All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <dtmf_scheduler.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>

#define ASSERT(got,want) \
	if (got != want) { \
		fprintf(stderr, "Values should be the same 0x%x 0x%x at %s:%d\n", \
			got, want, __FILE__, __LINE__); \
		abort(); \
	}

static void test_queue_while_play(void)
{
	struct dtmf_state state;
	char tone[sizeof(state.tones) + 1];
	unsigned int len = 0;

	dtmf_state_init(&state);

	ASSERT(dtmf_state_add(&state, 'a'), 0);
	ASSERT(dtmf_state_add(&state, 'b'), 0);
	ASSERT(dtmf_state_add(&state, 'c'), 0);

	len = dtmf_state_get_pending(&state, tone);
	ASSERT(len, 3);
	ASSERT(strlen(tone), 3);
	ASSERT(state.playing, 1);
	ASSERT(strcmp(tone, "abc"), 0);

	ASSERT(dtmf_state_add(&state, 'd'), 0);
	dtmf_state_played(&state);
	ASSERT(state.playing, 0);

	len = dtmf_state_get_pending(&state, tone);
	ASSERT(len, 1);
	ASSERT(strlen(tone), 1);
	ASSERT(state.playing, 1);
	ASSERT(strcmp(tone, "d"), 0);

	ASSERT(state.playing, 1);
	dtmf_state_played(&state);
	ASSERT(state.playing, 0);

	/* and check that nothing is played */
	len = dtmf_state_get_pending(&state, tone);
	ASSERT(len, 0);
	ASSERT(strlen(tone), 0);
	ASSERT(state.playing, 0);
}

static void test_queue_over_flow(void)
{
	struct dtmf_state state;
	const size_t max_items = sizeof(state.tones);
	char tone[sizeof(state.tones) + 1];
	int i;
	unsigned int len;

	dtmf_state_init(&state);

	/* add everything that should fit.. */
	for (i = 0; i < max_items; ++i) {
		ASSERT(dtmf_state_add(&state, 'a' + i), 0);
	}

	/* this should fail */
	ASSERT(dtmf_state_add(&state, 'Z'), -1);

	/* read all of it */
	len = dtmf_state_get_pending(&state, tone);
	ASSERT(len, max_items);
	ASSERT(strlen(tone), max_items);
	for (i = 0; i < strlen(tone); ++i)
		ASSERT(tone[i], 'a' + i);
	ASSERT(state.playing, 1);
	dtmf_state_played(&state);
	ASSERT(state.playing, 0);
}

static void test_queue_null_byte(void)
{
	struct dtmf_state state;
	dtmf_state_init(&state);

	ASSERT(dtmf_state_add(&state, 0), -2);
}

static void test_queue_single_pop(void)
{
	struct dtmf_state state;
	dtmf_state_init(&state);

	/* check the empty tone */
	ASSERT(dtmf_state_pop_tone(&state), CHAR_MAX);

	dtmf_state_add(&state, '0');
	dtmf_state_add(&state, '1');
	dtmf_state_add(&state, '2');
	ASSERT(dtmf_tones_queued(&state), 3);
	ASSERT(dtmf_state_pop_tone(&state), '0');
	ASSERT(dtmf_tones_queued(&state), 2);
	ASSERT(dtmf_state_pop_tone(&state), '1');
	ASSERT(dtmf_tones_queued(&state), 1);
	ASSERT(dtmf_state_pop_tone(&state), '2');
	ASSERT(dtmf_tones_queued(&state), 0);
	ASSERT(dtmf_state_pop_tone(&state), CHAR_MAX);

	dtmf_state_add(&state, '3');
	dtmf_state_add(&state, '4');
	dtmf_state_add(&state, '5');
	ASSERT(dtmf_tones_queued(&state), 3);
	ASSERT(dtmf_state_pop_tone(&state), '3');
	ASSERT(dtmf_tones_queued(&state), 2);
	ASSERT(dtmf_state_pop_tone(&state), '4');
	ASSERT(dtmf_tones_queued(&state), 1);
	dtmf_state_add(&state, '6');
	ASSERT(dtmf_tones_queued(&state), 2);
	ASSERT(dtmf_state_pop_tone(&state), '5');
	ASSERT(dtmf_tones_queued(&state), 1);
	ASSERT(dtmf_state_pop_tone(&state), '6');
	ASSERT(dtmf_tones_queued(&state), 0);
	ASSERT(dtmf_state_pop_tone(&state), CHAR_MAX);
}


int main(int argc, char **argv)
{
	test_queue_while_play();
	test_queue_over_flow();
	test_queue_null_byte();
	test_queue_single_pop();
	printf("All tests passed.\n");
	return 0;
}