Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! tests/unittests: Add tests for endian.h
Browse files Browse the repository at this point in the history
maribu committed Jan 30, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
miri64 Martine Lenders
1 parent e0caef3 commit 9af76cf
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions tests/unittests/tests-libc/tests-libc.c
Original file line number Diff line number Diff line change
@@ -40,6 +40,11 @@ static void test_libc_memchk(void)
TEST_ASSERT(memchk(buffer, 0xff, sizeof(buffer)) == &buffer[5]);
}

/**
* @name Unit test ensuring `<endian.h>` is provided and correct across
* all platforms
* @{
*/
union u16 {
uint8_t as_bytes[2];
uint16_t as_number;
@@ -57,20 +62,35 @@ union u64 {

static void test_libc_endian(void)
{
const union u16 u16_be_expected = { .as_bytes = { 0x01, 0x02 } };
const union u16 u16_le_expected = { .as_bytes = { 0x02, 0x01 } };
const union u32 u32_be_expected = { .as_bytes = { 0x01, 0x02, 0x03, 0x04 } };
const union u32 u32_le_expected = { .as_bytes = { 0x04, 0x03, 0x02, 0x01 } };
const union u64 u64_be_expected = { .as_bytes = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 } };
const union u64 u64_le_expected = { .as_bytes = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 } };

TEST_ASSERT_EQUAL_INT(htobe16(0x0102), u16_be_expected.as_number);
TEST_ASSERT_EQUAL_INT(htole16(0x0102), u16_le_expected.as_number);
TEST_ASSERT_EQUAL_INT(htobe32(0x01020304), u32_be_expected.as_number);
TEST_ASSERT_EQUAL_INT(htole32(0x01020304), u32_le_expected.as_number);
TEST_ASSERT_EQUAL_INT(htobe64(0x0102030405060708), u64_be_expected.as_number);
TEST_ASSERT_EQUAL_INT(htole64(0x0102030405060708), u64_le_expected.as_number);
/* format the numbers 0x0102, 0x01020304 and 0x0102030405060708
* in little/big endian format by hand: */
const uint16_t u16_host = 0x0102;
const union u16 u16_be = { .as_bytes = { 0x01, 0x02 } };
const union u16 u16_le = { .as_bytes = { 0x02, 0x01 } };
const uint32_t u32_host = 0x01020304;
const union u32 u32_be = { .as_bytes = { 0x01, 0x02, 0x03, 0x04 } };
const union u32 u32_le = { .as_bytes = { 0x04, 0x03, 0x02, 0x01 } };
const uint64_t u64_host = 0x0102030405060708;
const union u64 u64_be = { .as_bytes = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 } };
const union u64 u64_le = { .as_bytes = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 } };

/* check from host to big/little endian */
TEST_ASSERT_EQUAL_INT(htobe16(u16_host), u16_be.as_number);
TEST_ASSERT_EQUAL_INT(htole16(u16_host), u16_le.as_number);
TEST_ASSERT_EQUAL_INT(htobe32(u32_host), u32_be.as_number);
TEST_ASSERT_EQUAL_INT(htole32(u32_host), u32_le.as_number);
TEST_ASSERT_EQUAL_INT(htobe64(u64_host), u64_be.as_number);
TEST_ASSERT_EQUAL_INT(htole64(u64_host), u64_le.as_number);

/* check little/big endian to host */
TEST_ASSERT_EQUAL_INT(be16toh(u16_be.as_number), u16_host);
TEST_ASSERT_EQUAL_INT(le16toh(u16_le.as_number), u16_host);
TEST_ASSERT_EQUAL_INT(be32toh(u32_be.as_number), u32_host);
TEST_ASSERT_EQUAL_INT(le32toh(u32_le.as_number), u32_host);
TEST_ASSERT_EQUAL_INT(be64toh(u64_be.as_number), u64_host);
TEST_ASSERT_EQUAL_INT(le64toh(u64_le.as_number), u64_host);
}
/** @} */

Test *tests_libc_tests(void)
{

0 comments on commit 9af76cf

Please sign in to comment.