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 "CppTimer.h" 19 #include "CallbackQueue.h" 20 using namespace std; 21 22 namespace { Double(int & addNum)23 static void Double(int& addNum) 24 { 25 addNum += addNum; 26 } 27 28 // 测试默认构造函数是否被删除 TEST(CppTimerTest,DefaultConstructorDeletedTest)29 TEST(CppTimerTest, DefaultConstructorDeletedTest) 30 { 31 EXPECT_TRUE(std::is_default_constructible<CppTimer>::value == false); 32 } 33 34 // 测试拷贝构造函数是否被删除 TEST(CppTimerTest,CopyConstructorDeletedTest)35 TEST(CppTimerTest, CopyConstructorDeletedTest) 36 { 37 EXPECT_TRUE(std::is_copy_constructible<CppTimer>::value == false); 38 } 39 40 // 测试赋值运算符是否被删除 TEST(CppTimerTest,AssignmentOperatorDeletedTest)41 TEST(CppTimerTest, AssignmentOperatorDeletedTest) 42 { 43 EXPECT_TRUE(std::is_copy_assignable<CppTimer>::value == false); 44 } 45 TEST(CppTimerTest,SetShotTimesTest)46 TEST(CppTimerTest, SetShotTimesTest) 47 { 48 // same thread 49 int addNum = 3; 50 CppTimer timer(Double, std::ref(addNum)); 51 int newShotTime = 3; 52 timer.SetShotTimes(newShotTime); 53 EXPECT_EQ(timer.GetShotTimes(), newShotTime); 54 // not same thread 55 std::thread commandThead([&timer, &newShotTime]() { 56 timer.SetShotTimes(newShotTime); 57 EXPECT_EQ(timer.GetShotTimes(), newShotTime); 58 }); 59 commandThead.detach(); 60 } 61 TEST(CppTimerTest,IsRunningTest)62 TEST(CppTimerTest, IsRunningTest) 63 { 64 int addNum = 3; 65 int sum = addNum + addNum; 66 CppTimer timer(Double, std::ref(addNum)); 67 CallbackQueue queue; 68 int interval = 100; 69 // normal 70 timer.Start(interval); 71 this_thread::sleep_for(chrono::milliseconds(200)); 72 EXPECT_EQ(timer.interval, interval); 73 EXPECT_TRUE(timer.IsRunning()); 74 timer.RunTimerTick(queue); 75 EXPECT_TRUE(queue.callBackList.size() > 0); 76 queue.ConsumingCallback(); 77 EXPECT_EQ(addNum, sum); 78 timer.Stop(); 79 EXPECT_FALSE(timer.IsRunning()); 80 } 81 TEST(CppTimerTest,IsRunningTest2)82 TEST(CppTimerTest, IsRunningTest2) 83 { 84 int addNum = 3; 85 CppTimer timer(Double, std::ref(addNum)); 86 CallbackQueue queue; 87 int interval = 100; 88 // not same thread 89 std::thread commandThead([&timer, &queue, &interval]() { 90 timer.Start(interval); 91 timer.RunTimerTick(queue); 92 timer.Stop(); 93 EXPECT_FALSE(timer.IsRunning()); 94 }); 95 commandThead.detach(); 96 } 97 TEST(CppTimerTest,IsRunningTest3)98 TEST(CppTimerTest, IsRunningTest3) 99 { 100 int addNum = 3; 101 CppTimer timer(Double, std::ref(addNum)); 102 CallbackQueue queue; 103 int interval = 0; 104 // interval is 0 105 timer.Start(interval); 106 timer.RunTimerTick(queue); 107 timer.Stop(); 108 EXPECT_FALSE(timer.IsRunning()); 109 } 110 }