• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9 
10 #include "base/metrics/metrics_hashes.h"
11 
12 #include <stddef.h>
13 #include <stdint.h>
14 
15 #include "base/format_macros.h"
16 #include "base/strings/stringprintf.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 
19 namespace base {
20 
21 // Make sure our ID hashes are the same as what we see on the server side.
TEST(MetricsHashesTest,HashMetricName)22 TEST(MetricsHashesTest, HashMetricName) {
23   // The cases must match those in //tools/metrics/ukm/codegen_test.py.
24   static const struct {
25     std::string input;
26     std::string output;
27   } cases[] = {
28       {"Back", "0x0557fa923dcee4d0"},
29       {"NewTab", "0x290eb683f96572f1"},
30       {"Forward", "0x67d2f6740a8eaebf"},
31   };
32 
33   for (size_t i = 0; i < std::size(cases); ++i) {
34     uint64_t hash = HashMetricName(cases[i].input);
35     std::string hash_hex = base::StringPrintf("0x%016" PRIx64, hash);
36     EXPECT_EQ(cases[i].output, hash_hex);
37   }
38 }
39 
TEST(MetricsHashesTest,HashMetricNameAs32Bits)40 TEST(MetricsHashesTest, HashMetricNameAs32Bits) {
41   // The cases must match those in //tools/metrics/ukm/codegen_test.py.
42   static const struct {
43     std::string input;
44     std::string output;
45   } cases[] = {
46       {"Back", "0x0557fa92"},
47       {"NewTab", "0x290eb683"},
48       {"Forward", "0x67d2f674"},
49   };
50 
51   for (size_t i = 0; i < std::size(cases); ++i) {
52     uint32_t hash = HashMetricNameAs32Bits(cases[i].input);
53     std::string hash_hex = base::StringPrintf("0x%08" PRIx32, hash);
54     EXPECT_EQ(cases[i].output, hash_hex);
55   }
56 }
57 
58 }  // namespace metrics
59