File: recent_events_counter_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (110 lines) | stat: -rw-r--r-- 3,719 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/power/ml/recent_events_counter.h"

#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {
namespace power {
namespace ml {

TEST(RecentEventsCounterTest, TimeTest) {
  base::TimeDelta minute = base::Minutes(1);
  RecentEventsCounter counter(base::Hours(1), 60);
  ASSERT_EQ(counter.GetTotal(minute), 0);

  counter.Log(5 * minute);
  ASSERT_EQ(counter.GetTotal(10 * minute), 1);

  counter.Log(5 * minute);
  ASSERT_EQ(counter.GetTotal(10 * minute), 2);

  counter.Log(25.4 * minute);

  ASSERT_EQ(counter.GetTotal(30 * minute), 3);
  ASSERT_EQ(counter.GetTotal(70 * minute), 1);
  // Event at 25.4 minutes is counted 59 minutes later.
  ASSERT_EQ(counter.GetTotal(84.4 * minute), 1);
  // Event at 25.4 minutes is not counted 59.7 minutes later at 85.1 minutes. An
  // an event logged at 85.1 minutes would wipe out the event at 25.4 minutes,
  // so the event at 25.4 minutes cannot be counted to ensure consistency.
  ASSERT_EQ(counter.GetTotal(85.1 * minute), 0);

  counter.Log(75 * minute);
  ASSERT_EQ(counter.GetTotal(80 * minute), 2);
  ASSERT_EQ(counter.GetTotal(90 * minute), 1);

  // Overwrite the 25.4 minute logging.
  counter.Log(85.1 * minute);
  ASSERT_EQ(counter.GetTotal(90 * minute), 2);

  counter.Log(200 * minute);
  ASSERT_EQ(counter.GetTotal(210 * minute), 1);

  ASSERT_EQ(counter.GetTotal(300 * minute), 0);
}

TEST(RecentEventsCounterTest, TimeTestConsecutiveMinutes) {
  base::TimeDelta minute = base::Minutes(1);
  RecentEventsCounter counter(base::Hours(1), 60);

  for (int i = 0; i < 59; i++) {
    counter.Log(i * minute);
    EXPECT_EQ(counter.GetTotal(i * minute), i + 1);
    EXPECT_EQ(counter.GetTotal((i + 0.5) * minute), i + 1);
    EXPECT_EQ(counter.GetTotal((i + 1) * minute), i + 1);
  }
  for (int i = 59; i < 122; i++) {
    counter.Log(i * minute);
    EXPECT_EQ(counter.GetTotal(i * minute), 60);
    EXPECT_EQ(counter.GetTotal((i + 0.5) * minute), 60);
    EXPECT_EQ(counter.GetTotal((i + 1) * minute), 59);
  }
}

// Tests that, when logging a slightly-newer event, stale buckets are cleared.
TEST(RecentEventsCounterTest, SomeBucketsStale) {
  base::TimeDelta minute = base::Minutes(1);
  RecentEventsCounter counter(base::Hours(1), 60);

  // Start with 60 buckets covering [0, 60), with 1 event per bucket.
  for (int i = 0; i < 60; i++) {
    counter.Log(i * minute);
  }
  CHECK_EQ(counter.GetTotal(59.5 * minute), 60);

  // Logging an event at 64 should advance this to [5, 65), with:
  // * 55 buckets covering [5, 60) with 1 event each
  // * 4 buckets covering [60, 64) with 0 events each
  // * 1 bucket covering [64, 65) with 1 event each
  // Total: 56
  counter.Log(64 * minute);
  EXPECT_EQ(counter.GetTotal(64.5 * minute), 56);
}

// Tests that, when logging an event more than `duration` newer than any
// previous event, all buckets are cleared (since all will be stale).
TEST(RecentEventsCounterTest, AllBucketsStale) {
  base::TimeDelta minute = base::Minutes(1);
  RecentEventsCounter counter(base::Hours(1), 60);

  // Start with 60 buckets covering [0, 60), with 1 event per bucket.
  for (int i = 0; i < 60; i++) {
    counter.Log(i * minute);
  }
  CHECK_EQ(counter.GetTotal(59.5 * minute), 60);

  // Logging an event at 124 should advance this to [65, 125), with:
  // * 59 buckets covering [65, 124) with 0 events each
  // * 1 bucket covering [124, 125) with 1 event each
  // Total: 1
  counter.Log(124 * minute);
  EXPECT_EQ(counter.GetTotal(124.5 * minute), 1);
}

}  // namespace ml
}  // namespace power
}  // namespace ash