Skip to content

Commit

Permalink
tests/unittests: Add tests for endian.h
Browse files Browse the repository at this point in the history
  • Loading branch information
maribu committed Jan 30, 2024
1 parent 1c17365 commit bb99aa7
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/unittests/tests-libc/tests-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <stdint.h>
#include <string.h>
#include <endian.h>
#include "string_utils.h"

#include "tests-libc.h"
Expand Down Expand Up @@ -39,11 +40,44 @@ static void test_libc_memchk(void)
TEST_ASSERT(memchk(buffer, 0xff, sizeof(buffer)) == &buffer[5]);
}

union u16 {
uint8_t as_bytes[2];
uint16_t as_number;
};

union u32 {
uint8_t as_bytes[4];
uint32_t as_number;
};

union u64 {
uint8_t as_bytes[8];
uint64_t as_number;
};

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 } };

Check warning on line 64 in tests/unittests/tests-libc/tests-libc.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
const union u64 u64_le_expected = { .as_bytes = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 } };

Check warning on line 65 in tests/unittests/tests-libc/tests-libc.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters

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);
}

Test *tests_libc_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_libc_strscpy),
new_TestFixture(test_libc_memchk),
new_TestFixture(test_libc_endian),
};

EMB_UNIT_TESTCALLER(libc_tests, NULL, NULL, fixtures);
Expand Down

0 comments on commit bb99aa7

Please sign in to comment.