File: testsignals.cpp

package info (click to toggle)
fcitx5 5.1.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,356 kB
  • sloc: cpp: 75,670; sh: 1,770; xml: 1,545; python: 1,052; ansic: 71; makefile: 11
file content (175 lines) | stat: -rw-r--r-- 4,752 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * SPDX-FileCopyrightText: 2016-2016 CSSlayer <wengxt@gmail.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 */

#include "fcitx-utils/connectableobject.h"
#include "fcitx-utils/log.h"
#include "fcitx-utils/metastring.h"
#include "fcitx-utils/signals.h"

void test_simple_signal() {
    bool called = false;
    fcitx::Signal<void()> signal1;
    auto connection = signal1.connect([&called]() { called = true; });
    FCITX_ASSERT(connection.connected());
    FCITX_ASSERT(!called);
    signal1();
    called = false;
    auto connection2 = connection;
    FCITX_ASSERT(connection.connected());
    FCITX_ASSERT(connection2.connected());

    connection.disconnect();
    FCITX_ASSERT(!connection.connected());
    FCITX_ASSERT(!connection2.connected());
    signal1();
    FCITX_ASSERT(!called);
}

void test_combiner() {
    fcitx::Signal<int(int)> signal1;
    auto connection = signal1.connect([](int i) { return i; });
    FCITX_ASSERT(signal1(1) == 1);
    auto connection2 = signal1.connect([](int) { return -1; });
    FCITX_ASSERT(signal1(2) == -1);
}

class MaxInteger {
public:
    MaxInteger() : initial_(INT32_MIN) {}

    template <typename InputIterator>
    int operator()(InputIterator begin, InputIterator end) {
        int v = initial_;
        for (; begin != end; begin++) {
            int newv = *begin;
            if (newv > v) {
                v = newv;
            }
        }
        return v;
    }

private:
    int initial_;
};

void test_custom_combiner() {
    fcitx::Signal<int(int), MaxInteger> signal1;
    auto connection = signal1.connect([](int i) { return i; });
    FCITX_ASSERT(signal1(1) == 1);
    auto connection2 = signal1.connect([](int) { return -1; });
    FCITX_ASSERT(signal1(2) == 2);
    connection.disconnect();
    FCITX_ASSERT(signal1(2) == -1);
}

void test_destruct_order() {
    fcitx::Connection connection;
    FCITX_ASSERT(!connection.connected());
    {
        fcitx::Signal<void()> signal1;
        connection = signal1.connect([]() {});

        FCITX_ASSERT(connection.connected());
    }
    FCITX_ASSERT(!connection.connected());
    connection.disconnect();
}

class TestObject : public fcitx::ConnectableObject {
public:
    using fcitx::ConnectableObject::destroy;
};

void test_connectable_object() {
    using namespace fcitx;
    TestObject obj;
    bool called = false;
    auto connection =
        obj.connect<ConnectableObject::Destroyed>([&called, &obj](void *self) {
            FCITX_ASSERT(&obj == self);
            called = true;
        });
    FCITX_ASSERT(connection.connected());
    obj.destroy();
    FCITX_ASSERT(called);
    FCITX_ASSERT(!connection.connected());
    FCITX_ASSERT(
        !obj.connect<ConnectableObject::Destroyed>([](void *) {}).connected());
}

class TestObject2 : public fcitx::ConnectableObject {
public:
    FCITX_DECLARE_SIGNAL(TestObject2, test, void(int &));
    void emitTest(int &i) { emit<TestObject2::test>(i); }
    FCITX_DECLARE_SIGNAL(TestObject2, test2, void(std::string &));
    void emitTest2(std::string &s) { emit<TestObject2::test2>(s); }

private:
    FCITX_DEFINE_SIGNAL(TestObject2, test);
    FCITX_DEFINE_SIGNAL(TestObject2, test2);
};

void test_reference() {
    using namespace fcitx;
    TestObject2 obj;
    int i = 0;
    obj.connect<TestObject2::test>([](int &i) { i++; });
    obj.connect<TestObject2::test>([](int &i) { i++; });
    obj.emitTest(i);
    std::string s;
    obj.connect<TestObject2::test2>([](std::string &s) { s += "a"; });
    obj.connect<TestObject2::test2>([](std::string &s) { s += "b"; });
    obj.emitTest2(s);
    FCITX_ASSERT(s == "ab");
}

void test_move() {
    bool called = false;
    fcitx::Signal<void()> signal1;
    auto connection = signal1.connect([&called]() { called = true; });
    FCITX_ASSERT(connection.connected());
    FCITX_ASSERT(!called);
    signal1();
    FCITX_ASSERT(called);

    called = false;
    auto signal2 = std::move(signal1);
    FCITX_ASSERT(connection.connected());
    // shouldn't use signal1() anymore, unless we want to assign to it again.
    FCITX_ASSERT(!called);
    signal2();
    FCITX_ASSERT(called);
    signal2.disconnectAll();
    FCITX_ASSERT(!connection.connected());
}

void test_self_removal() {
    fcitx::Signal<void()> signal;
    int number = 0;
    fcitx::Connection connection = signal.connect([&connection, &number]() {
        connection.disconnect();
        number = 1234;
    });

    signal();
    FCITX_ASSERT(number == 1234);
    FCITX_ASSERT(!connection.connected());
}

int main() {
    test_simple_signal();
    test_combiner();
    test_custom_combiner();
    test_destruct_order();
    test_connectable_object();
    test_reference();
    test_move();
    test_self_removal();

    return 0;
}