From 166cec26204fcbd0419c6496cec2c742ab6b45fd Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 24 Mar 2024 17:04:36 +0800 Subject: [PATCH] test: using test helper --- test/hash.cc | 17 +++-------------- test/helper.h | 19 +++++++++++++++++++ test/simple.cc | 17 +++++++++-------- test/stream.cc | 6 ++---- 4 files changed, 33 insertions(+), 26 deletions(-) create mode 100644 test/helper.h diff --git a/test/hash.cc b/test/hash.cc index 82fc6ae..0d23b8e 100644 --- a/test/hash.cc +++ b/test/hash.cc @@ -1,4 +1,5 @@ #include "md5.h" +#include "helper.h" #include "gtest/gtest.h" using md5::MD5; @@ -262,14 +263,6 @@ const std::array, 256> test_items {{ {0xff, "11b7aaa64c413d2f0fccf893881c46a2"}, }}; -std::string build_test_data(uint8_t index) { - std::string data(index, 0x00); - for (uint8_t i = 0; i < index; ++i) { - data[i] = static_cast(i); - } - return data; -}; - TEST(md5sum, hash) { for (const auto &[index, expect] : test_items) { auto data = build_test_data(index); @@ -279,13 +272,9 @@ TEST(md5sum, hash) { } TEST(md5sum, hash_ce) { - auto to_string = [](const std::array &digest) { - return std::string {digest.data(), 32}; - }; - for (const auto &[index, expect] : test_items) { auto data = build_test_data(index); - EXPECT_EQ(to_string(MD5::HashCE(data)), expect); - EXPECT_EQ(to_string(MD5::HashCE(data.c_str(), index)), expect); + EXPECT_EQ(MD5::HashCE(data), expect); + EXPECT_EQ(MD5::HashCE(data.c_str(), index), expect); } } diff --git a/test/helper.h b/test/helper.h new file mode 100644 index 0000000..6aaf083 --- /dev/null +++ b/test/helper.h @@ -0,0 +1,19 @@ +#pragma once + +#include + +inline std::string build_test_data(const uint32_t size) { + std::string data(size, 0x00); + for (uint32_t i = 0; i < size; ++i) { + data[i] = static_cast(i); + } + return data; +}; + +namespace testing::internal { + +inline bool operator==(const std::array &s1, const std::string_view &s2) { + return std::string {s1.data(), 32} == s2; +} + +} // namespace testing::internal diff --git a/test/simple.cc b/test/simple.cc index b415d04..dce489f 100644 --- a/test/simple.cc +++ b/test/simple.cc @@ -1,4 +1,5 @@ #include "md5.h" +#include "helper.h" #include "gtest/gtest.h" using md5::MD5; @@ -6,21 +7,21 @@ using md5::MD5; TEST(md5sum, empty) { auto expect = "d41d8cd98f00b204e9800998ecf8427e"; + EXPECT_EQ(MD5::Hash(""), expect); + EXPECT_EQ(MD5::HashCE(""), expect); + EXPECT_EQ(MD5().Final().Digest(), expect); + MD5 md5; - EXPECT_EQ(md5.Final().Digest(), expect); - md5.Reset(); - EXPECT_EQ(md5.Final().Digest(), expect); - md5.Reset(); - EXPECT_EQ(md5.Update("").Final().Digest(), expect); + EXPECT_EQ(md5.Reset().Final().Digest(), expect); + EXPECT_EQ(md5.Reset().Final().Digest(), expect); + EXPECT_EQ(md5.Reset().Update("").Final().Digest(), expect); } TEST(md5sum, simple) { auto expect = "5227827849ea5e9d942ff40dbbfaffd6"; EXPECT_EQ(MD5::Hash("dnomd343"), expect); - - auto result = std::string {MD5::HashCE("dnomd343").data(), 32}; - EXPECT_EQ(result, expect); + EXPECT_EQ(MD5::HashCE("dnomd343"), expect); MD5 md5; EXPECT_EQ(md5.Reset().Update("").Update("dnomd343").Final().Digest(), expect); diff --git a/test/stream.cc b/test/stream.cc index 21e4774..b6a7f9e 100644 --- a/test/stream.cc +++ b/test/stream.cc @@ -1,13 +1,11 @@ #include "md5.h" +#include "helper.h" #include "gtest/gtest.h" using md5::MD5; TEST(md5sum, stream) { - std::string test_data(256 * 256, 0x00); // max -> 65536 - for (uint64_t i = 0; i < test_data.size(); ++i) { - test_data[i] = static_cast(i & 0xff); - } + auto test_data = build_test_data(256 * 256); MD5 md5; for (uint64_t size = 1; size <= 256; ++size) {