diff --git a/tests/unittests/tests-core/tests-core-byteorder.c b/tests/unittests/tests-core/tests-core-byteorder.c new file mode 100644 index 0000000000..3dc940eddf --- /dev/null +++ b/tests/unittests/tests-core/tests-core-byteorder.c @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2014 René Kijewski + * + * 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. + */ + +#include "embUnit/embUnit.h" + +#include "byteorder.h" + +#include "tests-core.h" + +static void test_byteorder_little_to_big_16(void) +{ + le_uint16_t little = { .u8 = { 0x12, 0x34 } }; + be_uint16_t big = { .u8 = { 0x34, 0x12 } }; + TEST_ASSERT_EQUAL_INT(big.u16, byteorder_ltobs(little).u16); +} + +static void test_byteorder_big_to_little_16(void) +{ + le_uint16_t little = { .u8 = { 0x12, 0x34 } }; + be_uint16_t big = { .u8 = { 0x34, 0x12 } }; + TEST_ASSERT_EQUAL_INT(little.u16, byteorder_btols(big).u16); +} + +static void test_byteorder_little_to_big_32(void) +{ + le_uint32_t little = { .u8 = { 0x12, 0x34, 0x56, 0x78 } }; + be_uint32_t big = { .u8 = { 0x78, 0x56, 0x34, 0x12 } }; + TEST_ASSERT_EQUAL_INT(big.u32, byteorder_ltobl(little).u32); +} + +static void test_byteorder_big_to_little_32(void) +{ + le_uint32_t little = { .u8 = { 0x12, 0x34, 0x56, 0x78 } }; + be_uint32_t big = { .u8 = { 0x78, 0x56, 0x34, 0x12 } }; + TEST_ASSERT_EQUAL_INT(little.u32, byteorder_btoll(big).u32); +} + +static void test_byteorder_little_to_big_64(void) +{ + le_uint64_t little = { .u8 = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } }; + be_uint64_t big = { .u8 = { 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12 } }; + TEST_ASSERT_EQUAL_INT(big.u64, byteorder_ltobll(little).u64); +} + +static void test_byteorder_big_to_little_64(void) +{ + le_uint64_t little = { .u8 = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } }; + be_uint64_t big = { .u8 = { 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12 } }; + TEST_ASSERT_EQUAL_INT(little.u64, byteorder_btolll(big).u64); +} + +static void test_byteorder_host_to_network_16(void) +{ + static const uint16_t host = 0x1234; + network_uint16_t network = { .u8 = { 0x12, 0x34 } }; + TEST_ASSERT_EQUAL_INT(network.u16, byteorder_htons(host).u16); + TEST_ASSERT_EQUAL_INT(host, byteorder_ntohs(network)); +} + +static void test_byteorder_host_to_network_32(void) +{ + static const uint32_t host = 0x12345678ul; + network_uint32_t network = { .u8 = { 0x12, 0x34, 0x56, 0x78 } }; + TEST_ASSERT_EQUAL_INT(network.u32, byteorder_htonl(host).u32); + TEST_ASSERT_EQUAL_INT(host, byteorder_ntohl(network)); +} + +static void test_byteorder_host_to_network_64(void) +{ + static const uint64_t host = 0x123456789abcdef0ull; + network_uint64_t network = { .u8 = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } }; + TEST_ASSERT_EQUAL_INT(network.u64, byteorder_htonll(host).u64); + TEST_ASSERT_EQUAL_INT(host, byteorder_ntohll(network)); +} + +Test *tests_core_byteorder_tests(void) +{ + EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(test_byteorder_little_to_big_16), + new_TestFixture(test_byteorder_big_to_little_16), + new_TestFixture(test_byteorder_little_to_big_32), + new_TestFixture(test_byteorder_big_to_little_32), + new_TestFixture(test_byteorder_little_to_big_64), + new_TestFixture(test_byteorder_big_to_little_64), + new_TestFixture(test_byteorder_host_to_network_16), + new_TestFixture(test_byteorder_host_to_network_32), + new_TestFixture(test_byteorder_host_to_network_64), + }; + + EMB_UNIT_TESTCALLER(core_byteorder_tests, NULL, NULL, fixtures); + return (Test *)&core_byteorder_tests; +} diff --git a/tests/unittests/tests-core/tests-core.c b/tests/unittests/tests-core/tests-core.c index 0659925a61..4d59f4f0ac 100644 --- a/tests/unittests/tests-core/tests-core.c +++ b/tests/unittests/tests-core/tests-core.c @@ -16,4 +16,5 @@ void tests_core(void) TESTS_RUN(tests_core_clist_tests()); TESTS_RUN(tests_core_lifo_tests()); TESTS_RUN(tests_core_priority_queue_tests()); + TESTS_RUN(tests_core_byteorder_tests()); } diff --git a/tests/unittests/tests-core/tests-core.h b/tests/unittests/tests-core/tests-core.h index ace070734f..6ad2775d2d 100644 --- a/tests/unittests/tests-core/tests-core.h +++ b/tests/unittests/tests-core/tests-core.h @@ -67,5 +67,12 @@ Test *tests_core_lifo_tests(void); */ Test *tests_core_priority_queue_tests(void); +/** + * @brief Generates tests for byteorder.h + * + * @return embUnit tests if successful, NULL if not. + */ +Test *tests_core_byteorder_tests(void); + #endif /* __TESTS_CORE_H_ */ /** @} */