tests: atomic operation: add new test cases

add new test cases to illustrate the zephyr OS
support an array of atomic variables, each bit
of which can be modified.

Signed-off-by: Ying ming <mingx.ying@intel.com>
This commit is contained in:
Ying ming 2020-05-15 14:28:20 +08:00 committed by Anas Nashif
commit 1b1d728a18

View file

@ -7,6 +7,9 @@
#include <ztest.h> #include <ztest.h>
#include <sys/atomic.h> #include <sys/atomic.h>
/* an example of the number of atomic bit in an array */
#define NUM_FLAG_BITS 100
/** /**
* @addtogroup kernel_common_tests * @addtogroup kernel_common_tests
* @{ * @{
@ -31,6 +34,8 @@ void test_atomic(void)
atomic_val_t oldvalue; atomic_val_t oldvalue;
void *ptr_value, *old_ptr_value; void *ptr_value, *old_ptr_value;
ATOMIC_DEFINE(flag_bits, NUM_FLAG_BITS) = {0};
target = 4; target = 4;
value = 5; value = 5;
oldvalue = 6; oldvalue = 6;
@ -188,6 +193,16 @@ void test_atomic(void)
atomic_set_bit_to(&target, i, true); atomic_set_bit_to(&target, i, true);
zassert_true(target == (orig | (1 << i)), "atomic_set_bit_to"); zassert_true(target == (orig | (1 << i)), "atomic_set_bit_to");
} }
/* ATOMIC_DEFINE */
for (i = 0; i < NUM_FLAG_BITS; i++) {
atomic_set_bit(flag_bits, i);
zassert_true(!!atomic_test_bit(flag_bits, i) == !!(1),
"Failed to set a single bit in an array of atomic variables");
atomic_clear_bit(flag_bits, i);
zassert_true(!!atomic_test_bit(flag_bits, i) == !!(0),
"Failed to clear a single bit in an array of atomic variables");
}
} }
/** /**
* @} * @}