]> code.delx.au - pulseaudio/blob - src/tests/format-test.c
format: Add more property getters
[pulseaudio] / src / tests / format-test.c
1 /***
2 This file is part of PulseAudio.
3
4 PulseAudio is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 2.1 of the License,
7 or (at your option) any later version.
8
9 PulseAudio is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with PulseAudio; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25
26 #include <pulsecore/macro.h>
27 #include <pulsecore/core-util.h>
28 #include <pulse/format.h>
29 #include <pulse/xmalloc.h>
30
31 #define INIT(f) f = pa_format_info_new()
32 #define DEINIT(f) pa_format_info_free(f);
33 #define REINIT(f) { DEINIT(f); INIT(f); }
34
35 int main(int argc, char *argv[]) {
36 pa_format_info *f1 = NULL, *f2 = NULL;
37 int rates1[] = { 32000, 44100, 48000 }, i, temp_int1 = -1, temp_int2 = -1, *temp_int_array;
38 const char *strings[] = { "thing1", "thing2", "thing3" };
39 char *temp_str, **temp_str_array;
40
41 /* 1. Simple fixed format int check */
42 INIT(f1); INIT(f2);
43 f1->encoding = PA_ENCODING_AC3_IEC61937;
44 pa_format_info_set_prop_int(f1, PA_PROP_FORMAT_RATE, 32000);
45 f2->encoding = PA_ENCODING_AC3_IEC61937;
46 pa_format_info_set_prop_int(f2, PA_PROP_FORMAT_RATE, 44100);
47 pa_assert(!pa_format_info_is_compatible(f1, f2));
48
49 /* 2. Check int array membership - positive */
50 REINIT(f1); REINIT(f2);
51 f1->encoding = PA_ENCODING_AC3_IEC61937;
52 pa_format_info_set_prop_int_array(f1, PA_PROP_FORMAT_RATE, rates1, PA_ELEMENTSOF(rates1));
53 f2->encoding = PA_ENCODING_AC3_IEC61937;
54 pa_format_info_set_prop_int(f2, PA_PROP_FORMAT_RATE, 44100);
55 pa_assert(pa_format_info_is_compatible(f1, f2));
56 pa_assert(pa_format_info_is_compatible(f2, f1));
57
58 /* 3. Check int array membership - negative */
59 REINIT(f2);
60 f2->encoding = PA_ENCODING_AC3_IEC61937;
61 pa_format_info_set_prop_int(f2, PA_PROP_FORMAT_RATE, 96000);
62 pa_assert(!pa_format_info_is_compatible(f1, f2));
63 pa_assert(!pa_format_info_is_compatible(f2, f1));
64
65 /* 4. Check int range - positive */
66 REINIT(f1); REINIT(f2);
67 f1->encoding = PA_ENCODING_AC3_IEC61937;
68 pa_format_info_set_prop_int_range(f1, PA_PROP_FORMAT_RATE, 32000, 48000);
69 f2->encoding = PA_ENCODING_AC3_IEC61937;
70 pa_format_info_set_prop_int(f2, PA_PROP_FORMAT_RATE, 44100);
71 pa_assert(pa_format_info_is_compatible(f1, f2));
72 pa_assert(pa_format_info_is_compatible(f2, f1));
73
74 /* 5. Check int range - negative */
75 REINIT(f2);
76 f2->encoding = PA_ENCODING_AC3_IEC61937;
77 pa_format_info_set_prop_int(f2, PA_PROP_FORMAT_RATE, 96000);
78 pa_assert(!pa_format_info_is_compatible(f1, f2));
79 pa_assert(!pa_format_info_is_compatible(f2, f1));
80
81 /* 6. Simple fixed format string check */
82 REINIT(f1); REINIT(f2);
83 f1->encoding = PA_ENCODING_AC3_IEC61937;
84 pa_format_info_set_prop_string(f1, "format.test_string", "thing1");
85 f2->encoding = PA_ENCODING_AC3_IEC61937;
86 pa_format_info_set_prop_string(f2, "format.test_string", "notthing1");
87 pa_assert(!pa_format_info_is_compatible(f1, f2));
88
89 /* 7. Check string array membership - positive */
90 REINIT(f1); REINIT(f2);
91 f1->encoding = PA_ENCODING_AC3_IEC61937;
92 pa_format_info_set_prop_string_array(f1, "format.test_string", strings, PA_ELEMENTSOF(strings));
93 f2->encoding = PA_ENCODING_AC3_IEC61937;
94 pa_format_info_set_prop_string(f2, "format.test_string", "thing3");
95 pa_assert(pa_format_info_is_compatible(f1, f2));
96 pa_assert(pa_format_info_is_compatible(f2, f1));
97
98 /* 8. Check string array membership - negative */
99 REINIT(f2);
100 f2->encoding = PA_ENCODING_AC3_IEC61937;
101 pa_format_info_set_prop_string(f2, "format.test_string", "thing5");
102 pa_assert(!pa_format_info_is_compatible(f1, f2));
103 pa_assert(!pa_format_info_is_compatible(f2, f1));
104
105 /* 9. Verify setting/getting an int */
106 REINIT(f1);
107 pa_format_info_set_prop_int(f1, "format.test_string", 42);
108 pa_assert(pa_format_info_get_prop_int(f1, "format.test_string", &temp_int1) == 0);
109 pa_assert(temp_int1 == 42);
110
111 /* 10. Verify setting/getting an int range */
112 REINIT(f1);
113 pa_format_info_set_prop_int_range(f1, "format.test_string", 0, 100);
114 pa_assert(pa_format_info_get_prop_int_range(f1, "format.test_string", &temp_int1, &temp_int2) == 0);
115 pa_assert(temp_int1 == 0 && temp_int2 == 100);
116
117 /* 11. Verify setting/getting an int array */
118 REINIT(f1);
119 pa_format_info_set_prop_int_array(f1, "format.test_string", rates1, PA_ELEMENTSOF(rates1));
120 pa_assert(pa_format_info_get_prop_int_array(f1, "format.test_string", &temp_int_array, &temp_int1) == 0);
121 pa_assert(temp_int1 == PA_ELEMENTSOF(rates1));
122 for (i = 0; i < temp_int1; i++)
123 pa_assert(temp_int_array[i] == rates1[i]);
124 pa_xfree(temp_int_array);
125
126 /* 12. Verify setting/getting a string */
127 REINIT(f1);
128 pa_format_info_set_prop_string(f1, "format.test_string", "foo");
129 pa_assert(pa_format_info_get_prop_string(f1, "format.test_string", &temp_str) == 0);
130 pa_assert(pa_streq(temp_str, "foo"));
131 pa_xfree(temp_str);
132
133 /* 13. Verify setting/getting an int array */
134 REINIT(f1);
135 pa_format_info_set_prop_string_array(f1, "format.test_string", strings, PA_ELEMENTSOF(strings));
136 pa_assert(pa_format_info_get_prop_string_array(f1, "format.test_string", &temp_str_array, &temp_int1) == 0);
137 pa_assert(temp_int1 == PA_ELEMENTSOF(strings));
138 for (i = 0; i < temp_int1; i++)
139 pa_assert(pa_streq(temp_str_array[i], strings[i]));
140 pa_format_info_free_string_array(temp_str_array, temp_int1);
141
142 DEINIT(f1);
143 DEINIT(f2);
144
145 return 0;
146 }