diff --git a/CMakeLists.txt b/CMakeLists.txt index fcc07f9..8eb005d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ else() set(CRC_LIBRARY 1) endif() +cmake_minimum_required(VERSION 3.1) project(crc) if(TARGET ${PROJECT_NAME}) @@ -11,8 +12,6 @@ if(TARGET ${PROJECT_NAME}) return() endif() -cmake_minimum_required(VERSION 3.1) - set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -30,7 +29,6 @@ target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}) if (CRC_TESTS) - file(GLOB SOURCE_CPP_TEST "unit-test/*.h" "unit-test/*.cpp" @@ -43,5 +41,7 @@ if (CRC_TESTS) add_executable(${PROJECT_NAME}_test ${SOURCE_CPP_TEST}) target_link_libraries(${PROJECT_NAME}_test PUBLIC ${PROJECT_NAME}) - + enable_testing() + add_test(NAME crc + COMMAND ${PROJECT_NAME}_test) endif() diff --git a/crc/crc16.c b/crc/crc16.c index 2944498..83a9b47 100644 --- a/crc/crc16.c +++ b/crc/crc16.c @@ -51,7 +51,7 @@ static const uint16_t crc16tab[256]= { 0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0 }; -uint16_t crc16(const char *buf, int len) { +uint16_t crc16(const unsigned char *buf, size_t len) { int counter; uint16_t crc = 0; for (counter = 0; counter < len; counter++) diff --git a/crc/crc16.h b/crc/crc16.h index 22ce833..c49c4a9 100644 --- a/crc/crc16.h +++ b/crc/crc16.h @@ -1,12 +1,14 @@ #ifndef _CRC_CRC16_H #define _CRC_CRC16_H -#include #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ -uint16_t crc16(const char *buf, int len); +#include +#include + +uint16_t crc16(const unsigned char *buf, size_t len); #ifdef __cplusplus } diff --git a/crc/crc32.c b/crc/crc32.c index 9c13b8f..db4d79b 100644 --- a/crc/crc32.c +++ b/crc/crc32.c @@ -69,7 +69,7 @@ static uint32_t crc32_tab[] = }; /* crc32 hash */ -uint32_t crc32(const char* s, int len) +uint32_t crc32(const unsigned char *s, size_t len) { int i; uint32_t crc32val = 0; diff --git a/crc/crc32.h b/crc/crc32.h index 4bfdc5f..9a90eab 100644 --- a/crc/crc32.h +++ b/crc/crc32.h @@ -1,12 +1,14 @@ #ifndef _CRC_CRC32_H #define _CRC_CRC32_H -#include #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ -uint32_t crc32(const char* s, int len); +#include +#include + +uint32_t crc32(const unsigned char *s, size_t len); #ifdef __cplusplus } diff --git a/crc/crc64.c b/crc/crc64.c index fd6b628..090b16c 100644 --- a/crc/crc64.c +++ b/crc/crc64.c @@ -170,7 +170,7 @@ static const uint64_t crc64_tab[256] = { 0x536fa08fdfd90e51ULL, 0x29b7d047efec8728ULL, }; -uint64_t crc64(const char *s, int l) { +uint64_t crc64(const unsigned char *s, size_t l) { int j = 0; uint64_t crc = 0; diff --git a/crc/crc64.h b/crc/crc64.h index 9c275e3..8ecbbc3 100644 --- a/crc/crc64.h +++ b/crc/crc64.h @@ -1,12 +1,14 @@ #ifndef _CRC_CRC64_H #define _CRC_CRC64_H -#include #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ -uint64_t crc64(const char *s, int l); +#include +#include + +uint64_t crc64(const unsigned char *s, size_t l); #ifdef __cplusplus } diff --git a/crc/crc8.c b/crc/crc8.c index bf4a430..bb95cc2 100644 --- a/crc/crc8.c +++ b/crc/crc8.c @@ -87,7 +87,7 @@ static const uint8_t crc8tab_msb[256] = { }; // LSB-first -uint8_t crc8_lsb(const char *buf, int len) { +uint8_t crc8_lsb(const unsigned char *buf, size_t len) { int counter; uint8_t crc = 0; for (counter = 0; counter < len; counter++) @@ -96,7 +96,7 @@ uint8_t crc8_lsb(const char *buf, int len) { } // MSB-first -uint8_t crc8_msb(const char *buf, int len) { +uint8_t crc8_msb(const unsigned char *buf, size_t len) { int counter; uint8_t crc = 0; for (counter = 0; counter < len; counter++) diff --git a/crc/crc8.h b/crc/crc8.h index 3a88d71..52d65b6 100644 --- a/crc/crc8.h +++ b/crc/crc8.h @@ -1,17 +1,18 @@ #ifndef _CRC_CRC8_H #define _CRC_CRC8_H -#include - #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ +#include +#include + // LSB-first -uint8_t crc8_lsb(const char *buf, int len); +uint8_t crc8_lsb(const unsigned char *buf, size_t len); // MSB-first -uint8_t crc8_msb(const char *buf, int len); +uint8_t crc8_msb(const unsigned char *buf, size_t len); #ifdef __cplusplus } diff --git a/crc/crc_poly.h b/crc/crc_poly.h index ab48e43..297b456 100644 --- a/crc/crc_poly.h +++ b/crc/crc_poly.h @@ -1,12 +1,12 @@ #ifndef _CRC_CRC_POLY_H #define _CRC_CRC_POLY_H -#include - #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ +#include + // LSB-first uint8_t crc8_poly_lsb(uint8_t poly, uint8_t value); diff --git a/crc/crchash.cpp b/crc/crchash.cpp index 945e0b8..27fb389 100644 --- a/crc/crchash.cpp +++ b/crc/crchash.cpp @@ -12,27 +12,27 @@ namespace common { uint16_t Hash16(const std::string& key) { - return crc16(key.c_str(), key.size()); + return crc16(reinterpret_cast(key.c_str()), key.size()); } - uint16_t Hash16(const char* cpKey, const int iKeyLen) + uint16_t Hash16(const unsigned char *cpKey, const size_t iKeyLen) { return crc16(cpKey, iKeyLen); } uint32_t Hash32(const std::string& key) { - return crc32(key.c_str(), key.size()); + return crc32(reinterpret_cast(key.c_str()), key.size()); } - uint32_t Hash32(const char* cpKey, const int iKeyLen) + uint32_t Hash32(const unsigned char *cpKey, const size_t iKeyLen) { return crc32(cpKey, iKeyLen); } uint64_t Hash64(const std::string& key) { - return crc64(key.c_str(), key.size()); + return crc64(reinterpret_cast(key.c_str()), key.size()); } - uint64_t Hash64(const char* cpKey, const int iKeyLen) + uint64_t Hash64(const unsigned char *cpKey, size_t iKeyLen) { return crc64(cpKey, iKeyLen); } diff --git a/crc/crchash.h b/crc/crchash.h index 5fd5bbb..007bc1b 100644 --- a/crc/crchash.h +++ b/crc/crchash.h @@ -8,18 +8,19 @@ #ifndef _COMMON_CRC_CRCHASH_H_ #define _COMMON_CRC_CRCHASH_H_ #include -#include +#include + namespace common { // string => 0x0000-0xffff uint16_t Hash16(const std::string& key); - uint16_t Hash16(const char* cpKey, const int iKeyLen); + uint16_t Hash16(const unsigned char *cpKey, size_t iKeyLen); // string => 0x00000000-0xffffffff uint32_t Hash32(const std::string& key); - uint32_t Hash32(const char* cpKey, const int iKeyLen); + uint32_t Hash32(const unsigned char *cpKey, size_t iKeyLen); // string => 0x0000000000000000-0xffffffffffffffff uint64_t Hash64(const std::string& key); - uint64_t Hash64(const char* cpKey, const int iKeyLen); + uint64_t Hash64(const unsigned char *cpKey, size_t iKeyLen); } // namespace common #endif // _COMMON_CRC_CRCHASH_H_ diff --git a/unit-test/crc_test.cpp b/unit-test/crc_test.cpp index 2482001..9c74935 100644 --- a/unit-test/crc_test.cpp +++ b/unit-test/crc_test.cpp @@ -6,26 +6,30 @@ */ #include "ut/test_harness.h" -#include "../crc/crc8.h" -#include "../crc/crc16.h" -#include "../crc/crc32.h" -#include "../crc/crc64.h" -#include "../crc/crc_poly.h" -#include -#include +#include "crc/crc8.h" +#include "crc/crc16.h" +#include "crc/crc32.h" +#include "crc/crc64.h" +#include "crc/crc_poly.h" +#include TEST(CRC8Test, BasicTest) { const char* inData = "123456789"; uint8_t expect = 0xa2; - EXPECT_EQ(crc8_msb(inData, 9), expect); + EXPECT_EQ(crc8_msb(reinterpret_cast(inData), 9), expect); const char* data = "Hello World!!!"; expect = 0x68; - EXPECT_EQ(crc8_msb(data, strlen(data)), expect); + EXPECT_EQ(crc8_msb(reinterpret_cast(data), strlen(data)), expect); expect = 0x2c; - EXPECT_EQ(crc8_lsb(data, strlen(data)), expect); + EXPECT_EQ(crc8_lsb(reinterpret_cast(data), strlen(data)), expect); + + const uint8_t buffer[] = {0xb0, 0, 0, 0, 0, 0, 0}; + + expect = 0x1a; + EXPECT_EQ(crc8_lsb(buffer, sizeof(buffer)), expect); } TEST(CRC16Test, BasicTest) @@ -33,7 +37,7 @@ TEST(CRC16Test, BasicTest) const char* inData = "123456789"; uint16_t expect = 0x31C3; - EXPECT_EQ(crc16(inData, 9), expect); + EXPECT_EQ(crc16(reinterpret_cast(inData), 9), expect); } TEST(CRC32Test, BasicTest) @@ -41,7 +45,7 @@ TEST(CRC32Test, BasicTest) const char* inData = "123456789"; uint32_t expect = 0xCBF43926; - EXPECT_EQ(crc32(inData, 9), expect); + EXPECT_EQ(crc32(reinterpret_cast(inData), 9), expect); } TEST(CRC64Test, BasicTest) @@ -49,7 +53,7 @@ TEST(CRC64Test, BasicTest) const char* inData = "123456789"; uint64_t expect = 0xe9c6d914c4b8d9caL; - EXPECT_EQ(crc64(inData, 9), expect); + EXPECT_EQ(crc64(reinterpret_cast(inData), 9), expect); } TEST(CRC8PolyTest, BasicTest) diff --git a/unit-test/ut/test_harness.cpp b/unit-test/ut/test_harness.cpp index 5c56b1b..b548d6c 100644 --- a/unit-test/ut/test_harness.cpp +++ b/unit-test/ut/test_harness.cpp @@ -1,12 +1,9 @@ #include "test_harness.h" -#include -#include -#include -#include -#include -#include #include +#include +#include +#include namespace common { namespace test { @@ -80,9 +77,15 @@ namespace common { startMs_, endMs, endMs - startMs_); } long TestPerfomence::NowMs() { - struct timeval timeNow; - gettimeofday(&timeNow, NULL); - return (timeNow.tv_sec) * 1000 + timeNow.tv_usec / 1000; + timespec timeNow{}; + + if (timespec_get(&timeNow, TIME_UTC) != TIME_UTC) + { + fputs("timespec_get failed!", stderr); + return 0; + } + + return static_cast(timeNow.tv_sec * 1000 + timeNow.tv_nsec / 1000); } } diff --git a/unit-test/ut/test_harness.h b/unit-test/ut/test_harness.h index e73633e..ebbf3bc 100644 --- a/unit-test/ut/test_harness.h +++ b/unit-test/ut/test_harness.h @@ -48,7 +48,7 @@ class Tester { fname_, line_, ss_.str().c_str()); fprintf(stderr, "\033[0m"); errCount++; - //exit(1); + exit(1); } } @@ -64,7 +64,7 @@ class Tester { template \ Tester& name(const X& x, const Y& y) { \ if (! (x op y)) { \ - ss_ << " failed: Expect:" << x << (" " #op " ") << "Actual:" << y; \ + ss_ << " failed: Expect:" << std::hex << uint64_t {x} << (" " #op " ") << "Actual:" << std::hex << uint64_t {y}; \ ok_ = false; \ } \ return *this; \