作者: 谢瑞阳 10225101483 徐翔宇 10225101535
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

55 行
1.6 KiB

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include "port/port.h"
  5. #include "util/testharness.h"
  6. namespace leveldb {
  7. namespace port {
  8. class SHA1 { };
  9. static std::string TestSHA1(const char* data, size_t len) {
  10. char hash_val[20];
  11. SHA1_Hash(data, len, hash_val);
  12. char buf[41];
  13. for (int i = 0; i < 20; i++) {
  14. snprintf(buf + i * 2, 41 - i * 2,
  15. "%02x",
  16. static_cast<unsigned int>(static_cast<unsigned char>(
  17. hash_val[i])));
  18. }
  19. return std::string(buf, 40);
  20. }
  21. TEST(SHA1, Simple) {
  22. ASSERT_EQ("da39a3ee5e6b4b0d3255bfef95601890afd80709", TestSHA1("", 0));
  23. ASSERT_EQ("aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d", TestSHA1("hello", 5));
  24. std::string x(10000, 'x');
  25. ASSERT_EQ("f8c5cde791c5056cf515881e701c8a9ecb439a75",
  26. TestSHA1(x.data(), x.size()));
  27. }
  28. TEST(SHA1, Benchmark) {
  29. std::string data(1048576 * 100, 'x');
  30. double start = Env::Default()->NowMicros() * 1e-6;
  31. static const int kIters = 10;
  32. uint32_t sha1 = 0;
  33. for (int i = 0; i < kIters; i++) {
  34. char hash_val[20];
  35. SHA1_Hash(data.data(), data.size(), hash_val);
  36. sha1 |= hash_val[0];
  37. }
  38. double finish = Env::Default()->NowMicros() * 1e-6;
  39. double mb = (static_cast<long long int>(data.size()) * kIters) / 1048576.0;
  40. fprintf(stderr, "SHA1 %0.0f MB: %.3f secs; %.1f MB/s, dummy=0x%02x\n",
  41. mb, (finish - start), mb / (finish - start), sha1);
  42. }
  43. }
  44. }
  45. int main(int argc, char** argv) {
  46. return leveldb::test::RunAllTests();
  47. }