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