1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 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 */ 15 #include <string> 16 #include "gtest/gtest.h" 17 #define private public 18 #include "CppTimerManager.h" 19 using namespace std; 20 21 namespace { Double(int & addNum)22 static void Double(int& addNum) 23 { 24 addNum += addNum; 25 } 26 27 // 测试赋值运算符是否被删除 TEST(CppTimerManagerTest,AssignmentOperatorDeletedTest)28 TEST(CppTimerManagerTest, AssignmentOperatorDeletedTest) 29 { 30 EXPECT_TRUE(std::is_copy_assignable<CppTimerManager>::value == false); 31 } 32 TEST(CppTimerManagerTest,AddAndRemoveCppTimerTest)33 TEST(CppTimerManagerTest, AddAndRemoveCppTimerTest) { 34 int num = 3; 35 CppTimerManager& manager = CppTimerManager::GetTimerManager(); 36 CppTimer timer([&num]() { num += num; }); 37 int value = 1; 38 manager.AddCppTimer(timer); 39 EXPECT_EQ(manager.runningTimers.size(), value); 40 int interval = 100; 41 timer.Start(interval); 42 value = num + num; 43 this_thread::sleep_for(chrono::milliseconds(200)); 44 manager.RunTimerTick(); 45 EXPECT_EQ(num, value); 46 value = 0; 47 manager.RemoveCppTimer(timer); 48 EXPECT_EQ(manager.runningTimers.size(), value); 49 } 50 }