Skip to content

Commit

Permalink
tests/unittests: add test for CRC-32 checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Oct 4, 2022
1 parent 52633aa commit c12a048
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
73 changes: 73 additions & 0 deletions tests/unittests/tests-checksum/tests-checksum-crc32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2022 Benjamin Valentin <benpicco@googlemail.com>
*
* 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;
}
1 change: 1 addition & 0 deletions tests/unittests/tests-checksum/tests-checksum.c
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
7 changes: 7 additions & 0 deletions tests/unittests/tests-checksum/tests-checksum.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit c12a048

Please sign in to comment.