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