From f393ae33d3ce7e52a5176f7bbc73da2635c1ce88 Mon Sep 17 00:00:00 2001 From: Michal Jerzy Wierzbicki Date: Wed, 8 Aug 2018 13:53:34 +0200 Subject: [PATCH] test: lib/bzero: Added unit tests for lib/bzero. Signed-off-by: Michal Jerzy Wierzbicki --- test/cmocka/Makefile.am | 7 +- test/cmocka/src/lib/lib/bzero.c | 200 ++++++++++++++++++++++++++++++++ 2 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 test/cmocka/src/lib/lib/bzero.c diff --git a/test/cmocka/Makefile.am b/test/cmocka/Makefile.am index ea917abfc..397c06c5e 100644 --- a/test/cmocka/Makefile.am +++ b/test/cmocka/Makefile.am @@ -55,7 +55,12 @@ check_PROGRAMS += rstrcmp rstrcmp_SOURCES = src/lib/lib/rstrcmp.c rstrcmp_LDADD = ../../src/lib/libcore.a $(LDADD) -#pipeline tests +check_PROGRAMS += bzero +bzero_SOURCES = src/lib/lib/bzero.c +bzero_LDADD = ../../src/lib/libcore.a $(LDADD) + +# pipeline tests + check_PROGRAMS += pipeline_new 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 diff --git a/test/cmocka/src/lib/lib/bzero.c b/test/cmocka/src/lib/lib/bzero.c new file mode 100644 index 000000000..facd3388f --- /dev/null +++ b/test/cmocka/src/lib/lib/bzero.c @@ -0,0 +1,200 @@ +/* + * Copyright (c) 2018, Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Intel Corporation nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Author: Michal Jerzy Wierzbicki + */ + +#include + +#include +#include +#include + +struct test_data_t { + size_t len; + char str[0]; +}; + +struct test_data_zero_middle_t { + int beg; + int end; + int len; +}; + +static struct test_data_t *test_data; +static struct test_data_zero_middle_t *test_data_zero_middle; +static const char default_char = 'a'; + +static void reset_test_arr(void) +{ + int i; + + for (i = 0; i < test_data->len-1; ++i) + test_data->str[i] = default_char; + test_data->str[test_data->len] = 0; +} + +static int setup(void **state) +{ + const int len = 6; + + test_data = malloc(sizeof(struct test_data_t) + + len * sizeof(test_data[0])); + test_data->len = len; + + const int zero_beg = 2; + const int zero_end = 4; + // aa000aa... - pattern of zeroing + + test_data_zero_middle = malloc(sizeof(struct test_data_zero_middle_t)); + test_data_zero_middle->beg = zero_beg; + test_data_zero_middle->end = zero_end; + test_data_zero_middle->len = zero_end - zero_beg; + + return 0; +} + +static int teardown(void **state) +{ + free(test_data); + + return 0; +} + +static int check_arr(char *arr, size_t n, char should_be) +{ + size_t i; + + for (i = 0; i < n; ++i) + if (arr[i] != should_be) + return i; + return -1; +} + +static int check_test_arr(char should_be) +{ + return check_arr(test_data->str, test_data->len-1, should_be); +} + +static int check_test_arr_with_offset( + int offset_beg, int offset_end, char should_be) +{ + return check_arr( + test_data->str+offset_beg, + test_data->len+offset_end-1, + should_be + ); +} + +/* Tests*/ +static void test_lib_lib_bzero_check_test_arr(void **state) +{ + reset_test_arr(); + + (void) state; + + assert_int_equal(check_test_arr(default_char), -1); +} + +static void test_lib_lib_bzero_check_test_arr_with_offset(void **state) +{ + const int it = 3; + + reset_test_arr(); + test_data->str[it] = default_char+1; + + (void) state; + + assert_int_equal(check_test_arr(default_char), it); +} + +static void test_lib_lib_bzero_char_zero_none(void **state) +{ + reset_test_arr(); + + (void) state; + + bzero(test_data->str, 0); + assert_int_equal(check_test_arr(default_char), -1); +} + +static void test_lib_lib_bzero_char_zero_all(void **state) +{ + reset_test_arr(); + + (void) state; + + bzero(test_data->str, test_data->len); + assert_int_equal(check_test_arr(0), -1); +} + +static void test_lib_lib_bzero_char_zero_middle_beg(void **state) +{ + reset_test_arr(); + + (void) state; + + bzero(test_data->str+test_data_zero_middle->beg, + test_data_zero_middle->len); + assert_int_equal(check_test_arr(default_char), + test_data_zero_middle->beg); +} + +static void test_lib_lib_bzero_char_zero_middle_mid(void **state) +{ + (void) state; + + assert_int_equal(check_test_arr_with_offset( + test_data_zero_middle->beg, -test_data_zero_middle->beg, + 0), test_data_zero_middle->len); +} + +static void test_lib_lib_bzero_char_zero_middle_end(void **state) +{ + (void) state; + + assert_int_equal(check_test_arr_with_offset( + test_data_zero_middle->end, -test_data_zero_middle->end, + default_char), -1); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_lib_lib_bzero_check_test_arr), + cmocka_unit_test(test_lib_lib_bzero_check_test_arr_with_offset), + cmocka_unit_test(test_lib_lib_bzero_char_zero_none), + cmocka_unit_test(test_lib_lib_bzero_char_zero_all), + cmocka_unit_test(test_lib_lib_bzero_char_zero_middle_beg), + cmocka_unit_test(test_lib_lib_bzero_char_zero_middle_mid), + cmocka_unit_test(test_lib_lib_bzero_char_zero_middle_end), + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + + return cmocka_run_group_tests(tests, setup, teardown); +}