1 // Copyright (C) 2022 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 #pragma once 15 16 #include <chrono> 17 18 namespace android { 19 namespace base { 20 21 // TestClock class for testing. Provides basic functions for advancing and resetting time. 22 // Satisfies requirements of TrivialClock: https://en.cppreference.com/w/cpp/named_req/TrivialClock 23 class TestClock { 24 public: 25 using rep = double; 26 using period = std::ratio<1, 1>; /* TestClock::duration(1) is 1 second */ 27 using duration = std::chrono::duration<rep, period>; 28 using time_point = std::chrono::time_point<TestClock>; 29 const bool is_steady = false; 30 now()31 static time_point now() { return time_point(duration(getInternalTime())); } 32 advance(double secondsPassed)33 static void advance(double secondsPassed) { getInternalTime() += secondsPassed; } 34 reset()35 static void reset() { getInternalTime() = 0.0; } 36 37 private: getInternalTime()38 static double& getInternalTime() { 39 static double internalTime = 0.0; 40 return internalTime; 41 } 42 }; 43 44 } // namespace base 45 } // namespace android