tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestRecentEventsBuffer.cpp (2311B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "APZTestCommon.h"
      8 
      9 #include "InputUtils.h"
     10 #include "gtest/gtest.h"
     11 
     12 #include "RecentEventsBuffer.h"
     13 
     14 struct TestEvent {
     15 public:
     16  explicit TestEvent(TimeStamp timeStamp, size_t id);
     17  TimeStamp mTimeStamp;
     18  size_t mId;
     19 };
     20 
     21 class RecentEventsBufferTest : public ::testing::Test {
     22 public:
     23  TimeStamp start;
     24 
     25  void SetUp() { start = TimeStamp::Now(); }
     26 };
     27 
     28 TestEvent::TestEvent(TimeStamp timeStamp, size_t id)
     29    : mTimeStamp(timeStamp), mId(id) {}
     30 
     31 TEST_F(RecentEventsBufferTest, Basic) {
     32  RecentEventsBuffer<TestEvent> buffer(TimeDuration::FromMilliseconds(200));
     33 
     34  // Push three events to the buffer, with the first being the oldest.
     35  buffer.push(TestEvent(start, 0U));
     36  buffer.push(TestEvent(start + TimeDuration::FromMilliseconds(100), 1));
     37  // Push an event to the buffer that will be one millisecond beyond the
     38  // max age duration from the first event pushed to the buffer.
     39  buffer.push(TestEvent(start + TimeDuration::FromMilliseconds(201), 2));
     40 
     41  // The oldest timestamp should be dropped when the last event is pushed.
     42  EXPECT_EQ(buffer.size(), 2U);
     43 
     44  // The first and last events in the buffer are the most recent events
     45  // that were pushed.
     46  EXPECT_EQ(buffer.front().mId, 1U);
     47  EXPECT_EQ(buffer.back().mId, 2U);
     48 }
     49 
     50 TEST_F(RecentEventsBufferTest, MinSize) {
     51  RecentEventsBuffer<TestEvent> buffer(TimeDuration::FromMilliseconds(100), 3);
     52 
     53  // Push two initial events.
     54  buffer.push(TestEvent(start, 0U));
     55  buffer.push(TestEvent(start + TimeDuration::FromMilliseconds(1), 1U));
     56 
     57  // Push and event that is greater than the max age from the initial events.
     58  buffer.push(TestEvent(start + TimeDuration::FromMilliseconds(101), 2U));
     59 
     60  // The minimum size requirement of the buffer should prevent the buffer
     61  // from removing items.
     62  EXPECT_EQ(buffer.size(), 3U);
     63 
     64  // Adding one item should allow the initial element to be removed.
     65  buffer.push(TestEvent(start + TimeDuration::FromMilliseconds(102), 3U));
     66  EXPECT_EQ(buffer.size(), 3U);
     67 }