summaryrefslogtreecommitdiff
path: root/dviware/dvisvgm/tests/HashFunctionTest.cpp
blob: dad7a6276c99eec96c41072e0ab36e955906cf6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*************************************************************************
** HashFunctionTest.cpp                                                 **
**                                                                      **
** This file is part of dvisvgm -- a fast DVI to SVG converter          **
** Copyright (C) 2005-2019 Martin Gieseking <martin.gieseking@uos.de>   **
**                                                                      **
** This program is free software; you can redistribute it and/or        **
** modify it under the terms of the GNU General Public License as       **
** published by the Free Software Foundation; either version 3 of       **
** the License, or (at your option) any later version.                  **
**                                                                      **
** This program is distributed in the hope that it will be useful, but  **
** WITHOUT ANY WARRANTY; without even the implied warranty of           **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         **
** GNU General Public License for more details.                         **
**                                                                      **
** You should have received a copy of the GNU General Public License    **
** along with this program; if not, see <http://www.gnu.org/licenses/>. **
*************************************************************************/

#include <gtest/gtest.h>
#include "MD5HashFunction.hpp"
#include "XXHashFunction.hpp"

using namespace std;


TEST(HashFunctionTest, md5) {
	MD5HashFunction md5;
	ASSERT_EQ(md5.digestSize(), 16);
	md5.update("0123456789");
	EXPECT_EQ(md5.digestString(), "781e5e245d69b566979b86e28d23f2c7");
	md5.update("abcdefghij");
	EXPECT_EQ(md5.digestString(), "644be06dfc54061fd1e67f5ebbabcd58");
	md5.reset();
	md5.update("0123456789");
	EXPECT_EQ(md5.digestString(), "781e5e245d69b566979b86e28d23f2c7");
	uint8_t bytes[] = {0x78, 0x1e, 0x5e, 0x24, 0x5d, 0x69, 0xb5, 0x66, 0x97, 0x9b, 0x86, 0xe2, 0x8d, 0x23, 0xf2, 0xc7};
	int i=0;
	for (uint8_t byte : md5.digestValue())
		EXPECT_EQ(byte, bytes[i++]);
}


TEST(HashFunctionTest, xxh32) {
	XXH32HashFunction xxh32;
	ASSERT_EQ(xxh32.digestSize(), 4);
	xxh32.update("0123456789");
	EXPECT_EQ(xxh32.digestString(), "950c9c0a");
	xxh32.update("abcdefghij");
	EXPECT_EQ(xxh32.digestString(), "35600916");
	xxh32.reset();
	xxh32.update("0123456789");
	EXPECT_EQ(xxh32.digestString(), "950c9c0a");
	uint8_t bytes[] = {0x95, 0x0c, 0x9c, 0x0a};
	int i=0;
	for (uint8_t byte : xxh32.digestValue())
		EXPECT_EQ(byte, bytes[i++]);
}


TEST(HashFunctionTest, xxh64) {
	XXH64HashFunction xxh64;
	ASSERT_EQ(xxh64.digestSize(), 8);
	xxh64.update("0123456789");
	EXPECT_EQ(xxh64.digestString(), "3f5fc178a81867e7");
	xxh64.update("abcdefghij");
	EXPECT_EQ(xxh64.digestString(), "45c2c0e1eb35a0b6");
	xxh64.reset();
	xxh64.update("0123456789");
	EXPECT_EQ(xxh64.digestString(), "3f5fc178a81867e7");
	uint8_t bytes[] = {0x3f, 0x5f, 0xc1, 0x78, 0xa8, 0x18, 0x67, 0xe7};
	int i=0;
	for (uint8_t byte : xxh64.digestValue())
		EXPECT_EQ(byte, bytes[i++]);
}


TEST(HashFunctionTest, createMD5) {
	auto hashfunc = HashFunction::create("md5");
	ASSERT_TRUE(dynamic_cast<MD5HashFunction*>(hashfunc.get()) != nullptr);
	hashfunc->update("0123456789");
	EXPECT_EQ(hashfunc->digestString(), "781e5e245d69b566979b86e28d23f2c7");

	hashfunc = HashFunction::create("md5", "0123456789");
	ASSERT_TRUE(dynamic_cast<MD5HashFunction*>(hashfunc.get()) != nullptr);
	EXPECT_EQ(hashfunc->digestString(), "781e5e245d69b566979b86e28d23f2c7");
}


TEST(HashFunctionTest, createXXH32) {
	auto hashfunc = HashFunction::create("xxh32");
	ASSERT_TRUE(dynamic_cast<XXH32HashFunction*>(hashfunc.get()) != nullptr);
	hashfunc->update("0123456789");
	EXPECT_EQ(hashfunc->digestString(), "950c9c0a");

	hashfunc = HashFunction::create("xxh32", "0123456789");
	ASSERT_TRUE(dynamic_cast<XXH32HashFunction*>(hashfunc.get()) != nullptr);
	EXPECT_EQ(hashfunc->digestString(), "950c9c0a");
}


TEST(HashFunctionTest, createXXH64) {
	auto hashfunc = HashFunction::create("xxh64");
	ASSERT_TRUE(dynamic_cast<XXH64HashFunction*>(hashfunc.get()) != nullptr);
	hashfunc->update("0123456789");
	EXPECT_EQ(hashfunc->digestString(), "3f5fc178a81867e7");

	hashfunc = HashFunction::create("xxh64", "0123456789");
	ASSERT_TRUE(dynamic_cast<XXH64HashFunction*>(hashfunc.get()) != nullptr);
	EXPECT_EQ(hashfunc->digestString(), "3f5fc178a81867e7");
}


TEST(HashFunctionTest, createFail) {
	ASSERT_TRUE(HashFunction::create("not-available") == nullptr);
}