1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00

Merge pull request #8119 from kaspar030/fix_cib

core: cib: fix overflow handling
This commit is contained in:
Kaspar Schleiser 2017-11-23 13:13:26 +01:00 committed by GitHub
commit 6b661bef04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 16 deletions

View File

@ -92,7 +92,7 @@ static inline unsigned int cib_full(const cib_t *cib)
*/
static inline int cib_get(cib_t *__restrict cib)
{
if (cib->write_count > cib->read_count) {
if (cib_avail(cib)) {
return (int) (cib->read_count++ & cib->mask);
}
@ -108,7 +108,7 @@ static inline int cib_get(cib_t *__restrict cib)
*/
static inline int cib_peek(cib_t *__restrict cib)
{
if (cib->write_count > cib->read_count) {
if (cib_avail(cib)) {
return (int) (cib->read_count & cib->mask);
}

View File

@ -6,13 +6,15 @@
* directory for more details.
*/
#include <limits.h>
#include "embUnit.h"
#include "cib.h"
#include "tests-core.h"
#define TEST_CIB_SIZE 2
#define TEST_CIB_SIZE (4)
static cib_t cib;
@ -23,8 +25,9 @@ static void set_up(void)
static void test_cib_put(void)
{
TEST_ASSERT_EQUAL_INT(0, cib_put(&cib));
TEST_ASSERT_EQUAL_INT(1, cib_put(&cib));
for (unsigned i = 0; i < TEST_CIB_SIZE; i++) {
TEST_ASSERT_EQUAL_INT(i, cib_put(&cib));
}
TEST_ASSERT_EQUAL_INT(-1, cib_put(&cib));
}
@ -36,6 +39,16 @@ static void test_cib_get(void)
TEST_ASSERT_EQUAL_INT(-1, cib_get(&cib));
}
static void test_cib_get__overflow(void)
{
/* XXX this is hacky, but bare with me ;-) */
cib.read_count = UINT_MAX;
cib.write_count = UINT_MAX;
TEST_ASSERT_EQUAL_INT(3, cib_put(&cib));
TEST_ASSERT_EQUAL_INT(3, cib_get(&cib));
}
static void test_cib_peek(void)
{
cib_init(&cib, TEST_CIB_SIZE);
@ -50,6 +63,16 @@ static void test_cib_peek(void)
TEST_ASSERT_EQUAL_INT(-1, cib_peek(&cib));
}
static void test_cib_peek__overflow(void)
{
/* XXX this is hacky, but bare with me ;-) */
cib.read_count = UINT_MAX;
cib.write_count = UINT_MAX;
TEST_ASSERT_EQUAL_INT(3, cib_put(&cib));
TEST_ASSERT_EQUAL_INT(3, cib_peek(&cib));
}
static void test_cib_avail(void)
{
TEST_ASSERT_EQUAL_INT(0, cib_avail(&cib));
@ -59,16 +82,6 @@ static void test_cib_avail(void)
TEST_ASSERT_EQUAL_INT(2, cib_avail(&cib));
}
static void test_cib_put_and_get(void)
{
TEST_ASSERT_EQUAL_INT(0, cib_put(&cib));
TEST_ASSERT_EQUAL_INT(0, cib_get(&cib));
TEST_ASSERT_EQUAL_INT(-1, cib_get(&cib));
TEST_ASSERT_EQUAL_INT(1, cib_put(&cib));
TEST_ASSERT_EQUAL_INT(0, cib_put(&cib));
TEST_ASSERT_EQUAL_INT(-1, cib_put(&cib));
}
static void test_empty_cib(void)
{
cib_init(&cib, 0);
@ -93,11 +106,12 @@ Test *tests_core_cib_tests(void)
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_cib_put),
new_TestFixture(test_cib_get),
new_TestFixture(test_cib_get__overflow),
new_TestFixture(test_cib_avail),
new_TestFixture(test_cib_put_and_get),
new_TestFixture(test_empty_cib),
new_TestFixture(test_singleton_cib),
new_TestFixture(test_cib_peek),
new_TestFixture(test_cib_peek__overflow),
};
EMB_UNIT_TESTCALLER(core_cib_tests, set_up, NULL, fixtures);