rocksdb/port/sha1_test.cc
jorlow@chromium.org f67e15e50f Initial checkin.
git-svn-id: https://leveldb.googlecode.com/svn/trunk@2 62dab493-f737-651d-591e-8d6aee1b9529
2011-03-18 22:37:00 +00:00

56 lines
1.6 KiB
C++

// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
#include "port/port.h"
#include "util/testharness.h"
namespace leveldb {
namespace port {
class SHA1 { };
static std::string TestSHA1(const char* data, size_t len) {
char hash_val[20];
SHA1_Hash(data, len, hash_val);
char buf[41];
for (int i = 0; i < 20; i++) {
snprintf(buf + i * 2, 41 - i * 2,
"%02x",
static_cast<unsigned int>(static_cast<unsigned char>(
hash_val[i])));
}
return std::string(buf, 40);
}
TEST(SHA1, Simple) {
ASSERT_EQ("da39a3ee5e6b4b0d3255bfef95601890afd80709", TestSHA1("", 0));
ASSERT_EQ("aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d", TestSHA1("hello", 5));
std::string x(10000, 'x');
ASSERT_EQ("f8c5cde791c5056cf515881e701c8a9ecb439a75",
TestSHA1(x.data(), x.size()));
}
TEST(SHA1, Benchmark) {
std::string data(1048576 * 100, 'x');
double start = Env::Default()->NowMicros() * 1e-6;
static const int kIters = 10;
uint32_t sha1 = 0;
for (int i = 0; i < kIters; i++) {
char hash_val[20];
SHA1_Hash(data.data(), data.size(), hash_val);
sha1 |= hash_val[0];
}
double finish = Env::Default()->NowMicros() * 1e-6;
double mb = (static_cast<long long int>(data.size()) * kIters) / 1048576.0;
fprintf(stderr, "SHA1 %0.0f MB: %.3f secs; %.1f MB/s, dummy=0x%02x\n",
mb, (finish - start), mb / (finish - start), sha1);
}
}
}
int main(int argc, char** argv) {
return leveldb::test::RunAllTests();
}