MbedCRC: improve default constructors

Original default constructor implementation wasn't quite working
properly; it didn't cope with the new "mode_limit" parameter.

Change mechanism so that this now works:

    MbedCRC<POLY32_BIT_ANSI_CRC, 32, CrcMode::TABLE> crc;
pull/11897/head
Kevin Bracey 2019-11-21 10:53:30 +02:00
parent 83a9606140
commit ccd2a32ad1
2 changed files with 51 additions and 34 deletions

View File

@ -143,6 +143,28 @@ void test_partial_crc()
}
}
void test_mode_limit()
{
const char test[] = "123456789";
uint32_t crc;
{
MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::BITWISE> ct;
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
TEST_ASSERT_EQUAL(0xCBF43926, crc);
}
{
MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE> ct;
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
TEST_ASSERT_EQUAL(0xCBF43926, crc);
}
{
MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::HARDWARE> ct;
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
TEST_ASSERT_EQUAL(0xCBF43926, crc);
}
}
void test_sd_crc()
{
MbedCRC<POLY_7BIT_SD, 7> crc7;
@ -232,6 +254,7 @@ void test_thread_safety()
Case cases[] = {
Case("Test supported polynomials", test_supported_polynomials),
Case("Test partial CRC", test_partial_crc),
Case("Test mode-limited CRC", test_mode_limit),
Case("Test SD CRC polynomials", test_sd_crc),
#if defined(MBED_CONF_RTOS_PRESENT)
Case("Test thread safety", test_thread_safety),

View File

@ -186,8 +186,34 @@ public:
{
}
constexpr
MbedCRC();
/* Default values for different types of polynomials
*/
// *INDENT-OFF*
template<uint32_t poly = polynomial, std::enable_if_t<poly == POLY_32BIT_ANSI && width == 32, int> = 0>
constexpr MbedCRC() : MbedCRC(0xFFFFFFFF, 0xFFFFFFFF, true, true)
{
}
template<uint32_t poly = polynomial, std::enable_if_t<poly == POLY_16BIT_IBM && width == 16, int> = 0>
constexpr MbedCRC() : MbedCRC(0, 0, true, true)
{
}
template<uint32_t poly = polynomial, std::enable_if_t<poly == POLY_16BIT_CCITT && width == 16, int> = 0>
constexpr MbedCRC() : MbedCRC(0xFFFF, 0, false, false)
{
}
template<uint32_t poly = polynomial, std::enable_if_t<poly == POLY_7BIT_SD && width == 7, int> = 0>
constexpr MbedCRC() : MbedCRC(0, 0, false, false)
{
}
template<uint32_t poly = polynomial, std::enable_if_t<poly == POLY_8BIT_CCITT && width == 8, int> = 0>
constexpr MbedCRC() : MbedCRC(0, 0, false, false)
{
}
// *INDENT-ON*
/** Compute CRC for the data input
* Compute CRC performs the initialization, computation and collection of
@ -842,38 +868,6 @@ const uint32_t MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE>::_crc_table[MBED_CRC
#endif // !defined(DOXYGEN_ONLY)
/* Default values for different types of polynomials
*/
template<>
inline MSTD_CONSTEXPR_FN_14
MbedCRC<POLY_32BIT_ANSI, 32>::MbedCRC() : MbedCRC(0xFFFFFFFF, 0xFFFFFFFF, true, true)
{
}
template<>
inline MSTD_CONSTEXPR_FN_14
MbedCRC<POLY_16BIT_IBM, 16>::MbedCRC() : MbedCRC(0, 0, true, true)
{
}
template<>
inline MSTD_CONSTEXPR_FN_14
MbedCRC<POLY_16BIT_CCITT, 16>::MbedCRC() : MbedCRC(0xFFFF, 0, false, false)
{
}
template<>
inline MSTD_CONSTEXPR_FN_14
MbedCRC<POLY_7BIT_SD, 7>::MbedCRC(): MbedCRC(0, 0, false, false)
{
}
template<>
inline MSTD_CONSTEXPR_FN_14
MbedCRC<POLY_8BIT_CCITT, 8>::MbedCRC(): MbedCRC(0, 0, false, false)
{
}
/** @}*/
/** @}*/