blob: f36a1382393d20607e3331e865a95264af893a3f [file] [log] [blame]
/*
* Copyright (c) 2022 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/sys/util.h>
#include <zephyr/kernel.h>
#include <zephyr/ztest.h>
/**
* @addtogroup sys_util_tests
* @{
*/
/**
* @brief Test wait_for works as expected with typical use cases
*
* @see WAIT_FOR()
*/
void test_wait_for(void)
{
uint32_t start, end, expected;
zassert_true(WAIT_FOR(true, 0, NULL), "true, no wait, NULL");
zassert_true(WAIT_FOR(true, 0, k_yield()), "true, no wait, yield");
zassert_false(WAIT_FOR(false, 0, k_yield()), "false, no wait, yield");
zassert_true(WAIT_FOR(true, 1, k_yield()), "true, 1usec, yield");
zassert_false(WAIT_FOR(false, 1, k_yield()), "false, 1usec, yield");
zassert_true(WAIT_FOR(true, 1000, k_yield()), "true, 1msec, yield");
expected = 1000*(sys_clock_hw_cycles_per_sec()/USEC_PER_SEC);
start = k_cycle_get_32();
zassert_false(WAIT_FOR(false, 1000, k_yield()), "true, 1msec, yield");
end = k_cycle_get_32();
zassert_true(end-start >= expected, "wait for 1ms");
}
/**
* @}
*/