From 2051ad184614f02975ea8ba923cf5bff13c2535a Mon Sep 17 00:00:00 2001 From: Anas Nashif Date: Sat, 28 Sep 2019 09:06:02 -0400 Subject: [PATCH] tests: convert util test to a unit test Merge tests/misc/util with existing util unit test. Signed-off-by: Anas Nashif --- tests/misc/util/CMakeLists.txt | 8 --- tests/misc/util/prj.conf | 1 - tests/misc/util/src/main.c | 110 --------------------------------- tests/misc/util/testcase.yaml | 3 - tests/unit/util/main.c | 86 +++++++++++++++++++++++++- 5 files changed, 85 insertions(+), 123 deletions(-) delete mode 100644 tests/misc/util/CMakeLists.txt delete mode 100644 tests/misc/util/prj.conf delete mode 100644 tests/misc/util/src/main.c delete mode 100644 tests/misc/util/testcase.yaml diff --git a/tests/misc/util/CMakeLists.txt b/tests/misc/util/CMakeLists.txt deleted file mode 100644 index 0b1bfc6258b..00000000000 --- a/tests/misc/util/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 - -cmake_minimum_required(VERSION 3.13.1) -include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) -project(util) - -FILE(GLOB app_sources src/*.c) -target_sources(app PRIVATE ${app_sources}) diff --git a/tests/misc/util/prj.conf b/tests/misc/util/prj.conf deleted file mode 100644 index 9467c292689..00000000000 --- a/tests/misc/util/prj.conf +++ /dev/null @@ -1 +0,0 @@ -CONFIG_ZTEST=y diff --git a/tests/misc/util/src/main.c b/tests/misc/util/src/main.c deleted file mode 100644 index 047f8707b4a..00000000000 --- a/tests/misc/util/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (c) 2019 Nordic Semiconductor ASA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include - -#define TEST_DEFINE_1 1 -#define TEST_DEFINE_0 0 - -void test_COND_CODE_1(void) -{ - /* Test validates that expected code has been injected. Failure would - * be seen in compilation (lack of variable or ununsed variable. - */ - COND_CODE_1(1, (u32_t x0 = 1;), (u32_t y0;)) - zassert_true((x0 == 1), NULL); - - COND_CODE_1(NOT_EXISTING_DEFINE, (u32_t x1 = 1;), (u32_t y1 = 1;)) - zassert_true((y1 == 1), NULL); - - COND_CODE_1(TEST_DEFINE_1, (u32_t x2 = 1;), (u32_t y2 = 1;)) - zassert_true((x2 == 1), NULL); - - COND_CODE_1(2, (u32_t x3 = 1;), (u32_t y3 = 1;)) - zassert_true((y3 == 1), NULL); -} - -void test_COND_CODE_0(void) -{ - /* Test validates that expected code has been injected. Failure would - * be seen in compilation (lack of variable or ununsed variable. - */ - COND_CODE_0(0, (u32_t x0 = 1;), (u32_t y0;)) - zassert_true((x0 == 1), NULL); - - COND_CODE_0(NOT_EXISTING_DEFINE, (u32_t x1 = 1;), (u32_t y1 = 1;)) - zassert_true((y1 == 1), NULL); - - COND_CODE_0(TEST_DEFINE_0, (u32_t x2 = 1;), (u32_t y2 = 1;)) - zassert_true((x2 == 1), NULL); - - COND_CODE_0(2, (u32_t x3 = 1;), (u32_t y3 = 1;)) - zassert_true((y3 == 1), NULL); -} - -void test_UTIL_LISTIFY(void) -{ - int i = 0; - -#define INC(x, _) \ - do { \ - i += x; \ - } while (0); - -#define DEFINE(x, _) int a##x; -#define MARK_UNUSED(x, _) ARG_UNUSED(a##x); - - UTIL_LISTIFY(4, DEFINE, _) - UTIL_LISTIFY(4, MARK_UNUSED, _) - - UTIL_LISTIFY(4, INC, _) - - zassert_equal(i, 0 + 1 + 2 + 3, NULL); -} - -static int inc_func(void) -{ - static int a = 1; - - return a++; -} - -/* Test checks if @ref Z_MAX and @ref Z_MIN return correct result and perform - * single evaluation of input arguments. - */ -static void test_z_max_z_min(void) -{ - zassert_equal(Z_MAX(inc_func(), 0), 1, "Unexpected macro result"); - /* Z_MAX should have call inc_func only once */ - zassert_equal(inc_func(), 2, "Unexpected return value"); - - zassert_equal(Z_MIN(inc_func(), 2), 2, "Unexpected macro result"); - /* Z_MIN should have call inc_func only once */ - zassert_equal(inc_func(), 4, "Unexpected return value"); -} - -/*test case main entry*/ -void test_main(void) -{ - ztest_test_suite(test_util_api, - ztest_unit_test(test_COND_CODE_1), - ztest_unit_test(test_COND_CODE_0), - ztest_unit_test(test_UTIL_LISTIFY), - ztest_unit_test(test_z_max_z_min) - ); - ztest_run_test_suite(test_util_api); -} diff --git a/tests/misc/util/testcase.yaml b/tests/misc/util/testcase.yaml deleted file mode 100644 index 9e85fa670e1..00000000000 --- a/tests/misc/util/testcase.yaml +++ /dev/null @@ -1,3 +0,0 @@ -tests: - misc.util: - tags: util diff --git a/tests/unit/util/main.c b/tests/unit/util/main.c index 276b1eb6b7c..6363409fe10 100644 --- a/tests/unit/util/main.c +++ b/tests/unit/util/main.c @@ -71,10 +71,94 @@ static void test_u8_to_dec(void) "Length of converted value using 0 byte buffer isn't 0"); } +#define TEST_DEFINE_1 1 +#define TEST_DEFINE_0 0 + +void test_COND_CODE_1(void) +{ + /* Test validates that expected code has been injected. Failure would + * be seen in compilation (lack of variable or ununsed variable. + */ + COND_CODE_1(1, (u32_t x0 = 1;), (u32_t y0;)) + zassert_true((x0 == 1), NULL); + + COND_CODE_1(NOT_EXISTING_DEFINE, (u32_t x1 = 1;), (u32_t y1 = 1;)) + zassert_true((y1 == 1), NULL); + + COND_CODE_1(TEST_DEFINE_1, (u32_t x2 = 1;), (u32_t y2 = 1;)) + zassert_true((x2 == 1), NULL); + + COND_CODE_1(2, (u32_t x3 = 1;), (u32_t y3 = 1;)) + zassert_true((y3 == 1), NULL); +} + +void test_COND_CODE_0(void) +{ + /* Test validates that expected code has been injected. Failure would + * be seen in compilation (lack of variable or ununsed variable. + */ + COND_CODE_0(0, (u32_t x0 = 1;), (u32_t y0;)) + zassert_true((x0 == 1), NULL); + + COND_CODE_0(NOT_EXISTING_DEFINE, (u32_t x1 = 1;), (u32_t y1 = 1;)) + zassert_true((y1 == 1), NULL); + + COND_CODE_0(TEST_DEFINE_0, (u32_t x2 = 1;), (u32_t y2 = 1;)) + zassert_true((x2 == 1), NULL); + + COND_CODE_0(2, (u32_t x3 = 1;), (u32_t y3 = 1;)) + zassert_true((y3 == 1), NULL); +} + +void test_UTIL_LISTIFY(void) +{ + int i = 0; + +#define INC(x, _) \ + do { \ + i += x; \ + } while (0); + +#define DEFINE(x, _) int a##x; +#define MARK_UNUSED(x, _) ARG_UNUSED(a##x); + + UTIL_LISTIFY(4, DEFINE, _) + UTIL_LISTIFY(4, MARK_UNUSED, _) + + UTIL_LISTIFY(4, INC, _) + + zassert_equal(i, 0 + 1 + 2 + 3, NULL); +} + +static int inc_func(void) +{ + static int a = 1; + + return a++; +} + +/* Test checks if @ref Z_MAX and @ref Z_MIN return correct result and perform + * single evaluation of input arguments. + */ +static void test_z_max_z_min(void) +{ + zassert_equal(Z_MAX(inc_func(), 0), 1, "Unexpected macro result"); + /* Z_MAX should have call inc_func only once */ + zassert_equal(inc_func(), 2, "Unexpected return value"); + + zassert_equal(Z_MIN(inc_func(), 2), 2, "Unexpected macro result"); + /* Z_MIN should have call inc_func only once */ + zassert_equal(inc_func(), 4, "Unexpected return value"); +} + void test_main(void) { ztest_test_suite(test_lib_sys_util_tests, - ztest_unit_test(test_u8_to_dec) + ztest_unit_test(test_u8_to_dec), + ztest_unit_test(test_COND_CODE_1), + ztest_unit_test(test_COND_CODE_0), + ztest_unit_test(test_UTIL_LISTIFY), + ztest_unit_test(test_z_max_z_min) ); ztest_run_test_suite(test_lib_sys_util_tests);