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, see <http://www.gnu.org/licenses/>.
16***/
17
18#ifdef HAVE_CONFIG_H
19#include <config.h>
20#endif
21
22#include <stdlib.h>
23
24#include <check.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
35START_TEST (format_test) {
36    pa_format_info *f1 = NULL, *f2 = NULL;
37    int rates1[] = { 32000, 44100, 48000 }, i, temp_int1 = -1, PA_UNUSED 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    fail_unless(!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    fail_unless(pa_format_info_is_compatible(f1, f2));
56    fail_unless(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    fail_unless(!pa_format_info_is_compatible(f1, f2));
63    fail_unless(!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    fail_unless(pa_format_info_is_compatible(f1, f2));
72    fail_unless(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    fail_unless(!pa_format_info_is_compatible(f1, f2));
79    fail_unless(!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    fail_unless(!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    fail_unless(pa_format_info_is_compatible(f1, f2));
96    fail_unless(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    fail_unless(!pa_format_info_is_compatible(f1, f2));
103    fail_unless(!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    fail_unless(pa_format_info_get_prop_type(f1, "format.test_string") == PA_PROP_TYPE_INT);
109    fail_unless(pa_format_info_get_prop_int(f1, "format.test_string", &temp_int1) == 0);
110    fail_unless(temp_int1 == 42);
111
112    /* 10. Verify setting/getting an int range */
113    REINIT(f1);
114    pa_format_info_set_prop_int_range(f1, "format.test_string", 0, 100);
115    pa_assert(pa_format_info_get_prop_type(f1, "format.test_string") == PA_PROP_TYPE_INT_RANGE);
116    pa_assert(pa_format_info_get_prop_int_range(f1, "format.test_string", &temp_int1, &temp_int2) == 0);
117    pa_assert(temp_int1 == 0 && temp_int2 == 100);
118
119    /* 11. Verify setting/getting an int array */
120    REINIT(f1);
121    pa_format_info_set_prop_int_array(f1, "format.test_string", rates1, PA_ELEMENTSOF(rates1));
122    fail_unless(pa_format_info_get_prop_type(f1, "format.test_string") == PA_PROP_TYPE_INT_ARRAY);
123    fail_unless(pa_format_info_get_prop_int_array(f1, "format.test_string", &temp_int_array, &temp_int1) == 0);
124    fail_unless(temp_int1 == PA_ELEMENTSOF(rates1));
125    for (i = 0; i < temp_int1; i++)
126        fail_unless(temp_int_array[i] == rates1[i]);
127    pa_xfree(temp_int_array);
128
129    /* 12. Verify setting/getting a string */
130    REINIT(f1);
131    pa_format_info_set_prop_string(f1, "format.test_string", "foo");
132    fail_unless(pa_format_info_get_prop_type(f1, "format.test_string") == PA_PROP_TYPE_STRING);
133    fail_unless(pa_format_info_get_prop_string(f1, "format.test_string", &temp_str) == 0);
134    fail_unless(pa_streq(temp_str, "foo"));
135    pa_xfree(temp_str);
136
137    /* 13. Verify setting/getting an int array */
138    REINIT(f1);
139    pa_format_info_set_prop_string_array(f1, "format.test_string", strings, PA_ELEMENTSOF(strings));
140    fail_unless(pa_format_info_get_prop_type(f1, "format.test_string") == PA_PROP_TYPE_STRING_ARRAY);
141    fail_unless(pa_format_info_get_prop_string_array(f1, "format.test_string", &temp_str_array, &temp_int1) == 0);
142    fail_unless(temp_int1 == PA_ELEMENTSOF(strings));
143    for (i = 0; i < temp_int1; i++)
144        fail_unless(pa_streq(temp_str_array[i], strings[i]));
145    pa_format_info_free_string_array(temp_str_array, temp_int1);
146
147    DEINIT(f1);
148    DEINIT(f2);
149}
150END_TEST
151
152int main(int argc, char *argv[]) {
153    int failed = 0;
154    Suite *s;
155    TCase *tc;
156    SRunner *sr;
157
158    s = suite_create("Format");
159    tc = tcase_create("format");
160    tcase_add_test(tc, format_test);
161    suite_add_tcase(s, tc);
162
163    sr = srunner_create(s);
164    srunner_run_all(sr, CK_NORMAL);
165    failed = srunner_ntests_failed(sr);
166    srunner_free(sr);
167
168    return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
169}
170