Skip to content

Commit

Permalink
tests: ztest: Add test for busy simulator
Browse files Browse the repository at this point in the history
Added test that validates busy simulator.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
  • Loading branch information
nordic-krch committed Jul 23, 2021
1 parent 395cfa5 commit 3a5a77f
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tests/ztest/busy_sim/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-License-Identifier: Apache-2.0

# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.13.1)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(uart_basic_api)

target_sources(app PRIVATE
src/main.c
)
1 change: 1 addition & 0 deletions tests/ztest/busy_sim/boards/nrf52840dk_nrf52840.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_COUNTER_TIMER0=y
18 changes: 18 additions & 0 deletions tests/ztest/busy_sim/boards/nrf52840dk_nrf52840.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

&timer0 {
status = "okay";
interrupts = <8 0>;
};

/ {
busy-sim {
compatible = "vnd,busy-sim";
status = "okay";
counter = <&timer0>;
};
};
19 changes: 19 additions & 0 deletions tests/ztest/busy_sim/boards/nrf52840dk_nrf52840_pin.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

&timer0 {
status = "okay";
interrupts = <8 0>;
};

/ {
busy-sim {
compatible = "vnd,busy-sim";
status = "okay";
counter = <&timer0>;
active-gpios = <&gpio0 27 GPIO_ACTIVE_HIGH>;
};
};
3 changes: 3 additions & 0 deletions tests/ztest/busy_sim/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CONFIG_ZTEST=y
CONFIG_TEST_BUSY_SIM=y
CONFIG_ZTEST_THREAD_PRIORITY=1
51 changes: 51 additions & 0 deletions tests/ztest/busy_sim/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <ztest.h>
#include <busy_sim.h>

static void test_busy_sim(void)
{
uint32_t ms = 1000;
uint32_t delta = 80;
uint32_t busy_ms;
uint32_t t = k_uptime_get_32();

k_busy_wait(1000 * ms);
t = k_uptime_get_32() - t;

zassert_true((t > (ms - delta)) && (t < (ms + delta)), NULL);

/* Start busy simulator and check that k_busy_wait last longer */
t = k_uptime_get_32();
busy_sim_start(500, 200, 1000, 400);
k_busy_wait(1000 * ms);
t = k_uptime_get_32() - t;
busy_ms = (3 * ms) / 2;

busy_sim_stop();
/* due to clock imprecision, randomness and addtional cpu load overhead
* expected time range is increased.
*/
zassert_true((t > (busy_ms - 2 * delta)) && (t < (busy_ms + 4 * delta)),
"expected in range: %d-%d, k_busy_wait lasted %d",
busy_ms - 2 * delta, busy_ms + 4 * delta, t);

/* Check that k_busy_wait is not interrupted after busy_sim_stop. */
t = k_uptime_get_32();
k_busy_wait(1000 * ms);
t = k_uptime_get_32() - t;
zassert_true((t > (ms - delta)) && (t < (ms + delta)), NULL);
}

void test_main(void)
{
ztest_test_suite(busy_sim_tests,
ztest_unit_test(test_busy_sim)
);

ztest_run_test_suite(busy_sim_tests);
}
9 changes: 9 additions & 0 deletions tests/ztest/busy_sim/testcase.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
common:
tags: test_framework
depends_on: counter
tests:
testing.ztest.busy_sim:
platform_allow: nrf52840dk_nrf52840
testing.ztest.busy_sim_nrf52840dk_pin:
platform_allow: nrf52840dk_nrf52840
extra_args: DTC_OVERLAY_FILE=boards/nrf52840dk_nrf52840_pin.overlay

0 comments on commit 3a5a77f

Please sign in to comment.