[utest][atomic]添加返回值测试 (#7124)

* [utest][atomic]添加返回值测试
This commit is contained in:
Yaochenger 2023-03-30 11:06:21 +08:00 committed by GitHub
parent eec78d9f5d
commit 38eb3fc40f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 18 deletions

View File

@ -29,52 +29,67 @@ static void test_atomic_api(void)
{ {
rt_atomic_t base; rt_atomic_t base;
rt_atomic_t oldval; rt_atomic_t oldval;
rt_atomic_t result;
/* rt_atomic_t */ /* rt_atomic_t */
uassert_true(sizeof(rt_atomic_t) == ATOMIC_WORD(sizeof(uint64_t), sizeof(uint32_t))); uassert_true(sizeof(rt_atomic_t) == ATOMIC_WORD(sizeof(uint64_t), sizeof(uint32_t)));
/* rt_atomic_add */ /* rt_atomic_add */
base = 0; base = 0;
rt_atomic_add(&base, 10); result = rt_atomic_add(&base, 10);
uassert_true(base == 10); uassert_true(base == 10);
uassert_true(result == 0);
/* rt_atomic_add negative */ /* rt_atomic_add negative */
base = 2; base = 2;
rt_atomic_add(&base, -4); result = rt_atomic_add(&base, -4);
uassert_true(base == -2); uassert_true(base == -2);
uassert_true(result == 2);
/* rt_atomic_sub */ /* rt_atomic_sub */
base = 11; base = 11;
rt_atomic_sub(&base, 10); result = rt_atomic_sub(&base, 10);
uassert_true(base == 1); uassert_true(base == 1);
uassert_true(result == 11);
/* rt_atomic_sub negative */ /* rt_atomic_sub negative */
base = 2; base = 2;
rt_atomic_sub(&base, -5); result = rt_atomic_sub(&base, -5);
uassert_true(base == 7); uassert_true(base == 7);
uassert_true(result == 2);
/* rt_atomic_or */ /* rt_atomic_or */
base = 0xFF00; base = 0xFF00;
rt_atomic_or(&base, 0x0F0F); result = rt_atomic_or(&base, 0x0F0F);
uassert_true(base == 0xFF0F); uassert_true(base == 0xFF0F);
uassert_true(result == 0xFF00);
/* rt_atomic_xor */ /* rt_atomic_xor */
base = 0xFF00; base = 0xFF00;
rt_atomic_xor(&base, 0x0F0F); result = rt_atomic_xor(&base, 0x0F0F);
uassert_true(base == 0xF00F); uassert_true(base == 0xF00F);
uassert_true(result == 0xFF00);
/* rt_atomic_and */ /* rt_atomic_and */
base = 0xFF00; base = 0xFF00;
rt_atomic_and(&base, 0x0F0F); result = rt_atomic_and(&base, 0x0F0F);
uassert_true(base == 0x0F00); uassert_true(base == 0x0F00);
uassert_true(result == 0xFF00);
/* rt_atomic_exchange */ /* rt_atomic_exchange */
base = 0xFF00; base = 0xFF00;
rt_atomic_exchange(&base, 0x0F0F); result = rt_atomic_exchange(&base, 0x0F0F);
uassert_true(base == 0x0F0F); uassert_true(base == 0x0F0F);
uassert_true(result == 0xFF00);
/* rt_atomic_flag_test_and_set */ /* rt_atomic_flag_test_and_set (Flag 0) */
base = 0x0; base = 0x0;
rt_atomic_flag_test_and_set(&base); result = rt_atomic_flag_test_and_set(&base);
uassert_true(base == 0x1); uassert_true(base == 0x1);
uassert_true(result == 0x0);
/* rt_atomic_flag_test_and_set (Flag 1) */
base = 0x1;
result = rt_atomic_flag_test_and_set(&base);
uassert_true(base == 0x1);
uassert_true(result == 0x1);
/* rt_atomic_flag_clear */ /* rt_atomic_flag_clear */
base = 0x1; base = 0x1;
@ -83,19 +98,27 @@ static void test_atomic_api(void)
/* rt_atomic_load */ /* rt_atomic_load */
base = 0xFF00; base = 0xFF00;
rt_atomic_load(&base); result = rt_atomic_load(&base);
uassert_true(base == 0xFF00); uassert_true(base == 0xFF00);
uassert_true(result == 0xFF00);
/* rt_atomic_store */ /* rt_atomic_store */
base = 0xFF00; base = 0xFF00;
rt_atomic_store(&base, 0x0F0F); rt_atomic_store(&base, 0x0F0F);
uassert_true(base == 0x0F0F); uassert_true(base == 0x0F0F);
/* rt_atomic_compare_exchange_strong */ /* rt_atomic_compare_exchange_strong (equal) */
base = 10; base = 10;
oldval = 10; oldval = 10;
uassert_true(rt_atomic_compare_exchange_strong(&base, &oldval, 11) == 1); result = rt_atomic_compare_exchange_strong(&base, &oldval, 11);
uassert_true(base == 11); uassert_true(base == 11);
uassert_true(result == 0x1);
/* rt_atomic_compare_exchange_strong (not equal) */
base = 10;
oldval = 5;
result = rt_atomic_compare_exchange_strong(&base, &oldval, 11);
uassert_true(base == 10);
uassert_true(result == 0x0);
} }
static void ture_entry(void *parameter) static void ture_entry(void *parameter)

View File

@ -77,7 +77,7 @@ rt_atomic_t rt_hw_atomic_load(volatile rt_atomic_t *ptr)
{ {
oldval = __LDREXW(ptr); oldval = __LDREXW(ptr);
} while ((__STREXW(oldval, ptr)) != 0U); } while ((__STREXW(oldval, ptr)) != 0U);
return *ptr; return oldval;
} }
void rt_hw_atomic_store(volatile rt_atomic_t *ptr, rt_atomic_t val) void rt_hw_atomic_store(volatile rt_atomic_t *ptr, rt_atomic_t val)
@ -166,7 +166,7 @@ rt_atomic_t rt_hw_atomic_flag_test_and_set(volatile rt_atomic_t *ptr)
return oldval; return oldval;
} }
rt_atomic_t rt_hw_atomic_compare_exchange_strong(volatile rt_atomic_t *ptr, rt_atomic_t *old, rt_atomic_t new) rt_atomic_t rt_hw_atomic_compare_exchange_strong(volatile rt_atomic_t *ptr, volatile rt_atomic_t *old, rt_atomic_t new)
{ {
rt_atomic_t result; rt_atomic_t result;
rt_atomic_t temp = *old; rt_atomic_t temp = *old;

View File

@ -120,7 +120,7 @@ void rt_hw_atomic_flag_clear(volatile rt_atomic_t *ptr)
#endif #endif
} }
rt_atomic_t rt_hw_atomic_compare_exchange_strong(volatile rt_atomic_t *ptr, rt_atomic_t *old, rt_atomic_t new) rt_atomic_t rt_hw_atomic_compare_exchange_strong(volatile rt_atomic_t *ptr, volatile rt_atomic_t *old, rt_atomic_t new)
{ {
rt_atomic_t tmp = *old; rt_atomic_t tmp = *old;
rt_atomic_t result; rt_atomic_t result;