Skip to content

Commit f393ae3

Browse files
committed
test: lib/bzero: Added unit tests for lib/bzero.
Signed-off-by: Michal Jerzy Wierzbicki <michalx.wierzbicki@linux.intel.com>
1 parent 1cdf8f1 commit f393ae3

2 files changed

Lines changed: 206 additions & 1 deletion

File tree

test/cmocka/Makefile.am

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ check_PROGRAMS += rstrcmp
5555
rstrcmp_SOURCES = src/lib/lib/rstrcmp.c
5656
rstrcmp_LDADD = ../../src/lib/libcore.a $(LDADD)
5757

58-
#pipeline tests
58+
check_PROGRAMS += bzero
59+
bzero_SOURCES = src/lib/lib/bzero.c
60+
bzero_LDADD = ../../src/lib/libcore.a $(LDADD)
61+
62+
# pipeline tests
63+
5964
check_PROGRAMS += pipeline_new
6065
pipeline_new_SOURCES = ../../src/audio/pipeline.c src/audio/pipeline/pipeline_new.c src/audio/pipeline/pipeline_mocks.c src/audio/pipeline/pipeline_mocks_rzalloc.c
6166

test/cmocka/src/lib/lib/bzero.c

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/*
2+
* Copyright (c) 2018, Intel Corporation
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of the Intel Corporation nor the
13+
* names of its contributors may be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
*
28+
* Author: Michal Jerzy Wierzbicki <michalx.wierzbicki@linux.intel.com>
29+
*/
30+
31+
#include <sof/alloc.h>
32+
33+
#include <stdarg.h>
34+
#include <setjmp.h>
35+
#include <cmocka.h>
36+
37+
struct test_data_t {
38+
size_t len;
39+
char str[0];
40+
};
41+
42+
struct test_data_zero_middle_t {
43+
int beg;
44+
int end;
45+
int len;
46+
};
47+
48+
static struct test_data_t *test_data;
49+
static struct test_data_zero_middle_t *test_data_zero_middle;
50+
static const char default_char = 'a';
51+
52+
static void reset_test_arr(void)
53+
{
54+
int i;
55+
56+
for (i = 0; i < test_data->len-1; ++i)
57+
test_data->str[i] = default_char;
58+
test_data->str[test_data->len] = 0;
59+
}
60+
61+
static int setup(void **state)
62+
{
63+
const int len = 6;
64+
65+
test_data = malloc(sizeof(struct test_data_t)
66+
+ len * sizeof(test_data[0]));
67+
test_data->len = len;
68+
69+
const int zero_beg = 2;
70+
const int zero_end = 4;
71+
// aa000aa... - pattern of zeroing
72+
73+
test_data_zero_middle = malloc(sizeof(struct test_data_zero_middle_t));
74+
test_data_zero_middle->beg = zero_beg;
75+
test_data_zero_middle->end = zero_end;
76+
test_data_zero_middle->len = zero_end - zero_beg;
77+
78+
return 0;
79+
}
80+
81+
static int teardown(void **state)
82+
{
83+
free(test_data);
84+
85+
return 0;
86+
}
87+
88+
static int check_arr(char *arr, size_t n, char should_be)
89+
{
90+
size_t i;
91+
92+
for (i = 0; i < n; ++i)
93+
if (arr[i] != should_be)
94+
return i;
95+
return -1;
96+
}
97+
98+
static int check_test_arr(char should_be)
99+
{
100+
return check_arr(test_data->str, test_data->len-1, should_be);
101+
}
102+
103+
static int check_test_arr_with_offset(
104+
int offset_beg, int offset_end, char should_be)
105+
{
106+
return check_arr(
107+
test_data->str+offset_beg,
108+
test_data->len+offset_end-1,
109+
should_be
110+
);
111+
}
112+
113+
/* Tests*/
114+
static void test_lib_lib_bzero_check_test_arr(void **state)
115+
{
116+
reset_test_arr();
117+
118+
(void) state;
119+
120+
assert_int_equal(check_test_arr(default_char), -1);
121+
}
122+
123+
static void test_lib_lib_bzero_check_test_arr_with_offset(void **state)
124+
{
125+
const int it = 3;
126+
127+
reset_test_arr();
128+
test_data->str[it] = default_char+1;
129+
130+
(void) state;
131+
132+
assert_int_equal(check_test_arr(default_char), it);
133+
}
134+
135+
static void test_lib_lib_bzero_char_zero_none(void **state)
136+
{
137+
reset_test_arr();
138+
139+
(void) state;
140+
141+
bzero(test_data->str, 0);
142+
assert_int_equal(check_test_arr(default_char), -1);
143+
}
144+
145+
static void test_lib_lib_bzero_char_zero_all(void **state)
146+
{
147+
reset_test_arr();
148+
149+
(void) state;
150+
151+
bzero(test_data->str, test_data->len);
152+
assert_int_equal(check_test_arr(0), -1);
153+
}
154+
155+
static void test_lib_lib_bzero_char_zero_middle_beg(void **state)
156+
{
157+
reset_test_arr();
158+
159+
(void) state;
160+
161+
bzero(test_data->str+test_data_zero_middle->beg,
162+
test_data_zero_middle->len);
163+
assert_int_equal(check_test_arr(default_char),
164+
test_data_zero_middle->beg);
165+
}
166+
167+
static void test_lib_lib_bzero_char_zero_middle_mid(void **state)
168+
{
169+
(void) state;
170+
171+
assert_int_equal(check_test_arr_with_offset(
172+
test_data_zero_middle->beg, -test_data_zero_middle->beg,
173+
0), test_data_zero_middle->len);
174+
}
175+
176+
static void test_lib_lib_bzero_char_zero_middle_end(void **state)
177+
{
178+
(void) state;
179+
180+
assert_int_equal(check_test_arr_with_offset(
181+
test_data_zero_middle->end, -test_data_zero_middle->end,
182+
default_char), -1);
183+
}
184+
185+
int main(void)
186+
{
187+
const struct CMUnitTest tests[] = {
188+
cmocka_unit_test(test_lib_lib_bzero_check_test_arr),
189+
cmocka_unit_test(test_lib_lib_bzero_check_test_arr_with_offset),
190+
cmocka_unit_test(test_lib_lib_bzero_char_zero_none),
191+
cmocka_unit_test(test_lib_lib_bzero_char_zero_all),
192+
cmocka_unit_test(test_lib_lib_bzero_char_zero_middle_beg),
193+
cmocka_unit_test(test_lib_lib_bzero_char_zero_middle_mid),
194+
cmocka_unit_test(test_lib_lib_bzero_char_zero_middle_end),
195+
};
196+
197+
cmocka_set_message_output(CM_OUTPUT_TAP);
198+
199+
return cmocka_run_group_tests(tests, setup, teardown);
200+
}

0 commit comments

Comments
 (0)