From ab3e5bd1a6ba79b18dd7dd91c54aa5c83df12abf Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Tue, 4 Oct 2022 12:03:42 +0200 Subject: [PATCH] tests/unittests: add test for CRC-32 checksum --- .../tests-checksum/tests-checksum-crc32.c | 73 +++++++++++++++++++ .../unittests/tests-checksum/tests-checksum.c | 1 + .../unittests/tests-checksum/tests-checksum.h | 7 ++ 3 files changed, 81 insertions(+) create mode 100644 tests/unittests/tests-checksum/tests-checksum-crc32.c diff --git a/tests/unittests/tests-checksum/tests-checksum-crc32.c b/tests/unittests/tests-checksum/tests-checksum-crc32.c new file mode 100644 index 0000000000000..32be40dbaebd0 --- /dev/null +++ b/tests/unittests/tests-checksum/tests-checksum-crc32.c @@ -0,0 +1,73 @@ +/* + * Copyright 2022 Benjamin Valentin + * + * 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 "checksum/crc32.h" +#include "tests-checksum.h" + +static void test_checksum_crc32_sequence_empty(void) +{ + unsigned char buf[] = ""; + uint32_t expect = 0x0; + + TEST_ASSERT_EQUAL_INT(expect, crc32(buf, sizeof(buf) - 1)); +} + +static void test_checksum_crc32_sequence_1a(void) +{ + unsigned char buf[] = "A"; + uint32_t expect = 0xd3d99e8b; + + TEST_ASSERT_EQUAL_INT(expect, crc32(buf, sizeof(buf) - 1)); +} + +static void test_checksum_crc32_sequence_256a(void) +{ + unsigned char buf[] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; + uint32_t expect = 0x49975b13; + + TEST_ASSERT_EQUAL_INT(expect, crc32(buf, sizeof(buf) - 1)); +} + +static void test_checksum_crc32_sequence_1to9(void) +{ + unsigned char buf[] = "123456789"; + uint32_t expect = 0xcbf43926; + + TEST_ASSERT_EQUAL_INT(expect, crc32(buf, sizeof(buf) - 1)); +} + +static void test_checksum_crc32_sequence_4bytes(void) +{ + unsigned char buf[] = { 0x12, 0x34, 0x56, 0x78 }; + uint32_t expect = 0x4a090e98; + + TEST_ASSERT_EQUAL_INT(expect, crc32(buf, sizeof(buf))); +} + +Test *tests_checksum_crc32_tests(void) +{ + EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(test_checksum_crc32_sequence_empty), + new_TestFixture(test_checksum_crc32_sequence_1a), + new_TestFixture(test_checksum_crc32_sequence_256a), + new_TestFixture(test_checksum_crc32_sequence_1to9), + new_TestFixture(test_checksum_crc32_sequence_4bytes), + }; + + EMB_UNIT_TESTCALLER(checksum_crc32_tests, NULL, NULL, fixtures); + + return (Test *)&checksum_crc32_tests; +} diff --git a/tests/unittests/tests-checksum/tests-checksum.c b/tests/unittests/tests-checksum/tests-checksum.c index 9ed7eb2a43e82..93aa035c9deee 100644 --- a/tests/unittests/tests-checksum/tests-checksum.c +++ b/tests/unittests/tests-checksum/tests-checksum.c @@ -15,6 +15,7 @@ void tests_checksum(void) TESTS_RUN(tests_checksum_crc16_ccitt_mcrf4xx_tests()); TESTS_RUN(tests_checksum_crc16_ccitt_aug_tests()); TESTS_RUN(tests_checksum_crc16_ccitt_false_tests()); + TESTS_RUN(tests_checksum_crc32_tests()); TESTS_RUN(tests_checksum_fletcher16_tests()); TESTS_RUN(tests_checksum_fletcher32_tests()); TESTS_RUN(tests_checksum_ucrc16_tests()); diff --git a/tests/unittests/tests-checksum/tests-checksum.h b/tests/unittests/tests-checksum/tests-checksum.h index b6c68fca26d2d..48999035bdcca 100644 --- a/tests/unittests/tests-checksum/tests-checksum.h +++ b/tests/unittests/tests-checksum/tests-checksum.h @@ -64,6 +64,13 @@ Test *tests_checksum_crc16_ccitt_false_tests(void); */ Test *tests_checksum_crc16_ccitt_aug_tests(void); +/** + * @brief Generates tests for checksum/crc32.h + * + * @return embUnit tests if successful, NULL if not. + */ +Test *tests_checksum_crc32_tests(void); + /** * @brief Generates tests for checksum/fletcher16.h *