2014-03-31 14:02:47 +02:00
|
|
|
/*
|
2015-02-08 19:19:22 +01:00
|
|
|
* Copyright (C) 2014 Martine Lenders <mlenders@inf.fu-berlin.de>
|
2014-03-31 14:02:47 +02:00
|
|
|
*
|
2014-07-31 19:45:27 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2014-03-31 14:02:47 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <limits.h>
|
2014-11-26 15:59:55 +01:00
|
|
|
#include <stdint.h>
|
2014-03-31 14:02:47 +02:00
|
|
|
|
2014-11-27 22:30:14 +01:00
|
|
|
#include "embUnit.h"
|
2014-03-31 14:02:47 +02:00
|
|
|
|
|
|
|
#include "bitarithm.h"
|
|
|
|
|
|
|
|
#include "tests-core.h"
|
|
|
|
|
|
|
|
static void test_SETBIT_null_null(void)
|
|
|
|
{
|
|
|
|
int res = 0x00;
|
|
|
|
|
|
|
|
SETBIT(res, 0x00);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0x00, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_SETBIT_null_limit(void)
|
|
|
|
{
|
|
|
|
unsigned int res = 0x00;
|
|
|
|
|
|
|
|
SETBIT(res, UINT_MAX);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(UINT_MAX, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_SETBIT_limit_null(void)
|
|
|
|
{
|
|
|
|
unsigned int res = UINT_MAX;
|
|
|
|
|
|
|
|
SETBIT(res, 0x00);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(UINT_MAX, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_SETBIT_limit_limit(void)
|
|
|
|
{
|
|
|
|
unsigned int res = UINT_MAX;
|
|
|
|
|
|
|
|
SETBIT(res, UINT_MAX);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(UINT_MAX, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_SETBIT_null_one(void)
|
|
|
|
{
|
|
|
|
unsigned int res = 0x00;
|
|
|
|
|
|
|
|
SETBIT(res, 0x01);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0x01, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_SETBIT_one_null(void)
|
|
|
|
{
|
|
|
|
unsigned int res = 0x01;
|
|
|
|
|
|
|
|
SETBIT(res, 0x00);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0x01, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_SETBIT_one_random(void)
|
|
|
|
{
|
|
|
|
unsigned int res = 0x01;
|
|
|
|
|
|
|
|
SETBIT(res, 0x06); /* randomized by fair dice roll ;-) */
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0x07, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_CLRBIT_null_null(void)
|
|
|
|
{
|
|
|
|
int res = 0x00;
|
|
|
|
|
|
|
|
CLRBIT(res, 0x00);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0x00, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_CLRBIT_null_limit(void)
|
|
|
|
{
|
|
|
|
unsigned int res = 0x00;
|
|
|
|
|
|
|
|
CLRBIT(res, UINT_MAX);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0x00, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_CLRBIT_limit_null(void)
|
|
|
|
{
|
|
|
|
unsigned int res = UINT_MAX;
|
|
|
|
|
|
|
|
CLRBIT(res, 0x00);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(UINT_MAX, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_CLRBIT_limit_limit(void)
|
|
|
|
{
|
|
|
|
unsigned int res = UINT_MAX;
|
|
|
|
|
|
|
|
CLRBIT(res, UINT_MAX);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0x00, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_CLRBIT_null_one(void)
|
|
|
|
{
|
|
|
|
unsigned int res = 0x00;
|
|
|
|
|
|
|
|
CLRBIT(res, 0x01);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0x00, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_CLRBIT_one_null(void)
|
|
|
|
{
|
|
|
|
unsigned int res = 0x01;
|
|
|
|
|
|
|
|
CLRBIT(res, 0x00);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0x01, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_CLRBIT_one_random(void)
|
|
|
|
{
|
|
|
|
unsigned int res = 0x01;
|
|
|
|
|
|
|
|
CLRBIT(res, 0x05); /* randomized by fair dice roll ;-) */
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0x00, res);
|
|
|
|
}
|
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
static void test_bitarithm_msb_one(void)
|
2014-03-31 14:02:47 +02:00
|
|
|
{
|
2014-04-10 22:28:35 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT(0, bitarithm_msb(1));
|
2014-03-31 14:02:47 +02:00
|
|
|
}
|
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
static void test_bitarithm_msb_limit(void)
|
2014-03-31 14:02:47 +02:00
|
|
|
{
|
|
|
|
TEST_ASSERT_EQUAL_INT(sizeof(unsigned) * 8 - 1,
|
2014-04-10 22:28:35 +02:00
|
|
|
bitarithm_msb(UINT_MAX));
|
2014-03-31 14:02:47 +02:00
|
|
|
}
|
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
static void test_bitarithm_msb_random(void)
|
2014-03-31 14:02:47 +02:00
|
|
|
{
|
2014-11-26 15:59:55 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT(4, bitarithm_msb(19)); /* randomized by fair
|
2018-10-05 15:14:43 +02:00
|
|
|
* dice roll ;-)
|
|
|
|
*/
|
2014-03-31 14:02:47 +02:00
|
|
|
}
|
|
|
|
|
2014-11-26 15:59:55 +01:00
|
|
|
static void test_bitarithm_msb_16bit(void)
|
2014-05-20 00:03:23 +02:00
|
|
|
{
|
2014-11-26 15:59:55 +01:00
|
|
|
for (unsigned i = 1; i < UINT16_MAX; i++) {
|
|
|
|
TEST_ASSERT_EQUAL_INT(((sizeof(unsigned) * 8) - __builtin_clz(i) - 1), bitarithm_msb(i));
|
2014-05-20 00:03:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
static void test_bitarithm_lsb_one(void)
|
2014-03-31 14:02:47 +02:00
|
|
|
{
|
2014-04-10 22:28:35 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT(0, bitarithm_lsb(1));
|
2014-03-31 14:02:47 +02:00
|
|
|
}
|
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
static void test_bitarithm_lsb_limit(void)
|
2014-03-31 14:02:47 +02:00
|
|
|
{
|
2014-07-09 07:15:19 +02:00
|
|
|
unsigned shift = sizeof(unsigned) * 8 - 1;
|
2014-05-20 00:03:23 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT(shift, bitarithm_lsb(1u << shift));
|
2014-03-31 14:02:47 +02:00
|
|
|
}
|
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
static void test_bitarithm_lsb_random(void)
|
2014-03-31 14:02:47 +02:00
|
|
|
{
|
2014-11-26 15:59:55 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT(3, bitarithm_lsb(24)); /* randomized by fair
|
2014-03-31 14:02:47 +02:00
|
|
|
dice roll ;-) */
|
|
|
|
}
|
|
|
|
|
2014-05-20 00:03:23 +02:00
|
|
|
static void test_bitarithm_lsb_all(void)
|
|
|
|
{
|
2014-11-26 15:59:55 +01:00
|
|
|
for (unsigned i = 1; i < UINT16_MAX; i++) {
|
|
|
|
TEST_ASSERT_EQUAL_INT(__builtin_ctz(i), bitarithm_lsb(i));
|
2014-05-20 00:03:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
static void test_bitarithm_bits_set_null(void)
|
2014-03-31 14:02:47 +02:00
|
|
|
{
|
2014-04-10 22:28:35 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT(0, bitarithm_bits_set(0));
|
2014-03-31 14:02:47 +02:00
|
|
|
}
|
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
static void test_bitarithm_bits_set_one(void)
|
2014-03-31 14:02:47 +02:00
|
|
|
{
|
2014-04-10 22:28:35 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT(1, bitarithm_bits_set(1));
|
2014-03-31 14:02:47 +02:00
|
|
|
}
|
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
static void test_bitarithm_bits_set_limit(void)
|
2014-03-31 14:02:47 +02:00
|
|
|
{
|
|
|
|
TEST_ASSERT_EQUAL_INT(sizeof(unsigned) * 8,
|
2014-04-10 22:28:35 +02:00
|
|
|
bitarithm_bits_set(UINT_MAX));
|
2014-03-31 14:02:47 +02:00
|
|
|
}
|
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
static void test_bitarithm_bits_set_random(void)
|
2014-03-31 14:02:47 +02:00
|
|
|
{
|
2014-04-10 22:28:35 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT(3, bitarithm_bits_set(7)); /* randomized by fair
|
2018-10-05 15:14:43 +02:00
|
|
|
* dice roll ;-)
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_bitarithm_bits_set_u32_random(void)
|
|
|
|
{
|
2022-10-27 16:11:19 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT(21, bitarithm_bits_set_u32(4072524027));
|
|
|
|
/* Source: https://www.random.org/bytes */
|
2014-03-31 14:02:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Test *tests_core_bitarithm_tests(void)
|
|
|
|
{
|
|
|
|
EMB_UNIT_TESTFIXTURES(fixtures) {
|
|
|
|
new_TestFixture(test_SETBIT_null_null),
|
|
|
|
new_TestFixture(test_SETBIT_null_limit),
|
|
|
|
new_TestFixture(test_SETBIT_limit_null),
|
|
|
|
new_TestFixture(test_SETBIT_limit_limit),
|
|
|
|
new_TestFixture(test_SETBIT_null_one),
|
|
|
|
new_TestFixture(test_SETBIT_one_null),
|
|
|
|
new_TestFixture(test_SETBIT_one_random),
|
2014-05-20 00:03:23 +02:00
|
|
|
|
2014-03-31 14:02:47 +02:00
|
|
|
new_TestFixture(test_CLRBIT_null_null),
|
|
|
|
new_TestFixture(test_CLRBIT_null_limit),
|
|
|
|
new_TestFixture(test_CLRBIT_limit_null),
|
|
|
|
new_TestFixture(test_CLRBIT_limit_limit),
|
|
|
|
new_TestFixture(test_CLRBIT_null_one),
|
|
|
|
new_TestFixture(test_CLRBIT_one_null),
|
|
|
|
new_TestFixture(test_CLRBIT_one_random),
|
2014-05-20 00:03:23 +02:00
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
new_TestFixture(test_bitarithm_msb_one),
|
|
|
|
new_TestFixture(test_bitarithm_msb_limit),
|
|
|
|
new_TestFixture(test_bitarithm_msb_random),
|
2014-11-26 15:59:55 +01:00
|
|
|
new_TestFixture(test_bitarithm_msb_16bit),
|
2014-05-20 00:03:23 +02:00
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
new_TestFixture(test_bitarithm_lsb_one),
|
|
|
|
new_TestFixture(test_bitarithm_lsb_limit),
|
|
|
|
new_TestFixture(test_bitarithm_lsb_random),
|
2014-05-20 00:03:23 +02:00
|
|
|
new_TestFixture(test_bitarithm_lsb_all),
|
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
new_TestFixture(test_bitarithm_bits_set_null),
|
|
|
|
new_TestFixture(test_bitarithm_bits_set_one),
|
|
|
|
new_TestFixture(test_bitarithm_bits_set_limit),
|
|
|
|
new_TestFixture(test_bitarithm_bits_set_random),
|
2018-10-05 15:14:43 +02:00
|
|
|
new_TestFixture(test_bitarithm_bits_set_u32_random),
|
2014-03-31 14:02:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
EMB_UNIT_TESTCALLER(core_bitarithm_tests, NULL, NULL, fixtures);
|
|
|
|
|
|
|
|
return (Test *)&core_bitarithm_tests;
|
|
|
|
}
|