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