test: Update 8 files
This commit is contained in:
@@ -6,70 +6,52 @@
|
||||
#include <cstring>
|
||||
|
||||
void test_string_hash() {
|
||||
auto hash = DSHash::hashString("abc");
|
||||
std::string hashStr = DSHash::toString(hash);
|
||||
DSHash hasher(std::string("abc"));
|
||||
std::string hashStr = hasher.toString();
|
||||
assert(hashStr == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
|
||||
std::cout << "✓ String hash test passed" << std::endl;
|
||||
}
|
||||
|
||||
void test_empty_string() {
|
||||
auto hash = DSHash::hashString("");
|
||||
std::string hashStr = DSHash::toString(hash);
|
||||
DSHash hasher(std::string(""));
|
||||
std::string hashStr = hasher.toString();
|
||||
assert(hashStr == "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
|
||||
std::cout << "✓ Empty string hash test passed" << std::endl;
|
||||
}
|
||||
|
||||
void test_long_string() {
|
||||
auto hash = DSHash::hashString("The quick brown fox jumps over the lazy dog");
|
||||
std::string hashStr = DSHash::toString(hash);
|
||||
DSHash hasher(std::string("The quick brown fox jumps over the lazy dog"));
|
||||
std::string hashStr = hasher.toString();
|
||||
assert(hashStr == "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592");
|
||||
std::cout << "✓ Long string hash test passed" << std::endl;
|
||||
}
|
||||
|
||||
void test_incremental_update() {
|
||||
DSHash hasher;
|
||||
hasher.update("a");
|
||||
hasher.update("b");
|
||||
hasher.update("c");
|
||||
auto hash = hasher.finalize();
|
||||
std::string hashStr = DSHash::toString(hash);
|
||||
assert(hashStr == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
|
||||
std::cout << "✓ Incremental update test passed" << std::endl;
|
||||
}
|
||||
|
||||
void test_file_hash() {
|
||||
std::string tempFile = "/tmp/test_hash_file.txt";
|
||||
std::ofstream out(tempFile);
|
||||
out << "Test content for hashing";
|
||||
out.close();
|
||||
|
||||
auto hash = DSHash::hashFile(tempFile);
|
||||
std::string hashStr = DSHash::toString(hash);
|
||||
DSHash fileHasher{std::filesystem::path(tempFile)};
|
||||
std::string hashStr = fileHasher.toString();
|
||||
|
||||
DSHash hasher;
|
||||
hasher.update("Test content for hashing");
|
||||
auto expectedHash = hasher.finalize();
|
||||
std::string expectedHashStr = DSHash::toString(expectedHash);
|
||||
DSHash stringHasher(std::string("Test content for hashing"));
|
||||
std::string expectedHashStr = stringHasher.toString();
|
||||
|
||||
assert(hashStr == expectedHashStr);
|
||||
std::filesystem::remove(tempFile);
|
||||
std::cout << "✓ File hash test passed" << std::endl;
|
||||
}
|
||||
|
||||
void test_large_data() {
|
||||
DSHash hasher;
|
||||
std::string chunk(1000, 'a');
|
||||
for (int i = 0; i < 100; i++) {
|
||||
hasher.update(chunk);
|
||||
}
|
||||
auto hash = hasher.finalize();
|
||||
std::string hashStr = DSHash::toString(hash);
|
||||
void test_get_method() {
|
||||
DSHash hasher(std::string("test"));
|
||||
tHash hash = hasher.get();
|
||||
|
||||
assert(hash.size() == 32);
|
||||
|
||||
std::string hashStr = hasher.toString();
|
||||
assert(hashStr.length() == 64);
|
||||
for (char c : hashStr) {
|
||||
assert((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'));
|
||||
}
|
||||
std::cout << "✓ Large data hash test passed" << std::endl;
|
||||
std::cout << "✓ get() method test passed" << std::endl;
|
||||
}
|
||||
|
||||
void test_known_vectors() {
|
||||
@@ -87,22 +69,36 @@ void test_known_vectors() {
|
||||
};
|
||||
|
||||
for (const auto& vec : vectors) {
|
||||
auto hash = DSHash::hashString(vec.input);
|
||||
std::string hashStr = DSHash::toString(hash);
|
||||
DSHash hasher(std::string(vec.input));
|
||||
std::string hashStr = hasher.toString();
|
||||
assert(hashStr == vec.expected);
|
||||
}
|
||||
std::cout << "✓ Known test vectors passed" << std::endl;
|
||||
}
|
||||
|
||||
void test_binary_data() {
|
||||
uint8_t binaryData[] = {0x00, 0x01, 0x02, 0x03, 0xFF, 0xFE, 0xFD};
|
||||
DSHash hasher;
|
||||
hasher.update(binaryData, sizeof(binaryData));
|
||||
auto hash = hasher.finalize();
|
||||
std::string hashStr = DSHash::toString(hash);
|
||||
void test_directory_hash() {
|
||||
std::filesystem::path tempDir = "/tmp/test_hash_dir";
|
||||
std::filesystem::create_directories(tempDir);
|
||||
std::filesystem::create_directories(tempDir / "subdir");
|
||||
|
||||
std::ofstream file1(tempDir / "file1.txt");
|
||||
file1 << "content1";
|
||||
file1.close();
|
||||
|
||||
std::ofstream file2(tempDir / "subdir" / "file2.txt");
|
||||
file2 << "content2";
|
||||
file2.close();
|
||||
|
||||
DSHash dirHasher(tempDir);
|
||||
std::string hashStr = dirHasher.toString();
|
||||
|
||||
assert(hashStr.length() == 64);
|
||||
std::cout << "✓ Binary data hash test passed" << std::endl;
|
||||
for (char c : hashStr) {
|
||||
assert((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'));
|
||||
}
|
||||
|
||||
std::filesystem::remove_all(tempDir);
|
||||
std::cout << "✓ Directory hash test passed" << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
@@ -110,11 +106,10 @@ int main() {
|
||||
test_string_hash();
|
||||
test_empty_string();
|
||||
test_long_string();
|
||||
test_incremental_update();
|
||||
test_file_hash();
|
||||
test_large_data();
|
||||
test_get_method();
|
||||
test_known_vectors();
|
||||
test_binary_data();
|
||||
test_directory_hash();
|
||||
|
||||
std::cout << "\nAll library tests passed!" << std::endl;
|
||||
return 0;
|
||||
|
Reference in New Issue
Block a user