From 8637dfa35239ea0fe21347ba8ab5d96039c064b6 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 24 Mar 2024 19:12:06 +0800 Subject: [PATCH] test: add static assertion --- CMakeLists.txt | 2 +- test/assert.cc | 200 +++++++++++++++++++++++++++++++++++++++++++++++++ test/hash.cc | 2 +- test/helper.h | 2 +- test/simple.cc | 4 +- test/stream.cc | 6 +- 6 files changed, 208 insertions(+), 8 deletions(-) create mode 100644 test/assert.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 3990a20..93faaa5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,7 @@ add_library(md5sum::md5sum ALIAS md5sum) if (MD5_ENABLE_TESTING) enable_testing() - add_executable(md5_test test/hash.cc test/simple.cc test/stream.cc) + add_executable(md5_test test/hash.cc test/assert.cc test/simple.cc test/stream.cc) target_link_libraries(md5_test PRIVATE md5sum::md5sum GTest::gtest_main) add_test(NAME md5_test COMMAND md5_test) endif() diff --git a/test/assert.cc b/test/assert.cc new file mode 100644 index 0000000..086186d --- /dev/null +++ b/test/assert.cc @@ -0,0 +1,200 @@ +#include "md5.h" + +using md5::impl::K; +using md5::impl::S; +using md5::impl::T; + +static_assert(K(0x00) == 0); +static_assert(K(0x01) == 1); +static_assert(K(0x02) == 2); +static_assert(K(0x03) == 3); +static_assert(K(0x04) == 4); +static_assert(K(0x05) == 5); +static_assert(K(0x06) == 6); +static_assert(K(0x07) == 7); +static_assert(K(0x08) == 8); +static_assert(K(0x09) == 9); +static_assert(K(0x0a) == 10); +static_assert(K(0x0b) == 11); +static_assert(K(0x0c) == 12); +static_assert(K(0x0d) == 13); +static_assert(K(0x0e) == 14); +static_assert(K(0x0f) == 15); +static_assert(K(0x10) == 1); +static_assert(K(0x11) == 6); +static_assert(K(0x12) == 11); +static_assert(K(0x13) == 0); +static_assert(K(0x14) == 5); +static_assert(K(0x15) == 10); +static_assert(K(0x16) == 15); +static_assert(K(0x17) == 4); +static_assert(K(0x18) == 9); +static_assert(K(0x19) == 14); +static_assert(K(0x1a) == 3); +static_assert(K(0x1b) == 8); +static_assert(K(0x1c) == 13); +static_assert(K(0x1d) == 2); +static_assert(K(0x1e) == 7); +static_assert(K(0x1f) == 12); +static_assert(K(0x20) == 5); +static_assert(K(0x21) == 8); +static_assert(K(0x22) == 11); +static_assert(K(0x23) == 14); +static_assert(K(0x24) == 1); +static_assert(K(0x25) == 4); +static_assert(K(0x26) == 7); +static_assert(K(0x27) == 10); +static_assert(K(0x28) == 13); +static_assert(K(0x29) == 0); +static_assert(K(0x2a) == 3); +static_assert(K(0x2b) == 6); +static_assert(K(0x2c) == 9); +static_assert(K(0x2d) == 12); +static_assert(K(0x2e) == 15); +static_assert(K(0x2f) == 2); +static_assert(K(0x30) == 0); +static_assert(K(0x31) == 7); +static_assert(K(0x32) == 14); +static_assert(K(0x33) == 5); +static_assert(K(0x34) == 12); +static_assert(K(0x35) == 3); +static_assert(K(0x36) == 10); +static_assert(K(0x37) == 1); +static_assert(K(0x38) == 8); +static_assert(K(0x39) == 15); +static_assert(K(0x3a) == 6); +static_assert(K(0x3b) == 13); +static_assert(K(0x3c) == 4); +static_assert(K(0x3d) == 11); +static_assert(K(0x3e) == 2); +static_assert(K(0x3f) == 9); + +static_assert(S(0x00) == 7); +static_assert(S(0x01) == 12); +static_assert(S(0x02) == 17); +static_assert(S(0x03) == 22); +static_assert(S(0x04) == 7); +static_assert(S(0x05) == 12); +static_assert(S(0x06) == 17); +static_assert(S(0x07) == 22); +static_assert(S(0x08) == 7); +static_assert(S(0x09) == 12); +static_assert(S(0x0a) == 17); +static_assert(S(0x0b) == 22); +static_assert(S(0x0c) == 7); +static_assert(S(0x0d) == 12); +static_assert(S(0x0e) == 17); +static_assert(S(0x0f) == 22); +static_assert(S(0x10) == 5); +static_assert(S(0x11) == 9); +static_assert(S(0x12) == 14); +static_assert(S(0x13) == 20); +static_assert(S(0x14) == 5); +static_assert(S(0x15) == 9); +static_assert(S(0x16) == 14); +static_assert(S(0x17) == 20); +static_assert(S(0x18) == 5); +static_assert(S(0x19) == 9); +static_assert(S(0x1a) == 14); +static_assert(S(0x1b) == 20); +static_assert(S(0x1c) == 5); +static_assert(S(0x1d) == 9); +static_assert(S(0x1e) == 14); +static_assert(S(0x1f) == 20); +static_assert(S(0x20) == 4); +static_assert(S(0x21) == 11); +static_assert(S(0x22) == 16); +static_assert(S(0x23) == 23); +static_assert(S(0x24) == 4); +static_assert(S(0x25) == 11); +static_assert(S(0x26) == 16); +static_assert(S(0x27) == 23); +static_assert(S(0x28) == 4); +static_assert(S(0x29) == 11); +static_assert(S(0x2a) == 16); +static_assert(S(0x2b) == 23); +static_assert(S(0x2c) == 4); +static_assert(S(0x2d) == 11); +static_assert(S(0x2e) == 16); +static_assert(S(0x2f) == 23); +static_assert(S(0x30) == 6); +static_assert(S(0x31) == 10); +static_assert(S(0x32) == 15); +static_assert(S(0x33) == 21); +static_assert(S(0x34) == 6); +static_assert(S(0x35) == 10); +static_assert(S(0x36) == 15); +static_assert(S(0x37) == 21); +static_assert(S(0x38) == 6); +static_assert(S(0x39) == 10); +static_assert(S(0x3a) == 15); +static_assert(S(0x3b) == 21); +static_assert(S(0x3c) == 6); +static_assert(S(0x3d) == 10); +static_assert(S(0x3e) == 15); +static_assert(S(0x3f) == 21); + +static_assert(T(0x00) == 0xd76aa478); +static_assert(T(0x01) == 0xe8c7b756); +static_assert(T(0x02) == 0x242070db); +static_assert(T(0x03) == 0xc1bdceee); +static_assert(T(0x04) == 0xf57c0faf); +static_assert(T(0x05) == 0x4787c62a); +static_assert(T(0x06) == 0xa8304613); +static_assert(T(0x07) == 0xfd469501); +static_assert(T(0x08) == 0x698098d8); +static_assert(T(0x09) == 0x8b44f7af); +static_assert(T(0x0a) == 0xffff5bb1); +static_assert(T(0x0b) == 0x895cd7be); +static_assert(T(0x0c) == 0x6b901122); +static_assert(T(0x0d) == 0xfd987193); +static_assert(T(0x0e) == 0xa679438e); +static_assert(T(0x0f) == 0x49b40821); +static_assert(T(0x10) == 0xf61e2562); +static_assert(T(0x11) == 0xc040b340); +static_assert(T(0x12) == 0x265e5a51); +static_assert(T(0x13) == 0xe9b6c7aa); +static_assert(T(0x14) == 0xd62f105d); +static_assert(T(0x15) == 0x02441453); +static_assert(T(0x16) == 0xd8a1e681); +static_assert(T(0x17) == 0xe7d3fbc8); +static_assert(T(0x18) == 0x21e1cde6); +static_assert(T(0x19) == 0xc33707d6); +static_assert(T(0x1a) == 0xf4d50d87); +static_assert(T(0x1b) == 0x455a14ed); +static_assert(T(0x1c) == 0xa9e3e905); +static_assert(T(0x1d) == 0xfcefa3f8); +static_assert(T(0x1e) == 0x676f02d9); +static_assert(T(0x1f) == 0x8d2a4c8a); +static_assert(T(0x20) == 0xfffa3942); +static_assert(T(0x21) == 0x8771f681); +static_assert(T(0x22) == 0x6d9d6122); +static_assert(T(0x23) == 0xfde5380c); +static_assert(T(0x24) == 0xa4beea44); +static_assert(T(0x25) == 0x4bdecfa9); +static_assert(T(0x26) == 0xf6bb4b60); +static_assert(T(0x27) == 0xbebfbc70); +static_assert(T(0x28) == 0x289b7ec6); +static_assert(T(0x29) == 0xeaa127fa); +static_assert(T(0x2a) == 0xd4ef3085); +static_assert(T(0x2b) == 0x04881d05); +static_assert(T(0x2c) == 0xd9d4d039); +static_assert(T(0x2d) == 0xe6db99e5); +static_assert(T(0x2e) == 0x1fa27cf8); +static_assert(T(0x2f) == 0xc4ac5665); +static_assert(T(0x30) == 0xf4292244); +static_assert(T(0x31) == 0x432aff97); +static_assert(T(0x32) == 0xab9423a7); +static_assert(T(0x33) == 0xfc93a039); +static_assert(T(0x34) == 0x655b59c3); +static_assert(T(0x35) == 0x8f0ccc92); +static_assert(T(0x36) == 0xffeff47d); +static_assert(T(0x37) == 0x85845dd1); +static_assert(T(0x38) == 0x6fa87e4f); +static_assert(T(0x39) == 0xfe2ce6e0); +static_assert(T(0x3a) == 0xa3014314); +static_assert(T(0x3b) == 0x4e0811a1); +static_assert(T(0x3c) == 0xf7537e82); +static_assert(T(0x3d) == 0xbd3af235); +static_assert(T(0x3e) == 0x2ad7d2bb); +static_assert(T(0x3f) == 0xeb86d391); diff --git a/test/hash.cc b/test/hash.cc index 0d23b8e..2996e71 100644 --- a/test/hash.cc +++ b/test/hash.cc @@ -4,7 +4,7 @@ using md5::MD5; -const std::array, 256> test_items {{ +constexpr std::array, 256> test_items {{ {0x00, "d41d8cd98f00b204e9800998ecf8427e"}, {0x01, "93b885adfe0da089cdf634904fd59f71"}, {0x02, "441077cc9e57554dd476bdfb8b8b8102"}, diff --git a/test/helper.h b/test/helper.h index 6aaf083..2776c27 100644 --- a/test/helper.h +++ b/test/helper.h @@ -8,7 +8,7 @@ inline std::string build_test_data(const uint32_t size) { data[i] = static_cast(i); } return data; -}; +} namespace testing::internal { diff --git a/test/simple.cc b/test/simple.cc index dce489f..37f2377 100644 --- a/test/simple.cc +++ b/test/simple.cc @@ -5,7 +5,7 @@ using md5::MD5; TEST(md5sum, empty) { - auto expect = "d41d8cd98f00b204e9800998ecf8427e"; + constexpr auto expect = "d41d8cd98f00b204e9800998ecf8427e"; EXPECT_EQ(MD5::Hash(""), expect); EXPECT_EQ(MD5::HashCE(""), expect); @@ -18,7 +18,7 @@ TEST(md5sum, empty) { } TEST(md5sum, simple) { - auto expect = "5227827849ea5e9d942ff40dbbfaffd6"; + constexpr auto expect = "5227827849ea5e9d942ff40dbbfaffd6"; EXPECT_EQ(MD5::Hash("dnomd343"), expect); EXPECT_EQ(MD5::HashCE("dnomd343"), expect); diff --git a/test/stream.cc b/test/stream.cc index b6a7f9e..587a6c2 100644 --- a/test/stream.cc +++ b/test/stream.cc @@ -5,21 +5,21 @@ using md5::MD5; TEST(md5sum, stream) { - auto test_data = build_test_data(256 * 256); + const auto test_data = build_test_data(256 * 256); MD5 md5; for (uint64_t size = 1; size <= 256; ++size) { auto expect = MD5::Hash(test_data.data(), size * 256); for (int times = 0; times < 256; ++times) { - auto offset = test_data.data() + times * size; + const auto offset = test_data.data() + times * size; md5.Update(offset, size); // update multiple times } EXPECT_EQ(md5.Final().Digest(), expect); md5.Reset(); // reset for next round for (int times = 0; times < 256; ++times) { - auto offset = test_data.data() + times * size; + const auto offset = test_data.data() + times * size; md5.Update(std::string_view {offset, size}); // update multiple times } EXPECT_EQ(md5.Final().Digest(), expect);