Skip to content

Commit 17d03ac

Browse files
authored
Merge pull request #21749 from benpicco/test/bit
tests/unittests: add test for bit.h
2 parents 7de0d1b + ac34cc7 commit 17d03ac

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

sys/include/bit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* @author Joakim Nohlgård <[email protected]>
1919
*/
2020

21+
#include <stdbool.h>
2122
#include <stdint.h>
2223
#include "cpu.h"
2324

tests/unittests/tests-bit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include $(RIOTBASE)/Makefile.base
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 ML!PA Consulting GmbH
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
6+
/**
7+
* @{
8+
*
9+
* @file
10+
* @author Benjamin Valentin <[email protected]>
11+
*
12+
* @}
13+
*/
14+
15+
#include <errno.h>
16+
#include "embUnit.h"
17+
18+
#include "bit.h"
19+
20+
static void test_bit8(void)
21+
{
22+
uint8_t tmp, word = UINT8_MAX;
23+
24+
for (unsigned i = 0; i < sizeof(word) * 8; ++i) {
25+
TEST_ASSERT_EQUAL_INT(1, bit_check8(&word, i));
26+
}
27+
28+
tmp = word;
29+
for (unsigned i = 0; i < sizeof(word) * 8; i += 2) {
30+
bit_clear8(&word, i);
31+
tmp &= ~(1UL << i);
32+
TEST_ASSERT_EQUAL_INT(tmp, word);
33+
}
34+
35+
word <<= 1;
36+
tmp = word;
37+
for (unsigned i = 1; i < sizeof(word) * 8; i += 2) {
38+
bit_set8(&word, i);
39+
tmp |= 1UL << i;
40+
TEST_ASSERT_EQUAL_INT(tmp, word);
41+
}
42+
}
43+
44+
static void test_bit16(void)
45+
{
46+
uint16_t tmp, word = UINT16_MAX;
47+
48+
for (unsigned i = 0; i < sizeof(word) * 8; ++i) {
49+
TEST_ASSERT_EQUAL_INT(1, bit_check16(&word, i));
50+
}
51+
52+
tmp = word;
53+
for (unsigned i = 0; i < sizeof(word) * 8; i += 2) {
54+
bit_clear16(&word, i);
55+
tmp &= ~(1UL << i);
56+
TEST_ASSERT_EQUAL_INT(tmp, word);
57+
}
58+
59+
word <<= 1;
60+
tmp = word;
61+
for (unsigned i = 1; i < sizeof(word) * 8; i += 2) {
62+
bit_set16(&word, i);
63+
tmp |= 1UL << i;
64+
TEST_ASSERT_EQUAL_INT(tmp, word);
65+
}
66+
}
67+
68+
static void test_bit32(void)
69+
{
70+
uint32_t tmp, word = UINT32_MAX;
71+
72+
for (unsigned i = 0; i < sizeof(word) * 8; ++i) {
73+
TEST_ASSERT_EQUAL_INT(1, bit_check32(&word, i));
74+
}
75+
76+
tmp = word;
77+
for (unsigned i = 0; i < sizeof(word) * 8; i += 2) {
78+
bit_clear32(&word, i);
79+
tmp &= ~(1UL << i);
80+
TEST_ASSERT_EQUAL_INT(tmp, word);
81+
}
82+
83+
word <<= 1;
84+
tmp = word;
85+
for (unsigned i = 1; i < sizeof(word) * 8; i += 2) {
86+
bit_set32(&word, i);
87+
tmp |= 1UL << i;
88+
TEST_ASSERT_EQUAL_INT(tmp, word);
89+
}
90+
}
91+
92+
Test *tests_bit_tests(void)
93+
{
94+
EMB_UNIT_TESTFIXTURES(fixtures) {
95+
new_TestFixture(test_bit8),
96+
new_TestFixture(test_bit16),
97+
new_TestFixture(test_bit32),
98+
};
99+
100+
EMB_UNIT_TESTCALLER(bit_tests, NULL, NULL, fixtures);
101+
102+
return (Test *)&bit_tests;
103+
}
104+
105+
void tests_bit(void)
106+
{
107+
TESTS_RUN(tests_bit_tests());
108+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 ML!PA Consulting GmbH
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
6+
#pragma once
7+
8+
/**
9+
* @addtogroup unittests
10+
* @{
11+
*
12+
* @file
13+
* @brief Unittests for the `bit` header
14+
*
15+
* This verifies that the functions to test/set/clear individual bits
16+
* (which may use hardware specific instructions to do so) work.
17+
*
18+
* @author Benjamin Valentin <[email protected]>
19+
*/
20+
21+
#include "embUnit/embUnit.h"
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
/**
28+
* @brief The entry point of this test suite.
29+
*/
30+
void tests_bit(void);
31+
32+
/**
33+
* @brief Generates tests for bit header
34+
*
35+
* @return embUnit tests if successful, NULL if not.
36+
*/
37+
Test *tests_bit_tests(void);
38+
39+
#ifdef __cplusplus
40+
}
41+
#endif
42+
43+
/** @} */

0 commit comments

Comments
 (0)