Browse Source

test: using test helper

master
Dnomd343 8 months ago
parent
commit
166cec2620
  1. 17
      test/hash.cc
  2. 19
      test/helper.h
  3. 17
      test/simple.cc
  4. 6
      test/stream.cc

17
test/hash.cc

@ -1,4 +1,5 @@
#include "md5.h" #include "md5.h"
#include "helper.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
using md5::MD5; using md5::MD5;
@ -262,14 +263,6 @@ const std::array<std::pair<uint8_t, std::string_view>, 256> test_items {{
{0xff, "11b7aaa64c413d2f0fccf893881c46a2"}, {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<char>(i);
}
return data;
};
TEST(md5sum, hash) { TEST(md5sum, hash) {
for (const auto &[index, expect] : test_items) { for (const auto &[index, expect] : test_items) {
auto data = build_test_data(index); auto data = build_test_data(index);
@ -279,13 +272,9 @@ TEST(md5sum, hash) {
} }
TEST(md5sum, hash_ce) { TEST(md5sum, hash_ce) {
auto to_string = [](const std::array<char, 32> &digest) {
return std::string {digest.data(), 32};
};
for (const auto &[index, expect] : test_items) { for (const auto &[index, expect] : test_items) {
auto data = build_test_data(index); auto data = build_test_data(index);
EXPECT_EQ(to_string(MD5::HashCE(data)), expect); EXPECT_EQ(MD5::HashCE(data), expect);
EXPECT_EQ(to_string(MD5::HashCE(data.c_str(), index)), expect); EXPECT_EQ(MD5::HashCE(data.c_str(), index), expect);
} }
} }

19
test/helper.h

@ -0,0 +1,19 @@
#pragma once
#include <string>
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<char>(i);
}
return data;
};
namespace testing::internal {
inline bool operator==(const std::array<char, 32> &s1, const std::string_view &s2) {
return std::string {s1.data(), 32} == s2;
}
} // namespace testing::internal

17
test/simple.cc

@ -1,4 +1,5 @@
#include "md5.h" #include "md5.h"
#include "helper.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
using md5::MD5; using md5::MD5;
@ -6,21 +7,21 @@ using md5::MD5;
TEST(md5sum, empty) { TEST(md5sum, empty) {
auto expect = "d41d8cd98f00b204e9800998ecf8427e"; auto expect = "d41d8cd98f00b204e9800998ecf8427e";
EXPECT_EQ(MD5::Hash(""), expect);
EXPECT_EQ(MD5::HashCE(""), expect);
EXPECT_EQ(MD5().Final().Digest(), expect);
MD5 md5; MD5 md5;
EXPECT_EQ(md5.Final().Digest(), expect); EXPECT_EQ(md5.Reset().Final().Digest(), expect);
md5.Reset(); EXPECT_EQ(md5.Reset().Final().Digest(), expect);
EXPECT_EQ(md5.Final().Digest(), expect); EXPECT_EQ(md5.Reset().Update("").Final().Digest(), expect);
md5.Reset();
EXPECT_EQ(md5.Update("").Final().Digest(), expect);
} }
TEST(md5sum, simple) { TEST(md5sum, simple) {
auto expect = "5227827849ea5e9d942ff40dbbfaffd6"; auto expect = "5227827849ea5e9d942ff40dbbfaffd6";
EXPECT_EQ(MD5::Hash("dnomd343"), expect); EXPECT_EQ(MD5::Hash("dnomd343"), expect);
EXPECT_EQ(MD5::HashCE("dnomd343"), expect);
auto result = std::string {MD5::HashCE("dnomd343").data(), 32};
EXPECT_EQ(result, expect);
MD5 md5; MD5 md5;
EXPECT_EQ(md5.Reset().Update("").Update("dnomd343").Final().Digest(), expect); EXPECT_EQ(md5.Reset().Update("").Update("dnomd343").Final().Digest(), expect);

6
test/stream.cc

@ -1,13 +1,11 @@
#include "md5.h" #include "md5.h"
#include "helper.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
using md5::MD5; using md5::MD5;
TEST(md5sum, stream) { TEST(md5sum, stream) {
std::string test_data(256 * 256, 0x00); // max -> 65536 auto test_data = build_test_data(256 * 256);
for (uint64_t i = 0; i < test_data.size(); ++i) {
test_data[i] = static_cast<char>(i & 0xff);
}
MD5 md5; MD5 md5;
for (uint64_t size = 1; size <= 256; ++size) { for (uint64_t size = 1; size <= 256; ++size) {

Loading…
Cancel
Save