This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
testaudio.c
182 lines (153 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* Original code: automated SDL audio test written by Edgar Simo "bobbens"
* New/updated tests: aschiffler at ferzkopp dot net
*/
#include <stdio.h>
#include <SDL/SDL.h>
#include "../../include/SDL_test.h"
/* Test cases */
static const TestCaseReference test1 =
14
(TestCaseReference){ "audio_enumerateAndNameAudioDevices", "Enumerate and name available audio devices (output and capture)", TEST_ENABLED, TEST_REQUIRES_AUDIO, 0};
15
16
static const TestCaseReference test2 =
17
(TestCaseReference){ "audio_enumerateAndNameAudioDevicesNegativeTests", "Netative tests around enumeration and naming of audio devices.", TEST_ENABLED, TEST_REQUIRES_AUDIO, 0};
18
19
static const TestCaseReference test3 =
20
(TestCaseReference){ "audio_printAudioDrivers", "Checks available audio driver names.", TEST_ENABLED, TEST_REQUIRES_AUDIO, 0};
21
22
static const TestCaseReference test4 =
23
(TestCaseReference){ "audio_printCurrentAudioDriver", "Checks current audio driver name with initialized audio.", TEST_ENABLED, TEST_REQUIRES_AUDIO, 0};
24
25
26
/* Test suite */
extern const TestCaseReference *testSuite[] = {
27
&test1, &test2, &test3, &test4, NULL
28
29
30
31
};
TestCaseReference **QueryTestSuite() {
32
return (TestCaseReference **)testSuite;
33
34
35
36
37
38
39
}
/* Fixture */
void
SetUp(void *arg)
{
40
41
42
/* Start SDL. */
int ret = SDL_Init( SDL_INIT_AUDIO );
AssertTrue(ret==0, "SDL_Init(SDL_INIT_AUDIO): %s", SDL_GetError());
43
44
45
46
47
}
void
TearDown(void *arg)
{
48
49
/* Quit SDL. */
SDL_Quit();
50
51
52
53
54
55
}
/* Test case functions */
/**
* \brief Enumerate and name available audio devices (output and capture).
56
*
57
58
59
60
61
62
63
64
65
66
67
68
* \sa http://wiki.libsdl.org/moin.cgi/SDL_GetNumAudioDevices
* \sa http://wiki.libsdl.org/moin.cgi/SDL_GetAudioDeviceName
*/
int audio_enumerateAndNameAudioDevices()
{
int ret;
int t, tt;
int i, n, nn;
const char *name, *nameAgain;
/* Iterate over types: t=0 output device, t=1 input/capture device */
for (t=0; t<2; t++) {
69
70
71
/* Get number of devices. */
n = SDL_GetNumAudioDevices(t);
72
73
AssertTrue(n>=0,
"Number of %s devices < 0, reported as %i: %s",
74
(t) ? "output" : "capture",
75
n,
76
77
78
79
80
81
82
83
84
SDL_GetError());
/* Variation of non-zero type */
if (t==1) {
tt = t + RandomIntegerInRange(1,10);
nn = SDL_GetNumAudioDevices(tt);
AssertTrue(n==nn, "SDL_GetNumAudioDevices(%i) : expected same number of audio devices %i, got %i", tt, n, nn);
nn = SDL_GetNumAudioDevices(-tt);
AssertTrue(n==nn, "SDL_GetNumAudioDevices(%i) : expected same number of audio devices %i, got %i", -tt, n, nn);
85
86
}
87
88
89
90
91
92
93
94
95
96
97
/* List devices. */
if (n>0) {
for (i=0; i<n; i++) {
name = SDL_GetAudioDeviceName(i, t);
AssertTrue(name != NULL, "SDL_GetAudioDeviceName(%i, %i): returned NULL name", i, t);
AssertTrue(strlen(name)>0, "SDL_GetAudioDeviceName(%i, %i): returned empty name string", i, t);
if (t==1) {
/* Also try non-zero type */
nameAgain = SDL_GetAudioDeviceName(i, tt);
AssertTrue(nameAgain != NULL, "SDL_GetAudioDeviceName(%i, %i): returned NULL name", i, tt);
AssertTrue(strlen(nameAgain)>0, "SDL_GetAudioDeviceName(%i, %i): returned empty name string", i, tt);
98
99
AssertTrue(strcmp(name, nameAgain)==0,
"SDL_GetAudioDeviceName(%i, %i): returned unexpected name string %s, expected %s",
100
101
102
103
104
105
106
107
108
i, tt, nameAgain, name);
}
}
}
}
}
/**
* \brief Negative tests around enumeration and naming of audio devices.
109
*
110
111
112
113
114
115
116
117
118
* \sa http://wiki.libsdl.org/moin.cgi/SDL_GetNumAudioDevices
* \sa http://wiki.libsdl.org/moin.cgi/SDL_GetAudioDeviceName
*/
int audio_enumerateAndNameAudioDevicesNegativeTests()
{
int ret;
int t;
int i, j, no, nc;
const char *name;
119
120
121
122
/* Get number of devices. */
no = SDL_GetNumAudioDevices(0);
nc = SDL_GetNumAudioDevices(1);
123
124
125
126
127
128
129
/* Invalid device index when getting name */
for (t=0; t<2; t++) {
/* Negative device index */
i = -1;
name = SDL_GetAudioDeviceName(i, t);
AssertTrue(name == NULL, "SDL_GetAudioDeviceName(%i, %i): returned a name, should return NULL", i, t);
130
131
132
133
134
135
136
/* Device index past range */
for (j=0; j<3; j++) {
i = (t) ? nc+j : no+j;
name = SDL_GetAudioDeviceName(i, t);
AssertTrue(name == NULL, "SDL_GetAudioDeviceName(%i, %i): returned a name, should return NULL", i, t);
}
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* Capture index past capture range but within output range */
if ((no>0) && (no>nc) && (t==1)) {
i = no-1;
name = SDL_GetAudioDeviceName(i, t);
AssertTrue(name == NULL, "SDL_GetAudioDeviceName(%i, %i): returned a name, should return NULL", i, t);
}
}
}
/**
* @brief Checks available audio driver names.
*/
int audio_printAudioDrivers()
{
int i, n;
const char *name;
/* Get number of drivers */
n = SDL_GetNumAudioDrivers();
AssertTrue(n>=0, "Number of audio drivers >= 0");
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
/* List drivers. */
if (n>0)
{
for (i=0; i<n; i++) {
name = SDL_GetAudioDriver(i);
AssertTrue(name != NULL, "name != NULL");
AssertTrue(strlen(name)>0, "name empty");
}
}
}
/**
* @brief Checks current audio driver name with initialized audio.
*/
int audio_printCurrentAudioDriver()
{
int ret;
const char *name;
/* Check current audio driver */
name = SDL_GetCurrentAudioDriver();
AssertTrue(name != NULL, "name != NULL");
AssertTrue(strlen(name)>0, "name empty");
}