metrics_hashes_unittest.cc 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2014 The Chromium 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.
  4. #include "base/metrics/metrics_hashes.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include "base/format_macros.h"
  8. #include "base/strings/stringprintf.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace base {
  11. // Make sure our ID hashes are the same as what we see on the server side.
  12. TEST(MetricsHashesTest, HashMetricName) {
  13. // The cases must match those in //tools/metrics/ukm/codegen_test.py.
  14. static const struct {
  15. std::string input;
  16. std::string output;
  17. } cases[] = {
  18. {"Back", "0x0557fa923dcee4d0"},
  19. {"NewTab", "0x290eb683f96572f1"},
  20. {"Forward", "0x67d2f6740a8eaebf"},
  21. };
  22. for (size_t i = 0; i < std::size(cases); ++i) {
  23. uint64_t hash = HashMetricName(cases[i].input);
  24. std::string hash_hex = base::StringPrintf("0x%016" PRIx64, hash);
  25. EXPECT_EQ(cases[i].output, hash_hex);
  26. }
  27. }
  28. TEST(MetricsHashesTest, HashMetricNameAs32Bits) {
  29. // The cases must match those in //tools/metrics/ukm/codegen_test.py.
  30. static const struct {
  31. std::string input;
  32. std::string output;
  33. } cases[] = {
  34. {"Back", "0x0557fa92"},
  35. {"NewTab", "0x290eb683"},
  36. {"Forward", "0x67d2f674"},
  37. };
  38. for (size_t i = 0; i < std::size(cases); ++i) {
  39. uint32_t hash = HashMetricNameAs32Bits(cases[i].input);
  40. std::string hash_hex = base::StringPrintf("0x%08" PRIx32, hash);
  41. EXPECT_EQ(cases[i].output, hash_hex);
  42. }
  43. }
  44. } // namespace metrics