Skip to content

Commit

Permalink
#50 impelement some more helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mcerdeiro committed Jun 13, 2014
1 parent b33fd94 commit 46bd2d2
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 6 deletions.
10 changes: 10 additions & 0 deletions modules/libs/inc/ciaaLibs_CircBuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ extern "C" {
#define ciaaLibs_circBufWritePos(cbuf) \
((void*)(&(cbuf)->buf[(cbuf)->tail]))

/** \brief get pointer to the position to read
**
** This function is provided for the circular buffer reader.
**
** \param[in] cbuf pointer to the circular buffer
** \return pointer to the first byte to be read
**/
#define ciaaLibs_circBufReadPos(cbuf) \
((void*)(&(cbuf)->buf[(cbuf)->head]))

/** \brief calculate new tail
**
** This function is provided for the circular buffer writter.
Expand Down
29 changes: 29 additions & 0 deletions modules/libs/inc/ciaaLibs_Maths.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
/*
* modification history (new versions first)
* -----------------------------------------------------------
* 20140613 v0.0.2 MaCe implement min, max, setBit and clearBit
* 20140611 v0.0.1 initials initial version
*/

Expand All @@ -70,16 +71,44 @@ extern "C" {
**/
#define ciaaLibs_isPowerOfTwo(num) ( ((num) != 0) && (!((num) & ((num) - 1))) )

/** \brief Return the minimum of two values
**
** \param[in] x first value to be compared
** \param[in] y second value to be compared
** \return the smaller of both given values
**/
#define ciaaLibs_min(x,y) \
( ( (x) > (y) ) ? \
(y) : \
(x) )

/** \brief Return the maximum of two values
**
** \param[in] x first value to be compared
** \param[in] y second value to be compared
** \return the bigues of both given values
**/
#define ciaaLibs_max(x,y) \
( ( (x) > (y) ) ? \
(x) : \
(y) )

/** \brief Set a specific bit of a variable
**
** \param[inout] var variable to set a bit
** \param[in] bit bit to be set
**/
#define ciaaLibs_setBit(var, bit) \
((var) |= ( 1 << (bit) ))

/** \brief Clear a specific bit of a variable
**
** \param[inout] var variable to clear a bit
** \param[in] bit bit to be cleared
**/
#define ciaaLibs_clearBit(var, bit) \
((var) &= (~( 1 << (bit) )))

/*==================[typedef]================================================*/

/*==================[external data declaration]==============================*/
Expand Down
4 changes: 2 additions & 2 deletions modules/libs/src/ciaaLibs_CircBuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ extern size_t ciaaLibs_circBufGet(ciaaLibs_CircBufType * cbuf, void * data, size
/* check if all data can be read without wrapping */
if (nbytes <= rawCount)
{
ciaaPOSIX_memcpy(data, (void*)&cbuf->buf[cbuf->head], nbytes);
ciaaPOSIX_memcpy(data, ciaaLibs_circBufReadPos(cbuf), nbytes);
}
else
{
ciaaPOSIX_memcpy(data, (void*)(&cbuf->buf[cbuf->head]), rawCount);
ciaaPOSIX_memcpy(data, ciaaLibs_circBufReadPos(cbuf), rawCount);
ciaaPOSIX_memcpy((void*)((intptr_t)data + rawCount), (void*)(&cbuf->buf[0]), nbytes-rawCount);
}

Expand Down
58 changes: 54 additions & 4 deletions modules/libs/test/utest/src/test_ciaaLibs_Maths.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
/*
* modification history (new versions first)
* -----------------------------------------------------------
* 20140613 v0.0.2 MaCe implement min, max, setBit and clearBit
* 20140611 v0.0.1 MaCe implement power of two
*/

Expand Down Expand Up @@ -90,10 +91,7 @@ void tearDown(void) {
void doNothing(void) {
}

/** \brief test strncmp
**
** test the function ciaaPOSIX_strncmp
**
/** \brief test ciaaLibs_isPowerOfTwo
**/
void test_ciaaLibs_isPowerOfTwo(void) {
int32_t val;
Expand All @@ -114,6 +112,58 @@ void test_ciaaLibs_isPowerOfTwo(void) {
TEST_ASSERT_TRUE(ciaaLibs_isPowerOfTwo(val) == 1);
}

/** \brief test ciaaLibs_min
**/
void test_ciaaLibs_min(void) {
TEST_ASSERT_EQUAL_INT(ciaaLibs_min(10, 15), 10);
TEST_ASSERT_EQUAL_INT(ciaaLibs_min(-10, 15), -10);
TEST_ASSERT_EQUAL_INT(ciaaLibs_min(-10, -15), -15);
}

/** \brief test ciaaLibs_max
**/
void test_ciaaLibs_max(void) {
TEST_ASSERT_EQUAL_INT(ciaaLibs_max(10, 15), 15);
TEST_ASSERT_EQUAL_INT(ciaaLibs_max(-10, 15), 15);
TEST_ASSERT_EQUAL_INT(ciaaLibs_max(-10, -15), -10);
}

/** \brief test ciaaLibs_setBit
**/
void test_ciaaLibs_setBit(void) {
int32_t val = 0;

ciaaLibs_setBit(val, 1);
TEST_ASSERT_EQUAL_INT(0x2, val);

ciaaLibs_setBit(val, 7);
TEST_ASSERT_EQUAL_INT(0x82, val);

ciaaLibs_setBit(val, 2);
TEST_ASSERT_EQUAL_INT(0x86, val);

ciaaLibs_setBit(val, 6);
TEST_ASSERT_EQUAL_INT(0xC6, val);
}

/** \brief test ciaaLibs_clearBit
**/
void test_ciaaLibs_clearBit(void) {
int32_t val = 0xFF;

ciaaLibs_clearBit(val, 1);
TEST_ASSERT_EQUAL_INT(0xFD, val);

ciaaLibs_clearBit(val, 7);
TEST_ASSERT_EQUAL_INT(0x7D, val);

ciaaLibs_clearBit(val, 2);
TEST_ASSERT_EQUAL_INT(0x79, val);

ciaaLibs_clearBit(val, 6);
TEST_ASSERT_EQUAL_INT(0x39, val);
}

/** @} doxygen end group definition */
/** @} doxygen end group definition */
/** @} doxygen end group definition */
Expand Down

0 comments on commit 46bd2d2

Please sign in to comment.